Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 312c4e9

Browse files
committed
feat: trash area chart total endpoint
1 parent aac746f commit 312c4e9

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

apps/api/src/model/trash/trash.controller.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Body, Controller, ForbiddenException, Param, ParseIntPipe, UseInterceptors } from "@nestjs/common";
1+
import { Body, Controller, ForbiddenException, Get, Param, ParseIntPipe, UseInterceptors } from "@nestjs/common";
22
import { Override, ResponseFormatInterface, TrashCreateDTO, TrashModel, TrashUpdateDTO } from "@trashtrack/common";
33

4-
import { ResponseFormatInterceptor } from "../../interceptor/response-format.interceptor";
4+
import { ResponseFormatInterceptor, formatResponse } from "../../interceptor/response-format.interceptor";
55

66
import { BaseController } from "../../global/base.controller";
77

@@ -29,4 +29,23 @@ export class TrashController
2929
this.loggerService.error(`Change: Method Is Disabled`);
3030
throw new ForbiddenException("Method Is Disabled");
3131
}
32+
33+
@Get("area-chart-total")
34+
public async findAreaChartTotal(): Promise<ResponseFormatInterface<{ name: string; total: number }[]>> {
35+
try {
36+
const response: ResponseFormatInterface<{ name: string; total: number }[]> = formatResponse<
37+
{
38+
name: string;
39+
total: number;
40+
}[]
41+
>(true, 200, "Area Chart Total Found", await this.modelService.findAreaChartTotal());
42+
43+
this.loggerService.log(`Find Area Chart Total: ${JSON.stringify(response)}`);
44+
45+
return response;
46+
} catch (error) {
47+
this.loggerService.error(`Find Area Chart Total: ${error.message}`);
48+
return formatResponse<null>(false, 500, error.message, null);
49+
}
50+
}
3251
}

apps/api/src/model/trash/trash.rest

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ GET {{url}}/id/1
1313

1414
###
1515

16+
GET {{url}}/area-chart-total
17+
18+
###
19+
1620
POST {{url}}
1721
Content-Type: application/json
1822

1923
{
20-
"subTrashBinId": 1
24+
"subTrashBinId": 4
2125
}
2226

2327
###

apps/api/src/model/trash/trash.service.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from "@nestjs/common";
1+
import { Injectable, InternalServerErrorException } from "@nestjs/common";
22
import { TrashCreateDTO, TrashModel, TrashUpdateDTO } from "@trashtrack/common";
33

44
import { BaseService } from "../../global/base.service";
@@ -15,4 +15,38 @@ export class TrashService
1515
constructor(prismaService: PrismaService) {
1616
super(TrashService.name, prismaService);
1717
}
18+
19+
public async findAreaChartTotal(): Promise<{ name: string; total: number }[]> {
20+
try {
21+
const today: Date = new Date();
22+
const data: { name: string; total: number }[] = await Promise.all(
23+
[0, 1, 2, 3, 4, 5, 6].map(async (value: number): Promise<{ name: string; total: number }> => {
24+
const ltDate: Date = new Date(today);
25+
ltDate.setDate(today.getDate() - value);
26+
27+
const gteDate: Date = new Date(today);
28+
gteDate.setDate(today.getDate() - (value + 1));
29+
30+
return {
31+
name: `${ltDate.getDate()}-${ltDate.getMonth()}-${ltDate.getFullYear()}`,
32+
total: await this.prismaService[this.modelName].count({
33+
where: {
34+
createdAt: {
35+
gte: gteDate.toISOString(),
36+
lt: ltDate.toISOString(),
37+
},
38+
},
39+
}),
40+
};
41+
})
42+
);
43+
44+
this.loggerService.log(`Find Area Chart Total: ${JSON.stringify(data)}`);
45+
46+
return data;
47+
} catch (error) {
48+
this.loggerService.error(`Find Area Chart Total: ${error.message}`);
49+
throw new InternalServerErrorException("Internal Server Error");
50+
}
51+
}
1852
}

0 commit comments

Comments
 (0)