Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Dec 13, 2023
1 parent 066051a commit 2981d5b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
70 changes: 39 additions & 31 deletions packages/core/src/car.ts
Original file line number Diff line number Diff line change
@@ -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<CarType[]> => {
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<Document & { _id: InferIdType<Document> }> => {
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<Document> }
> => getCarsByFuelType(FUEL_TYPE.ELECTRIC);

export const petrol = async (): Promise<
Document & { _id: InferIdType<Document> }
> => getCarsByFuelType(FUEL_TYPE.PETROL);
5 changes: 1 addition & 4 deletions packages/core/src/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -66,5 +62,6 @@ export const updater = async () => {
}

console.log(message);

return { message };
};
7 changes: 1 addition & 6 deletions packages/functions/src/car.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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,
Expand Down

0 comments on commit 2981d5b

Please sign in to comment.