Skip to content

Commit 2303eb7

Browse files
authored
fix: add uuid to App_RoutingForms_FormResponse (calcom#21910)
* fix: add uuid to App_RoutingForms_FormResponse * update RoutingFormResponseDenormalized * fix type error
1 parent c3666dd commit 2303eb7

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

packages/app-store/routing-forms/lib/handleResponse.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ describe("handleResponse", () => {
139139
it("should record form response when not in preview and not queued", async () => {
140140
const dbFormResponse = {
141141
id: 1,
142+
uuid: "d8b4b7d2-3f45-4f67-9aa1-98c4b49cf283",
142143
formId: mockForm.id,
143144
response: mockResponse,
144145
chosenRouteId: null,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
-- Step 1: Add column as nullable
2+
ALTER TABLE "App_RoutingForms_FormResponse" ADD COLUMN "uuid" TEXT;
3+
4+
-- Step 2: Populate existing rows with UUIDs
5+
UPDATE "App_RoutingForms_FormResponse" SET "uuid" = gen_random_uuid()::text WHERE "uuid" IS NULL;
6+
7+
-- Step 3: Make column NOT NULL
8+
ALTER TABLE "App_RoutingForms_FormResponse" ALTER COLUMN "uuid" SET NOT NULL;
9+
10+
-- Step 4: Add unique constraint
11+
CREATE UNIQUE INDEX "App_RoutingForms_FormResponse_uuid_key" ON "App_RoutingForms_FormResponse"("uuid");
12+
13+
-- Step 5: Add UUID to RoutingFormResponseDenormalized
14+
ALTER TABLE "RoutingFormResponseDenormalized" ADD COLUMN "uuid" TEXT;
15+
16+
-- Step 6: Populate existing rows with UUIDs
17+
UPDATE "RoutingFormResponseDenormalized" d
18+
SET "uuid" = f."uuid"
19+
FROM "App_RoutingForms_FormResponse" f
20+
WHERE d."id" = f."id";
21+
22+
-- Step 7: Make column NOT NULL
23+
ALTER TABLE "RoutingFormResponseDenormalized" ALTER COLUMN "uuid" SET NOT NULL;
24+
25+
-- Step 8: Add unique constraint
26+
CREATE UNIQUE INDEX "RoutingFormResponseDenormalized_uuid_key" ON "RoutingFormResponseDenormalized"("uuid");
27+
28+
-- Step 9: Add UUID to RoutingFormResponseDenormalized
29+
CREATE OR REPLACE FUNCTION refresh_routing_form_response_denormalized(response_id INTEGER)
30+
RETURNS VOID AS $$
31+
BEGIN
32+
-- Delete existing entry if any
33+
DELETE FROM "RoutingFormResponseDenormalized" WHERE id = response_id;
34+
35+
-- Insert form response with all related data
36+
INSERT INTO "RoutingFormResponseDenormalized" (
37+
id,
38+
uuid,
39+
"formId",
40+
"formName",
41+
"formTeamId",
42+
"formUserId",
43+
"bookingUid",
44+
"bookingId",
45+
"bookingStatus",
46+
"bookingStatusOrder",
47+
"bookingCreatedAt",
48+
"bookingStartTime",
49+
"bookingEndTime",
50+
"bookingUserId",
51+
"bookingUserName",
52+
"bookingUserEmail",
53+
"bookingUserAvatarUrl",
54+
"bookingAssignmentReason",
55+
"eventTypeId",
56+
"eventTypeParentId",
57+
"eventTypeSchedulingType",
58+
"createdAt",
59+
"utm_source",
60+
"utm_medium",
61+
"utm_campaign",
62+
"utm_term",
63+
"utm_content"
64+
)
65+
SELECT
66+
r.id,
67+
r."uuid",
68+
r."formId",
69+
f.name as "formName",
70+
f."teamId" as "formTeamId",
71+
f."userId" as "formUserId",
72+
b.uid as "bookingUid",
73+
b.id as "bookingId",
74+
b.status as "bookingStatus",
75+
calculate_booking_status_order(b.status::text) as "bookingStatusOrder",
76+
b."createdAt" as "bookingCreatedAt",
77+
b."startTime" as "bookingStartTime",
78+
b."endTime" as "bookingEndTime",
79+
b."userId" as "bookingUserId",
80+
u.name as "bookingUserName",
81+
u.email as "bookingUserEmail",
82+
u."avatarUrl" as "bookingUserAvatarUrl",
83+
COALESCE(
84+
(
85+
SELECT ar."reasonString"
86+
FROM "AssignmentReason" ar
87+
WHERE ar."bookingId" = b.id
88+
LIMIT 1
89+
),
90+
''
91+
) as "bookingAssignmentReason",
92+
et.id as "eventTypeId",
93+
et."parentId" as "eventTypeParentId",
94+
et."schedulingType" as "eventTypeSchedulingType",
95+
r."createdAt",
96+
t.utm_source,
97+
t.utm_medium,
98+
t.utm_campaign,
99+
t.utm_term,
100+
t.utm_content
101+
FROM "App_RoutingForms_FormResponse" r
102+
INNER JOIN "App_RoutingForms_Form" f ON r."formId" = f.id
103+
INNER JOIN "users" u ON f."userId" = u.id
104+
LEFT JOIN "Booking" b ON b.uid = r."routedToBookingUid"
105+
LEFT JOIN "EventType" et ON b."eventTypeId" = et.id
106+
LEFT JOIN "Tracking" t ON t."bookingId" = b.id
107+
WHERE r.id = response_id;
108+
END;
109+
$$ LANGUAGE plpgsql;

packages/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ model App_RoutingForms_Form {
11591159

11601160
model App_RoutingForms_FormResponse {
11611161
id Int @id @default(autoincrement())
1162+
uuid String @unique @default(uuid())
11621163
formFillerId String @default(cuid())
11631164
form App_RoutingForms_Form @relation(fields: [formId], references: [id], onDelete: Cascade)
11641165
formId String
@@ -1239,6 +1240,7 @@ view RoutingFormResponse {
12391240

12401241
model RoutingFormResponseDenormalized {
12411242
id Int @id
1243+
uuid String @unique
12421244
formId String
12431245
formName String
12441246
formTeamId Int?

0 commit comments

Comments
 (0)