Skip to content

Commit

Permalink
Update onboard-partner.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 15, 2024
1 parent 5c339d9 commit fbeed9e
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions apps/web/lib/actions/partners/onboard-partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 },
});
}
}

Expand Down

0 comments on commit fbeed9e

Please sign in to comment.