-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
066051a
commit 2981d5b
Showing
3 changed files
with
41 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters