From 2981d5b7956aeb8521909bf1401c91926121dd83 Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Wed, 13 Dec 2023 22:16:35 +0800 Subject: [PATCH] Clean up --- packages/core/src/car.ts | 70 +++++++++++++++++++---------------- packages/core/src/datasets.ts | 5 +-- packages/functions/src/car.ts | 7 +--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/packages/core/src/car.ts b/packages/core/src/car.ts index 5d9813f..e9228f7 100644 --- a/packages/core/src/car.ts +++ b/packages/core/src/car.ts @@ -1,39 +1,47 @@ -export * as Car from "./car"; +import { Document, InferIdType } from "mongodb"; +import { format, subMonths } from "date-fns"; import db from "../../config/db"; import { FUEL_TYPE } from "./config"; -import { filterDataLast12Months } from "./lib/filterDataLast12Months"; -import { sortByMake } from "./lib/sortByMake"; import type { CarType } from "./types"; -export const electric = async (): Promise => { - let electricCars = await db +export * as Car from "./car"; + +const today = new Date(); +const trailingTwelveMonths = format(subMonths(today, 12), "yyyy-MM"); + +const getCarsByFuelType = async ( + fuelType: FUEL_TYPE, +): Promise }> => { + let cars = await db .collection("cars") - .find({ - fuel_type: FUEL_TYPE.ELECTRIC, - }) + .find({ fuel_type: fuelType, month: { $gte: trailingTwelveMonths } }) .toArray(); - return electricCars - .reduce((result: any[], { _id, month, make, fuel_type, number }) => { - const existingCar = result.find( - (car) => car.month === month && car.make === make, - ); - - if (existingCar) { - existingCar.number += Number(number); - } else { - result.push({ - _id, - month, - make, - fuel_type, - number: Number(number), - }); - } - - return result; - }, []) - .map((car) => ({ ...car, number: +car.number })) - .filter(filterDataLast12Months) - .sort(sortByMake); + return cars.reduce((result: any, { _id, month, make, fuel_type, number }) => { + const existingCar = result.find( + (car: CarType) => car.month === month && car.make === make, + ); + + if (existingCar) { + existingCar.number += Number(number); + } else { + result.push({ + _id, + month, + make, + fuel_type, + number: Number(number), + }); + } + + return result; + }); }; + +export const electric = async (): Promise< + Document & { _id: InferIdType } +> => getCarsByFuelType(FUEL_TYPE.ELECTRIC); + +export const petrol = async (): Promise< + Document & { _id: InferIdType } +> => getCarsByFuelType(FUEL_TYPE.PETROL); diff --git a/packages/core/src/datasets.ts b/packages/core/src/datasets.ts index e4a3219..cb02857 100644 --- a/packages/core/src/datasets.ts +++ b/packages/core/src/datasets.ts @@ -18,10 +18,6 @@ export const updater = async () => { destination: zipFilePath, }); - // if (!response.ok) { - // throw new Error(`Failed to download the ZIP file: ${response.statusText}`); - // } - const zip = new AdmZip(zipFilePath); zip.extractAllTo(`${extractToPath}`, true); const zipEntries = zip.getEntries(); @@ -66,5 +62,6 @@ export const updater = async () => { } console.log(message); + return { message }; }; diff --git a/packages/functions/src/car.ts b/packages/functions/src/car.ts index 426e0e7..fc0f4da 100644 --- a/packages/functions/src/car.ts +++ b/packages/functions/src/car.ts @@ -1,7 +1,5 @@ import { ApiHandler } from "sst/node/api"; import { Car } from "@lta-datasets-updater/core/car"; -import { FUEL_TYPE } from "@lta-datasets-updater/core/config"; -import db from "../../config/db"; export const electric = ApiHandler(async (_evt) => { const electricCars = await Car.electric(); @@ -13,10 +11,7 @@ export const electric = ApiHandler(async (_evt) => { }); export const petrol = ApiHandler(async (_evt) => { - const petrolCars = await db - .collection("cars") - .find({ fuel_type: { $ne: FUEL_TYPE.PETROL } }) - .toArray(); + const petrolCars = await Car.petrol(); return { statusCode: 200,