8
8
UnsecuredDto ,
9
9
} from '../../common' ;
10
10
import { HandleIdLookup , ILogger , Logger } from '../../core' ;
11
- import { mapListResults } from '../../core/database/results' ;
12
11
import { Privileges } from '../authorization' ;
13
12
import { CeremonyRepository } from './ceremony.repository' ;
14
13
import {
@@ -23,26 +22,14 @@ import {
23
22
export class CeremonyService {
24
23
constructor (
25
24
private readonly privileges : Privileges ,
26
- private readonly ceremonyRepo : CeremonyRepository ,
25
+ private readonly repo : CeremonyRepository ,
27
26
@Logger ( 'ceremony:service' ) private readonly logger : ILogger ,
28
27
) { }
29
28
30
29
async create ( input : CreateCeremony ) : Promise < ID > {
31
- try {
32
- const result = await this . ceremonyRepo . create ( input ) ;
33
-
34
- if ( ! result ) {
35
- throw new ServerException ( 'failed to create a ceremony' ) ;
36
- }
30
+ const { id } = await this . repo . create ( input ) ;
37
31
38
- return result . id ;
39
- } catch ( exception ) {
40
- this . logger . warning ( 'Failed to create ceremony' , {
41
- exception,
42
- } ) ;
43
-
44
- throw exception ;
45
- }
32
+ return id ;
46
33
}
47
34
48
35
@HandleIdLookup ( Ceremony )
@@ -56,36 +43,41 @@ export class CeremonyService {
56
43
throw new InputException ( 'No ceremony id to search for' , 'ceremony.id' ) ;
57
44
}
58
45
59
- const dto = await this . ceremonyRepo . readOne ( id , session ) ;
60
- return await this . secure ( dto , session ) ;
46
+ const dto = await this . repo . readOne ( id , session ) ;
47
+ return this . secure ( dto , session ) ;
61
48
}
62
49
63
50
async readMany ( ids : readonly ID [ ] , session : Session ) {
64
- const ceremonies = await this . ceremonyRepo . readMany ( ids , session ) ;
65
- return await Promise . all (
66
- ceremonies . map ( ( dto ) => this . secure ( dto , session ) ) ,
67
- ) ;
51
+ const ceremonies = await this . repo . readMany ( ids , session ) ;
52
+ return ceremonies . map ( ( dto ) => this . secure ( dto , session ) ) ;
68
53
}
69
54
70
- async secure ( dto : UnsecuredDto < Ceremony > , session : Session ) {
55
+ secure ( dto : UnsecuredDto < Ceremony > , session : Session ) {
71
56
return this . privileges . for ( session , Ceremony ) . secure ( dto ) ;
72
57
}
73
58
74
59
async update ( input : UpdateCeremony , session : Session ) : Promise < Ceremony > {
75
- const object = await this . readOne ( input . id , session ) ;
76
- const changes = this . ceremonyRepo . getActualChanges ( object , input ) ;
60
+ const object = await this . repo . readOne ( input . id , session ) ;
61
+ const changes = this . repo . getActualChanges ( object , input ) ;
77
62
this . privileges . for ( session , Ceremony , object ) . verifyChanges ( changes ) ;
78
- return await this . ceremonyRepo . update ( object , changes ) ;
63
+ const updated = await this . repo . update (
64
+ {
65
+ id : input . id ,
66
+ ...changes ,
67
+ } ,
68
+ session ,
69
+ ) ;
70
+ return this . secure ( updated , session ) ;
79
71
}
80
72
81
73
async delete ( id : ID , session : Session ) : Promise < void > {
82
- const object = await this . readOne ( id , session ) ;
74
+ const object = await this . repo . readOne ( id , session ) ;
83
75
84
76
// Only called internally, not exposed directly to users
85
77
// this.privileges.for(session, Ceremony, object).verifyCan('delete');
86
78
87
79
try {
88
- await this . ceremonyRepo . deleteNode ( object ) ;
80
+ await this . repo . deleteNode ( object ) ;
89
81
} catch ( exception ) {
90
82
this . logger . warning ( 'Failed to delete Ceremony' , {
91
83
exception,
@@ -98,7 +90,10 @@ export class CeremonyService {
98
90
input : CeremonyListInput ,
99
91
session : Session ,
100
92
) : Promise < CeremonyListOutput > {
101
- const results = await this . ceremonyRepo . list ( input , session ) ;
102
- return await mapListResults ( results , ( dto ) => this . secure ( dto , session ) ) ;
93
+ const results = await this . repo . list ( input , session ) ;
94
+ return {
95
+ ...results ,
96
+ items : results . items . map ( ( dto ) => this . secure ( dto , session ) ) ,
97
+ } ;
103
98
}
104
99
}
0 commit comments