From fbeed9ea827083f4012603b37b776a0dd2882cb9 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Thu, 14 Nov 2024 20:05:20 -0800 Subject: [PATCH] Update onboard-partner.ts --- .../lib/actions/partners/onboard-partner.ts | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.ts index 2e29d4ed83..1b43929199 100644 --- a/apps/web/lib/actions/partners/onboard-partner.ts +++ b/apps/web/lib/actions/partners/onboard-partner.ts @@ -58,26 +58,6 @@ export const onboardPartnerAction = authUserActionClient }); } - const programInvite = await prisma.programInvite.findFirst({ - where: { email: user.email }, - }); - - // If the partner has invites, we need to enroll them in the program and delete the invites - if (programInvite) { - await prisma.programEnrollment.create({ - data: { - programId: programInvite.programId, - linkId: programInvite.linkId, - partnerId: partner.id, - status: "approved", - }, - }); - - await prisma.programInvite.delete({ - where: { id: programInvite.id }, - }); - } - // Create the Dots user with DOTS_DEFAULT_APP_ID const [firstName, lastName] = name.split(" "); const countryCode = COUNTRY_PHONE_CODES[country]; @@ -94,28 +74,52 @@ export const onboardPartnerAction = authUserActionClient phoneNumber, }; - await createDotsUser({ + const dotsUser = await createDotsUser({ userInfo: dotsUserInfo, }); - // Create the Dots user with the program's DOTS_APP_ID + await prisma.partner.update({ + where: { id: partner.id }, + data: { dotsUserId: dotsUser.id }, + }); + + const programInvite = await prisma.programInvite.findFirst({ + where: { email: user.email }, + include: { + program: { select: { workspace: { select: { dotsAppId: true } } } }, + }, + }); + + // If the partner has invites, we need to enroll them in the program and delete the invites if (programInvite) { - const program = await prisma.program.findUniqueOrThrow({ - where: { id: programInvite.programId }, - select: { - workspace: { - select: { - dotsAppId: true, - }, + const [programEnrollment, _] = await Promise.all([ + prisma.programEnrollment.create({ + data: { + programId: programInvite.programId, + linkId: programInvite.linkId, + partnerId: partner.id, + status: "approved", }, - }, - }); + }), + prisma.programInvite.delete({ + where: { id: programInvite.id }, + }), + ]); + + const workspace = programInvite.program.workspace; - if (program.workspace.dotsAppId) { - await createDotsUser({ - dotsAppId: program.workspace.dotsAppId, + if (workspace.dotsAppId) { + const dotsUser = await createDotsUser({ + dotsAppId: workspace.dotsAppId, userInfo: dotsUserInfo, }); + + await prisma.programEnrollment.update({ + where: { + id: programEnrollment.id, + }, + data: { dotsUserId: dotsUser.id }, + }); } }