Skip to content

Commit 8fb2f87

Browse files
authored
Merge pull request #3023 from SeedCompany/edgedb/department-id
2 parents 2dad437 + 8767124 commit 8fb2f87

File tree

6 files changed

+104
-7
lines changed

6 files changed

+104
-7
lines changed

dbschema/funding-account.esdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module default {
33
overloaded name {
44
constraint exclusive;
55
}
6-
6+
77
required accountNumber: int16 {
88
constraint min_value(0);
99
constraint max_value(9);

dbschema/migrations/00034.edgeql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE MIGRATION m1abuwgh5eb3m3ivtyo6lru3w6k7ermhoof3bphd72jtx2cezj7ewa
2+
ONTO m1ijgeabkfengfe2lpxjtq7xxqjiulhehay7tuutdoa5g3scsenwvq
3+
{
4+
ALTER TYPE default::Project {
5+
CREATE LINK fieldRegion: default::FieldRegion;
6+
CREATE LINK marketingLocation: default::Location;
7+
CREATE LINK primaryLocation: default::Location;
8+
CREATE TRIGGER enforceFundingAccount
9+
AFTER UPDATE
10+
FOR EACH DO (std::assert((std::any((__new__.primaryLocation.fundingAccount.accountNumber > 0)) OR NOT (EXISTS (__new__.primaryLocation))), message := 'Project must have a primary location with a specified funding account'));
11+
ALTER PROPERTY departmentId {
12+
SET TYPE std::int32 USING (<std::int32>.departmentId);
13+
};
14+
};
15+
};

dbschema/migrations/00035.edgeql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE MIGRATION m13bndkbclxwviy3uj4eskx56bd2chw2xbvfn5pae557oslmbz2ssa
2+
ONTO m1abuwgh5eb3m3ivtyo6lru3w6k7ermhoof3bphd72jtx2cezj7ewa
3+
{
4+
ALTER TYPE default::Project {
5+
ALTER PROPERTY departmentId {
6+
CREATE CONSTRAINT std::exclusive;
7+
CREATE CONSTRAINT std::max_value(99999);
8+
CREATE CONSTRAINT std::min_value(10000);
9+
};
10+
};
11+
};

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: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,32 @@ module default {
1010
default := Sensitivity.High;
1111
};
1212

13-
departmentId: str;
13+
departmentId: int32 {
14+
constraint exclusive;
15+
constraint min_value(10000);
16+
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+
);
38+
};
1439

1540
required step: Project::Step {
1641
default := Project::Step.EarlyConversations;
@@ -44,9 +69,17 @@ module default {
4469
# multi link engagements := .<project[is Engagement];
4570
property engagementTotal := count(.<project[is Engagement]);
4671

47-
# link primaryLocation: Location;
48-
# link marketingLocation: Location;
49-
# link fieldRegion: FieldRegion;
72+
primaryLocation: Location;
73+
trigger enforceFundingAccount after update for each do (
74+
assert(
75+
any(__new__.primaryLocation.fundingAccount.accountNumber > 0)
76+
or not exists __new__.primaryLocation, # allow clearing
77+
message := "Project must have a primary location with a specified funding account"
78+
)
79+
);
80+
marketingLocation: Location;
81+
fieldRegion: FieldRegion;
82+
5083
link rootDirectory: Directory;
5184

5285
overloaded link projectContext: Project::Context {

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)