Skip to content

Commit

Permalink
Add proper relationship from org to user
Browse files Browse the repository at this point in the history
In preparation for removing userId from user to avoid duplicate users, we are ensuring that the lookup from orgs to users is using a proper id
  • Loading branch information
paustint committed Oct 16, 2024
1 parent 14a8799 commit a535446
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
10 changes: 8 additions & 2 deletions apps/api/src/app/db/salesforce-org.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Maybe, SalesforceOrgUi } from '@jetstream/types';
import { Prisma, SalesforceOrg } from '@prisma/client';
import { parseISO } from 'date-fns/parseISO';
import isUndefined from 'lodash/isUndefined';
import { findIdByUserId } from './user.db';

const SELECT = Prisma.validator<Prisma.SalesforceOrgSelect>()({
jetstreamOrganizationId: true,
Expand Down Expand Up @@ -50,8 +51,9 @@ const findUniqueOrg = ({ jetstreamUserId, uniqueId }: { jetstreamUserId: string;
});
};

const findUsersOrgs = ({ jetstreamUserId }: { jetstreamUserId: string }) => {
const findUsersOrgs = ({ jetstreamUserId, actualUserId }: { jetstreamUserId: string; actualUserId: string }) => {
return Prisma.validator<Prisma.SalesforceOrgWhereInput>()({
jetstreamUserId2: actualUserId,
jetstreamUserId,
jetstreamUrl: ENV.JETSTREAM_SERVER_URL,
});
Expand Down Expand Up @@ -103,13 +105,15 @@ export async function findByUniqueId(jetstreamUserId: string, uniqueId: string)
}

export async function findByUserId(jetstreamUserId: string) {
const actualUserId = await findIdByUserId({ userId: jetstreamUserId });
return await prisma.salesforceOrg.findMany({
select: SELECT,
where: findUsersOrgs({ jetstreamUserId }),
where: findUsersOrgs({ jetstreamUserId, actualUserId }),
});
}

export async function createOrUpdateSalesforceOrg(jetstreamUserId: string, salesforceOrgUi: Partial<SalesforceOrgUi>) {
const actualUserId = await findIdByUserId({ userId: jetstreamUserId });
const existingOrg = await prisma.salesforceOrg.findUnique({
where: findUniqueOrg({ jetstreamUserId, uniqueId: salesforceOrgUi.uniqueId! }),
});
Expand All @@ -126,6 +130,7 @@ export async function createOrUpdateSalesforceOrg(jetstreamUserId: string, sales
orgToDelete = await prisma.salesforceOrg.findFirst({
select: { id: true },
where: {
jetstreamUserId2: { equals: actualUserId },
jetstreamUserId: { equals: jetstreamUserId },
jetstreamUrl: { equals: ENV.JETSTREAM_SERVER_URL! },
username: { equals: salesforceOrgUi.username },
Expand Down Expand Up @@ -178,6 +183,7 @@ export async function createOrUpdateSalesforceOrg(jetstreamUserId: string, sales
const org = await prisma.salesforceOrg.create({
select: SELECT,
data: {
jetstreamUserId2: actualUserId,
jetstreamUserId,
jetstreamUrl: ENV.JETSTREAM_SERVER_URL,
jetstreamOrganizationId: salesforceOrgUi.jetstreamOrganizationId,
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/app/db/user.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export async function createOrUpdateUser(user: UserProfileServer): Promise<{ cre
data: {
appMetadata: JSON.stringify(user._json[ENV.AUTH_AUDIENCE!]),
deletedAt: null,
lastLoggedIn: new Date(),
preferences: {
upsert: {
create: { skipFrontdoorLogin: false },
Expand All @@ -98,6 +99,7 @@ export async function createOrUpdateUser(user: UserProfileServer): Promise<{ cre
picture: user._json.picture,
appMetadata: JSON.stringify(user._json[ENV.AUTH_AUDIENCE!]),
deletedAt: null,
lastLoggedIn: new Date(),
preferences: { create: { skipFrontdoorLogin: false } },
},
select: userSelect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "lastLoggedIn" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "salesforce_org" ADD COLUMN "jetstreamUserId2" UUID;

UPDATE salesforce_org
SET "jetstreamUserId2" = u.id
FROM "User" u
WHERE salesforce_org."jetstreamUserId" = u."userId";

-- AddForeignKey
ALTER TABLE "salesforce_org" ADD CONSTRAINT "salesforce_org_jetstreamUserId2_fkey" FOREIGN KEY ("jetstreamUserId2") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
28 changes: 16 additions & 12 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ datasource db {
}

model User {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
userId String @unique @db.VarChar
email String @db.VarChar
name String? @db.VarChar
nickname String? @db.VarChar
picture String? @db.VarChar
appMetadata Json? @db.Json
preferences UserPreference?
organizations JetstreamOrganization[]
deletedAt DateTime?
createdAt DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
userId String @unique @db.VarChar
email String @db.VarChar
name String? @db.VarChar
nickname String? @db.VarChar
picture String? @db.VarChar
appMetadata Json? @db.Json
preferences UserPreference?
salesforceOrgs SalesforceOrg[]
organizations JetstreamOrganization[]
lastLoggedIn DateTime?
deletedAt DateTime?
createdAt DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @updatedAt
}

model UserPreference {
Expand Down Expand Up @@ -63,6 +65,8 @@ model SalesforceApi {
model SalesforceOrg {
id Int @id @default(autoincrement())
jetstreamUserId String @db.VarChar
jetstreamUser User? @relation(fields: [jetstreamUserId2], references: [id], onDelete: Cascade)
jetstreamUserId2 String? @db.Uuid
jetstreamOrganization JetstreamOrganization? @relation(fields: [jetstreamOrganizationId], references: [id])
jetstreamOrganizationId String? @db.Uuid
uniqueId String @db.VarChar
Expand Down

0 comments on commit a535446

Please sign in to comment.