Skip to content

Commit

Permalink
fix(backend): Use subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Dec 29, 2023
1 parent 7e1f79b commit c8841e1
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions packages/backend/src/services/outreach.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export async function getOutreachTime(
const { items, total } = await db.transaction(async (tx) => {
await tx.execute(sql`SET LOCAL intervalstyle = 'iso_8601'`); // set the interval style to iso_8601

const conditionalAnd = and(
eq(event.type, "Outreach"),
not(arrayOverlaps(user.roles, [env.MENTOR_ROLE_ID])),
isNotNull(rsvp.checkinTime),
isNotNull(rsvp.checkoutTime),
gte(event.startDate, aprilIsoDate)
);

const items = await tx
.select({
/** Username */
Expand All @@ -59,34 +67,36 @@ export async function getOutreachTime(
.orderBy(
sql<string>`sum(${rsvp.checkoutTime} - ${rsvp.checkinTime}) DESC`
)
.where(
and(
eq(event.type, "Outreach"),
not(arrayOverlaps(user.roles, [env.MENTOR_ROLE_ID])),
isNotNull(rsvp.checkinTime),
isNotNull(rsvp.checkoutTime),
gte(event.startDate, aprilIsoDate)
)
)
.where(conditionalAnd)
.limit(limit)
.offset(offset);

let total = 0;
const totalQuery = tx
.select({
/** Username */
username: user.username,
/** UserId */
userId: user.id,
/** Duration (in ISO8601 format) */
duration: sql<string>`sum(${rsvp.checkoutTime} - ${rsvp.checkinTime})`,
})
.from(rsvp)
.groupBy(user.id)
.leftJoin(event, eq(rsvp.eventId, event.id))
.innerJoin(user, eq(rsvp.userId, user.id))
.orderBy(
sql<string>`sum(${rsvp.checkoutTime} - ${rsvp.checkinTime}) DESC`
)
.where(conditionalAnd)
.as("totalQuery");

const [firstData] = await tx
.select({ total: count() })
.from(user)
.leftJoin(rsvp, eq(user.id, rsvp.userId))
.leftJoin(event, eq(rsvp.eventId, event.id))
.where(
and(
eq(event.type, "Outreach"),
not(arrayOverlaps(user.roles, [env.MENTOR_ROLE_ID])),
isNotNull(rsvp.checkinTime),
isNotNull(rsvp.checkoutTime),
gte(event.startDate, aprilIsoDate)
)
);
.select({
total: count(),
})
.from(totalQuery);

let total = 0;

if (firstData) {
total = firstData.total;
Expand Down

0 comments on commit c8841e1

Please sign in to comment.