-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Improve routing insights view (#18557)
* perf: Improve routing insights view * fix: remove old join --------- Co-authored-by: Eunjae Lee <hey@eunjae.dev> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
- Loading branch information
1 parent
b4c18f7
commit 2fd8d24
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
packages/prisma/migrations/20250109145213_update_routing_form_response/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
CREATE OR REPLACE VIEW "RoutingFormResponse" AS | ||
WITH assignment_reasons_agg AS ( | ||
SELECT | ||
ar."bookingId", | ||
json_agg( | ||
json_build_object( | ||
'reasonString', ar."reasonString" | ||
) | ||
) as reasons | ||
FROM "AssignmentReason" ar | ||
GROUP BY ar."bookingId" | ||
) | ||
SELECT | ||
r.id, | ||
r.response, | ||
( | ||
SELECT jsonb_object_agg( | ||
key, | ||
CASE | ||
WHEN jsonb_typeof(value->'value') = 'string' | ||
THEN jsonb_build_object( | ||
'label', value->'label', | ||
'value', lower((value->>'value')::text) | ||
) | ||
ELSE value | ||
END | ||
) | ||
FROM jsonb_each(r.response::jsonb) | ||
) as "responseLowercase", | ||
f.id as "formId", | ||
f.name as "formName", | ||
f."teamId" as "formTeamId", | ||
f."userId" as "formUserId", | ||
b.uid as "bookingUid", | ||
b.status as "bookingStatus", | ||
CASE b.status | ||
WHEN 'accepted' THEN 1 | ||
WHEN 'pending' THEN 2 | ||
WHEN 'awaiting_host' THEN 3 | ||
WHEN 'cancelled' THEN 4 | ||
WHEN 'rejected' THEN 5 | ||
END as "bookingStatusOrder", | ||
b."createdAt" as "bookingCreatedAt", | ||
b."startTime" as "bookingStartTime", | ||
b."endTime" as "bookingEndTime", | ||
(SELECT | ||
json_agg( | ||
json_build_object( | ||
'name', a.name, 'timeZone', a."timeZone", 'email', a.email | ||
) | ||
) | ||
FROM "Attendee" a | ||
WHERE "a"."bookingId" = b.id | ||
) as "bookingAttendees", | ||
u.id as "bookingUserId", | ||
u.name as "bookingUserName", | ||
u.email as "bookingUserEmail", | ||
u."avatarUrl" as "bookingUserAvatarUrl", | ||
COALESCE((ar.reasons->0)->>'reasonString', '') as "bookingAssignmentReason", | ||
LOWER(COALESCE((ar.reasons->0)->>'reasonString', '')) as "bookingAssignmentReasonLowercase", | ||
r."createdAt" as "createdAt" | ||
FROM "App_RoutingForms_FormResponse" r | ||
LEFT JOIN "App_RoutingForms_Form" f ON r."formId" = f.id | ||
LEFT JOIN "Booking" b ON r."routedToBookingUid" = b.uid | ||
LEFT JOIN "users" u ON b."userId" = u.id | ||
LEFT JOIN assignment_reasons_agg ar ON b.id = ar."bookingId"; |