Skip to content

Commit

Permalink
fix(backend): Incl total in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Dec 29, 2023
1 parent 05afff1 commit 474f5fb
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions packages/backend/src/services/outreach.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,11 @@ export async function getOutreachTime(
throw new Error("Could not convert last April 25th to ISO date");
}

const [totalData] = await db
.select({
total: count(),
})
.from(rsvp)
.groupBy(user.id)
.leftJoin(event, eq(rsvp.eventId, event.id))
.innerJoin(user, eq(rsvp.userId, user.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)
)
);

let total = 0;

if (totalData) {
total = totalData.total;
console.log({ total });
}

const offset = page * limit;
const transactionResult = await db.transaction(async (tx) => {
const { items, total } = await db.transaction(async (tx) => {
await tx.execute(sql`SET LOCAL intervalstyle = 'iso_8601'`); // set the interval style to iso_8601

return tx
const items = await tx
.select({
/** Username */
username: user.username,
Expand All @@ -95,10 +70,36 @@ export async function getOutreachTime(
)
.limit(limit)
.offset(offset);

let total = 0;

const [firstData] = await tx
.select({ total: count() })
.from(rsvp)
.leftJoin(event, eq(rsvp.eventId, event.id))
.innerJoin(user, eq(rsvp.userId, user.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)
)
);

if (firstData) {
total = firstData.total;
}

return {
items,
total,
};
});

// add rank to the result
const result = transactionResult.map((row, index) => ({
const result = items.map((row, index) => ({
...row,
rank: index + 1 + offset,
}));
Expand Down

0 comments on commit 474f5fb

Please sign in to comment.