Skip to content

Commit 8767124

Browse files
committed
Implement departmentId assignment
1 parent 3ad8e2b commit 8767124

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

dbschema/migrations/00036.edgeql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CREATE MIGRATION m1ydhle3dwjru34saeiidf5dyepbesugdwttrvnsyxhezpme5bfqga
2+
ONTO m13bndkbclxwviy3uj4eskx56bd2chw2xbvfn5pae557oslmbz2ssa
3+
{
4+
ALTER TYPE default::Project {
5+
ALTER PROPERTY departmentId {
6+
CREATE REWRITE
7+
UPDATE
8+
USING ((IF ((NOT (EXISTS (.departmentId)) AND (.status <= Project::Status.Active)) AND (.step >= Project::Step.PendingFinanceConfirmation)) THEN (WITH
9+
fa :=
10+
std::assert_exists(__subject__.primaryLocation.fundingAccount, message := 'Project must have a primary location')
11+
,
12+
existing :=
13+
(SELECT
14+
(DETACHED default::Project).departmentId
15+
FILTER
16+
(default::Project.primaryLocation.fundingAccount = fa)
17+
)
18+
,
19+
available :=
20+
(std::range_unpack(std::range(((fa.accountNumber * 10000) + 11), ((fa.accountNumber * 10000) + 9999))) EXCEPT existing)
21+
SELECT
22+
std::min(available)
23+
) ELSE .departmentId));
24+
};
25+
};
26+
};

dbschema/project.esdl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ module default {
1414
constraint exclusive;
1515
constraint min_value(10000);
1616
constraint max_value(99999);
17+
rewrite update using (
18+
if (
19+
not exists .departmentId and
20+
.status <= Project::Status.Active and
21+
.step >= Project::Step.PendingFinanceConfirmation
22+
) then ((
23+
with
24+
fa := assert_exists(
25+
__subject__.primaryLocation.fundingAccount,
26+
message := "Project must have a primary location"
27+
),
28+
existing := (
29+
select detached Project.departmentId filter Project.primaryLocation.fundingAccount = fa
30+
),
31+
available := (
32+
range_unpack(range(fa.accountNumber * 10000 + 11, fa.accountNumber * 10000 + 9999))
33+
except existing
34+
)
35+
select min(available)
36+
)) else .departmentId
37+
);
1738
};
1839

1940
required step: Project::Step {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ import {
44
ServerException,
55
UnsecuredDto,
66
} from '../../../common';
7-
import { DatabaseService, EventsHandler, IEventHandler } from '../../../core';
7+
import {
8+
ConfigService,
9+
DatabaseService,
10+
EventsHandler,
11+
IEventHandler,
12+
} from '../../../core';
813
import { Project, ProjectStep } from '../dto';
914
import { ProjectUpdatedEvent } from '../events';
1015

1116
type SubscribedEvent = ProjectUpdatedEvent;
1217

1318
@EventsHandler(ProjectUpdatedEvent)
1419
export class SetDepartmentId implements IEventHandler<SubscribedEvent> {
15-
constructor(private readonly db: DatabaseService) {}
20+
constructor(
21+
private readonly db: DatabaseService,
22+
private readonly config: ConfigService,
23+
) {}
1624

1725
async handle(event: SubscribedEvent) {
26+
if (this.config.databaseEngine === 'edgedb') {
27+
return;
28+
}
29+
1830
const shouldSetDepartmentId =
1931
event.updates.step === ProjectStep.PendingFinanceConfirmation &&
2032
!event.updated.departmentId;

0 commit comments

Comments
 (0)