Skip to content

Commit

Permalink
Update types
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Jan 1, 2024
1 parent 65ada59 commit d9e3d1d
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 70 deletions.
22 changes: 9 additions & 13 deletions packages/core/src/cars.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { ObjectId } from "mongodb";
import { format, subMonths } from "date-fns";
import db from "../../config/db";
import { FUEL_TYPE } from "./config";
import type { CarType } from "./types";
import { Car, FUEL_TYPE } from "./types";
import { WithId } from "mongodb";

// TODO: Will return to clean up the types
interface CarDocument extends CarType {
_id: ObjectId;
}
const collection = db.collection<Car>("cars");

const today = new Date();
const trailingTwelveMonths = format(subMonths(today, 12), "yyyy-MM");

const getCarsByFuelType = async (
fuelType: FUEL_TYPE,
month?: string,
): Promise<CarDocument[]> => {
): Promise<WithId<Car>[]> => {
const filter = {
fuel_type: fuelType,
month: month ?? { $gte: trailingTwelveMonths },
};

let cars = await db.collection<CarDocument>("cars").find(filter).toArray();
const cars: WithId<Car>[] = await collection.find(filter).toArray();

return cars.reduce(
(result: CarDocument[], { _id, month, make, fuel_type, number }) => {
(result: WithId<Car>[], { _id, month, make, fuel_type, number }) => {
const existingCar = result.find(
(car: CarType) => car.month === month && car.make === make,
(car) => car.month === month && car.make === make,
);

if (existingCar) {
Expand All @@ -51,12 +47,12 @@ export const electric = async ({
month,
}: {
month?: string;
}): Promise<CarDocument[]> => getCarsByFuelType(FUEL_TYPE.ELECTRIC, month);
}): Promise<WithId<Car>[]> => getCarsByFuelType(FUEL_TYPE.ELECTRIC, month);

export const petrol = async ({
month,
}: {
month?: string;
}): Promise<CarDocument[]> => getCarsByFuelType(FUEL_TYPE.PETROL, month);
}): Promise<WithId<Car>[]> => getCarsByFuelType(FUEL_TYPE.PETROL, month);

export * as Cars from "./cars";
4 changes: 2 additions & 2 deletions packages/core/src/coe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Collection, WithId } from "mongodb";
import { WithId } from "mongodb";
import db from "../../config/db";
import { COEResult } from "./types";

const collection: Collection<COEResult> = db.collection<COEResult>("coe");
const collection = db.collection<COEResult>("coe");

export const list = async (): Promise<WithId<COEResult>[]> =>
collection.find().toArray();
Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/config/index.ts

This file was deleted.

22 changes: 16 additions & 6 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { ObjectId } from "mongodb";
import { FUEL_TYPE } from "../config";
export enum FUEL_TYPE {
DIESEL = "Diesel",
ELECTRIC = "Electric",
OTHERS = "Others",
PETROL = "Petrol",
}

export type CarType = {
export interface Car {
month: string;
make: string;
fuel_type: FUEL_TYPE | string;
fuel_type: FUEL_TYPE;
number: number;
selected?: boolean;
};
}

export interface COEResult {
// _id: ObjectId;
month: string;
bidding_no: string;
vehicle_class: string;
Expand All @@ -19,3 +22,10 @@ export interface COEResult {
bids_received: string;
premium: string;
}

export interface UpdateParams {
collectionName: string;
zipFileName: string;
zipUrl: string;
keyFields: string[];
}
24 changes: 11 additions & 13 deletions packages/core/src/updater.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { Document, WithId } from "mongodb";
import fs from "fs/promises";
import * as d3 from "d3";
import { downloadFile } from "./utils/downloadFile";
import { extractZipFile } from "./utils/extractZipFile";
import db from "../../config/db";
import { UpdateParams } from "./types";

const EXTRACT_PATH: string = "/tmp";

interface UpdateParams {
collectionName: string;
zipFileName: string;
zipUrl: string;
keyFields: string[];
}

export const update = async ({
collectionName,
zipFileName,
zipUrl,
keyFields,
}: UpdateParams): Promise<{ message: string }> => {
const collection = db.collection<Document>(collectionName);

try {
const zipFilePath = `${EXTRACT_PATH}/${zipFileName}`;
await downloadFile({ url: zipUrl, destination: zipFilePath });
Expand All @@ -30,15 +27,18 @@ export const update = async ({
const csvData = await fs.readFile(destinationPath, "utf-8");
const parsedData = d3.csvParse(csvData);

const existingData = await db.collection(collectionName).find().toArray();
const existingData: WithId<Document>[] = await collection.find().toArray();

const createUniqueKey = (item: any, keyFields: string[]) =>
const createUniqueKey = <T extends object>(
item: T,
keyFields: Array<keyof T>,
): string =>
keyFields
.filter((field) => item[field])
.map((field) => item[field])
.join("-");

const existingDataMap = new Map(
const existingDataMap: Map<string, WithId<Document>> = new Map(
existingData.map((item) => [createUniqueKey(item, keyFields), item]),
);
const newDataToInsert = parsedData.filter(
Expand All @@ -47,9 +47,7 @@ export const update = async ({

let message: string;
if (newDataToInsert.length > 0) {
const result = await db
.collection(collectionName)
.insertMany(newDataToInsert);
const result = await collection.insertMany(newDataToInsert);
message = `${result.insertedCount} document(s) inserted`;
} else {
message = `No new data to insert. The provided data matches the existing records.`;
Expand Down
15 changes: 0 additions & 15 deletions packages/core/src/utils/filterDataLast12Months.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/core/src/utils/sortByMake.ts

This file was deleted.

7 changes: 6 additions & 1 deletion packages/core/src/vehicle-make.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import db from "../../config/db";
import { Car } from "./types";
import { Flatten, WithId } from "mongodb";

export const list = async () => db.collection("cars").distinct("make");
const collection = db.collection<Car>("cars");

export const list = async (): Promise<Array<Flatten<WithId<Car>["make"]>>> =>
collection.distinct("make");

export * as VehicleMake from "./vehicle-make";
12 changes: 6 additions & 6 deletions packages/functions/src/cars.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ApiHandler, useQueryParam } from "sst/node/api";
import { Cars } from "@lta-cars-dataset/core/cars";
import { createResponse } from "./utils/createResponse";
import { WithId } from "mongodb";
import { Car } from "@lta-cars-dataset/core/types";

export const electric = ApiHandler(async (_evt) => {
const month = useQueryParam("month");
const electricCars = await Cars.electric({ month });

return createResponse(electricCars);
const cars: WithId<Car>[] = await Cars.electric({ month });
return createResponse(cars);
});

export const petrol = ApiHandler(async (_evt) => {
const month = useQueryParam("month");
const petrolCars = await Cars.petrol({ month });

return createResponse(petrolCars);
const cars: WithId<Car>[] = await Cars.petrol({ month });
return createResponse(cars);
});
3 changes: 1 addition & 2 deletions packages/functions/src/vehicle-make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { VehicleMake } from "@lta-cars-dataset/core/vehicle-make";
import { createResponse } from "./utils/createResponse";

export const list = ApiHandler(async (_evt) => {
const make = await VehicleMake.list();

const make: string[] = await VehicleMake.list();
return createResponse(make);
});

0 comments on commit d9e3d1d

Please sign in to comment.