From 62e69aad1fe4b238b1783a971c6cbf755e1b12c2 Mon Sep 17 00:00:00 2001 From: Ru Chern CHONG Date: Sun, 16 Feb 2025 17:53:33 +0800 Subject: [PATCH] Refactor COE PQP API Results are expected to be the same --- src/v1/routes/coe.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/v1/routes/coe.ts b/src/v1/routes/coe.ts index 47264a4..0c0d307 100644 --- a/src/v1/routes/coe.ts +++ b/src/v1/routes/coe.ts @@ -1,14 +1,13 @@ import { CACHE_TTL } from "@/config"; import db from "@/config/db"; import redis from "@/config/redis"; -import getPqpRates from "@/lib/getPqpRates"; import { getUniqueMonths } from "@/lib/getUniqueMonths"; import { groupMonthsByYear } from "@/lib/groupMonthsByYear"; +import { coePQP } from "@/schema"; import { type COE, COEQuerySchema, MonthsQuerySchema } from "@/schemas"; -import { VehicleClass } from "@/types"; import { zValidator } from "@hono/zod-validator"; import { coe } from "@sgcarstrends/schema"; -import { and, asc, desc, eq, gte, inArray, lte, max, ne } from "drizzle-orm"; +import { and, asc, desc, eq, gte, inArray, lte, max } from "drizzle-orm"; import { Hono } from "hono"; const app = new Hono(); @@ -86,7 +85,7 @@ app.get("/latest", async (c) => { }); app.get("/pqp", async (c) => { - const CACHE_KEY = "coe:pqp"; + const CACHE_KEY = "COE:PQP"; const cachedData = await redis.get(CACHE_KEY); if (cachedData) { @@ -95,11 +94,16 @@ app.get("/pqp", async (c) => { const results = await db .select() - .from(coe) - .where(ne(coe.vehicle_class, VehicleClass.CategoryE)) // Category E COEs are not included in PQP calculation - .orderBy(desc(coe.month), desc(coe.bidding_no), asc(coe.vehicle_class)); - - const pqpRates = getPqpRates(results); + .from(coePQP) + .orderBy(desc(coePQP.month), asc(coePQP.vehicle_class)); + + const pqpRates = results.reduce((grouped, { month, vehicle_class, pqp }) => { + if (!grouped[month]) { + grouped[month] = {}; + } + grouped[month][vehicle_class] = pqp; + return grouped; + }, {}); await redis.set(CACHE_KEY, pqpRates, { ex: CACHE_TTL });