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 all 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unocha/hpc-api-core",
"version": "0.1.0",
"version": "0.2.0",
"description": "Core libraries supporting HPC.Tools API Backend",
"license": "Apache-2.0",
"private": false,
Expand Down 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
10 changes: 9 additions & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ 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 planVersion from './models/planVersion';
import planYear from './models/planYear';
import project from './models/project';
import projectVersion from './models/projectVersion';
import projectVersionAttachment from './models/projectVersionAttachment';
import projectVersionPlan from './models/projectVersionPlan';
import reportingWindow from './models/reportingWindow';
import reportingWindowAssignment from './models/reportingWindowAssignment';
import usageYear from './models/usageYear';
import workflowStatusOption from './models/workflowStatusOption';

export default (conn: Knex) => ({
Expand All @@ -44,17 +48,21 @@ 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),
plan: plan(conn),
planEntity: planEntity(conn),
planEntityVersion: planEntityVersion(conn),
planVersion: planVersion(conn),
planYear: planYear(conn),
project: project(conn),
projectVersion: projectVersion(conn),
projectVersionAttachment: projectVersionAttachment(conn),
projectVersionPlan: projectVersionPlan(conn),
reportingWindow: reportingWindow(conn),
reportingWindowAssignment: reportingWindowAssignment(conn),
usageYear: usageYear(conn),
workflowStatusOption: workflowStatusOption(conn),
});
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 = t.keyof({
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: 'checked',
type: LOCATION_STATUS,
},
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
44 changes: 44 additions & 0 deletions src/db/models/planVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { DATE } from '../util/datatypes';
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
import { PLAN_ID } from './plan';
import { PLAN_REPORTING_PERIOD_ID } from './planReportingPeriod';

export type PlanVersionId = Brand<
number,
Expand All @@ -10,3 +14,43 @@ export type PlanVersionId = Brand<
>;

export const PLAN_VERSION_ID = brandedType<number, PlanVersionId>(t.number);

const PLAN_VERSION_CLUSTER_SELECTION_TYPE = t.keyof({
single: null,
multi: null,
});

export default defineLegacyVersionedModel({
tableName: 'planVersion',
fields: {
generated: {
id: { kind: 'branded-integer', brand: PLAN_VERSION_ID },
},
nonNullWithDefault: {
isForHPCProjects: { kind: 'checked', type: t.boolean },
},
accidentallyOptional: {
planId: { kind: 'branded-integer', brand: PLAN_ID },
name: { kind: 'checked', type: t.string },
startDate: { kind: 'checked', type: DATE },
endDate: { kind: 'checked', type: DATE },
},
optional: {
comments: { kind: 'checked', type: t.string },
code: { kind: 'checked', type: t.string },
customLocationCode: { kind: 'checked', type: t.string },
currentReportingPeriodId: {
kind: 'branded-integer',
brand: PLAN_REPORTING_PERIOD_ID,
},
lastPublishedReportingPeriodId: { kind: 'checked', type: t.number },
// Even though this column isn't defined using DB enum only two values are used
clusterSelectionType: {
kind: 'checked',
type: PLAN_VERSION_CLUSTER_SELECTION_TYPE,
},
},
},
idField: 'id',
softDeletionEnabled: false,
});
18 changes: 18 additions & 0 deletions src/db/models/planYear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { defineLegacyVersionedModel } from '../util/legacy-versioned-model';
import { PLAN_ID } from './plan';
import { USAGE_YEAR_ID } from './usageYear';

export type PlanYearId = Brand<
number,
Expand All @@ -10,3 +13,18 @@ export type PlanYearId = Brand<
>;

export const PLAN_YEAR_ID = brandedType<number, PlanYearId>(t.number);

export default defineLegacyVersionedModel({
tableName: 'planYear',
fields: {
generated: {
id: { kind: 'branded-integer', brand: PLAN_YEAR_ID },
},
required: {
planId: { kind: 'branded-integer', brand: PLAN_ID },
usageYearId: { kind: 'branded-integer', brand: USAGE_YEAR_ID },
},
},
idField: 'id',
softDeletionEnabled: true,
});
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
15 changes: 15 additions & 0 deletions src/db/models/usageYear.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 UsageYearId = Brand<
number,
Expand All @@ -10,3 +11,17 @@ export type UsageYearId = Brand<
>;

export const USAGE_YEAR_ID = brandedType<number, UsageYearId>(t.number);

export default defineIDModel({
tableName: 'usageYear',
fields: {
generated: {
id: { kind: 'branded-integer', brand: USAGE_YEAR_ID },
},
required: {
year: { kind: 'checked', type: t.string },
},
},
idField: 'id',
softDeletionEnabled: false,
});
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
Loading