Files
calendar/packages/prisma/seed-utils.ts
T
c28eb90928 chore: Upgrade prisma to 6.7.0 (#21264)
* chore: Upgrade prisma to 6.7.0

* Build fixes

* type fixes

Signed-off-by: Omar López <zomars@me.com>

* Update schema.prisma

* Patching

* Revert "Update schema.prisma"

This reverts commit 47d8618bf89ef4d007b30084df766f17281e21a1.

* Revert "Patching"

This reverts commit a1d2e3040e71690a44d4324db95d73b4d68c6adb.

* Revert schema changes

Signed-off-by: Omar López <zomars@me.com>

* WIP

Signed-off-by: Omar López <zomars@me.com>

* Update getPublicEvent.ts

* Update imports

Signed-off-by: Omar López <zomars@me.com>

* Update gitignore

Signed-off-by: Omar López <zomars@me.com>

* update remaining imports

Signed-off-by: Omar López <zomars@me.com>

* Delete .cursor/config.json

* Discard changes to packages/features/eventtypes/lib/getPublicEvent.ts

* Update _get.ts

* Update user.ts

* Update .gitignore

* update

* Update WorkflowStepContainer.tsx

* Update next-auth-custom-adapter.ts

* Update getPublicEvent.ts

* Update workflow.ts

* Update next-auth-custom-adapter.ts

* Update next-auth-options.ts

* Update bookingScenario.ts

* fix missing imports

* upgrades prismock

Signed-off-by: Omar López <zomars@me.com>

* patches prismock

Signed-off-by: Omar López <zomars@me.com>

* Update reschedule.test.ts

* Update prisma.ts

* patch prismock

Signed-off-by: Omar López <zomars@me.com>

* fix enums imports

Signed-off-by: Omar López <zomars@me.com>

* Revert "Update prisma.ts"

This reverts commit 64edcf8db54171ff4456c209d563b5d431d99619.

* Revert "patch prismock"

This reverts commit e95819113dc9d88e7130947aa120cd42710977c8.

* fix patch

* Fix test that overrun the boundary, it shouldn't test too much

* Move prisma import to changeSMSLockState

* Bring back broken test without illegal imports

* Merge with main and fix filter hosts by same round robin host

* Fixed buildDryRunBooking fn tests

* Fix and move ooo create or update handler test

* Fix packages/features/eventtypes/lib/isCurrentlyAvailable.test.ts

* Fix packages/trpc/server/routers/viewer/organizations/listMembers.handler.test.ts

* Mock @calcom/prisma

* Fix: verify-email.test.ts

* fix: Moved WebhookService test and fixed default import mock

* Fix: Added missing prisma mock, handleNewBooking uses that of course

* We're not testing createContext here

* fix: Prisma mock fix for listMembers.test.ts

* More fixes to broken testcases

* Forgot to remove borked test

* Prevent the need to mock a lot of dependencies by moving out buildBaseWhereCondition to its own file

* Temporarily skip getCalendarEvents, needs a rewrite

* Fix: turns out you can access protected in testcases

* fix further mocks

* Added packages/features/insights/server/buildBaseWhereCondition.ts, types

* Always great to have a mock and then not use it

* And one less again.

* fix: confirm.handler.test, didn't mock prisma

* fix: Address minor nit by @eunjae & fix ImpersonationProvider test

* Updated isPrismaAvailableCheck that doesn't crash on import

* fix: Get Prisma directly from the client, it usually involves the Validator and does not need 'local' inclusion

* Add zod-prisma-types without the generator enabled (commented out)

* Uncomment and see what happens

* Change method of import as imports did not work in Input Schemas

* Remove custom 'zod' booking model, it does not belong with Prisma

* Fix all other global Model imports

* Rewrite most schema includes AND remove barrel file

* Add bookingCreateBodySchema to features/bookings

* Flurry of type fixes for compatibility with new zod gen

* Refactor out the custom prisma type createEventTypeInput

* Work around nullable eventTypeLocations

* HandlePayment type fix

* More fixes, final fix remaining is CompleteEventType

* Should fix a bunch more booking related type errors

* Missed one

* Some props missing from BookingCreateBodySchema

* Fix location type in handleChildrenEventTypes

* Little bit hacky imo but it works

* Final type error \o/

* Forgot to include Prisma

* Do not include zod-utils in booker/types

* Oops, was already including Booker/types

* Fix membership type, also disallow updating createdAt/updatedAt, make part of patch/post

* Fix api v1 type errors

* Fix EventTypeDescription typings

* Remove getParserWithGeneric, use userBodySchema with UserSchema

* use centralized timeZoneSchema

* Implement feedback by @zomars

* Couple of WIP pushes

* Fix tests

* Type fixes in `handleChildrenEventTypes` test

* Try and parse metadata before use

* Change zod-prisma-types configuration for optimal performance

* Fix prisma validator error in `prisma/selects/credential`

* Disable seperate relations model, hits a bug

* Import absolute - this makes rollup work in @platform/libraries

* Attempt at removing resolutions override

* Refactor using `Prisma.validator` to `satisfies`

* Build atoms using @calcom/prisma/client

* Build atoms using @calcom/prisma/client

* fixes

* Update eventTypeSelect.ts

* Adjust `eventTypeMetaDataSchemaWithUntypedApps` from `unknown` to `record(any)`

* `EventTypeDescription` rely on `descriptionAsSafeHTML` instead of `description`

* Add `seatsPerTimeSlot` to event type public select

* Fix typing in `users-public-view` getServerSide props

* Add missing `schedulingType` to prop

* chore: bump platform libraries

* Function return type is illegal, not sure how this passed eslint (#21567)

* Merged with main

* Update updateTokenObject.ts

* Update handleResponse.ts

* Update index.ts

* Update handleChildrenEventTypes.ts

* Update booking-idempotency-key.ts

* Update WebhookService.test.ts

* Update events.test.ts

* Update queued-response.test.ts

* Update events.test.ts

* Update getRoutedUrl.test.ts

* fix: type checks

Signed-off-by: Omar López <zomars@me.com>

* fixes

Signed-off-by: Omar López <zomars@me.com>

* chore: bump platform libraries

* Update yarn.lock

* more fixes

Signed-off-by: Omar López <zomars@me.com>

* fixes

Signed-off-by: Omar López <zomars@me.com>

* biuld fixes

* chore: bump platform libraries

* Update conferencing.repository.ts

* Update conferencing.repository.ts

* Update getCalendarsEvents.test.ts

* Update vite.config.js

* chore: bump platform libraries

* Update users.ts

* Discard changes to docs/api-reference/v2/openapi.json

* Update vite.config.ts

* updated platform libraries

* Update get.handler.test.ts

* Update get.handler.test.ts

* Update schema.prisma

* Discard changes to docs/api-reference/v2/openapi.json

* Update next-auth-custom-adapter.ts

* Update team.ts

* Flurry of type fixes

* Fix majority of insight related type errors

* Type fixes for unlink of account

* Make user nullable again

* Fixed a bunch of unit tests and one type error

* Attempted mock fix

* Attempted fix for Attribute type

* Ensure default import becomes prisma, but not direct usage

* Import default as prisma in prisma.module

* Add attributeOption to attribute type

* Fix calcom/prisma mock

* Refactor Prisma client imports to @calcom/prisma/client

Updated all imports from '@prisma/client' to '@calcom/prisma/client' across tests and repository files for consistency and to use the correct Prisma client package. This change improves maintainability and ensures the correct client is referenced throughout the codebase.

* Undo removal of max-warnings=0 to get main to merge

* Remove unit tests for e2e fixtures, provide new prisma mock

* Mock @calcom/prisma in event manager

* Mock @calcom/prisma in event manager

* Add correct format even with --no-verify

* Mock prisma in CalendarManager

* Add mock for permission-check.service

* Better injection in PrismaApiKeyRepository imports

* More mock fixes :)

* Fix listMembers.handler.test

* Fix User import

* Appropriately adjust all types to be imported as types, there were a lot of types imported as normal deps

* Why was this a thing?

* Strictly speaking; Not using prismock anymore

* Ditched patch file for prismock

* Fix output.service.ts platform type imports, need concrete for plainToClass

* Better typing and tests for unlinkConnectedAccount.handler

* Small type fix

* Disable calendar cache tests as they are dependent on prismock

* chore: bump platform lib

* getRoutedUrl test remove of unused import

* Extract select to external const on getEventTypesFromDB

* Direct select of userSelect from selects/user

* fix type error from merging 23653

* Fixed integration tests by removing hardcoded values that were possible due to mocking, but as its now directly hitting the db no longer

* fix: vite config atoms prisma client type location

* revert: example app prisma client

* revert: example app prisma client

* bump platform libs

* fix: use class instead of type for DI of PlatformBookingsService

* update platform libs

* remove unused variable

* chore: generate prisma client for api v2

* fix: api v2 e2e

* fix: atoms e2e

* fix: atoms e2e

* fix: atoms e2e

* fix: api v2 e2e

* fix: tsconfig apiv2 enums

* publish libraries

* Simplify check for existence teamId

---------

Signed-off-by: Omar López <zomars@me.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com>
Co-authored-by: supalarry <laurisskraucis@gmail.com>
Co-authored-by: cal.com <morgan@cal.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
2025-09-11 15:27:50 +01:00

789 lines
22 KiB
TypeScript

import { faker } from "@faker-js/faker";
import { randomUUID } from "crypto";
import { uuid } from "short-uuid";
import type z from "zod";
import dayjs from "@calcom/dayjs";
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "@calcom/lib/availability";
import { MembershipRole } from "@calcom/prisma/enums";
import prisma from ".";
import type { Prisma, UserPermissionRole } from "./client";
import type { teamMetadataSchema } from "./zod-utils";
export async function createUserAndEventType({
user,
eventTypes = [],
credentials,
}: {
user: {
email: string;
password: string;
username: string;
name: string;
completedOnboarding?: boolean;
timeZone?: string;
role?: UserPermissionRole;
theme?: "dark" | "light";
avatarUrl?: string | null;
};
eventTypes?: Array<
Prisma.EventTypeUncheckedCreateInput & {
_bookings?: Prisma.BookingCreateInput[];
_numBookings?: number;
}
>;
credentials?: ({
type: string;
key: Prisma.JsonObject;
appId: string;
} | null)[];
}) {
const { password: _password, ...restOfUser } = user;
const userData = {
...restOfUser,
emailVerified: new Date(),
completedOnboarding: user.completedOnboarding ?? true,
locale: "en",
schedules:
user.completedOnboarding ?? true
? {
create: {
name: "Working Hours",
availability: {
createMany: {
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE),
},
},
},
}
: undefined,
};
const theUser = await prisma.user.upsert({
where: { email_username: { email: user.email, username: user.username } },
update: userData,
create: userData,
});
await prisma.userPassword.upsert({
where: { userId: theUser.id },
update: {
hash: await hashPassword(user.password),
},
create: {
hash: await hashPassword(user.password),
user: {
connect: {
id: theUser.id,
},
},
},
});
console.log(
`👤 Upserted '${user.username}' with email "${user.email}" & password "${user.password}". Booking page 👉 ${process.env.NEXT_PUBLIC_WEBAPP_URL}/${user.username}`
);
for (const eventTypeInput of eventTypes) {
const { _bookings, _numBookings, ...eventTypeData } = eventTypeInput;
let bookingFields;
if (_bookings && _numBookings) {
throw new Error("You can't set both _bookings and _numBookings");
} else if (_numBookings) {
bookingFields = [...Array(_numBookings).keys()].map((i) => ({
startTime: dayjs()
.add(1, "day")
.add(i * 5 + 0, "minutes")
.toDate(),
endTime: dayjs()
.add(1, "day")
.add(i * 5 + 30, "minutes")
.toDate(),
title: `${eventTypeInput.title}:${i + 1}`,
uid: uuid(),
}));
} else {
bookingFields = _bookings || [];
}
eventTypeData.userId = theUser.id;
eventTypeData.users = { connect: { id: theUser.id } };
const eventType = await prisma.eventType.findFirst({
where: {
slug: eventTypeData.slug,
users: {
some: {
id: eventTypeData.userId,
},
},
},
select: {
id: true,
},
});
if (eventType) {
console.log(
`\t📆 Event type ${eventTypeData.slug} already seems seeded - ${process.env.NEXT_PUBLIC_WEBAPP_URL}/${user.username}/${eventTypeData.slug}`
);
continue;
}
const { id } = await prisma.eventType.create({
data: eventTypeData,
});
console.log(
`\t📆 Event type ${eventTypeData.slug} with id ${id}, length ${eventTypeData.length}min - ${process.env.NEXT_PUBLIC_WEBAPP_URL}/${user.username}/${eventTypeData.slug}`
);
for (const bookingInput of bookingFields) {
await prisma.booking.create({
data: {
...bookingInput,
user: {
connect: {
email: user.email,
},
},
attendees: {
create: {
email: user.email,
name: user.name,
timeZone: "Europe/London",
},
},
eventType: {
connect: {
id,
},
},
status: bookingInput.status,
iCalUID: "",
},
});
console.log(
`\t\t☎️ Created booking ${bookingInput.title} at ${new Date(
bookingInput.startTime
).toLocaleDateString()}`
);
}
}
console.log("👤 User with it's event-types and bookings created", theUser.email);
if (credentials) {
for (const credential of credentials) {
if (credential) {
await prisma.credential.create({
data: {
...credential,
userId: theUser.id,
},
});
console.log(`🔑 ${credential.type} credentials created for ${theUser.email}`);
}
}
}
return theUser;
}
export async function createTeamAndAddUsers(
teamInput: Prisma.TeamCreateInput,
users: { id: number; username: string; role?: MembershipRole }[] = []
) {
const checkUnpublishedTeam = async (slug: string) => {
return await prisma.team.findFirst({
where: {
metadata: {
path: ["requestedSlug"],
equals: slug,
},
},
});
};
const createTeam = async (team: Prisma.TeamCreateInput) => {
try {
const requestedSlug = (team.metadata as z.infer<typeof teamMetadataSchema>)?.requestedSlug;
if (requestedSlug) {
const unpublishedTeam = await checkUnpublishedTeam(requestedSlug);
if (unpublishedTeam) {
throw Error("Unique constraint failed on the fields");
}
}
return await prisma.team.create({
data: {
...team,
},
include: {
eventTypes: true,
},
});
} catch (_err) {
if (_err instanceof Error && _err.message.indexOf("Unique constraint failed on the fields") !== -1) {
console.log(`Team '${team.name}' already exists, skipping.`);
return;
}
throw _err;
}
};
const team = await createTeam(teamInput);
if (!team) {
return;
}
console.log(
`🏢 Created team '${teamInput.name}' - ${process.env.NEXT_PUBLIC_WEBAPP_URL}/team/${team.slug}`
);
for (const eventType of team.eventTypes) {
await prisma.eventType.update({
where: {
id: eventType.id,
},
data: {
users: {
connect: users.map((user) => ({ id: user.id })),
},
},
});
}
for (const user of users) {
const { role = MembershipRole.OWNER, id, username } = user;
await prisma.membership.create({
data: {
createdAt: new Date(),
teamId: team.id,
userId: id,
role: role,
accepted: true,
},
});
console.log(`\t👤 Added '${teamInput.name}' membership for '${username}' with role '${role}'`);
}
return team;
}
export async function seedAttributes(teamId: number) {
const mockAttributes = [
{
name: "Department",
type: "SINGLE_SELECT",
options: ["Engineering", "Sales", "Marketing", "Product", "Design"],
},
{
name: "Location",
type: "SINGLE_SELECT",
options: ["New York", "London", "Tokyo", "Berlin", "Remote"],
},
{
name: "Skills",
type: "MULTI_SELECT",
options: ["JavaScript", "React", "Node.js", "Python", "Design", "Sales"],
},
{
name: "Years of Experience",
type: "NUMBER",
},
{
name: "Bio",
type: "TEXT",
},
];
// Check if attributes already exist
const existingAttributes = await prisma.attribute.findMany({
where: {
teamId: teamId,
name: {
in: mockAttributes.map((attr) => attr.name),
},
},
});
if (existingAttributes.length > 0) {
console.log(`Skipping attributes seed, attributes already exist`);
return;
}
// Get team members
const memberships = await prisma.membership.findMany({
where: {
teamId: teamId,
},
select: {
id: true,
userId: true,
},
});
console.log(`🎯 Creating attributes for team ${teamId}`);
const attributeRaw: { id: string; options: { id: string; value: string }[] }[] = [];
for (const attr of mockAttributes) {
const attribute = await prisma.attribute.create({
data: {
name: attr.name,
slug: attr.name.toLowerCase().replace(/ /g, "-"),
type: attr.type as "TEXT" | "NUMBER" | "SINGLE_SELECT" | "MULTI_SELECT",
teamId: teamId,
enabled: true,
options: attr.options
? {
create: attr.options.map((opt) => ({
value: opt,
slug: opt.toLowerCase().replace(/ /g, "-"),
})),
}
: undefined,
},
include: {
options: true,
},
});
attributeRaw.push({
id: attribute.id,
options: attribute.options.map((opt) => ({
id: opt.id,
value: opt.value,
})),
});
console.log(`\t📝 Created attribute: ${attr.name}`);
// Assign random values/options to members
for (const member of memberships) {
if (attr.type === "TEXT") {
const mockText = `Sample ${attr.name.toLowerCase()} text for user ${member.userId}`;
await prisma.attributeOption.create({
data: {
value: mockText,
slug: mockText.toLowerCase().replace(/ /g, "-"),
attribute: {
connect: {
id: attribute.id,
},
},
assignedUsers: {
create: {
memberId: member.id,
},
},
},
});
} else if (attr.type === "NUMBER") {
const mockNumber = Math.floor(Math.random() * 10 + 1).toString();
await prisma.attributeOption.create({
data: {
value: mockNumber,
slug: mockNumber,
attribute: {
connect: {
id: attribute.id,
},
},
assignedUsers: {
create: {
memberId: member.id,
},
},
},
});
} else if (attr.type === "SINGLE_SELECT" && attribute.options.length > 0) {
const randomOption = attribute.options[Math.floor(Math.random() * attribute.options.length)];
await prisma.attributeToUser.create({
data: {
memberId: member.id,
attributeOptionId: randomOption.id,
},
});
} else if (attr.type === "MULTI_SELECT" && attribute.options.length > 0) {
// Assign 1-3 random options
const numOptions = Math.floor(Math.random() * 3) + 1;
const shuffledOptions = [...attribute.options].sort(() => Math.random() - 0.5);
const selectedOptions = shuffledOptions.slice(0, numOptions);
for (const option of selectedOptions) {
await prisma.attributeToUser.create({
data: {
memberId: member.id,
attributeOptionId: option.id,
},
});
}
}
}
console.log(`\t✅ Assigned ${attr.name} values to ${memberships.length} members`);
}
return attributeRaw;
}
export async function seedRoutingForms(
teamId: number,
userId: number,
attributeRaw: { id: string; options: { id: string; value: string }[] }[],
javascriptEventId: number,
salesEventId: number
) {
const seededForm = {
id: "948ae412-d995-4865-885a-48302588de03",
name: `Seeded Form - Insights for ${teamId}`,
routes: [
{
id: "8a898988-89ab-4cde-b012-31823f708642",
value: "team/insights-team/team-javascript",
eventTypeId: javascriptEventId,
},
{
id: "8b2224b2-89ab-4cde-b012-31823f708642",
value: "team/insights-team/team-sales",
eventTypeId: salesEventId,
},
],
formFieldLocation: {
id: "674c169a-e40a-492c-b4bb-6f5213873bd6",
},
formFieldSkills: {
id: "83316968-45bf-4c9d-b5d4-5368a8d2d2a8",
},
formFieldEmail: {
id: "dd28ffcf-7029-401e-bddb-ce2e7496a1c1",
},
formFieldManager: {
id: "57734f65-8bbb-4065-9e71-fb7f0b7485f8",
},
formFieldRating: {
id: "f4e9fa6c-5c5d-4d8e-b15c-7f37e9d0c729",
},
};
const form = await prisma.app_RoutingForms_Form.findUnique({
where: {
id: seededForm.id,
},
});
if (form) {
console.log(`Skipping Routing Form - Form Seed, ${seededForm.name} already exists`);
return;
}
if (attributeRaw.length === 0) {
throw new Error("No attributes found - Please call seedAttributes first");
}
const formFieldSkillsOptions = attributeRaw[2].options.map((opt) => ({
id: opt.id,
label: opt.value,
}));
const formFieldLocationOptions = attributeRaw[1].options.map((opt) => ({
id: opt.id,
label: opt.value,
}));
await prisma.app_RoutingForms_Form.create({
data: {
id: seededForm.id,
routes: [
{
id: seededForm.routes[0].id,
action: {
type: "eventTypeRedirectUrl",
value: seededForm.routes[0].value,
eventTypeId: seededForm.routes[0].eventTypeId,
},
queryValue: {
id: "aaba9988-cdef-4012-b456-719300f53ef8",
type: "group",
children1: {
"b98b98a8-0123-4456-b89a-b19300f55277": {
type: "rule",
properties: {
field: seededForm.formFieldSkills.id,
value: [
formFieldSkillsOptions.filter((opt) => opt.label === "JavaScript").map((opt) => opt.id),
],
operator: "multiselect_equals",
valueSrc: ["value"],
valueType: ["multiselect"],
valueError: [null],
},
},
},
},
attributesQueryValue: {
id: "ab99bbb9-89ab-4cde-b012-319300f53ef8",
type: "group",
children1: {
"b98b98a8-0123-4456-b89a-b19300f55277": {
type: "rule",
properties: {
field: attributeRaw[2].id,
value: [
attributeRaw[2].options.filter((opt) => opt.value === "JavaScript").map((opt) => opt.id),
],
operator: "multiselect_some_in",
valueSrc: ["value"],
valueType: ["multiselect"],
valueError: [null],
},
},
},
},
},
{
id: seededForm.routes[1].id,
action: {
type: "eventTypeRedirectUrl",
value: seededForm.routes[1].value,
eventTypeId: seededForm.routes[1].eventTypeId,
},
queryValue: {
id: "aaba9948-cdef-4012-b456-719300f53ef8",
type: "group",
children1: {
"c98b98a8-1123-4456-e89a-a19300f55277": {
type: "rule",
properties: {
field: seededForm.formFieldSkills.id,
value: [formFieldSkillsOptions.filter((opt) => opt.label === "Sales").map((opt) => opt.id)],
operator: "multiselect_equals",
valueSrc: ["value"],
valueType: ["multiselect"],
valueError: [null],
},
},
},
},
attributesQueryValue: {
id: "ab988888-89ab-4cde-b012-319300f53ef8",
type: "group",
children1: {
"b98b98a12-0123-4456-b89a-b19300f55277": {
type: "rule",
properties: {
field: attributeRaw[2].id,
value: [
attributeRaw[2].options.filter((opt) => opt.value === "Sales").map((opt) => opt.id),
],
operator: "multiselect_some_in",
valueSrc: ["value"],
valueType: ["multiselect"],
valueError: [null],
},
},
},
},
fallbackAttributesQueryValue: {
id: "a9888488-4567-489a-bcde-f19300f53ef8",
type: "group",
},
},
{
id: "148899aa-4567-489a-bcde-f1823f708646",
action: { type: "customPageMessage", value: "Fallback Message" },
isFallback: true,
queryValue: { id: "814899aa-4567-489a-bcde-f1823f708646", type: "group" },
},
],
fields: [
{
id: seededForm.formFieldLocation.id,
type: "select",
label: "Location",
options: formFieldLocationOptions,
required: true,
},
{
id: seededForm.formFieldSkills.id,
type: "multiselect",
label: "skills",
options: formFieldSkillsOptions,
required: true,
},
{
id: seededForm.formFieldEmail.id,
type: "email",
label: "Email",
required: true,
},
{
id: seededForm.formFieldManager.id,
type: "text",
label: "Manager",
required: true,
},
{
id: seededForm.formFieldRating.id,
type: "number",
label: "Rating",
required: true,
},
],
team: {
connect: {
id: teamId,
},
},
user: {
connect: {
id: userId,
},
},
name: seededForm.name,
},
});
return seededForm;
}
type SeededForm = {
id: string;
name: string;
routes: {
id: string;
value: string;
}[];
formFieldLocation: {
id: string;
};
formFieldSkills: {
id: string;
};
formFieldEmail: {
id: string;
};
formFieldManager: {
id: string;
};
formFieldRating: {
id: string;
};
};
// id formFillerId formId response createdAt routedToBookingUid
// 1 cm35rrf1x0001rqjskhydev9i 948ae412-d995-4865-885a-48302588de03 "{""83316968-45bf-4c9d-b5d4-5368a8d2d2a8"": {""label"": ""skills"", ""value"": [""8d841a71-3b96-4930-9e2d-57a3584ec21b""]}}" 2024-11-06 11:01:56.853
export async function seedRoutingFormResponses(
seededForm: SeededForm,
attributeRaw: { id: string; options: { id: string; value: string }[] }[],
teamId: number
) {
// Get all bookings for this team
const bookings = await prisma.booking.findMany({
where: {
eventType: {
teamId: teamId,
},
},
take: 1000, // Limit to 1000 responses
select: {
id: true,
uid: true,
},
});
if (bookings.length === 0) {
console.log("No bookings found for team - skipping routing form responses");
return;
}
// Create routing form responses linked to random bookings
for (const booking of bookings) {
// Randomly select 1-3 skills from the form field options
const numSkills = Math.floor(Math.random() * 3) + 1;
const shuffledSkillOptions = [...attributeRaw[2].options].sort(() => Math.random() - 0.5);
const selectedSkills = shuffledSkillOptions.slice(0, numSkills);
const selectedLocation =
attributeRaw[1].options[Math.floor(Math.random() * attributeRaw[1].options.length)];
// Generate a random date within the last 30 days
const randomDate = dayjs()
.subtract(Math.floor(Math.random() * 30), "days")
.subtract(Math.floor(Math.random() * 24), "hours")
.subtract(Math.floor(Math.random() * 60), "minutes");
// Create the form response with the routedToBookingUid field set
const response = await prisma.app_RoutingForms_FormResponse.create({
data: {
formId: seededForm.id,
formFillerId: randomUUID(),
createdAt: randomDate.toDate(),
response: {
[seededForm.formFieldLocation.id]: {
label: "Location",
value: selectedLocation.id,
},
[seededForm.formFieldSkills.id]: {
label: "skills",
value: selectedSkills.map((opt) => opt.id),
},
[seededForm.formFieldEmail.id]: {
label: "Email",
value: faker.internet.email(),
},
[seededForm.formFieldManager.id]: {
label: "Manager",
value: faker.person.fullName(),
},
[seededForm.formFieldRating.id]: {
label: "Rating",
value: Math.floor(Math.random() * 5) + 1,
},
},
},
});
// Update the response with the booking UID
await prisma.booking.update({
where: {
id: booking.id,
},
data: {
routedFromRoutingFormReponse: { connect: { id: response.id } },
startTime: randomDate.toDate(),
endTime: randomDate.add(1, "hour").toDate(),
createdAt: randomDate.toDate(),
},
});
await prisma.app_RoutingForms_FormResponse.create({
data: {
formId: seededForm.id,
formFillerId: randomUUID(),
createdAt: randomDate.subtract(2, "hour").toDate(),
response: {
[seededForm.formFieldLocation.id]: {
label: "Location",
value: selectedLocation.id,
},
[seededForm.formFieldSkills.id]: {
label: "skills",
value: selectedSkills.map((opt) => opt.id),
},
[seededForm.formFieldEmail.id]: {
label: "Email",
value: faker.internet.email(),
},
[seededForm.formFieldManager.id]: {
label: "Manager",
value: faker.person.fullName(),
},
[seededForm.formFieldRating.id]: {
label: "Rating",
value: Math.floor(Math.random() * 5) + 1,
},
},
},
});
}
console.log(`Created ${bookings.length} routing form responses`);
}