Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define models needed for HPC-7904 #30

Merged
merged 16 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
/index.d.ts
/yarn-error.log
package-lock.json

# IDE artifacts
.vscode
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"husky": "^7.0.2",
"lint-staged": "^11.2.4",
"prettier": "2.4.1",
"typescript": "^4.3.5"
"typescript": "^4.4.4"
},
"lint-staged": {
"*.{ts,js}": [
Expand Down
4 changes: 3 additions & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import expiredData from './models/expiredData';
import form from './models/form';
import governingEntity from './models/governingEntity';
import governingEntityVersion from './models/governingEntityVersion';
import location from './models/location';
import operation from './models/operation';
import operationCluster from './models/operationCluster';
import participant from './models/participant';
import project from './models/project';
import plan from './models/plan';
import planEntity from './models/planEntity';
import planEntityVersion from './models/planEntityVersion';
import project from './models/project';
import projectVersion from './models/projectVersion';
import projectVersionAttachment from './models/projectVersionAttachment';
import projectVersionPlan from './models/projectVersionPlan';
Expand All @@ -44,6 +45,7 @@ export default (conn: Knex) => ({
form: form(conn),
governingEntity: governingEntity(conn),
governingEntityVersion: governingEntityVersion(conn),
location: location(conn),
operation: operation(conn),
operationCluster: operationCluster(conn),
participant: participant(conn),
Expand Down
9 changes: 2 additions & 7 deletions src/db/models/attachmentPrototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { LOCALIZED_STRING } from '../util/datatypes';
import { defineIDModel } from '../util/id-model';
import { PLAN_ID } from './plan';

Expand All @@ -26,10 +27,6 @@ export const ATTACHMENT_TYPE = t.keyof({
});
export type AttachmentType = t.TypeOf<typeof ATTACHMENT_TYPE>;

const LOCALIZED_STRING = t.type({
en: t.string,
});

const FIELDS = t.array(
t.type({
name: LOCALIZED_STRING,
Expand Down Expand Up @@ -59,10 +56,8 @@ export default defineIDModel({
generated: {
id: { kind: 'branded-integer', brand: ATTACHMENT_PROTOTYPE_ID },
},
optional: {
planId: { kind: 'branded-integer', brand: PLAN_ID },
},
accidentallyOptional: {
planId: { kind: 'branded-integer', brand: PLAN_ID },
refCode: { kind: 'checked', type: t.string },
type: { kind: 'checked', type: ATTACHMENT_TYPE },
value: { kind: 'checked', type: ATTACHMENT_PROTOTYPE_VALUE },
Expand Down
2 changes: 1 addition & 1 deletion src/db/models/entityPrototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export default defineIDModel({
generated: {
id: { kind: 'branded-integer', brand: ENTITY_PROTOTYPE_ID },
},
optional: { orderNumber: { kind: 'checked', type: t.number } },
accidentallyOptional: {
refCode: { kind: 'checked', type: ENTITY_PROTOTYPE_REF_CODE },
type: { kind: 'checked', type: ENTITY_PROTOTYPE_TYPE },
planId: { kind: 'checked', type: PLAN_ID },
orderNumber: { kind: 'checked', type: t.number },
value: { kind: 'checked', type: t.unknown },
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/db/models/governingEntityVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineLegacyVersionedModel({
brand: GOVERNING_ENTITY_VERSION_ID,
},
},
optional: { tags: { kind: 'checked', type: t.array(t.string) } },
accidentallyOptional: {
governingEntityId: {
kind: 'branded-integer',
Expand All @@ -33,9 +34,8 @@ export default defineLegacyVersionedModel({
name: { kind: 'checked', type: t.string },
customReference: { kind: 'checked', type: t.string },
value: { kind: 'checked', type: t.unknown },
tags: { kind: 'checked', type: t.array(t.string) },
},
required: {
nonNullWithDefault: {
overriding: {
kind: 'checked',
type: t.boolean,
Expand Down
37 changes: 37 additions & 0 deletions src/db/models/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { defineIDModel } from '../util/id-model';

export type LocationId = Brand<
number,
Expand All @@ -10,3 +11,39 @@ export type LocationId = Brand<
>;

export const LOCATION_ID = brandedType<number, LocationId>(t.number);

const LOCATION_STATUS = {
active: null,
expired: null,
};

export default defineIDModel({
tableName: 'location',
fields: {
generated: {
id: { kind: 'branded-integer', brand: LOCATION_ID },
},
optional: {
externalId: { kind: 'checked', type: t.string },
name: { kind: 'checked', type: t.string },
latitude: { kind: 'checked', type: t.number },
longitude: { kind: 'checked', type: t.number },
iso3: { kind: 'checked', type: t.string },
pcode: { kind: 'checked', type: t.string },
// Even though this column is defined as int8, it is
// fetched as a string by knex, since it is bigint
validOn: { kind: 'checked', type: t.string },
parentId: { kind: 'branded-integer', brand: LOCATION_ID },
},
accidentallyOptional: {
adminLevel: { kind: 'checked', type: t.number },
status: {
kind: 'enum',
values: LOCATION_STATUS,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum should only be used when the property uses an enum defined in sql, this particular field is a string, so instead we should use a checked string with t.keyof to validate upon read, and restrict the type when writing.

The reason for this is that at some point I'd like to extend the validation function / unit tests to check that enum types are defined correctly, and allow the same values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense even now, without enum definition checks

itosSync: { kind: 'checked', type: t.boolean },
},
},
idField: 'id',
softDeletionEnabled: false,
});
8 changes: 2 additions & 6 deletions src/db/models/planEntityVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { LOCALIZED_PLURAL_STRING } from '../util/datatypes';
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
import { ENTITY_PROTOTYPE_ID } from './entityPrototype';
import { PLAN_ENTITY_ID } from './planEntity';
Expand Down Expand Up @@ -29,12 +30,7 @@ export const PLAN_ENTITY_VERSION_VALUE = t.type({
}),
])
),
type: t.type({
en: t.type({
singular: t.string,
plural: t.string,
}),
}),
type: LOCALIZED_PLURAL_STRING,
});
export type PlanEntityVersionValue = t.TypeOf<typeof PLAN_ENTITY_VERSION_VALUE>;

Expand Down
13 changes: 12 additions & 1 deletion src/db/models/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export const PROJECT_VERSION_ID = brandedType<number, ProjectVersionId>(
t.number
);

const PROJECT_IMPLEMENTATION_STATUS = {
Planning: null,
Implementing: null,
'Ended - Completed': null,
'Ended - Terminated': null,
'Ended - Not started and abandoned': null,
};

const PROJECT_PDF_ENTRY = t.type({
/**
* TODO: use something more stable, like UNIX OFFSET as a number
Expand Down Expand Up @@ -60,7 +68,10 @@ export default defineIDModel({
},
optional: {
code: { kind: 'checked', type: t.string },
implementationStatus: { kind: 'checked', type: t.string },
implementationStatus: {
kind: 'enum',
values: PROJECT_IMPLEMENTATION_STATUS,
},
currentPublishedVersionId: {
kind: 'branded-integer',
brand: PROJECT_VERSION_ID,
Expand Down
2 changes: 2 additions & 0 deletions src/db/models/projectVersionAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default defineSequelizeModel({
kind: 'branded-integer',
brand: ATTACHMENT_VERSION_ID,
},
},
optional: {
value: { kind: 'checked', type: t.unknown },
total: { kind: 'checked', type: t.number },
},
Expand Down
5 changes: 2 additions & 3 deletions src/db/models/workflowStatusOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { LOCALIZED_STRING } from '../util/datatypes';
import { defineIDModel } from '../util/id-model';
import { PLAN_ID } from './plan';

Expand Down Expand Up @@ -33,9 +34,7 @@ export const WORKFLOW_STATUS_OPTION_TYPE = t.keyof({
});

export const WORKFLOW_STATUS_OPTION_VALUE = t.type({
label: t.type({
en: t.string,
}),
label: LOCALIZED_STRING,
});

export default defineIDModel({
Expand Down
14 changes: 14 additions & 0 deletions src/db/util/datatypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ export const FILE_REFERENCE = t.type({
});

export type FileReference = t.TypeOf<typeof FILE_REFERENCE>;

const localized = <T extends t.Type<any>>(type: T) =>
t.type({
en: type,
});

export const LOCALIZED_STRING = localized(t.string);

export const LOCALIZED_PLURAL_STRING = localized(
t.type({
singular: t.string,
plural: t.string,
})
);
4 changes: 3 additions & 1 deletion src/util/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const createGroupableAsyncFunction = <
}
} catch (err) {
for (const call of cs) {
call.reject(err);
if (err instanceof Error) {
call.reject(err);
}
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2256,10 +2256,10 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

typescript@^4.3.5:
version "4.3.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4"
integrity sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==
typescript@^4.4.4:
version "4.4.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c"
integrity sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==

unc-path-regex@^0.1.2:
version "0.1.2"
Expand Down