* 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>
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { createOrUpdateMemberships } from "@calcom/features/auth/signup/utils/createOrUpdateMemberships";
|
|
import slugify from "@calcom/lib/slugify";
|
|
import prisma from "@calcom/prisma";
|
|
import type { IdentityProvider } from "@calcom/prisma/enums";
|
|
import { CreationSource } from "@calcom/prisma/enums";
|
|
|
|
import dSyncUserSelect from "./dSyncUserSelect";
|
|
|
|
type createUsersAndConnectToOrgPropsType = {
|
|
emailsToCreate: string[];
|
|
organizationId: number;
|
|
identityProvider: IdentityProvider;
|
|
identityProviderId: string | null;
|
|
};
|
|
|
|
const createUsersAndConnectToOrg = async (
|
|
createUsersAndConnectToOrgProps: createUsersAndConnectToOrgPropsType
|
|
) => {
|
|
const { emailsToCreate, organizationId, identityProvider, identityProviderId } =
|
|
createUsersAndConnectToOrgProps;
|
|
// As of Mar 2024 Prisma createMany does not support nested creates and returning created records
|
|
await prisma.user.createMany({
|
|
data: emailsToCreate.map((email) => {
|
|
const [emailUser, emailDomain] = email.split("@");
|
|
const username = slugify(`${emailUser}-${emailDomain.split(".")[0]}`);
|
|
const name = username
|
|
.split("-")
|
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
.join(" ");
|
|
return {
|
|
username,
|
|
email,
|
|
name,
|
|
// Assume verified since coming from directory
|
|
verified: true,
|
|
emailVerified: new Date(),
|
|
invitedTo: organizationId,
|
|
organizationId,
|
|
identityProvider,
|
|
identityProviderId,
|
|
creationSource: CreationSource.WEBAPP,
|
|
};
|
|
}),
|
|
});
|
|
|
|
const users = await prisma.user.findMany({
|
|
where: {
|
|
email: {
|
|
in: emailsToCreate,
|
|
},
|
|
},
|
|
select: dSyncUserSelect,
|
|
});
|
|
// Assign created users to organization
|
|
for (const user of users) {
|
|
await createOrUpdateMemberships({
|
|
user,
|
|
team: {
|
|
id: organizationId,
|
|
isOrganization: true,
|
|
parentId: null, // orgs don't have a parentId
|
|
},
|
|
});
|
|
}
|
|
return users;
|
|
};
|
|
|
|
export default createUsersAndConnectToOrg;
|