Skip to content

Commit 620331c

Browse files
authored
Merge pull request #29 from UN-OCHA/HPC-8205-models-2
🎨 Migrate tables needed for HPC-8205
2 parents 85b058f + 4b0b1bc commit 620331c

12 files changed

+398
-11
lines changed

src/db/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
11
import Knex = require('knex');
22
import attachment from './models/attachment';
3+
import attachmentPrototype from './models/attachmentPrototype';
34
import attachmentVersion from './models/attachmentVersion';
45
import authGrant from './models/authGrant';
56
import authGrantee from './models/authGrantee';
67
import authGrantLog from './models/authGrantLog';
78
import authInvite from './models/authInvite';
89
import authTarget from './models/authTarget';
910
import authToken from './models/authToken';
11+
import entitiesAssociation from './models/entitiesAssociation';
12+
import entityPrototype from './models/entityPrototype';
1013
import expiredData from './models/expiredData';
1114
import form from './models/form';
1215
import governingEntity from './models/governingEntity';
16+
import governingEntityVersion from './models/governingEntityVersion';
1317
import operation from './models/operation';
1418
import operationCluster from './models/operationCluster';
1519
import participant from './models/participant';
20+
import project from './models/project';
21+
import plan from './models/plan';
22+
import planEntity from './models/planEntity';
23+
import planEntityVersion from './models/planEntityVersion';
1624
import projectVersion from './models/projectVersion';
25+
import projectVersionAttachment from './models/projectVersionAttachment';
1726
import projectVersionPlan from './models/projectVersionPlan';
1827
import reportingWindow from './models/reportingWindow';
1928
import reportingWindowAssignment from './models/reportingWindowAssignment';
29+
import workflowStatusOption from './models/workflowStatusOption';
2030

2131
export default (conn: Knex) => ({
2232
attachment: attachment(conn),
33+
attachmentPrototype: attachmentPrototype(conn),
2334
attachmentVersion: attachmentVersion(conn),
2435
authGrant: authGrant(conn),
2536
authGrantee: authGrantee(conn),
2637
authGrantLog: authGrantLog(conn),
2738
authInvite: authInvite(conn),
2839
authTarget: authTarget(conn),
2940
authToken: authToken(conn),
41+
entitiesAssociation: entitiesAssociation(conn),
42+
entityPrototype: entityPrototype(conn),
3043
expiredData: expiredData(conn),
3144
form: form(conn),
3245
governingEntity: governingEntity(conn),
46+
governingEntityVersion: governingEntityVersion(conn),
3347
operation: operation(conn),
3448
operationCluster: operationCluster(conn),
3549
participant: participant(conn),
50+
plan: plan(conn),
51+
planEntity: planEntity(conn),
52+
planEntityVersion: planEntityVersion(conn),
53+
project: project(conn),
3654
projectVersion: projectVersion(conn),
55+
projectVersionAttachment: projectVersionAttachment(conn),
3756
projectVersionPlan: projectVersionPlan(conn),
3857
reportingWindow: reportingWindow(conn),
3958
reportingWindowAssignment: reportingWindowAssignment(conn),
59+
workflowStatusOption: workflowStatusOption(conn),
4060
});

src/db/models/attachmentPrototype.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import type { Brand } from '../../util/types';
5+
import { defineIDModel } from '../util/id-model';
6+
import { PLAN_ID } from './plan';
57

68
export type AttachmentPrototypeId = Brand<
79
number,
@@ -13,3 +15,59 @@ export const ATTACHMENT_PROTOTYPE_ID = brandedType<
1315
number,
1416
AttachmentPrototypeId
1517
>(t.number);
18+
19+
export const ATTACHMENT_TYPE = t.keyof({
20+
caseLoad: null,
21+
contact: null,
22+
cost: null,
23+
fileWebContent: null,
24+
indicator: null,
25+
textWebContent: null,
26+
});
27+
export type AttachmentType = t.TypeOf<typeof ATTACHMENT_TYPE>;
28+
29+
const LOCALIZED_STRING = t.type({
30+
en: t.string,
31+
});
32+
33+
const FIELDS = t.array(
34+
t.type({
35+
name: LOCALIZED_STRING,
36+
type: t.string,
37+
})
38+
);
39+
40+
export const ATTACHMENT_PROTOTYPE_VALUE = t.intersection([
41+
// Required Fields
42+
t.type({
43+
hasMeasures: t.number,
44+
name: LOCALIZED_STRING,
45+
entities: t.array(t.string),
46+
}),
47+
// Optional Fields
48+
t.partial({
49+
measureFields: FIELDS,
50+
min: t.number,
51+
max: t.number,
52+
metrics: FIELDS,
53+
}),
54+
]);
55+
56+
export default defineIDModel({
57+
tableName: 'attachmentPrototype',
58+
fields: {
59+
generated: {
60+
id: { kind: 'branded-integer', brand: ATTACHMENT_PROTOTYPE_ID },
61+
},
62+
optional: {
63+
planId: { kind: 'branded-integer', brand: PLAN_ID },
64+
},
65+
accidentallyOptional: {
66+
refCode: { kind: 'checked', type: t.string },
67+
type: { kind: 'checked', type: ATTACHMENT_TYPE },
68+
value: { kind: 'checked', type: ATTACHMENT_PROTOTYPE_VALUE },
69+
},
70+
},
71+
idField: 'id',
72+
softDeletionEnabled: false,
73+
});

src/db/models/entitiesAssociation.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as t from 'io-ts';
2+
import { defineSequelizeModel } from '../util/sequelize-model';
3+
import { GOVERNING_ENTITY_ID } from './governingEntity';
4+
import { PLAN_ENTITY_ID } from './planEntity';
5+
6+
export default defineSequelizeModel({
7+
tableName: 'entitiesAssociation',
8+
fields: {
9+
required: {
10+
parentId: { kind: 'branded-integer', brand: GOVERNING_ENTITY_ID },
11+
parentType: { kind: 'checked', type: t.literal('governingEntity') },
12+
childId: { kind: 'branded-integer', brand: PLAN_ENTITY_ID },
13+
childType: { kind: 'checked', type: t.literal('planEntity') },
14+
},
15+
},
16+
softDeletionEnabled: false,
17+
});

src/db/models/entityPrototype.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import { Brand } from '../../util/types';
5+
import { defineIDModel } from '../util/id-model';
6+
7+
import { PLAN_ID } from './plan';
58

69
export type EntityPrototypeId = Brand<
710
number,
@@ -12,3 +15,43 @@ export type EntityPrototypeId = Brand<
1215
export const ENTITY_PROTOTYPE_ID = brandedType<number, EntityPrototypeId>(
1316
t.number
1417
);
18+
19+
export const ENTITY_PROTOTYPE_REF_CODE = t.keyof({
20+
CA: null,
21+
CL: null,
22+
CO: null,
23+
CQ: null,
24+
CSO: null,
25+
IO: null,
26+
OC: null,
27+
OP: null,
28+
OUT: null,
29+
SO: null,
30+
SP: null,
31+
SSO: null,
32+
});
33+
34+
export type EntityPrototypeRefCode = t.TypeOf<typeof ENTITY_PROTOTYPE_REF_CODE>;
35+
36+
export const ENTITY_PROTOTYPE_TYPE = t.keyof({
37+
GVE: null,
38+
PE: null,
39+
});
40+
41+
export default defineIDModel({
42+
tableName: 'entityPrototype',
43+
fields: {
44+
generated: {
45+
id: { kind: 'branded-integer', brand: ENTITY_PROTOTYPE_ID },
46+
},
47+
accidentallyOptional: {
48+
refCode: { kind: 'checked', type: ENTITY_PROTOTYPE_REF_CODE },
49+
type: { kind: 'checked', type: ENTITY_PROTOTYPE_TYPE },
50+
planId: { kind: 'checked', type: PLAN_ID },
51+
orderNumber: { kind: 'checked', type: t.number },
52+
value: { kind: 'checked', type: t.unknown },
53+
},
54+
},
55+
idField: 'id',
56+
softDeletionEnabled: false,
57+
});

src/db/models/governingEntityVersion.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import type { Brand } from '../../util/types';
5+
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
6+
import { GOVERNING_ENTITY_ID } from './governingEntity';
57

68
export type GoverningEntityVersionId = Brand<
79
number,
@@ -13,3 +15,33 @@ export const GOVERNING_ENTITY_VERSION_ID = brandedType<
1315
number,
1416
GoverningEntityVersionId
1517
>(t.number);
18+
19+
export default defineLegacyVersionedModel({
20+
tableName: 'governingEntityVersion',
21+
fields: {
22+
generated: {
23+
id: {
24+
kind: 'branded-integer',
25+
brand: GOVERNING_ENTITY_VERSION_ID,
26+
},
27+
},
28+
accidentallyOptional: {
29+
governingEntityId: {
30+
kind: 'branded-integer',
31+
brand: GOVERNING_ENTITY_ID,
32+
},
33+
name: { kind: 'checked', type: t.string },
34+
customReference: { kind: 'checked', type: t.string },
35+
value: { kind: 'checked', type: t.unknown },
36+
tags: { kind: 'checked', type: t.array(t.string) },
37+
},
38+
required: {
39+
overriding: {
40+
kind: 'checked',
41+
type: t.boolean,
42+
},
43+
},
44+
},
45+
idField: 'id',
46+
softDeletionEnabled: false,
47+
});

src/db/models/plan.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,32 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import { Brand } from '../../util/types';
5+
import { defineIDModel } from '../util/id-model';
56

67
export type PlanId = Brand<number, { readonly s: unique symbol }, 'plan.id'>;
78

89
export const PLAN_ID = brandedType<number, PlanId>(t.number);
10+
11+
const PLAN_REVISION_STATE = t.keyof({
12+
projectsOnly: null,
13+
none: null,
14+
planDataAndProjects: null,
15+
planDataOnly: null,
16+
});
17+
18+
export default defineIDModel({
19+
tableName: 'plan',
20+
fields: {
21+
generated: {
22+
id: { kind: 'branded-integer', brand: PLAN_ID },
23+
},
24+
optional: {
25+
revisionState: { kind: 'checked', type: PLAN_REVISION_STATE },
26+
},
27+
accidentallyOptional: {
28+
restricted: { kind: 'checked', type: t.boolean },
29+
},
30+
},
31+
idField: 'id',
32+
softDeletionEnabled: false,
33+
});

src/db/models/planEntity.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import type { Brand } from '../../util/types';
5+
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
6+
import { ENTITY_PROTOTYPE_ID } from './entityPrototype';
7+
import { PLAN_ID } from './plan';
58

69
export type PlanEntityId = Brand<
710
number,
@@ -10,3 +13,23 @@ export type PlanEntityId = Brand<
1013
>;
1114

1215
export const PLAN_ENTITY_ID = brandedType<number, PlanEntityId>(t.number);
16+
17+
export default defineLegacyVersionedModel({
18+
tableName: 'planEntity',
19+
fields: {
20+
generated: {
21+
id: { kind: 'branded-integer', brand: PLAN_ENTITY_ID },
22+
},
23+
accidentallyOptional: {
24+
planId: { kind: 'branded-integer', brand: PLAN_ID },
25+
},
26+
required: {
27+
entityPrototypeId: {
28+
kind: 'branded-integer',
29+
brand: ENTITY_PROTOTYPE_ID,
30+
},
31+
},
32+
},
33+
idField: 'id',
34+
softDeletionEnabled: true,
35+
});

src/db/models/planEntityVersion.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as t from 'io-ts';
22

33
import { brandedType } from '../../util/io-ts';
44
import type { Brand } from '../../util/types';
5+
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
6+
import { ENTITY_PROTOTYPE_ID } from './entityPrototype';
7+
import { PLAN_ENTITY_ID } from './planEntity';
58

69
export type PlanEntityVersionId = Brand<
710
number,
@@ -12,3 +15,41 @@ export type PlanEntityVersionId = Brand<
1215
export const PLAN_ENTITY_VERSION_ID = brandedType<number, PlanEntityVersionId>(
1316
t.number
1417
);
18+
19+
export const PLAN_ENTITY_VERSION_VALUE = t.type({
20+
categories: t.array(t.unknown),
21+
description: t.string,
22+
support: t.array(
23+
t.intersection([
24+
t.type({
25+
planEntityIds: t.array(PLAN_ENTITY_ID),
26+
}),
27+
t.partial({
28+
entityPrototypeId: ENTITY_PROTOTYPE_ID,
29+
}),
30+
])
31+
),
32+
type: t.type({
33+
en: t.type({
34+
singular: t.string,
35+
plural: t.string,
36+
}),
37+
}),
38+
});
39+
export type PlanEntityVersionValue = t.TypeOf<typeof PLAN_ENTITY_VERSION_VALUE>;
40+
41+
export default defineLegacyVersionedModel({
42+
tableName: 'planEntityVersion',
43+
fields: {
44+
generated: {
45+
id: { kind: 'branded-integer', brand: PLAN_ENTITY_VERSION_ID },
46+
},
47+
accidentallyOptional: {
48+
planEntityId: { kind: 'branded-integer', brand: PLAN_ENTITY_ID },
49+
customReference: { kind: 'checked', type: t.string },
50+
value: { kind: 'checked', type: PLAN_ENTITY_VERSION_VALUE },
51+
},
52+
},
53+
idField: 'id',
54+
softDeletionEnabled: false,
55+
});

0 commit comments

Comments
 (0)