Skip to content

Commit

Permalink
perf: Improve routing insights view (#18557)
Browse files Browse the repository at this point in the history
* 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
3 people authored Jan 9, 2025
1 parent b4c18f7 commit 2fd8d24
Showing 1 changed file with 66 additions and 0 deletions.
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";

0 comments on commit 2fd8d24

Please sign in to comment.