Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ Now install the API's dependencies by 'cd'-ing into the root of the repository a
pnpm install
```

Start your local database with `pnpm db:start`, `pnpm db:push` (if this is your first time) and then start the server with `pnpm dev` and it should work, assuming you have the correct env variables. (To see the contents of the database, I recommend using DBeaver. You can also run `pnpm db:studio` to start up drizzle studio)
Then start your local database with (assuming you have the correct env variables)

```bash
pnpm db:start
pnpm db:push # if this is your first time running the db
pnpm dev
```

To see the contents of the database, I recommend using DBeaver. You can also run `pnpm db:studio` to start up drizzle studio

## Database schema changes (important!)

Expand Down
15 changes: 15 additions & 0 deletions src/db/dbQueryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { avg, sql } from "drizzle-orm";
import {
externalIdToInternalIdTable,
emailTable,
locationDataTable,
overwritesTable,
specialsTable,
starReviewTable,
timeOverwritesTable,
timesTable,
weeklyTimeOverwritesTable,
Expand Down Expand Up @@ -183,6 +185,19 @@ export class QueryUtils {
return { idToPointOverrides, idToWeeklyOverrides };
}

async getRatingsAvgs() {
const ratingsAvgs = await this.db
.select({
// <number> only declares it to be a number, we explicitly cast it to a num
starRating: sql`cast(${avg(starReviewTable.starRating)} as decimal(2,1))`.mapWith(Number),
locationId: starReviewTable.locationId,
})
.from(starReviewTable)
.groupBy(starReviewTable.locationId);

return Object.fromEntries(ratingsAvgs.map((e) => [e.locationId, e.starRating]));
}

async getEmails(): Promise<{ name: string; email: string }[]> {
const result = await this.db
.select({
Expand Down
2 changes: 2 additions & 0 deletions src/db/getLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function getAllLocationsFromDB(db: DBType, today: DateTime<true>) {
const { idToPointOverrides, idToWeeklyOverrides } = await DB.getTimeOverrides(
timeSearchCutoff.toSQLDate(),
);
const ratingsAvgs = await DB.getRatingsAvgs();

// apply overrides, merge all time intervals, and add specials
const finalLocationData = Object.entries(locationIdToData).map(
Expand All @@ -43,6 +44,7 @@ export async function getAllLocationsFromDB(db: DBType, today: DateTime<true>) {
// time.end
// ).toLocaleString()}`
// ),
ratingsAvg: ratingsAvgs[id] ?? null,
todaysSoups: specials[id]?.soups ?? [],
todaysSpecials: specials[id]?.specials ?? [],
};
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ reviewEndpoints
buckets: t.Array(t.Number(), {
example: [0, 1, 0, 4, 12, 4],
description:
"Count of ratings of star rating [{.5},{1,1.5},{2,2.5},{3,3.5},{4,4.5},{5}",
"Count of ratings of star rating [{.5},{1,1.5},{2,2.5},{3,3.5},{4,4.5},{5}]",
}),
}),
tagData: t.Array(
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const LocationSchema = t.Object({

name: t.Nullable(t.String({ examples: ["Schatz"] })),
location: t.String(),
ratingsAvg: t.Nullable(t.Number()),
shortDescription: t.Nullable(t.String()),
description: t.String(),
url: t.String(),
Expand Down
1 change: 1 addition & 0 deletions tests/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const locationIn: ILocation = {
const locationOut = {
id: "DYNAMICALLY GENERATED, replace with real id",
name: "dbTest",
ratingsAvg: null,
shortDescription: "hi",
description: "description",
url: "https://hi.com",
Expand Down