Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lista onde estão faltando os suprimentos sobrando em um abrigo #85

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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!

}
}