From 844426894ea9f160c98ad77339875d399d54e72d Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Wed, 25 Dec 2024 02:24:57 +0800 Subject: [PATCH] refactor: types for car schema and cleanup caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Add number field to car schema • Move CACHE_TTL to config • Remove strict() from schema definitions • Clean up tsconfig formatting • Fix schema imports and fields --- src/lib/__tests__/groupMonthsByYear.test.ts | 34 ++++++++++++++++++++ src/lib/getUniqueMonths.ts | 5 ++- src/lib/groupMonthsByYear.ts | 2 +- src/schemas/index.ts | 35 ++++++++++----------- tsconfig.json | 15 ++++++--- 5 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/lib/__tests__/groupMonthsByYear.test.ts diff --git a/src/lib/__tests__/groupMonthsByYear.test.ts b/src/lib/__tests__/groupMonthsByYear.test.ts new file mode 100644 index 0000000..9b9750e --- /dev/null +++ b/src/lib/__tests__/groupMonthsByYear.test.ts @@ -0,0 +1,34 @@ +import { describe, it, expect } from "vitest"; +import { groupMonthsByYear } from "../groupMonthsByYear"; + +describe("groupMonthsByYear", () => { + it("should group months by year", () => { + const months = ["2022-01", "2022-12", "2023-01", "2023-02"]; + + expect(Object.keys(groupMonthsByYear(months))).toHaveLength(2); + expect(groupMonthsByYear(months)).toEqual([ + { year: "2023", months: ["02", "01"] }, + { year: "2022", months: ["12", "01"] }, + ]); + }); + + it("should handle empty array", () => { + const months: string[] = []; + + const result = groupMonthsByYear(months); + + expect(Object.keys(result)).toHaveLength(0); + }); + + it("should handle single year", () => { + const months = ["2023-01", "2023-02", "2023-03"]; + + expect(Object.keys(groupMonthsByYear(months))).toHaveLength(1); + expect(groupMonthsByYear(months)).toEqual([ + { + year: "2023", + months: ["03", "02", "01"], + }, + ]); + }); +}); diff --git a/src/lib/getUniqueMonths.ts b/src/lib/getUniqueMonths.ts index 3a88fd8..a2f62ad 100644 --- a/src/lib/getUniqueMonths.ts +++ b/src/lib/getUniqueMonths.ts @@ -1,13 +1,12 @@ +import { CACHE_TTL } from "@/config"; import db from "@/config/db"; import redis from "@/config/redis"; import { desc, getTableName } from "drizzle-orm"; import type { PgTable } from "drizzle-orm/pg-core"; -const CACHE_TTL = 60 * 60 * 24; // 1 day in seconds - export const getUniqueMonths = async ( table: T, - key = "month", + key = "month" ) => { const tableName = getTableName(table); const CACHE_KEY = `${tableName}:months`; diff --git a/src/lib/groupMonthsByYear.ts b/src/lib/groupMonthsByYear.ts index 6bbdca8..e90ceb9 100644 --- a/src/lib/groupMonthsByYear.ts +++ b/src/lib/groupMonthsByYear.ts @@ -1,5 +1,5 @@ export const groupMonthsByYear = ( - months: string[], + months: string[] ): { year: string; months: string[] }[] => { const groupedData: Record = {}; diff --git a/src/schemas/index.ts b/src/schemas/index.ts index 1d637de..032670f 100644 --- a/src/schemas/index.ts +++ b/src/schemas/index.ts @@ -55,26 +55,23 @@ export const LatestMonthQuerySchema = z // Response schemas export const MakeArraySchema = z.array(z.string()); -export const CarSchema = z - .object({ - make: z.string(), - model: z.string(), - fuel_type: z.string(), - vehicle_type: z.string(), - month: z.string(), - }) - .strict(); +export const CarSchema = z.object({ + make: z.string(), + model: z.string(), + fuel_type: z.string(), + vehicle_type: z.string(), + month: z.string(), + number: z.number(), +}); -export const COESchema = z - .object({ - month: z.string(), - bidding_no: z.number(), - vehicle_class: z.string(), - quota: z.number(), - bids_received: z.number(), - premium: z.number(), - }) - .strict(); +export const COESchema = z.object({ + month: z.string(), + bidding_no: z.number(), + vehicle_class: z.string(), + quota: z.number(), + bids_received: z.number(), + premium: z.number(), +}); export const LatestMonthResponseSchema = z .object({ diff --git a/tsconfig.json b/tsconfig.json index 026bce6..cf5acb8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,9 @@ { "compilerOptions": { "baseUrl": ".", - "lib": ["ESNext"], + "lib": [ + "ESNext" + ], "module": "ESNext", "moduleResolution": "Node", "target": "ESNext", @@ -10,9 +12,14 @@ "verbatimModuleSyntax": true, "skipLibCheck": true, "paths": { - "@/*": ["src/*"] + "@/*": [ + "src/*" + ] }, "resolveJsonModule": true }, - "include": ["**/*.ts"] -} + "include": [ + "**/*.ts", + "src/v1/index.test.js" + ] +} \ No newline at end of file