Files
calendar/packages/features/ee/dsync/lib/createUserAndInviteToOrg.ts
T
41f63582b2 feat: Add booking and user creation source (#18768)
* migration and init to accept creationSource for new bookings

* V1 create booking

* V1 user creation

* webapp booking + V1 user

* user creation in V1, V2 and webapp

* booking source V2 and fix for v1 user

* fit type

* --fix type

* add test -- WIP

* fix type

* fix type

* ^

* Need more sleep zzz

* -_-

* bump libraries platform

* adds for v2 recurring booking

* fix lint

* instant meetings

* fix: api v2 creation source

* fixup! fix: api v2 creation source

* bump libraries

* add user

* fix test

* fixup! fix test

* add more source

* more source...

* fix type & test --1

* fix type & test --2

* typefix

* fixup test

---------

Co-authored-by: Morgan Vernay <morgan@cal.com>
2025-01-27 11:01:33 +01:00

70 lines
1.8 KiB
TypeScript

import type { TFunction } from "next-i18next";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { CreationSource } from "@calcom/prisma/enums";
import { MembershipRole } from "@calcom/prisma/enums";
import type { getTeamOrThrow } from "@calcom/trpc/server/routers/viewer/teams/inviteMember/utils";
import { sendSignupToOrganizationEmail } from "@calcom/trpc/server/routers/viewer/teams/inviteMember/utils";
const createUserAndInviteToOrg = async ({
userEmail,
org,
translation,
}: {
userEmail: string;
org: Awaited<ReturnType<typeof getTeamOrThrow>>;
translation: TFunction;
}) => {
const orgId = org.id;
const [emailUser, emailDomain] = userEmail.split("@");
const username = slugify(`${emailUser}-${emailDomain.split(".")[0]}`);
await prisma.user.create({
data: {
username,
email: userEmail,
// name: event.data?.givenName,
// Assume verified since coming from directory
verified: true,
invitedTo: orgId,
organizationId: orgId,
teams: {
create: {
teamId: orgId,
role: MembershipRole.MEMBER,
accepted: true,
},
},
profiles: {
createMany: {
data: [
{
uid: ProfileRepository.generateProfileUid(),
username,
organizationId: orgId,
},
],
},
},
creationSource: CreationSource.WEBAPP,
},
});
await sendSignupToOrganizationEmail({
usernameOrEmail: userEmail,
team: org,
translation,
inviterName: org.name,
input: {
teamId: orgId,
role: MembershipRole.MEMBER,
usernameOrEmail: userEmail,
language: "en",
isOrg: true,
},
});
};
export default createUserAndInviteToOrg;