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
- } from '../../common' ;
11
- import { HandleIdLookup , ILogger , Logger } from '../../core' ;
12
- import { mapListResults } from '../../core/database/results' ;
8
+ } from '~/common' ;
9
+ import { HandleIdLookup , ILogger , Logger } from '~/core' ;
13
10
import { Privileges } from '../authorization' ;
14
11
import {
15
12
CreateFieldRegion ,
@@ -33,21 +30,8 @@ export class FieldRegionService {
33
30
session : Session ,
34
31
) : Promise < FieldRegion > {
35
32
this . privileges . for ( session , FieldRegion ) . verifyCan ( 'create' ) ;
36
- if ( ! ( await this . repo . isUnique ( input . name ) ) ) {
37
- throw new DuplicateException (
38
- 'fieldRegion.name' ,
39
- 'FieldRegion with this name already exists.' ,
40
- ) ;
41
- }
42
-
43
- const result = await this . repo . create ( input , session ) ;
44
-
45
- if ( ! result ) {
46
- throw new ServerException ( 'failed to create field region' ) ;
47
- }
48
-
49
- this . logger . debug ( `field region created` , { id : result . id } ) ;
50
- return await this . readOne ( result . id , session ) ;
33
+ const dto = await this . repo . create ( input , session ) ;
34
+ return this . secure ( dto , session ) ;
51
35
}
52
36
53
37
@HandleIdLookup ( FieldRegion )
@@ -62,36 +46,31 @@ export class FieldRegionService {
62
46
} ) ;
63
47
64
48
const result = await this . repo . readOne ( id ) ;
65
- return await this . secure ( result , session ) ;
49
+ return this . secure ( result , session ) ;
66
50
}
67
51
68
52
async readMany ( ids : readonly ID [ ] , session : Session ) {
69
53
const fieldRegions = await this . repo . readMany ( ids ) ;
70
- return await Promise . all (
71
- fieldRegions . map ( ( dto ) => this . secure ( dto , session ) ) ,
72
- ) ;
54
+ return fieldRegions . map ( ( dto ) => this . secure ( dto , session ) ) ;
73
55
}
74
56
75
- private async secure (
76
- dto : UnsecuredDto < FieldRegion > ,
77
- session : Session ,
78
- ) : Promise < FieldRegion > {
57
+ private secure ( dto : UnsecuredDto < FieldRegion > , session : Session ) {
79
58
return this . privileges . for ( session , FieldRegion ) . secure ( dto ) ;
80
59
}
81
60
82
61
async update (
83
62
input : UpdateFieldRegion ,
84
63
session : Session ,
85
64
) : Promise < FieldRegion > {
86
- const fieldRegion = await this . readOne ( input . id , session ) ;
65
+ const fieldRegion = await this . repo . readOne ( input . id ) ;
66
+
87
67
const changes = this . repo . getActualChanges ( fieldRegion , input ) ;
88
68
this . privileges
89
69
. for ( session , FieldRegion , fieldRegion )
90
70
. verifyChanges ( changes ) ;
91
71
92
- await this . repo . update ( fieldRegion , changes ) ;
93
-
94
- return await this . readOne ( input . id , session ) ;
72
+ const updated = await this . repo . update ( fieldRegion , changes ) ;
73
+ return this . secure ( updated , session ) ;
95
74
}
96
75
97
76
async delete ( id : ID , session : Session ) : Promise < void > {
@@ -111,11 +90,10 @@ export class FieldRegionService {
111
90
input : FieldRegionListInput ,
112
91
session : Session ,
113
92
) : Promise < FieldRegionListOutput > {
114
- if ( this . privileges . for ( session , FieldRegion ) . can ( 'read' ) ) {
115
- const results = await this . repo . list ( input , session ) ;
116
- return await mapListResults ( results , ( dto ) => this . secure ( dto , session ) ) ;
117
- } else {
118
- return SecuredList . Redacted ;
119
- }
93
+ const results = await this . repo . list ( input , session ) ;
94
+ return {
95
+ ...results ,
96
+ items : results . items . map ( ( dto ) => this . secure ( dto , session ) ) ,
97
+ } ;
120
98
}
121
99
}
0 commit comments