Skip to content

Commit

Permalink
0573 Location.defaultMarketingRegion (#2987)
Browse files Browse the repository at this point in the history
  • Loading branch information
willdch authored Jan 16, 2024
1 parent a501c9a commit 1bbaab1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/components/location/dto/create-location.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { ID, IdField, ISO31661Alpha3, NameField } from '../../../common';
import { ID, IdField, IdOf, ISO31661Alpha3, NameField } from '../../../common';
import { Transform } from '../../../common/transform.decorator';
import { CreateDefinedFileVersionInput } from '../../file/dto';
import { LocationType } from './location-type.enum';
Expand Down Expand Up @@ -29,6 +29,9 @@ export abstract class CreateLocation {
@IdField({ nullable: true })
readonly defaultFieldRegionId?: ID;

@IdField({ nullable: true })
readonly defaultMarketingRegionId?: IdOf<Location>;

@Field({ nullable: true })
@Type(() => CreateDefinedFileVersionInput)
@ValidateNested()
Expand Down
3 changes: 3 additions & 0 deletions src/components/location/dto/location.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DbLabel,
DbUnique,
ID,
IdOf,
NameField,
Resource,
Secured,
Expand Down Expand Up @@ -49,6 +50,8 @@ export class Location extends Resource {

readonly defaultFieldRegion: Secured<ID | null>;

readonly defaultMarketingRegion: Secured<IdOf<Location> | null>;

readonly mapImage: DefinedFile;
}

Expand Down
5 changes: 4 additions & 1 deletion src/components/location/dto/update-location.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { ID, IdField, ISO31661Alpha3, NameField } from '../../../common';
import { ID, IdField, IdOf, ISO31661Alpha3, NameField } from '../../../common';
import { Transform } from '../../../common/transform.decorator';
import { CreateDefinedFileVersionInput } from '../../file/dto';
import { LocationType } from './location-type.enum';
Expand Down Expand Up @@ -32,6 +32,9 @@ export abstract class UpdateLocation {
@IdField({ nullable: true })
readonly defaultFieldRegionId?: ID | null;

@IdField({ nullable: true })
readonly defaultMarketingRegionId?: IdOf<Location>;

@Field({ nullable: true })
@Type(() => CreateDefinedFileVersionInput)
@ValidateNested()
Expand Down
17 changes: 17 additions & 0 deletions src/components/location/location.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class LocationRepository extends DtoRepository(Location) {
createRelationships(Location, 'out', {
fundingAccount: ['FundingAccount', input.fundingAccountId],
defaultFieldRegion: ['FieldRegion', input.defaultFieldRegionId],
defaultMarketingRegion: ['Location', input.defaultMarketingRegionId],
}),
)
.return<{ id: ID }>('node.id as id');
Expand All @@ -74,6 +75,7 @@ export class LocationRepository extends DtoRepository(Location) {
id,
fundingAccountId,
defaultFieldRegionId,
defaultMarketingRegionId,
mapImage,
...simpleChanges
} = changes;
Expand All @@ -97,6 +99,15 @@ export class LocationRepository extends DtoRepository(Location) {
defaultFieldRegionId,
);
}

if (defaultMarketingRegionId !== undefined) {
await this.updateRelation(
'defaultMarketingRegion',
'Location',
id,
defaultMarketingRegionId,
);
}
}

protected hydrate() {
Expand All @@ -113,10 +124,16 @@ export class LocationRepository extends DtoRepository(Location) {
relation('out', '', 'defaultFieldRegion', ACTIVE),
node('defaultFieldRegion', 'FieldRegion'),
])
.optionalMatch([
node('node'),
relation('out', '', 'defaultMarketingRegion', ACTIVE),
node('defaultMarketingRegion', 'Location'),
])
.return<{ dto: UnsecuredDto<Location> }>(
merge('props', {
fundingAccount: 'fundingAccount.id',
defaultFieldRegion: 'defaultFieldRegion.id',
defaultMarketingRegion: 'defaultMarketingRegion.id',
}).as('dto'),
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/components/location/location.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Location,
LocationListInput,
LocationListOutput,
SecuredLocation,
UpdateLocationInput,
UpdateLocationOutput,
} from './dto';
Expand Down Expand Up @@ -85,6 +86,16 @@ export class LocationResolver {
);
}

@ResolveField(() => SecuredLocation)
async defaultMarketingRegion(
@Parent() location: Location,
@Loader(LocationLoader) defaultMarketingRegions: LoaderOf<LocationLoader>,
): Promise<SecuredLocation> {
return await mapSecuredValue(location.defaultMarketingRegion, (id) =>
defaultMarketingRegions.load(id),
);
}

@ResolveField(() => SecuredFile)
async mapImage(
@Parent() location: Location,
Expand Down
34 changes: 33 additions & 1 deletion test/location.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faker } from '@faker-js/faker';
import { times } from 'lodash';
import { generateId, isValidId } from '../src/common';
import { generateId, IdOf, isValidId } from '../src/common';
import { Location } from '../src/components/location';
import {
createFundingAccount,
Expand Down Expand Up @@ -160,6 +160,38 @@ describe('Location e2e', () => {
expect(updated.defaultFieldRegion.value.id).toBe(newFieldRegion.id);
});

it('update location with defaultMarketingRegion', async () => {
const defaultMarketingRegion = await createLocation(app);
const l = await createLocation(app, {
defaultMarketingRegionId: defaultMarketingRegion.id as IdOf<Location>,
});
const newMarketingRegion = await createLocation(app);

const result = await app.graphql.mutate(
gql`
mutation updateLocation($input: UpdateLocationInput!) {
updateLocation(input: $input) {
location {
...location
}
}
}
${fragments.location}
`,
{
input: {
location: {
id: l.id,
defaultMarketingRegionId: newMarketingRegion.id,
},
},
},
);
const updated = result.updateLocation.location;
expect(updated).toBeTruthy();
expect(updated.defaultMarketingRegion.value.id).toBe(newMarketingRegion.id);
});

it('update location with funding account', async () => {
const fundingAccount = await createFundingAccount(app);
const st = await createLocation(app, {
Expand Down
17 changes: 17 additions & 0 deletions test/utility/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,17 @@ export const fundingAccount = gql`
}
`;

export const locationName = gql`
fragment locationName on Location {
id
name {
value
canRead
canEdit
}
}
`;

export const location = gql`
fragment location on Location {
id
Expand Down Expand Up @@ -1158,9 +1169,15 @@ export const location = gql`
...fieldRegion
}
}
defaultMarketingRegion {
value {
...locationName
}
}
}
${fundingAccount}
${fieldRegion}
${locationName}
`;

export const projectChangeRequest = gql`
Expand Down

0 comments on commit 1bbaab1

Please sign in to comment.