@@ -8,18 +8,59 @@ import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
8
8
export class ShelterSupplyService {
9
9
constructor ( private readonly prismaService : PrismaService ) { }
10
10
11
+ private async handleUpdateShelterSum (
12
+ shelterId : string ,
13
+ oldPriority : number ,
14
+ newPriority : number ,
15
+ ) {
16
+ await this . prismaService . shelter . update ( {
17
+ where : {
18
+ id : shelterId ,
19
+ } ,
20
+ data : {
21
+ prioritySum : {
22
+ increment : newPriority - oldPriority ,
23
+ } ,
24
+ updatedAt : new Date ( ) . toISOString ( ) ,
25
+ } ,
26
+ } ) ;
27
+ }
28
+
11
29
async store ( body : z . infer < typeof CreateShelterSupplySchema > ) {
12
- const payload = CreateShelterSupplySchema . parse ( body ) ;
30
+ const { shelterId, priority, supplyId } =
31
+ CreateShelterSupplySchema . parse ( body ) ;
32
+ await this . handleUpdateShelterSum ( shelterId , 0 , priority ) ;
13
33
await this . prismaService . shelterSupply . create ( {
14
34
data : {
15
- ...payload ,
35
+ shelterId,
36
+ priority,
37
+ supplyId,
16
38
createdAt : new Date ( ) . toISOString ( ) ,
17
39
} ,
18
40
} ) ;
19
41
}
20
42
21
43
async update ( body : z . infer < typeof UpdateShelterSupplySchema > ) {
22
44
const { data, where } = UpdateShelterSupplySchema . parse ( body ) ;
45
+ const { priority } = data ;
46
+ if ( priority !== null && priority !== undefined ) {
47
+ const shelterSupply = await this . prismaService . shelterSupply . findFirst ( {
48
+ where : {
49
+ shelterId : where . shelterId ,
50
+ supplyId : where . supplyId ,
51
+ } ,
52
+ select : {
53
+ priority : true ,
54
+ } ,
55
+ } ) ;
56
+ if ( shelterSupply )
57
+ await this . handleUpdateShelterSum (
58
+ where . shelterId ,
59
+ shelterSupply . priority ,
60
+ priority ,
61
+ ) ;
62
+ }
63
+
23
64
await this . prismaService . shelterSupply . update ( {
24
65
where : {
25
66
shelterId_supplyId : where ,
@@ -42,7 +83,6 @@ export class ShelterSupplyService {
42
83
select : {
43
84
id : true ,
44
85
name : true ,
45
- priority : true ,
46
86
supplyCategory : {
47
87
select : {
48
88
id : true ,
0 commit comments