Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
feat: report are chart total #36
Browse files Browse the repository at this point in the history
  • Loading branch information
NotHydra committed Mar 2, 2024
1 parent 312c4e9 commit f13b24b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apps/api/src/model/report/report.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ export class ReportController
}
}

@Get("area-chart-total")
public async findAreaChartTotal(): Promise<ResponseFormatInterface<{ name: string; total: number }[]>> {
try {
const response: ResponseFormatInterface<{ name: string; total: number }[]> = formatResponse<
{
name: string;
total: number;
}[]
>(true, 200, "Area Chart Total Found", await this.modelService.findAreaChartTotal());

this.loggerService.log(`Find Area Chart Total: ${JSON.stringify(response)}`);

return response;
} catch (error) {
this.loggerService.error(`Find Area Chart Total: ${error.message}`);
return formatResponse<null>(false, 500, error.message, null);
}
}

@Override
public async change(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/model/report/report.rest
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ GET {{url}}/pie-chart-status

###

GET {{url}}/area-chart-total

###

GET {{url}}/nik/64711092301923/extend

###
Expand Down
34 changes: 34 additions & 0 deletions apps/api/src/model/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,40 @@ export class ReportService
}
}

public async findAreaChartTotal(): Promise<{ name: string; total: number }[]> {
try {
const today: Date = new Date();
const data: { name: string; total: number }[] = await Promise.all(
[0, 1, 2, 3, 4, 5, 6].map(async (value: number): Promise<{ name: string; total: number }> => {
const ltDate: Date = new Date(today);
ltDate.setDate(today.getDate() - value);

const gteDate: Date = new Date(today);
gteDate.setDate(today.getDate() - (value + 1));

return {
name: `${ltDate.getDate()}-${ltDate.getMonth()}-${ltDate.getFullYear()}`,
total: await this.prismaService[this.modelName].count({
where: {
createdAt: {
gte: gteDate.toISOString(),
lt: ltDate.toISOString(),
},
},
}),
};
})
);

this.loggerService.log(`Find Area Chart Total: ${JSON.stringify(data)}`);

return data;
} catch (error) {
this.loggerService.error(`Find Area Chart Total: ${error.message}`);
throw new InternalServerErrorException("Internal Server Error");
}
}

@Override
public async add(payload: ReportCreateDTO): Promise<ReportModel> {
try {
Expand Down

0 comments on commit f13b24b

Please sign in to comment.