From 416a7728dd91d39a27dd6d5c03ab1d13d5f05160 Mon Sep 17 00:00:00 2001 From: Bruno Campos Date: Sun, 12 May 2024 12:57:17 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20adiciona=20rota=20que=20lista=20onde=20?= =?UTF-8?q?est=C3=A3o=20faltando=20os=20suprimentos=20sobrando=20de=20um?= =?UTF-8?q?=20determinado=20abrigo.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shelter-supply.controller.ts | 15 +++++ src/shelter-supply/shelter-supply.service.ts | 55 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/shelter-supply/shelter-supply.controller.ts b/src/shelter-supply/shelter-supply.controller.ts index e2d3df69..174a7279 100644 --- a/src/shelter-supply/shelter-supply.controller.ts +++ b/src/shelter-supply/shelter-supply.controller.ts @@ -33,6 +33,21 @@ export class ShelterSupplyController { } } + @Get(':shelterId/remaining') + async remaining(@Param('shelterId') shelterId: string) { + try { + const data = await this.shelterSupplyService.remaining(shelterId); + return new ServerResponse( + 200, + 'Successfully get shelter supplies remaining', + data, + ); + } catch (err: any) { + this.logger.error(`Failed to get shelter supplies remaining: ${err}`); + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); + } + } + @Post('') async store(@Body() body) { try { diff --git a/src/shelter-supply/shelter-supply.service.ts b/src/shelter-supply/shelter-supply.service.ts index ef4cf97a..41e39b25 100644 --- a/src/shelter-supply/shelter-supply.service.ts +++ b/src/shelter-supply/shelter-supply.service.ts @@ -150,4 +150,59 @@ export class ShelterSupplyService { }, }); } + + async remaining(shelterId: string) { + const remainingSupplies = await this.prismaService.shelterSupply.findMany({ + where: { + shelterId, + priority: SupplyPriority.Remaining, + }, + select: { + priority: true, + quantity: true, + supply: { + select: { + id: true, + name: true, + supplyCategory: { + select: { + id: true, + name: true, + }, + }, + updatedAt: true, + createdAt: true, + }, + }, + createdAt: true, + updatedAt: true, + }, + }); + + const promises = remainingSupplies.map(async (remaining) => { + const needing = await this.prismaService.shelterSupply.findMany({ + select: { + shelter: { + select: { + id: true, + name: true, + address: true, + latitude: true, + longitude: true, + verified: true, + contact: true, + }, + }, + }, + where: { + supplyId: remaining.supply.id, + priority: { in: [SupplyPriority.Urgent, SupplyPriority.Needing] }, + }, + }); + + return { ...remaining, needing }; + }); + + return await Promise.all(promises); + } }