Files
calendar/packages/features/ee/dsync/lib/inviteExistingUserToOrg.ts
T
3a0c766ed5 fix: Support adding a non-existent email as the owner of the organization (#14569)
* fix: Support adding an non-existent email as the owner of the organization

* Remove dead code

* fixes

* add test

* self review changes

* Update packages/trpc/server/routers/viewer/organizations/create.handler.ts

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>

* Fix typo everywhere

---------

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2024-05-06 07:09:22 +00:00

55 lines
1.4 KiB
TypeScript

import type { TFunction } from "next-i18next";
import { createAProfileForAnExistingUser } from "@calcom/lib/createAProfileForAnExistingUser";
import prisma from "@calcom/prisma";
import { sendExistingUserTeamInviteEmails } from "@calcom/trpc/server/routers/viewer/teams/inviteMember/utils";
import type { UserWithMembership } from "@calcom/trpc/server/routers/viewer/teams/inviteMember/utils";
/**
* This should only be used in a dsync context
*/
const inviteExistingUserToOrg = async ({
user,
org,
translation,
}: {
user: UserWithMembership;
org: { id: number; name: string; parent: { name: string } | null };
translation: TFunction;
}) => {
await createAProfileForAnExistingUser({
user,
organizationId: org.id,
});
await prisma.user.update({
where: {
id: user.id,
},
data: {
organizationId: org.id,
teams: {
create: {
teamId: org.id,
role: "MEMBER",
// Since coming from directory assume it'll be verified
accepted: true,
},
},
},
});
await sendExistingUserTeamInviteEmails({
currentUserName: user.username,
currentUserTeamName: org.name,
existingUsersWithMemberships: [user],
language: translation,
isOrg: true,
teamId: org.id,
isAutoJoin: true,
currentUserParentTeamName: org?.parent?.name,
});
};
export default inviteExistingUserToOrg;