Skip to content

Commit 590f62c

Browse files
William HarrisCarsonF
andcommitted
Limit Partner.countries to only allow locations that are type == Country
Co-authored-by: Carson Full <[email protected]>
1 parent dbc9772 commit 590f62c

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/components/partner/partner.service.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common';
22
import {
33
DuplicateException,
44
ID,
5+
IdOf,
56
InputException,
7+
loadManyIgnoreMissingThrowAny,
68
NotFoundException,
79
ObjectView,
810
ServerException,
911
Session,
1012
UnauthorizedException,
1113
UnsecuredDto,
1214
} from '../../common';
13-
import { HandleIdLookup, ILogger, Logger } from '../../core';
15+
import { HandleIdLookup, ILogger, Logger, ResourceLoader } from '../../core';
1416
import { mapListResults } from '../../core/database/results';
1517
import { Privileges } from '../authorization';
18+
import { Location, LocationLoader, LocationType } from '../location';
1619
import { FinancialReportingType } from '../partnership/dto/financial-reporting-type';
1720
import {
1821
IProject,
@@ -38,6 +41,7 @@ export class PartnerService {
3841
@Inject(forwardRef(() => ProjectService))
3942
private readonly projectService: ProjectService & {},
4043
private readonly repo: PartnerRepository,
44+
private readonly resourceLoader: ResourceLoader,
4145
) {}
4246

4347
async create(input: CreatePartner, session: Session): Promise<Partner> {
@@ -55,6 +59,10 @@ export class PartnerService {
5559
);
5660
}
5761

62+
if (input.countries) {
63+
await this.verifyCountries(input.countries);
64+
}
65+
5866
const id = await this.repo.create(input, session);
5967

6068
this.logger.debug(`Partner created`, { id });
@@ -150,6 +158,8 @@ export class PartnerService {
150158
}
151159

152160
if (countries) {
161+
await this.verifyCountries(countries);
162+
153163
try {
154164
await this.repo.updateRelationList({
155165
id: partner.id,
@@ -249,4 +259,26 @@ export class PartnerService {
249259
? false
250260
: true;
251261
}
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+
}
252284
}

0 commit comments

Comments
 (0)