1
- import { z } from 'zod' ;
2
1
import { Injectable } from '@nestjs/common' ;
3
2
4
3
import { PrismaService } from '../prisma/prisma.service' ;
8
7
UpdateShelterSupplySchema ,
9
8
} from './types' ;
10
9
import { SupplyPriority } from '../supply/types' ;
10
+ import { CreateShelterSupplyDTO } from './dtos/CreateShelterSupplyDTO' ;
11
+ import { UpdateShelterSupplyDTO } from './dtos/UpdateShelterSupplyDTO' ;
12
+ import { UpdateManyShelterSupplySchemaDTO } from './dtos/UpdateManyShelterSupplyDTO' ;
11
13
12
14
@Injectable ( )
13
15
export class ShelterSupplyService {
@@ -31,7 +33,7 @@ export class ShelterSupplyService {
31
33
} ) ;
32
34
}
33
35
34
- async store ( body : z . infer < typeof CreateShelterSupplySchema > ) {
36
+ async store ( body : CreateShelterSupplyDTO ) {
35
37
const { shelterId, priority, supplyId, quantity } =
36
38
CreateShelterSupplySchema . parse ( body ) ;
37
39
await this . handleUpdateShelterSum ( shelterId , 0 , priority ) ;
@@ -46,45 +48,58 @@ export class ShelterSupplyService {
46
48
} ) ;
47
49
}
48
50
49
- async update ( body : z . infer < typeof UpdateShelterSupplySchema > ) {
50
- const { data, where } = UpdateShelterSupplySchema . parse ( body ) ;
51
- const { priority, quantity } = data ;
51
+ async update (
52
+ body : UpdateShelterSupplyDTO ,
53
+ shelterId : string ,
54
+ supplyId : string ,
55
+ ) {
56
+ const {
57
+ shelterId : shelterIdParse ,
58
+ supplyId : supplyIdParse ,
59
+ priority,
60
+ quantity,
61
+ } = UpdateShelterSupplySchema . parse ( { ...body , shelterId, supplyId } ) ;
52
62
if ( priority !== null && priority !== undefined ) {
53
63
const shelterSupply = await this . prismaService . shelterSupply . findFirst ( {
54
64
where : {
55
- shelterId : where . shelterId ,
56
- supplyId : where . supplyId ,
65
+ shelterId : shelterIdParse ,
66
+ supplyId : supplyIdParse ,
57
67
} ,
58
68
select : {
59
69
priority : true ,
60
70
} ,
61
71
} ) ;
72
+
62
73
if ( shelterSupply )
63
74
await this . handleUpdateShelterSum (
64
- where . shelterId ,
75
+ shelterIdParse ,
65
76
shelterSupply . priority ,
66
77
priority ,
67
78
) ;
68
79
}
69
80
70
81
await this . prismaService . shelterSupply . update ( {
71
82
where : {
72
- shelterId_supplyId : where ,
83
+ shelterId_supplyId : {
84
+ shelterId : shelterIdParse ,
85
+ supplyId : supplyIdParse ,
86
+ } ,
73
87
} ,
74
88
data : {
75
- ... data ,
89
+ priority : priority ?? undefined ,
76
90
quantity : priority !== SupplyPriority . UnderControl ? quantity : null ,
77
91
updatedAt : new Date ( ) . toISOString ( ) ,
78
92
} ,
79
93
} ) ;
80
94
}
81
95
82
- async updateMany ( body : z . infer < typeof UpdateManyShelterSupplySchema > ) {
83
- const { ids, shelterId } = UpdateManyShelterSupplySchema . parse ( body ) ;
96
+ async updateMany ( body : UpdateManyShelterSupplySchemaDTO , shelterId : string ) {
97
+ const { ids, shelterId : shelterIdParsed } =
98
+ UpdateManyShelterSupplySchema . parse ( { ...body , shelterId } ) ;
84
99
85
100
const supplies = await this . prismaService . shelterSupply . findMany ( {
86
101
where : {
87
- shelterId,
102
+ shelterId : shelterIdParsed ,
88
103
supplyId : {
89
104
in : ids ,
90
105
} ,
@@ -99,7 +114,7 @@ export class ShelterSupplyService {
99
114
await this . prismaService . $transaction ( [
100
115
this . prismaService . shelter . update ( {
101
116
where : {
102
- id : shelterId ,
117
+ id : shelterIdParsed ,
103
118
} ,
104
119
data : {
105
120
prioritySum : {
@@ -110,7 +125,7 @@ export class ShelterSupplyService {
110
125
} ) ,
111
126
this . prismaService . shelterSupply . updateMany ( {
112
127
where : {
113
- shelterId,
128
+ shelterId : shelterIdParsed ,
114
129
supplyId : {
115
130
in : ids ,
116
131
} ,
0 commit comments