Skip to content

Commit 26bf577

Browse files
Updated departmentId generation for Multiplication Projects (#3176)
Co-authored-by: Carson Full <[email protected]>
1 parent 0784c59 commit 26bf577

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed

dbschema/migrations/00003.edgeql

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dbschema/project.esdl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@ module default {
2828
.step >= Project::Step.PendingFinanceConfirmation
2929
) then ((
3030
with
31-
fa := assert_exists(
32-
__subject__.primaryLocation.fundingAccount,
33-
message := "Project must have a primary location"
34-
),
35-
existing := (
36-
select detached Project.departmentId filter Project.primaryLocation.fundingAccount = fa
37-
),
38-
available := (
39-
<str>range_unpack(range(fa.accountNumber * 10000 + 11, fa.accountNumber * 10000 + 9999))
40-
except existing
31+
info := (
32+
if __subject__ is MultiplicationTranslationProject
33+
then (prefix := 8, startingOffset := 201)
34+
else (
35+
prefix := (
36+
assert_exists(
37+
__subject__.primaryLocation.fundingAccount,
38+
message := "Project must have a primary location"
39+
).accountNumber
40+
),
41+
startingOffset := 11
42+
)
43+
),
44+
select min(
45+
<str>range_unpack(range(info.prefix * 10000 + info.startingOffset, info.prefix * 10000 + 9999))
46+
except detached Project.departmentId
4147
)
42-
select min(available)
43-
)) else .departmentId
48+
)) else .departmentId
4449
);
4550
};
4651

src/components/project/handlers/set-department-id.handler.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
EventsHandler,
1111
IEventHandler,
1212
} from '../../../core';
13-
import { Project, ProjectStep } from '../dto';
13+
import { Project, ProjectStep, ProjectType } from '../dto';
1414
import { ProjectUpdatedEvent } from '../events';
1515

1616
type SubscribedEvent = ProjectUpdatedEvent;
@@ -55,7 +55,17 @@ export class SetDepartmentId implements IEventHandler<SubscribedEvent> {
5555
}
5656

5757
private async assignDepartmentIdForProject(project: UnsecuredDto<Project>) {
58-
const departmentIdPrefix = await this.getFundingAccountNumber(project);
58+
const info =
59+
project.type === ProjectType.MultiplicationTranslation
60+
? {
61+
departmentIdPrefix: '8',
62+
startingOffset: 201,
63+
}
64+
: {
65+
departmentIdPrefix: await this.getFundingAccountNumber(project),
66+
startingOffset: 11,
67+
};
68+
5969
const res = await this.db
6070
.query()
6171
.raw(
@@ -64,7 +74,7 @@ export class SetDepartmentId implements IEventHandler<SubscribedEvent> {
6474
MATCH ()-[:departmentId]-(departmentIdPropertyNode:Property)
6575
WHERE departmentIdPropertyNode.value STARTS WITH $departmentIdPrefix
6676
WITH collect(distinct(toInteger(right(departmentIdPropertyNode.value, 4)))) as listOfDepartmentIds
67-
WITH [n IN range(11, 9999) WHERE NOT n IN listOfDepartmentIds] as listOfUnusedDepartmentIds
77+
WITH [n IN range($startingOffset, 9999) WHERE NOT n IN listOfDepartmentIds] as listOfUnusedDepartmentIds
6878
WITH apoc.coll.shuffle(listOfUnusedDepartmentIds) AS randomizedIds
6979
WITH toString(randomizedIds[0]) AS nextIdBase
7080
WITH $departmentIdPrefix + substring("0000", 1, 4 - size(nextIdBase)) + nextIdBase as nextId
@@ -75,7 +85,7 @@ export class SetDepartmentId implements IEventHandler<SubscribedEvent> {
7585
CREATE (project)-[newDepartmentIdRelationship:departmentId { active: true, createdAt: datetime() }]->(:Property { createdAt: datetime(), value: departmentId })
7686
RETURN departmentId
7787
`,
78-
{ departmentIdPrefix: departmentIdPrefix, projectId: project.id },
88+
{ ...info, projectId: project.id },
7989
)
8090
.asResult<{ departmentId: ID }>()
8191
.first();

0 commit comments

Comments
 (0)