Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/shelter-supply/shelter-supply.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions src/shelter-supply/shelter-supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Em vez de Promise.all usar o Promise.allSettled, dessa forma é possível fazer um filtro em cima de todas as buscas que deram certo. Se usar o Promise.all se alguma das querys falhar ele já vai estourar uma exception.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up!

}
}