Skip to content

Commit 1bbaab1

Browse files
authored
0573 Location.defaultMarketingRegion (#2987)
1 parent a501c9a commit 1bbaab1

File tree

7 files changed

+89
-3
lines changed

7 files changed

+89
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Field, InputType, ObjectType } from '@nestjs/graphql';
22
import { Type } from 'class-transformer';
33
import { ValidateNested } from 'class-validator';
4-
import { ID, IdField, ISO31661Alpha3, NameField } from '../../../common';
4+
import { ID, IdField, IdOf, ISO31661Alpha3, NameField } from '../../../common';
55
import { Transform } from '../../../common/transform.decorator';
66
import { CreateDefinedFileVersionInput } from '../../file/dto';
77
import { LocationType } from './location-type.enum';
@@ -29,6 +29,9 @@ export abstract class CreateLocation {
2929
@IdField({ nullable: true })
3030
readonly defaultFieldRegionId?: ID;
3131

32+
@IdField({ nullable: true })
33+
readonly defaultMarketingRegionId?: IdOf<Location>;
34+
3235
@Field({ nullable: true })
3336
@Type(() => CreateDefinedFileVersionInput)
3437
@ValidateNested()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DbLabel,
77
DbUnique,
88
ID,
9+
IdOf,
910
NameField,
1011
Resource,
1112
Secured,
@@ -49,6 +50,8 @@ export class Location extends Resource {
4950

5051
readonly defaultFieldRegion: Secured<ID | null>;
5152

53+
readonly defaultMarketingRegion: Secured<IdOf<Location> | null>;
54+
5255
readonly mapImage: DefinedFile;
5356
}
5457

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Field, InputType, ObjectType } from '@nestjs/graphql';
22
import { Type } from 'class-transformer';
33
import { ValidateNested } from 'class-validator';
4-
import { ID, IdField, ISO31661Alpha3, NameField } from '../../../common';
4+
import { ID, IdField, IdOf, ISO31661Alpha3, NameField } from '../../../common';
55
import { Transform } from '../../../common/transform.decorator';
66
import { CreateDefinedFileVersionInput } from '../../file/dto';
77
import { LocationType } from './location-type.enum';
@@ -32,6 +32,9 @@ export abstract class UpdateLocation {
3232
@IdField({ nullable: true })
3333
readonly defaultFieldRegionId?: ID | null;
3434

35+
@IdField({ nullable: true })
36+
readonly defaultMarketingRegionId?: IdOf<Location>;
37+
3538
@Field({ nullable: true })
3639
@Type(() => CreateDefinedFileVersionInput)
3740
@ValidateNested()

src/components/location/location.repository.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class LocationRepository extends DtoRepository(Location) {
5757
createRelationships(Location, 'out', {
5858
fundingAccount: ['FundingAccount', input.fundingAccountId],
5959
defaultFieldRegion: ['FieldRegion', input.defaultFieldRegionId],
60+
defaultMarketingRegion: ['Location', input.defaultMarketingRegionId],
6061
}),
6162
)
6263
.return<{ id: ID }>('node.id as id');
@@ -74,6 +75,7 @@ export class LocationRepository extends DtoRepository(Location) {
7475
id,
7576
fundingAccountId,
7677
defaultFieldRegionId,
78+
defaultMarketingRegionId,
7779
mapImage,
7880
...simpleChanges
7981
} = changes;
@@ -97,6 +99,15 @@ export class LocationRepository extends DtoRepository(Location) {
9799
defaultFieldRegionId,
98100
);
99101
}
102+
103+
if (defaultMarketingRegionId !== undefined) {
104+
await this.updateRelation(
105+
'defaultMarketingRegion',
106+
'Location',
107+
id,
108+
defaultMarketingRegionId,
109+
);
110+
}
100111
}
101112

102113
protected hydrate() {
@@ -113,10 +124,16 @@ export class LocationRepository extends DtoRepository(Location) {
113124
relation('out', '', 'defaultFieldRegion', ACTIVE),
114125
node('defaultFieldRegion', 'FieldRegion'),
115126
])
127+
.optionalMatch([
128+
node('node'),
129+
relation('out', '', 'defaultMarketingRegion', ACTIVE),
130+
node('defaultMarketingRegion', 'Location'),
131+
])
116132
.return<{ dto: UnsecuredDto<Location> }>(
117133
merge('props', {
118134
fundingAccount: 'fundingAccount.id',
119135
defaultFieldRegion: 'defaultFieldRegion.id',
136+
defaultMarketingRegion: 'defaultMarketingRegion.id',
120137
}).as('dto'),
121138
);
122139
}

src/components/location/location.resolver.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
Location,
3131
LocationListInput,
3232
LocationListOutput,
33+
SecuredLocation,
3334
UpdateLocationInput,
3435
UpdateLocationOutput,
3536
} from './dto';
@@ -85,6 +86,16 @@ export class LocationResolver {
8586
);
8687
}
8788

89+
@ResolveField(() => SecuredLocation)
90+
async defaultMarketingRegion(
91+
@Parent() location: Location,
92+
@Loader(LocationLoader) defaultMarketingRegions: LoaderOf<LocationLoader>,
93+
): Promise<SecuredLocation> {
94+
return await mapSecuredValue(location.defaultMarketingRegion, (id) =>
95+
defaultMarketingRegions.load(id),
96+
);
97+
}
98+
8899
@ResolveField(() => SecuredFile)
89100
async mapImage(
90101
@Parent() location: Location,

test/location.e2e-spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { faker } from '@faker-js/faker';
22
import { times } from 'lodash';
3-
import { generateId, isValidId } from '../src/common';
3+
import { generateId, IdOf, isValidId } from '../src/common';
44
import { Location } from '../src/components/location';
55
import {
66
createFundingAccount,
@@ -160,6 +160,38 @@ describe('Location e2e', () => {
160160
expect(updated.defaultFieldRegion.value.id).toBe(newFieldRegion.id);
161161
});
162162

163+
it('update location with defaultMarketingRegion', async () => {
164+
const defaultMarketingRegion = await createLocation(app);
165+
const l = await createLocation(app, {
166+
defaultMarketingRegionId: defaultMarketingRegion.id as IdOf<Location>,
167+
});
168+
const newMarketingRegion = await createLocation(app);
169+
170+
const result = await app.graphql.mutate(
171+
gql`
172+
mutation updateLocation($input: UpdateLocationInput!) {
173+
updateLocation(input: $input) {
174+
location {
175+
...location
176+
}
177+
}
178+
}
179+
${fragments.location}
180+
`,
181+
{
182+
input: {
183+
location: {
184+
id: l.id,
185+
defaultMarketingRegionId: newMarketingRegion.id,
186+
},
187+
},
188+
},
189+
);
190+
const updated = result.updateLocation.location;
191+
expect(updated).toBeTruthy();
192+
expect(updated.defaultMarketingRegion.value.id).toBe(newMarketingRegion.id);
193+
});
194+
163195
it('update location with funding account', async () => {
164196
const fundingAccount = await createFundingAccount(app);
165197
const st = await createLocation(app, {

test/utility/fragments.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,17 @@ export const fundingAccount = gql`
11271127
}
11281128
`;
11291129

1130+
export const locationName = gql`
1131+
fragment locationName on Location {
1132+
id
1133+
name {
1134+
value
1135+
canRead
1136+
canEdit
1137+
}
1138+
}
1139+
`;
1140+
11301141
export const location = gql`
11311142
fragment location on Location {
11321143
id
@@ -1158,9 +1169,15 @@ export const location = gql`
11581169
...fieldRegion
11591170
}
11601171
}
1172+
defaultMarketingRegion {
1173+
value {
1174+
...locationName
1175+
}
1176+
}
11611177
}
11621178
${fundingAccount}
11631179
${fieldRegion}
1180+
${locationName}
11641181
`;
11651182

11661183
export const projectChangeRequest = gql`

0 commit comments

Comments
 (0)