Skip to content

Commit dbc9772

Browse files
author
William Harris
committed
added countries field to the partner object
1 parent b79639f commit dbc9772

File tree

7 files changed

+55
-0
lines changed

7 files changed

+55
-0
lines changed

src/components/location/dto/location.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Secured,
1111
SecuredEnum,
1212
SecuredProperty,
13+
SecuredPropertyList,
1314
SecuredProps,
1415
SecuredString,
1516
SecuredStringNullable,
@@ -54,6 +55,11 @@ export class Location extends Resource {
5455
})
5556
export class SecuredLocation extends SecuredProperty(Location) {}
5657

58+
@ObjectType({
59+
description: SecuredPropertyList.descriptionFor('a list of locations'),
60+
})
61+
export class SecuredLocations extends SecuredPropertyList(Location) {}
62+
5763
declare module '~/core/resources/map' {
5864
interface ResourceMap {
5965
Location: typeof Location;

src/components/partner/dto/create-partner.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Transform, Type } from 'class-transformer';
33
import { Matches, ValidateNested } from 'class-validator';
44
import { uniq } from 'lodash';
55
import { ID, IdField, IdOf, IsId, NameField } from '../../../common';
6+
import { Location } from '../../../components/location';
67
import { FieldRegion } from '../../field-region';
78
import type { Language } from '../../language';
89
import { FinancialReportingType } from '../../partnership/dto/financial-reporting-type';
@@ -43,6 +44,11 @@ export abstract class CreatePartner {
4344
@IdField({ nullable: true })
4445
readonly languageOfWiderCommunicationId?: IdOf<Language> | null;
4546

47+
@Field(() => [IDType], { nullable: true })
48+
@IsId({ each: true })
49+
@Transform(({ value }) => uniq(value))
50+
readonly countries?: ReadonlyArray<IdOf<Location>> = [];
51+
4652
@Field(() => [IDType], { nullable: true })
4753
@IsId({ each: true })
4854
@Transform(({ value }) => uniq(value))

src/components/partner/dto/partner.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Sensitivity,
2020
SensitivityField,
2121
} from '../../../common';
22+
import { Location } from '../../../components/location';
2223
import { ScopedRole } from '../../authorization';
2324
import { FieldRegion } from '../../field-region';
2425
import type { Language } from '../../language';
@@ -78,6 +79,7 @@ export class Partner extends Interfaces {
7879
readonly languageOfWiderCommunication: Secured<IdOf<Language> | null>;
7980

8081
readonly fieldRegions: Required<Secured<ReadonlyArray<IdOf<FieldRegion>>>>;
82+
readonly countries: Required<Secured<ReadonlyArray<IdOf<Location>>>>;
8183

8284
@DateTimeField()
8385
readonly modifiedAt: DateTime;

src/components/partner/dto/update-partner.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Transform, Type } from 'class-transformer';
33
import { Matches, ValidateNested } from 'class-validator';
44
import { uniq } from 'lodash';
55
import { ID, IdField, IdOf, IsId, NameField } from '../../../common';
6+
import { Location } from '../../../components/location';
67
import { FieldRegion } from '../../field-region';
78
import type { Language } from '../../language';
89
import { FinancialReportingType } from '../../partnership/dto/financial-reporting-type';
@@ -43,6 +44,11 @@ export abstract class UpdatePartner {
4344
@IdField({ nullable: true })
4445
readonly languageOfWiderCommunicationId?: IdOf<Language> | null;
4546

47+
@Field(() => [IDType], { nullable: true })
48+
@IsId({ each: true })
49+
@Transform(({ value }) => (value ? uniq(value) : undefined))
50+
readonly countries?: ReadonlyArray<IdOf<Location>>;
51+
4652
@Field(() => [IDType], { nullable: true })
4753
@IsId({ each: true })
4854
@Transform(({ value }) => (value ? uniq(value) : undefined))

src/components/partner/partner.repository.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class PartnerRepository extends DtoRepository<
6464
input.languageOfWiderCommunicationId,
6565
],
6666
fieldRegions: ['FieldRegion', input.fieldRegions],
67+
countries: ['Location', input.countries],
6768
}),
6869
)
6970
.return<{ id: ID }>('node.id as id')
@@ -114,6 +115,15 @@ export class PartnerRepository extends DtoRepository<
114115
])
115116
.return(collect('fieldRegions.id').as('fieldRegionsIds')),
116117
)
118+
.subQuery('node', (sub) =>
119+
sub
120+
.match([
121+
node('node'),
122+
relation('out', '', 'countries'),
123+
node('countries', 'Location'),
124+
])
125+
.return(collect('countries.id').as('countriesIds')),
126+
)
117127
.apply(matchProps())
118128
.optionalMatch([
119129
node('node'),
@@ -137,6 +147,7 @@ export class PartnerRepository extends DtoRepository<
137147
pointOfContact: 'pointOfContact.id',
138148
languageOfWiderCommunication: 'languageOfWiderCommunication.id',
139149
fieldRegions: 'fieldRegionsIds',
150+
countries: 'countriesIds',
140151
scope: 'scopedRoles',
141152
pinned: 'exists((:User { id: $requestingUser })-[:pinned]->(node))',
142153
}).as('dto'),

src/components/partner/partner.resolver.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { Loader, LoaderOf } from '../../core';
2020
import { FieldRegionLoader, SecuredFieldRegions } from '../field-region';
2121
import { LanguageLoader, SecuredLanguageNullable } from '../language';
22+
import { LocationLoader, SecuredLocations } from '../location';
2223
import { OrganizationLoader, SecuredOrganization } from '../organization';
2324
import { PartnerLoader, PartnerService } from '../partner';
2425
import {
@@ -103,6 +104,14 @@ export class PartnerResolver {
103104
return await loadSecuredIds(loader, partner.fieldRegions);
104105
}
105106

107+
@ResolveField(() => SecuredLocations)
108+
async countries(
109+
@Parent() partner: Partner,
110+
@Loader(LocationLoader) loader: LoaderOf<LocationLoader>,
111+
): Promise<SecuredLocations> {
112+
return await loadSecuredIds(loader, partner.countries);
113+
}
114+
106115
@ResolveField(() => SecuredProjectList, {
107116
description: 'The list of projects the partner has a partnership with.',
108117
})

src/components/partner/partner.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export class PartnerService {
125125
pointOfContactId,
126126
languageOfWiderCommunicationId,
127127
fieldRegions,
128+
countries,
128129
...simpleChanges
129130
} = changes;
130131

@@ -148,6 +149,20 @@ export class PartnerService {
148149
);
149150
}
150151

152+
if (countries) {
153+
try {
154+
await this.repo.updateRelationList({
155+
id: partner.id,
156+
relation: 'countries',
157+
newList: countries,
158+
});
159+
} catch (e) {
160+
throw e instanceof InputException
161+
? e.withField('partner.countries')
162+
: e;
163+
}
164+
}
165+
151166
if (fieldRegions) {
152167
try {
153168
await this.repo.updateRelationList({

0 commit comments

Comments
 (0)