Skip to content

Commit

Permalink
feat: sync all cognito contacts and support ken email updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikZed committed Oct 18, 2024
1 parent 83c9736 commit 9ca8475
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 19 deletions.
11 changes: 6 additions & 5 deletions pkg/integration-aws-cognito/src/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("CognitoUserSyncService", () => {
it("should be able to run the user sync", async () => {
const cognitoApp = await db.aWSCognitoApp.findFirst({
where: {
tenantId: "test",
tenantId: "ken_prod",
},
});

Expand All @@ -32,14 +32,15 @@ describe("CognitoUserSyncService", () => {
}

const sync = new CognitoUserSyncService({
tenantId: "test",
db: null as any,
tenantId: cognitoApp.tenantId,
db,
logger: new AssertionLogger(),
AWSCognitoApp: cognitoApp,
});

await sync.syncFromEci();
});
await sync.syncToEci();
// await sync.syncFromEci();
}, 200000000);

// it("should be able to get a list of users", async () => {
// // Authenticate with AWS cognito and pull a list of all users
Expand Down
58 changes: 44 additions & 14 deletions pkg/integration-aws-cognito/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@aws-sdk/client-cognito-identity-provider";
import { CronStateHandler } from "@eci/pkg/cronstate";
import { subHours, subYears } from "date-fns";
import { id } from "@eci/pkg/ids";

export interface CognitoUserSyncServiceConfig {
tenantId: string;
Expand Down Expand Up @@ -134,18 +135,15 @@ export class CognitoUserSyncService {

public async syncToEci(): Promise<void> {
/**
* pull all cognito users and store them in our internal DB
* pull all cognito users
*/

const allCognitoUsers = await this.getAllCognitoUsers();

if (!allCognitoUsers) {
this.logger.info("No users found in cognito");
return;
}

this.logger.info(`Found ${allCognitoUsers.length} users in cognito`);

const existingCognitoUsers = await this.db.aWSCognitoUser.findMany({
where: {
awsCognitoAppId: this.AWSCognitoApp.id,
Expand All @@ -154,6 +152,11 @@ export class CognitoUserSyncService {
},
},
});

this.logger.info(
`Found ${allCognitoUsers.length} users in cognito, ${existingCognitoUsers.length} already in our DB`,
);

const existingCognitoUserIds = existingCognitoUsers.map(
(user) => user.id,
);
Expand All @@ -163,6 +166,9 @@ export class CognitoUserSyncService {

this.logger.info(
`Found ${newCognitoUsers.length} new users in cognito`,
{
newCognitoUsers: newCognitoUsers.map((user) => user.Username),
},
);

/**
Expand All @@ -172,13 +178,24 @@ export class CognitoUserSyncService {
const email = user.Attributes?.find(
(attribute) => attribute.Name === "email",
)?.Value?.toLowerCase();
const firstName = user.Attributes?.find(
(attribute) => attribute.Name === "given_name",
)?.Value;
const lastName = user.Attributes?.find(
(attribute) => attribute.Name === "family_name",
)?.Value;
if (!email) {
this.logger.error(
`User ${user.Username} has no email attribute`,
);
continue;
}
try {
this.logger.debug(`Creating user ${user.Username} in our DB`, {
email,
firstName,
lastName,
});
await this.db.aWSCognitoUser.create({
data: {
id: user.Username!,
Expand All @@ -188,28 +205,41 @@ export class CognitoUserSyncService {
},
},
contact: {
connect: {
email_tenantId: {
connectOrCreate: {
where: {
email_tenantId: {
email,
tenantId: this.tenantId,
},
},
create: {
id: id.id("contact"),
email,
tenantId: this.tenantId,
firstName,
lastName,
tenant: {
connect: {
id: this.tenantId,
},
},
},
},
},
},
});
} catch (error) {
/**
* When a user email does not exist in our
* DB yet, this call will fail. We can safely
* ignore this error, as we don't create users based
* on cognito internally
*/
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === "P2003") {
if (error.code === "P2025") {
this.logger.info(
`User ${user.Username} has no contact`,
);
continue;
} else {
this.logger.error(
`Error creating user ${user.Username} in our DB`,
);
this.logger.error(JSON.stringify(error));
continue;
}
} else {
this.logger.error(
Expand Down
58 changes: 58 additions & 0 deletions pkg/integration-kencove-api/src/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,64 @@ export class KencoveApiAppContactSyncService {
update: {
customerCode: contact.customer_code,
contact: {
connectOrCreate: {
where: {
email_tenantId: {
email,
tenantId: this.kencoveApiApp.tenantId,
},
},
create: {
id: id.id("contact"),
email,
firstName: contact.firstname,
lastName: contact.lastname,
phone: contact.phone,
externalIdentifier:
contact.commerical_customer_code,
externalIdentifier2: contact.customer_code,
company: companyName
? {
connectOrCreate: {
where: {
normalizedName_tenantId: {
normalizedName:
companyNameNormalized,
tenantId:
this.kencoveApiApp
.tenantId,
},
},
create: {
id: id.id("company"),
name: companyName,
normalizedName:
companyNameNormalized,
tenant: {
connect: {
id: this
.kencoveApiApp
.tenantId,
},
},
},
},
}
: undefined,
tenant: {
connect: {
id: this.kencoveApiApp.tenantId,
},
},
channels: salesChannel
? {
connect: {
id: salesChannel.id,
},
}
: undefined,
},
},
update: {
firstName: contact.firstname,
lastName: contact.lastname,
Expand Down

0 comments on commit 9ca8475

Please sign in to comment.