From 5e5f293b371d065ea62afab84591859350e8978a Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Tue, 26 Dec 2023 02:48:57 +0800 Subject: [PATCH] Refactor APIs --- packages/core/src/coe.ts | 14 +++++++++----- packages/functions/src/coe.ts | 17 ++++++++++++++--- stacks/ApiStack.ts | 14 ++++++++++++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/packages/core/src/coe.ts b/packages/core/src/coe.ts index bf44432..c440d86 100644 --- a/packages/core/src/coe.ts +++ b/packages/core/src/coe.ts @@ -2,13 +2,17 @@ import db from "../../config/db"; const collection = db.collection("coe"); -export const latest = async () => { +export const list = async () => collection.find().toArray(); + +export const getCOEResultByMonth = async (month?: string) => { const date = new Date(); - const year = date.getFullYear(); - const month = date.getMonth() + 1; - const formattedMonth = [year, month].join("-"); + const formattedMonth = + month || [date.getFullYear(), date.getMonth() + 1].join("-"); - return collection.find({ month: formattedMonth }).toArray(); + return collection + .find({ month: formattedMonth }) + .sort({ vehicle_class: 1 }) + .toArray(); }; export * as COE from "./coe"; diff --git a/packages/functions/src/coe.ts b/packages/functions/src/coe.ts index e9ab518..107f426 100644 --- a/packages/functions/src/coe.ts +++ b/packages/functions/src/coe.ts @@ -1,9 +1,20 @@ -import { ApiHandler } from "sst/node/api"; +import { ApiHandler, useQueryParam, useQueryParams } from "sst/node/api"; import { COE } from "@lta-cars-dataset/core/coe"; import { createResponse } from "./utils/createResponse"; -export const latest = ApiHandler(async (_evt) => { - const coeResult = await COE.latest(); +export const list = ApiHandler(async (_evt) => { + const result = await COE.list(); + return createResponse(result); +}); + +export const byMonth = ApiHandler(async (_evt) => { + const month = useQueryParam("month"); + const coeResult = await COE.getCOEResultByMonth(month); return createResponse(coeResult); }); + +export const latest = ApiHandler(async (_evt) => { + const result = await COE.getCOEResultByMonth(); + return createResponse(result); +}); diff --git a/stacks/ApiStack.ts b/stacks/ApiStack.ts index 1c6aa5d..67d73f5 100644 --- a/stacks/ApiStack.ts +++ b/stacks/ApiStack.ts @@ -11,6 +11,15 @@ const CUSTOM_DOMAINS: Record = { }, }; +const CORS_SETTINGS: Record = { + dev: { + allowOrigins: ["*"], + }, + prod: { + allowOrigins: ["https://singapore-ev-trends.ruchern.xyz"], + }, +}; + export const api = ({ stack }: StackContext) => { const MONGODB_URI = new Config.Secret(stack, "MONGODB_URI"); @@ -23,13 +32,14 @@ export const api = ({ stack }: StackContext) => { }, customDomain: CUSTOM_DOMAINS[stack.stage], cors: { - allowOrigins: ["https://singapore-ev-trends.ruchern.xyz"], + ...CORS_SETTINGS[stack.stage], }, routes: { "GET /": "packages/functions/src/cars.electric", "GET /cars/electric": "packages/functions/src/cars.electric", "GET /cars/petrol": "packages/functions/src/cars.petrol", - "GET /coe": "packages/functions/src/coe.latest", + "GET /coe": "packages/functions/src/coe.list", + "GET /coe/latest": "packages/functions/src/coe.latest", "GET /updater/cars": "packages/functions/src/updater.cars", "GET /updater/coe": "packages/functions/src/updater.coe", "GET /vehicle-make": "packages/functions/src/vehicle-make.list",