Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages: [.]
strictDepBuilds: false
14 changes: 13 additions & 1 deletion src/db/dbQueryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { avg, count, sql } from "drizzle-orm";
import { avg, count, gt, sql } from "drizzle-orm";
Comment thread
cirex-web marked this conversation as resolved.
import {
externalIdToInternalIdTable,
emailTable,
Expand All @@ -9,6 +9,7 @@ import {
timeOverwritesTable,
timesTable,
weeklyTimeOverwritesTable,
reportsTable,
} from "./schema";

import { DBType } from "./db";
Expand All @@ -31,6 +32,17 @@ export class QueryUtils {
this.db = db;
}

async getReportsAfter(start_time: Date, for_location_id?: string) {
const reports = await this.db.select().from(reportsTable).where(
and(
gt(reportsTable.createdAt, start_time),
for_location_id ? eq(reportsTable.locationId, for_location_id) : undefined
Comment thread
cirex-web marked this conversation as resolved.
Outdated
Comment thread
cirex-web marked this conversation as resolved.
Outdated
)
)

return reports;
Comment thread
cirex-web marked this conversation as resolved.
Outdated
}
Comment thread
cirex-web marked this conversation as resolved.
Outdated

async getSpecials(todayAsSQLString: string) {
const data = await this.db
.select()
Expand Down
14 changes: 14 additions & 0 deletions src/db/getLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export async function getAllLocationsFromDB(db: DBType, today: DateTime<true>) {
);
const [ratingsAvgs, ratingsCounts] = await DB.getRatingsAvgsAndCounts();

const reports = await DB.getReportsAfter(timeSearchCutoff.toJSDate(), undefined);
Comment thread
cirex-web marked this conversation as resolved.
Outdated

let reportCounts = reports.reduce<{
[locationId: string]: number
}>(
(acc, currentloc) => {
let amt = acc[currentloc.locationId] ?? 0;
acc[currentloc.locationId] = amt + 1;
return acc
},
{}
)
Comment thread
cirex-web marked this conversation as resolved.
Outdated

// apply overrides, merge all time intervals, and add specials
const finalLocationData = Object.entries(locationIdToData).map(
([id, data]) => {
Expand All @@ -48,6 +61,7 @@ export async function getAllLocationsFromDB(db: DBType, today: DateTime<true>) {
ratingsCount: ratingsCounts[id] ?? 0,
todaysSoups: specials[id]?.soups ?? [],
todaysSpecials: specials[id]?.specials ?? [],
reportCount: reportCounts[id] ?? 0
Comment thread
cirex-web marked this conversation as resolved.
Outdated
};
},
);
Expand Down
23 changes: 12 additions & 11 deletions src/endpoints/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import cookieSigner from "cookie-signature";
const OIDCConfig = await client.discovery(
env.OIDC_SERVER,
env.OIDC_CLIENT_ID,
env.OIDC_CLIENT_SECRET
env.OIDC_CLIENT_SECRET,
);
export const authEndpoints = new Elysia();
authEndpoints.get(
Expand All @@ -23,7 +23,8 @@ authEndpoints.get(
const redirectURL = client.buildAuthorizationUrl(OIDCConfig, {
redirect_uri: `${curOrigin.origin}/code-exchange`,
scope: "openid email profile",
prompt: "select_account", // force account picker
// prompt: "select_account", // force account picker
Comment thread
cirex-web marked this conversation as resolved.
hd: "andrew.cmu.edu", // idk if this excludes cmu.edu emails... (most ppl should see the login.cmu.edu screen though)
Comment thread
cirex-web marked this conversation as resolved.
});
return new Response(null, {
status: 303,
Expand All @@ -35,7 +36,7 @@ authEndpoints.get(
{
query: t.Object({ redirectURL: t.Nullable(t.String()) }),
detail: { hide: true },
}
},
);
authEndpoints.get(
"/logout",
Expand All @@ -54,7 +55,7 @@ authEndpoints.get(
{
query: t.Object({ redirectURL: t.Nullable(t.String()) }),
detail: { hide: true },
}
},
);
authEndpoints.get(
"/code-exchange",
Expand All @@ -68,8 +69,8 @@ authEndpoints.get(
console.error(e);
notifySlack(
`<!channel> OIDC code exchange failed with error ${e} ${JSON.stringify(
e.cause
)} CODE: ${e.code}`
e.cause,
)} CODE: ${e.code}`,
);
return undefined;
});
Expand All @@ -86,7 +87,7 @@ authEndpoints.get(
if (sessionId !== undefined) {
cookie["session_id"]!.value = cookieSigner.sign(
sessionId,
env.SESSION_COOKIE_SIGNING_SECRET
env.SESSION_COOKIE_SIGNING_SECRET,
);
cookie["session_id"]!.httpOnly = true;
cookie["session_id"]!.secure = true;
Expand All @@ -108,15 +109,15 @@ authEndpoints.get(
},
});
},
{ query: t.Object({ code: t.String() }), detail: { hide: true } }
{ query: t.Object({ code: t.String() }), detail: { hide: true } },
);
export async function fetchUserDetails(sessionId?: string) {
if (env.HARDCODE_SESSION_FOR_DEV_TESTING)
sessionId = env.HARDCODE_SESSION_FOR_DEV_TESTING;
if (sessionId === undefined) return null;
const unsignedSessionId = cookieSigner.unsign(
sessionId,
env.SESSION_COOKIE_SIGNING_SECRET
env.SESSION_COOKIE_SIGNING_SECRET,
);
if (!unsignedSessionId) return null;
return await fetchUserSession(db, unsignedSessionId);
Expand Down Expand Up @@ -153,12 +154,12 @@ authEndpoints.get(
firstName: t.Nullable(t.String()),
lastName: t.Nullable(t.String()),
pictureUrl: t.Nullable(t.String()),
})
}),
),
}),
detail: {
description:
"If you have an active login session with cmueats, this will return your user info. (this is NOT intended to work cross-site)",
},
}
},
);
10 changes: 7 additions & 3 deletions src/endpoints/reviews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Elysia, { status, t } from "elysia";
import { fetchUserDetails } from "./auth";
import { eq } from "drizzle-orm"
import { eq, and, gt} from "drizzle-orm"
import {
addStarReview,
deleteStarReview,
Expand All @@ -11,6 +11,8 @@ import {
} from "db/reviews";
import { db } from "db/db";
import { reportsTable } from "db/schema";
import { QueryUtils } from "db/dbQueryUtils";
import { DateTime } from "luxon";

export const reviewEndpoints = new Elysia();
reviewEndpoints
Expand Down Expand Up @@ -133,9 +135,11 @@ reviewEndpoints
.get(
"/v2/locations/:locationId/reports",
async ({ params: { locationId } }) => {
const reports = await db.select().from(reportsTable).where(eq(reportsTable.locationId, locationId))
let yesterday = DateTime.now().minus({days: 1})

return reports
let ret = await (new QueryUtils(db)).getReportsAfter(yesterday.toJSDate(), locationId)

return ret;
Comment thread
cirex-web marked this conversation as resolved.
Outdated
Comment thread
cirex-web marked this conversation as resolved.
Outdated
},
{
response: t.Array(
Expand Down
Loading