Skip to content

Commit

Permalink
refactor: types for car schema and cleanup caching
Browse files Browse the repository at this point in the history
• 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
  • Loading branch information
ruchernchong committed Dec 24, 2024
1 parent abf111c commit 8444268
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
34 changes: 34 additions & 0 deletions src/lib/__tests__/groupMonthsByYear.test.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
]);
});
});
5 changes: 2 additions & 3 deletions src/lib/getUniqueMonths.ts
Original file line number Diff line number Diff line change
@@ -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 <T extends PgTable>(
table: T,
key = "month",
key = "month"
) => {
const tableName = getTableName(table);
const CACHE_KEY = `${tableName}:months`;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/groupMonthsByYear.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const groupMonthsByYear = (
months: string[],
months: string[]
): { year: string; months: string[] }[] => {
const groupedData: Record<string, string[]> = {};

Expand Down
35 changes: 16 additions & 19 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
15 changes: 11 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"lib": ["ESNext"],
"lib": [
"ESNext"
],
"module": "ESNext",
"moduleResolution": "Node",
"target": "ESNext",
Expand All @@ -10,9 +12,14 @@
"verbatimModuleSyntax": true,
"skipLibCheck": true,
"paths": {
"@/*": ["src/*"]
"@/*": [
"src/*"
]
},
"resolveJsonModule": true
},
"include": ["**/*.ts"]
}
"include": [
"**/*.ts",
"src/v1/index.test.js"
]
}

0 comments on commit 8444268

Please sign in to comment.