Skip to content

Commit b12e063

Browse files
authored
hotfix: Correção da Atualização Inconsistente de Prioridade em Shelter Supplies (#68)
* hotfix: fixed update shelter prioritySum when use update many supplies
1 parent 7abcf8e commit b12e063

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

docker-compose.dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
depends_on:
1010
- db
1111
ports:
12-
- "${PORT}:${PORT}"
12+
- '${PORT}:${PORT}'
1313
volumes:
1414
- .:/usr/app
1515
- /usr/app/node_modules
@@ -30,7 +30,7 @@ services:
3030
container_name: sos-rs-db
3131
image: postgres
3232
ports:
33-
- "${DB_PORT}:${DB_PORT}"
33+
- '${DB_PORT}:${DB_PORT}'
3434
environment:
3535
- POSTGRES_PASSWORD=${DB_PASSWORD}
3636
- POSTGRES_USER=${DB_USER}

src/shelter-supply/shelter-supply.service.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,45 @@ export class ShelterSupplyService {
8282
async updateMany(body: z.infer<typeof UpdateManyShelterSupplySchema>) {
8383
const { ids, shelterId } = UpdateManyShelterSupplySchema.parse(body);
8484

85-
await this.prismaService.shelterSupply.updateMany({
85+
const supplies = await this.prismaService.shelterSupply.findMany({
8686
where: {
87-
shelterId: shelterId,
87+
shelterId,
8888
supplyId: {
8989
in: ids,
9090
},
9191
},
92-
data: {
93-
priority: SupplyPriority.UnderControl,
94-
updatedAt: new Date().toISOString(),
95-
},
9692
});
93+
94+
const prioritySum = supplies.reduce(
95+
(prev, current) => prev + current.priority,
96+
0,
97+
);
98+
99+
await this.prismaService.$transaction([
100+
this.prismaService.shelter.update({
101+
where: {
102+
id: shelterId,
103+
},
104+
data: {
105+
prioritySum: {
106+
decrement: prioritySum,
107+
},
108+
updatedAt: new Date().toISOString(),
109+
},
110+
}),
111+
this.prismaService.shelterSupply.updateMany({
112+
where: {
113+
shelterId,
114+
supplyId: {
115+
in: ids,
116+
},
117+
},
118+
data: {
119+
priority: SupplyPriority.UnderControl,
120+
updatedAt: new Date().toISOString(),
121+
},
122+
}),
123+
]);
97124
}
98125

99126
async index(shelterId: string) {

0 commit comments

Comments
 (0)