Skip to content

Commit 016eb37

Browse files
authored
Add Project.marketingRegionOverride (#3045)
1 parent 1bbaab1 commit 016eb37

File tree

10 files changed

+76
-3
lines changed

10 files changed

+76
-3
lines changed

src/components/authorization/policies/by-role/intern.policy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import { inherit, member, Policy, Role } from '../util';
2727
r.Project.when(member)
2828
.read.specifically((p) => [
2929
p.rootDirectory.edit,
30-
p.many('departmentId', 'marketingLocation', 'fieldRegion').none,
30+
p.many(
31+
'departmentId',
32+
'marketingLocation',
33+
'marketingRegionOverride',
34+
'fieldRegion',
35+
).none,
3136
])
3237
.children((c) => c.posts.edit),
3338
r.ProjectMember.when(member).read,

src/components/authorization/policies/by-role/marketing.policy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
.many('rootDirectory', 'primaryLocation', 'otherLocations')
3737
.whenAny(member, sensOnlyLow).read,
3838
p.marketingLocation.edit,
39+
p.marketingRegionOverride.edit,
3940
])
4041
.children((c) => c.posts.edit),
4142
])

src/components/authorization/policies/by-role/mentor.policy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ import { inherit, member, Policy, Role } from '../util';
2828
r.Project.when(member)
2929
.read.specifically((p) => [
3030
p.rootDirectory.edit,
31-
p.many('departmentId', 'marketingLocation', 'fieldRegion').none,
31+
p.many(
32+
'departmentId',
33+
'marketingLocation',
34+
'marketingRegionOverride',
35+
'fieldRegion',
36+
).none,
3237
])
3338
.children((c) => c.posts.edit),
3439
r.ProjectMember.when(member).read,

src/components/authorization/policies/by-role/translator.policy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ import { member, Policy, Role, variant } from '../util';
4040
r.Project.when(member)
4141
.read.specifically((p) => [
4242
p.rootDirectory.edit,
43-
p.many('departmentId', 'marketingLocation', 'fieldRegion').none,
43+
p.many(
44+
'departmentId',
45+
'marketingLocation',
46+
'marketingRegionOverride',
47+
'fieldRegion',
48+
).none,
4449
])
4550
.children((c) => c.posts.edit),
4651
r.ProjectMember.when(member).read,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
DateTimeField,
1010
ID,
1111
IdField,
12+
IdOf,
1213
IsId,
1314
NameField,
1415
Sensitivity,
1516
SensitivityField,
1617
} from '../../../common';
18+
import { Location } from '../../location/dto';
1719
import { ReportPeriod } from '../../periodic-report/dto';
1820
import { ProjectStep } from './project-step.enum';
1921
import { ProjectType } from './project-type.enum';
@@ -46,6 +48,12 @@ export abstract class CreateProject {
4648
})
4749
readonly marketingLocationId?: ID;
4850

51+
@IdField({
52+
description: 'A marketing region override location ID',
53+
nullable: true,
54+
})
55+
readonly marketingRegionOverrideId?: IdOf<Location> | null;
56+
4957
@IdField({
5058
description: 'A field region ID',
5159
nullable: true,

src/components/project/dto/project.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
DbSort,
1515
DbUnique,
1616
ID,
17+
IdOf,
1718
IntersectionType,
1819
NameField,
1920
parentIdMiddleware,
@@ -128,6 +129,7 @@ class Project extends Interfaces {
128129

129130
readonly marketingLocation: Secured<ID | null>;
130131

132+
readonly marketingRegionOverride: Secured<IdOf<Location> | null>;
131133
readonly fieldRegion: Secured<ID | null>;
132134

133135
readonly owningOrganization: Secured<ID | null>;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
DateTimeField,
1010
ID,
1111
IdField,
12+
IdOf,
1213
NameField,
1314
Sensitivity,
1415
SensitivityField,
1516
} from '../../../common';
1617
import { ChangesetIdField } from '../../changeset';
18+
import { Location } from '../../location/dto';
1719
import { ReportPeriod } from '../../periodic-report/dto';
1820
import { ProjectStep } from './project-step.enum';
1921
import { IProject, Project } from './project.dto';
@@ -38,6 +40,12 @@ export abstract class UpdateProject {
3840
})
3941
readonly marketingLocationId?: ID | null;
4042

43+
@IdField({
44+
description: 'A marketing region override location ID',
45+
nullable: true,
46+
})
47+
readonly marketingRegionOverrideId?: IdOf<Location> | null;
48+
4149
@IdField({
4250
description: 'A field region ID',
4351
nullable: true,

src/components/project/project.repository.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export class ProjectRepository extends CommonRepository {
115115
relation('out', '', 'owningOrganization', ACTIVE),
116116
node('organization', 'Organization'),
117117
])
118+
.optionalMatch([
119+
node('node'),
120+
relation('out', '', 'marketingRegionOverride', ACTIVE),
121+
node('marketingRegionOverride', 'Location'),
122+
])
118123
.subQuery('node', (sub) =>
119124
sub
120125
.match([
@@ -135,6 +140,7 @@ export class ProjectRepository extends CommonRepository {
135140
owningOrganization: 'organization.id',
136141
engagementTotal: 'engagementTotal',
137142
changeset: 'changeset.id',
143+
marketingRegionOverride: 'marketingRegionOverride.id',
138144
}).as('dto'),
139145
);
140146
}
@@ -156,6 +162,7 @@ export class ProjectRepository extends CommonRepository {
156162
primaryLocationId,
157163
fieldRegionId,
158164
marketingLocationId,
165+
marketingRegionOverrideId,
159166
otherLocationIds,
160167
type,
161168
...initialProps
@@ -194,6 +201,7 @@ export class ProjectRepository extends CommonRepository {
194201
primaryLocation: ['Location', primaryLocationId],
195202
otherLocations: ['Location', otherLocationIds],
196203
marketingLocation: ['Location', marketingLocationId],
204+
marketingRegionOverride: ['Location', marketingRegionOverrideId],
197205
owningOrganization: ['Organization', this.config.defaultOrg.id],
198206
}),
199207
)
@@ -213,6 +221,7 @@ export class ProjectRepository extends CommonRepository {
213221
const {
214222
primaryLocationId,
215223
marketingLocationId,
224+
marketingRegionOverrideId,
216225
fieldRegionId,
217226
...simpleChanges
218227
} = changes;
@@ -269,6 +278,20 @@ export class ProjectRepository extends CommonRepository {
269278
};
270279
}
271280

281+
if (marketingRegionOverrideId !== undefined) {
282+
await this.updateRelation(
283+
'marketingRegionOverride',
284+
'Location',
285+
existing.id,
286+
marketingRegionOverrideId,
287+
'Project',
288+
);
289+
result = {
290+
...result,
291+
marketingRegionOverride: marketingRegionOverrideId,
292+
};
293+
}
294+
272295
return result;
273296
}
274297

src/components/project/project.resolver.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ export class ProjectResolver {
316316
);
317317
}
318318

319+
@ResolveField(() => SecuredLocation)
320+
async marketingRegionOverride(
321+
@Parent() project: Project,
322+
@Loader(LocationLoader) locations: LoaderOf<LocationLoader>,
323+
): Promise<SecuredLocation> {
324+
return await mapSecuredValue(project.marketingRegionOverride, (id) =>
325+
locations.load(id),
326+
);
327+
}
328+
319329
@ResolveField(() => SecuredFieldRegion)
320330
async fieldRegion(
321331
@Parent() project: Project,

src/components/project/project.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ export class ProjectService {
135135
'marketingLocationId',
136136
'Marketing location not found',
137137
);
138+
await this.validateOtherResourceId(
139+
input.marketingRegionOverrideId,
140+
'Location',
141+
'marketingRegionOverrideId',
142+
'Marketing Region Override not found',
143+
);
138144

139145
try {
140146
const id = await this.repo.create(input);

0 commit comments

Comments
 (0)