@@ -2,17 +2,20 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common';
2
2
import {
3
3
DuplicateException ,
4
4
ID ,
5
+ IdOf ,
5
6
InputException ,
7
+ loadManyIgnoreMissingThrowAny ,
6
8
NotFoundException ,
7
9
ObjectView ,
8
10
ServerException ,
9
11
Session ,
10
12
UnauthorizedException ,
11
13
UnsecuredDto ,
12
14
} from '../../common' ;
13
- import { HandleIdLookup , ILogger , Logger } from '../../core' ;
15
+ import { HandleIdLookup , ILogger , Logger , ResourceLoader } from '../../core' ;
14
16
import { mapListResults } from '../../core/database/results' ;
15
17
import { Privileges } from '../authorization' ;
18
+ import { Location , LocationLoader , LocationType } from '../location' ;
16
19
import { FinancialReportingType } from '../partnership/dto/financial-reporting-type' ;
17
20
import {
18
21
IProject ,
@@ -38,6 +41,7 @@ export class PartnerService {
38
41
@Inject ( forwardRef ( ( ) => ProjectService ) )
39
42
private readonly projectService : ProjectService & { } ,
40
43
private readonly repo : PartnerRepository ,
44
+ private readonly resourceLoader : ResourceLoader ,
41
45
) { }
42
46
43
47
async create ( input : CreatePartner , session : Session ) : Promise < Partner > {
@@ -55,6 +59,10 @@ export class PartnerService {
55
59
) ;
56
60
}
57
61
62
+ if ( input . countries ) {
63
+ await this . verifyCountries ( input . countries ) ;
64
+ }
65
+
58
66
const id = await this . repo . create ( input , session ) ;
59
67
60
68
this . logger . debug ( `Partner created` , { id } ) ;
@@ -125,6 +133,7 @@ export class PartnerService {
125
133
pointOfContactId,
126
134
languageOfWiderCommunicationId,
127
135
fieldRegions,
136
+ countries,
128
137
...simpleChanges
129
138
} = changes ;
130
139
@@ -148,6 +157,22 @@ export class PartnerService {
148
157
) ;
149
158
}
150
159
160
+ if ( countries ) {
161
+ await this . verifyCountries ( countries ) ;
162
+
163
+ try {
164
+ await this . repo . updateRelationList ( {
165
+ id : partner . id ,
166
+ relation : 'countries' ,
167
+ newList : countries ,
168
+ } ) ;
169
+ } catch ( e ) {
170
+ throw e instanceof InputException
171
+ ? e . withField ( 'partner.countries' )
172
+ : e ;
173
+ }
174
+ }
175
+
151
176
if ( fieldRegions ) {
152
177
try {
153
178
await this . repo . updateRelationList ( {
@@ -234,4 +259,26 @@ export class PartnerService {
234
259
? false
235
260
: true ;
236
261
}
262
+
263
+ private async verifyCountries ( ids : ReadonlyArray < IdOf < Location > > ) {
264
+ const loader = await this . resourceLoader . getLoader ( LocationLoader ) ;
265
+ const locations = await loadManyIgnoreMissingThrowAny ( loader , ids ) ;
266
+ const invalidIds = locations . flatMap ( ( location ) =>
267
+ location . type . value !== 'Country' ? location . id : [ ] ,
268
+ ) ;
269
+ if ( invalidIds . length === 0 ) {
270
+ return ;
271
+ }
272
+ const ex = new LocationTypeException ( [ LocationType . Country ] , invalidIds ) ;
273
+ throw ex . withField ( 'partner.countries' ) ;
274
+ }
275
+ }
276
+
277
+ class LocationTypeException extends InputException {
278
+ constructor (
279
+ readonly allowedTypes : readonly LocationType [ ] ,
280
+ readonly invalidIds : ID [ ] ,
281
+ ) {
282
+ super ( 'Given locations do not match the expected type' ) ;
283
+ }
237
284
}
0 commit comments