1
1
import { Injectable } from '@nestjs/common' ;
2
2
import {
3
- DuplicateException ,
4
3
ID ,
5
4
ObjectView ,
6
- SecuredList ,
7
5
ServerException ,
8
6
Session ,
9
7
UnsecuredDto ,
10
8
} from '../../common' ;
11
9
import { HandleIdLookup , ILogger , Logger } from '../../core' ;
12
- import { mapListResults } from '../../core/database/results' ;
13
10
import { Privileges } from '../authorization' ;
14
11
import {
15
12
CreateFieldZone ,
@@ -30,20 +27,8 @@ export class FieldZoneService {
30
27
31
28
async create ( input : CreateFieldZone , session : Session ) : Promise < FieldZone > {
32
29
this . privileges . for ( session , FieldZone ) . verifyCan ( 'create' ) ;
33
- if ( ! ( await this . repo . isUnique ( input . name ) ) ) {
34
- throw new DuplicateException (
35
- 'fieldZone.name' ,
36
- 'FieldZone with this name already exists.' ,
37
- ) ;
38
- }
39
- const result = await this . repo . create ( input , session ) ;
40
-
41
- if ( ! result ) {
42
- throw new ServerException ( 'failed to create field zone' ) ;
43
- }
44
-
45
- this . logger . debug ( `field zone created` , { id : result . id } ) ;
46
- return await this . readOne ( result . id , session ) ;
30
+ const dto = await this . repo . create ( input , session ) ;
31
+ return this . secure ( dto , session ) ;
47
32
}
48
33
49
34
@HandleIdLookup ( FieldZone )
@@ -58,32 +43,26 @@ export class FieldZoneService {
58
43
} ) ;
59
44
60
45
const result = await this . repo . readOne ( id ) ;
61
- return await this . secure ( result , session ) ;
46
+ return this . secure ( result , session ) ;
62
47
}
63
48
64
49
async readMany ( ids : readonly ID [ ] , session : Session ) {
65
50
const fieldZones = await this . repo . readMany ( ids ) ;
66
- return await Promise . all (
67
- fieldZones . map ( ( dto ) => this . secure ( dto , session ) ) ,
68
- ) ;
51
+ return fieldZones . map ( ( dto ) => this . secure ( dto , session ) ) ;
69
52
}
70
53
71
- private async secure (
72
- dto : UnsecuredDto < FieldZone > ,
73
- session : Session ,
74
- ) : Promise < FieldZone > {
54
+ private secure ( dto : UnsecuredDto < FieldZone > , session : Session ) {
75
55
return this . privileges . for ( session , FieldZone ) . secure ( dto ) ;
76
56
}
77
57
78
58
async update ( input : UpdateFieldZone , session : Session ) : Promise < FieldZone > {
79
- const fieldZone = await this . readOne ( input . id , session ) ;
59
+ const fieldZone = await this . repo . readOne ( input . id ) ;
80
60
81
61
const changes = this . repo . getActualChanges ( fieldZone , input ) ;
82
62
this . privileges . for ( session , FieldZone , fieldZone ) . verifyChanges ( changes ) ;
83
63
84
- await this . repo . update ( fieldZone , changes ) ;
85
-
86
- return await this . readOne ( input . id , session ) ;
64
+ const updated = await this . repo . update ( fieldZone , changes ) ;
65
+ return this . secure ( updated , session ) ;
87
66
}
88
67
89
68
async delete ( id : ID , session : Session ) : Promise < void > {
@@ -103,11 +82,10 @@ export class FieldZoneService {
103
82
input : FieldZoneListInput ,
104
83
session : Session ,
105
84
) : Promise < FieldZoneListOutput > {
106
- if ( this . privileges . for ( session , FieldZone ) . can ( 'read' ) ) {
107
- const results = await this . repo . list ( input , session ) ;
108
- return await mapListResults ( results , ( dto ) => this . secure ( dto , session ) ) ;
109
- } else {
110
- return SecuredList . Redacted ;
111
- }
85
+ const results = await this . repo . list ( input , session ) ;
86
+ return {
87
+ ...results ,
88
+ items : results . items . map ( ( dto ) => this . secure ( dto , session ) ) ,
89
+ } ;
112
90
}
113
91
}
0 commit comments