Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor "has active mandate" to reduce db stress #535

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions src/lib/server/loadHomeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const loadHomeData = async ({

/* files - subject to change */

const year = new Date().getFullYear();
const now = new Date();
const year = now.getFullYear();
const files = await fileHandler.getInBucket(
user,
PUBLIC_BUCKETS_DOCUMENTS,
Expand Down Expand Up @@ -67,7 +68,7 @@ export const loadHomeData = async ({
where: {
...BASIC_EVENT_FILTER(),
startDatetime: {
gt: new Date(),
gt: now,
},
},
orderBy: {
Expand All @@ -80,7 +81,7 @@ export const loadHomeData = async ({
const upcomingMeetingPromise = prisma.meeting.findFirst({
where: {
date: {
gt: new Date(),
gt: now,
},
},
orderBy: {
Expand All @@ -90,7 +91,7 @@ export const loadHomeData = async ({
const previousMeetingPromise = prisma.meeting.findFirst({
where: {
date: {
lt: new Date(),
lt: now,
},
},
orderBy: {
Expand All @@ -101,7 +102,7 @@ export const loadHomeData = async ({
// CAFE OPENING TIMES
const cafeOpenPromise = prisma.markdown.findFirst({
where: {
name: `cafe:open:${new Date().getDay() - 1}`, // we assign monday to 0, not sunday
name: `cafe:open:${now.getDay() - 1}`, // we assign monday to 0, not sunday
},
});

Expand All @@ -110,15 +111,36 @@ export const loadHomeData = async ({
res.json(),
) as Promise<GetCommitDataResponse>;

const [news, events, upcomingMeeting, previousMeeting, cafeOpen, commitData] =
await Promise.allSettled([
newsPromise,
eventsPromise,
upcomingMeetingPromise,
previousMeetingPromise,
cafeOpenPromise,
commitPromise,
]);
// ACTIVE MANDATE?
const hasActiveMandatePromise =
prisma.mandate.findFirst({
where: {
startDate: { lte: now },
endDate: { gte: now },
memberId: locals.user?.memberId,
},
select: {
id: true,
},
}) !== null;
Comment on lines +116 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfredgrip Is this really correct? It looks to me like we're checking if promise !== null, since we're not awaiting the promise here but rather with Promise.allSettled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Fixed in #536


const [
news,
events,
upcomingMeeting,
previousMeeting,
cafeOpen,
commitData,
hasActiveMandate,
] = await Promise.allSettled([
newsPromise,
eventsPromise,
upcomingMeetingPromise,
previousMeetingPromise,
cafeOpenPromise,
commitPromise,
hasActiveMandatePromise,
]);
if (news.status === "rejected") {
throw error(500, "Failed to fetch news");
}
Expand All @@ -137,14 +159,9 @@ export const loadHomeData = async ({
if (commitData.status === "rejected") {
throw error(500, "Failed to fetch commit data");
}

const memberMandate = await prisma.mandate.findMany({
where: {
startDate: { lte: new Date() },
endDate: { gte: new Date() },
memberId: locals.user?.memberId,
},
});
if (hasActiveMandate.status === "rejected") {
throw error(500, "Failed to fetch mandate data");
}

return {
files: { next: nextBoardMeetingFiles, last: lastBoardMeetingFiles },
Expand All @@ -157,6 +174,6 @@ export const loadHomeData = async ({
cafeOpen: cafeOpen.value,
commitCount: commitData.value.commitCount,
latestCommit: commitData.value.latestCommit,
hasActiveMandate: memberMandate.length > 0,
hasActiveMandate: hasActiveMandate.value,
};
};
Loading