@@ -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 } ) ;
@@ -150,6 +158,8 @@ export class PartnerService {
150
158
}
151
159
152
160
if ( countries ) {
161
+ await this . verifyCountries ( countries ) ;
162
+
153
163
try {
154
164
await this . repo . updateRelationList ( {
155
165
id : partner . id ,
@@ -249,4 +259,26 @@ export class PartnerService {
249
259
? false
250
260
: true ;
251
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
+ }
252
284
}
0 commit comments