This commit is contained in:
Emily
2024-06-21 16:56:44 +02:00
parent 08ca6cf7ea
commit d8445b3f1a
5 changed files with 122 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
{
"dependencies": {
"dayjs": "^1.11.11",
"mongoose": "^8.4.0",
"nodemailer": "^6.9.13",
"redis": "^4.6.14"

8
shared/pnpm-lock.yaml generated
View File

@@ -8,6 +8,9 @@ importers:
.:
dependencies:
dayjs:
specifier: ^1.11.11
version: 1.11.11
mongoose:
specifier: ^8.4.0
version: 8.4.0
@@ -79,6 +82,9 @@ packages:
resolution: {integrity: sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==}
engines: {node: '>=0.10.0'}
dayjs@1.11.11:
resolution: {integrity: sha512-okzr3f11N6WuqYtZSvm+F776mB41wRZMhKP+hc34YdW+KmtYYK9iqvHSwo2k9FEH3fhGXvOPV6yz2IcSrfRUDg==}
debug@4.3.4:
resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==}
engines: {node: '>=6.0'}
@@ -232,6 +238,8 @@ snapshots:
cluster-key-slot@1.1.2: {}
dayjs@1.11.11: {}
debug@4.3.4:
dependencies:
ms: 2.1.2

View File

@@ -0,0 +1,80 @@
import dayjs from 'dayjs';
export type Slice = 'day' | 'hour' | 'month' | 'year';
class DateService {
getQueryDateRange(slice: Slice) {
const group: Record<string, any> = {}
const sort: Record<string, any> = {}
const fromParts: Record<string, any> = {}
switch (slice) {
case 'hour':
group.hour = { $hour: '$created_at' }
sort['_id.hour'] = 1;
fromParts.hour = "$_id.hour";
case 'day':
group.day = { $dayOfMonth: '$created_at' }
sort['_id.day'] = 1;
fromParts.day = "$_id.day";
case 'month':
group.month = { $month: '$created_at' }
sort['_id.month'] = 1;
fromParts.month = "$_id.month";
case 'year':
group.year = { $year: '$created_at' }
sort['_id.year'] = 1;
fromParts.year = "$_id.year";
}
return { group, sort, fromParts }
}
prepareDateRange(from: string, to: string, slice: Slice) {
let fromDate = dayjs(from).minute(0).second(0).millisecond(0);
let toDate = dayjs(to).minute(0).second(0).millisecond(0);
switch (slice) {
case 'day':
fromDate = fromDate.hour(0);
toDate = toDate.hour(0);
break;
case 'hour':
break;
}
return {
from: fromDate.toDate(),
to: toDate.toDate()
}
}
fillDates(dates: string[], slice: Slice) {
const allDates: dayjs.Dayjs[] = [];
const firstDate = dayjs(dates.at(0));
const lastDate = dayjs(dates.at(-1));
let currentDate = firstDate.clone();
while (currentDate.isBefore(lastDate)) {
currentDate = currentDate.add(1, slice);
allDates.push(currentDate);
}
return allDates;
}
mergeFilledDates<T extends Record<string, any>, K extends keyof T>(dates: dayjs.Dayjs[], items: T[], dateField: K, slice: Slice, fillData: Omit<T, K>) {
const result = new Array<T>();
for (const date of dates) {
const item = items.find(e => dayjs(e[dateField]).isSame(date), slice);
result.push(item ?? { ...fillData, [dateField]: date.format() } as T);
}
return result;
}
}
const dateServiceInstance = new DateService();
export default dateServiceInstance;