1
1
import { Injectable } from '@nestjs/common' ;
2
- import {
3
- ID ,
4
- NotFoundException ,
5
- ObjectView ,
6
- ServerException ,
7
- Session ,
8
- UnsecuredDto ,
9
- } from '../../../common' ;
10
- import { HandleIdLookup , ILogger , Logger } from '../../../core' ;
11
- import { mapListResults } from '../../../core/database/results' ;
2
+ import { ID , ObjectView , Session , UnsecuredDto } from '~/common' ;
3
+ import { HandleIdLookup } from '~/core' ;
12
4
import { Privileges } from '../../authorization' ;
13
5
import {
14
6
CreateUnavailability ,
@@ -22,7 +14,6 @@ import { UnavailabilityRepository } from './unavailability.repository';
22
14
@Injectable ( )
23
15
export class UnavailabilityService {
24
16
constructor (
25
- @Logger ( 'unavailability:service' ) private readonly logger : ILogger ,
26
17
private readonly privileges : Privileges ,
27
18
private readonly repo : UnavailabilityRepository ,
28
19
) { }
@@ -31,24 +22,9 @@ export class UnavailabilityService {
31
22
input : CreateUnavailability ,
32
23
session : Session ,
33
24
) : Promise < Unavailability > {
34
- try {
35
- this . privileges . for ( session , Unavailability ) . verifyCan ( 'create' ) ;
36
-
37
- // create and connect the Unavailability to the User.
38
- const id = await this . repo . create ( input ) ;
39
-
40
- this . logger . debug ( `Created user unavailability` , {
41
- id,
42
- userId : input . userId ,
43
- } ) ;
44
-
45
- return await this . readOne ( id , session ) ;
46
- } catch {
47
- this . logger . error ( `Could not create unavailability` , {
48
- userId : input . userId ,
49
- } ) ;
50
- throw new ServerException ( 'Could not create unavailability' ) ;
51
- }
25
+ this . privileges . for ( session , Unavailability ) . verifyCan ( 'create' ) ;
26
+ const result = await this . repo . create ( input ) ;
27
+ return this . secure ( result , session ) ;
52
28
}
53
29
54
30
@HandleIdLookup ( Unavailability )
@@ -58,57 +34,37 @@ export class UnavailabilityService {
58
34
_view ?: ObjectView ,
59
35
) : Promise < Unavailability > {
60
36
const result = await this . repo . readOne ( id ) ;
61
- return await this . secure ( result , session ) ;
37
+ return this . secure ( result , session ) ;
62
38
}
63
39
64
40
async readMany ( ids : readonly ID [ ] , session : Session ) {
65
41
const unavailabilities = await this . repo . readMany ( ids ) ;
66
- return await Promise . all (
67
- unavailabilities . map ( ( dto ) => this . secure ( dto , session ) ) ,
68
- ) ;
42
+ return unavailabilities . map ( ( dto ) => this . secure ( dto , session ) ) ;
69
43
}
70
44
71
- private async secure (
72
- dto : UnsecuredDto < Unavailability > ,
73
- session : Session ,
74
- ) : Promise < Unavailability > {
45
+ private secure ( dto : UnsecuredDto < Unavailability > , session : Session ) {
75
46
return this . privileges . for ( session , Unavailability ) . secure ( dto ) ;
76
47
}
77
48
78
49
async update (
79
50
input : UpdateUnavailability ,
80
51
session : Session ,
81
52
) : Promise < Unavailability > {
82
- const unavailability = await this . readOne ( input . id , session ) ;
83
-
53
+ const unavailability = await this . repo . readOne ( input . id ) ;
84
54
const result = await this . repo . getUserIdByUnavailability ( input . id ) ;
85
- if ( ! result ) {
86
- throw new NotFoundException (
87
- 'Could not find user associated with unavailability' ,
88
- 'user.unavailability' ,
89
- ) ;
90
- }
91
-
92
55
const changes = this . repo . getActualChanges ( unavailability , input ) ;
93
-
94
56
// TODO move this condition into policies
95
57
if ( result . id !== session . userId ) {
96
58
this . privileges
97
59
. for ( session , Unavailability , unavailability )
98
60
. verifyChanges ( changes ) ;
99
61
}
100
- return await this . repo . update ( unavailability , changes ) ;
62
+ const updated = await this . repo . update ( { id : input . id , ...changes } ) ;
63
+ return this . secure ( updated , session ) ;
101
64
}
102
65
103
- async delete ( id : ID , session : Session ) : Promise < void > {
104
- this . logger . debug ( `mutation delete unavailability` ) ;
105
- const ua = await this . readOne ( id , session ) ;
106
- if ( ! ua ) {
107
- throw new NotFoundException (
108
- 'Unavailability not found' ,
109
- 'unavailability.id' ,
110
- ) ;
111
- }
66
+ async delete ( id : ID , _session : Session ) : Promise < void > {
67
+ const ua = await this . repo . readOne ( id ) ;
112
68
await this . repo . deleteNode ( ua ) ;
113
69
}
114
70
@@ -117,6 +73,9 @@ export class UnavailabilityService {
117
73
session : Session ,
118
74
) : Promise < UnavailabilityListOutput > {
119
75
const results = await this . repo . list ( input ) ;
120
- return await mapListResults ( results , ( dto ) => this . secure ( dto , session ) ) ;
76
+ return {
77
+ ...results ,
78
+ items : results . items . map ( ( dto ) => this . secure ( dto , session ) ) ,
79
+ } ;
121
80
}
122
81
}
0 commit comments