diff --git a/api/api_sources/schema-files/blowBy.schema.yaml b/api/api_sources/schema-files/blowBy.schema.yaml new file mode 100644 index 00000000..11c033e4 --- /dev/null +++ b/api/api_sources/schema-files/blowBy.schema.yaml @@ -0,0 +1,54 @@ +version: '1.0' +description: Schema file for table blow_by +externalTables: [] +includes: + - observerWorkflow.schema.yaml +schemas: + BlowBySchema: + name: blow_by + description: 'Table to store blow by data for watercraft observer.' + baseSchema: RecordSchema + meta: + resource: true + api: /mussels/blow-bys + base: api + resource: true + baseModel: Record + columns: + id: + name: blow_by_id + comment: Auto generated primary key + definition: SERIAL PRIMARY KEY + observerWorkflowId: + name: observer_workflow_id + comment: Foreign key to observer_workflow + definition: INT NULL + foreignTable: observer_workflow + refColumn: observer_workflow_id + blowByTime: + name: blow_by_time + comment: Time of blow by + definition: TIMESTAMP NULL + watercraftComplexity: + name: watercraft_complexity + comment: Watercraft complexity + definition: VARCHAR(100) NULL + reportedToRapp: + name: reported_to_rapp + comment: Reported to rapp + definition: BOOLEAN NOT NULL DEFAULT false + relations: + observerWorkflow: + header: + key: blow_by.observer_workflow + default: Observer Workflow + description: + key: blow_by.observer_workflow.description + default: Observer workflow associated with the blow by + type: single + relationshipType: many-to-one + schema: ObserverWorkflowSchema + meta: + skipValidation: true + versions: [] + fields: {} diff --git a/api/api_sources/schema-migration-sql/BlowBySchema/BlowBySchema.sql b/api/api_sources/schema-migration-sql/BlowBySchema/BlowBySchema.sql new file mode 100644 index 00000000..bb5f95be --- /dev/null +++ b/api/api_sources/schema-migration-sql/BlowBySchema/BlowBySchema.sql @@ -0,0 +1,41 @@ +-- ### Creating Table: blow_by ### -- + + +CREATE TABLE blow_by (); +ALTER TABLE blow_by ADD COLUMN blow_by_id SERIAL PRIMARY KEY; +ALTER TABLE blow_by ADD COLUMN observer_workflow_id INT NULL REFERENCES observer_workflow(observer_workflow_id) ON DELETE SET NULL; +ALTER TABLE blow_by ADD COLUMN blow_by_time TIMESTAMP NULL; +ALTER TABLE blow_by ADD COLUMN watercraft_complexity VARCHAR(100) NULL; +ALTER TABLE blow_by ADD COLUMN reported_to_rapp BOOLEAN NOT NULL DEFAULT false; + + + +-- ### Creating Comments on table ### -- + + +COMMENT ON TABLE blow_by IS 'Table to store blow by data for watercraft observer.'; +COMMENT ON COLUMN blow_by.blow_by_id IS 'Auto generated primary key'; +COMMENT ON COLUMN blow_by.observer_workflow_id IS 'Foreign key to observer_workflow'; +COMMENT ON COLUMN blow_by.blow_by_time IS 'Time of blow by'; +COMMENT ON COLUMN blow_by.watercraft_complexity IS 'Watercraft complexity'; +COMMENT ON COLUMN blow_by.reported_to_rapp IS 'Reported to rapp'; + + + +-- ### Creating Timestamp column ### -- + + +ALTER TABLE blow_by ADD COLUMN created_at TIMESTAMP DEFAULT NOW(); +ALTER TABLE blow_by ADD COLUMN updated_at TIMESTAMP DEFAULT NOW(); +COMMENT ON COLUMN blow_by.created_at IS 'Timestamp column to check creation time of record'; +COMMENT ON COLUMN blow_by.updated_at IS 'Timestamp column to check modify time of record'; + + +-- ### Creating User Audit Columns ### -- + + +ALTER TABLE blow_by ADD COLUMN updated_by_user_id INT NULL DEFAULT NULL REFERENCES application_user(user_id) ON DELETE SET NULL; +ALTER TABLE blow_by ADD COLUMN created_by_user_id INT NULL DEFAULT NULL REFERENCES application_user(user_id) ON DELETE SET NULL; +COMMENT ON COLUMN blow_by.updated_by_user_id IS 'Audit column to track creator'; +COMMENT ON COLUMN blow_by.created_by_user_id IS 'Audit column to track modifier'; + -- ### End: blow_by ### -- diff --git a/api/api_sources/sources/database/database-schema/musselApp.schema.ts b/api/api_sources/sources/database/database-schema/musselApp.schema.ts index 1c68cb36..000440c3 100644 --- a/api/api_sources/sources/database/database-schema/musselApp.schema.ts +++ b/api/api_sources/sources/database/database-schema/musselApp.schema.ts @@ -112,3 +112,9 @@ export class AdultMusselsLocationSchema extends RecordTableSchema { return true; } } + +export class BlowBySchema extends RecordTableSchema { + get schemaFilePath(): string { + return getYAMLFilePath('blowBy.schema.yaml'); + } +} diff --git a/api/api_sources/sources/database/migrations/1703285389562-CreateBlowBy.ts b/api/api_sources/sources/database/migrations/1703285389562-CreateBlowBy.ts new file mode 100644 index 00000000..fd60fdc3 --- /dev/null +++ b/api/api_sources/sources/database/migrations/1703285389562-CreateBlowBy.ts @@ -0,0 +1,42 @@ +import {MigrationInterface, QueryRunner} from 'typeorm'; +import { AppDBMigrator } from '../applicationSchemaInterface'; +import { BlowBySchema, ObserverWorkflowSchema } from '../database-schema'; + +export class CreateBlowBy1703888022971 extends AppDBMigrator implements MigrationInterface { + blowBySchema: BlowBySchema; + observerWorkflowSchema: ObserverWorkflowSchema; + + /** + * Setup + */ + setup() { + // Adding BlowBy schema to migrator + this.blowBySchema = new BlowBySchema(); + this.observerWorkflowSchema = new ObserverWorkflowSchema(); + + // Create BlowBy table + this.addSchemaInitVersion(this.blowBySchema); + } + + /** + * UP: Create DB method + */ + public async up(queryRunner: QueryRunner): Promise { + // Start Log + this.log('[START]', 'UP'); + // Running all up migration files + await this.runQuerySqlFiles(this.upMigrations(), queryRunner); + this.log('[END]', 'UP'); + } + + /** + * Down: Revert + */ + public async down(queryRunner: QueryRunner): Promise { + this.log('[START]', 'DOWN'); + await queryRunner.query(this.blowBySchema.dropTable()); + await queryRunner.query(this.observerWorkflowSchema.dropTable()); + this.log('[END]', 'DOWN'); + } + +} diff --git a/api/api_sources/sources/database/models/blowBy.ts b/api/api_sources/sources/database/models/blowBy.ts new file mode 100644 index 00000000..f36a803c --- /dev/null +++ b/api/api_sources/sources/database/models/blowBy.ts @@ -0,0 +1,87 @@ +// ** Model: BlowBy from schema BlowBySchema ** + +import { Column, Entity, PrimaryGeneratedColumn, JoinColumn, ManyToOne } from 'typeorm'; +import { BlowBySchema, ObserverWorkflowSchema } from '../database-schema'; +import { ModelProperty, PropertyType, ModelDescription } from '../../libs/core-model'; +import { ObserverWorkflow, Record } from '../models'; +import { DateTimeTransformer } from '../../libs/transformer'; + +/** Interface **/ +/** + * @description BlowBy create interface + */ +export interface BlowBySpec { + observerWorkflowId: ObserverWorkflow; + blowByTime: string; + watercraftComplexity: string; + reportedToRapp: boolean; +} +// -- End: BlowBySpec -- + + +/** Interface **/ +/** + * @description BlowBy update interface + */ +export interface BlowByUpdateSpec { + observerWorkflowId?: ObserverWorkflow; + blowByTime?: string; + watercraftComplexity?: string; + reportedToRapp?: boolean; +} +// -- End: BlowByUpdateSpec -- + +/** + * @description Data Model Class for BlowBySchema + */ +@ModelDescription({ + description: 'Data Model Class for BlowBySchema', + schema: BlowBySchema, + apiResource: false +}) +@Entity( { name: BlowBySchema.dbTable} ) +export class BlowBy extends Record implements BlowBySpec { + + /** + * Class Properties + */ + + /** + * @description Getter/Setter property for column {blow_by_id} + */ + @PrimaryGeneratedColumn() + @ModelProperty({type: PropertyType.number}) + blow_by_id: number; + + /** + * @description Getter/Setter property for column {observer_workflow_id} + */ + @ManyToOne( type => ObserverWorkflow, { eager: true}) + @JoinColumn({ name: BlowBySchema.columns.observerWorkflowId, referencedColumnName: ObserverWorkflowSchema.pk}) + @ModelProperty({type: PropertyType.object}) + observerWorkflowId: ObserverWorkflow; + + /** + * @description Getter/Setter property for column {blow_by_time} + */ + @Column({ name: BlowBySchema.columns.blowByTime, transformer: new DateTimeTransformer()}) + @ModelProperty({type: PropertyType.string}) + blowByTime: string; + + /** + * @description Getter/Setter property for column {watercraft_complexity} + */ + @Column({ name: BlowBySchema.columns.watercraftComplexity}) + @ModelProperty({type: PropertyType.string}) + watercraftComplexity: string; + + /** + * @description Getter/Setter property for column {reported_to_rapp} + */ + @Column({ name: BlowBySchema.columns.reportedToRapp}) + @ModelProperty({type: PropertyType.boolean}) + reportedToRapp: boolean; + +} + +// ------------------------------------- diff --git a/api/api_sources/sources/database/models/controllers/blowBy.controller.ts b/api/api_sources/sources/database/models/controllers/blowBy.controller.ts new file mode 100644 index 00000000..da3f75bf --- /dev/null +++ b/api/api_sources/sources/database/models/controllers/blowBy.controller.ts @@ -0,0 +1,49 @@ +// ** BlowByController ** // + +import { RecordController } from '../generic.data.models'; +import { BlowBy} from '../../models'; +import { BlowBySchema } from '../../database-schema'; + +// ** BlowByController ** // + + +/** + * @description Data Model Controller Class for BlowBySchema and BlowBy + */ +export class BlowByController extends RecordController { + /** + * @description Getter for shared instance + */ + public static get shared(): BlowByController { + return this.sharedInstance(BlowBy, BlowBySchema) as BlowByController; + } + + public async all(query?: any): Promise { + const options = query || {}; + options.relations = ['observerWorkflowId']; + return await this.repo.find(options) as BlowBy[]; + } + + get exportKeyPriorities(): {[key: string]: number} { + const basePriority = 1000; + const topPriority = 100; + return { + id: basePriority + topPriority, + observerWorkflowId: (basePriority + topPriority - 10), + blowByTime: (basePriority + topPriority - 50), + watercraftComplexity: (basePriority + topPriority - 60), + reportedToRapp: (basePriority + topPriority - 70), + }; + } + + processForExport(data: BlowBy): any { + const result: any = {}; + Object.keys(data).forEach((key) => { + if (this.exportKeyMapper[key]) { + result[this.exportKeyMapper[key]] = data[key]; + } + }); + return result; + } +} +// ---------------- diff --git a/api/api_sources/sources/database/models/controllers/watercraftRiskAssessment.controller.ts b/api/api_sources/sources/database/models/controllers/watercraftRiskAssessment.controller.ts index 022c0ea9..44e46cb8 100644 --- a/api/api_sources/sources/database/models/controllers/watercraftRiskAssessment.controller.ts +++ b/api/api_sources/sources/database/models/controllers/watercraftRiskAssessment.controller.ts @@ -129,8 +129,8 @@ export class WatercraftRiskAssessmentController extends RecordController { // Observer Workflow app.use('/api/mussels/workflow', ObserverWorkflowRouteController.shared.router); + // Watercraft Risk Assessment + app.use('/api/mussels/blow-bys', BlowByRouteController.shared.router); + // Mussels App Codes app.use('/api/mussels/codes', MusselsAppCodesRouteController.shared.router); diff --git a/api/api_sources/sources/server/modules/blowBy/blowBy.route.ts b/api/api_sources/sources/server/modules/blowBy/blowBy.route.ts new file mode 100644 index 00000000..a461bcc1 --- /dev/null +++ b/api/api_sources/sources/server/modules/blowBy/blowBy.route.ts @@ -0,0 +1,74 @@ +/* + * Copyright © 2019 Province of British Columbia + * Licensed under the Apache License, Version 2.0 (the "License") + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * ** + * http://www.apache.org/licenses/LICENSE-2.0 + * ** + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * File: watercraftRiskAssessment.route.ts + * Project: lucy + * File Created: Tuesday, 5th November 2019 10:16:47 am + * Author: pushan + * ----- + * Last Modified: Tuesday, 5th November 2019 10:58:51 am + * Modified By: pushan + * ----- + */ +/** + * Imports + */ +import { + // SecureRouteController, + ResourceRoute, + CreateMiddleware, + ResourceRouteController, + UpdateMiddleware, + Get, + inspectAppEditorRoute, + inspectAppAdminRoute +} from '../../core'; +import { + BlowByController, + BlowBySpec +} from '../../../database/models'; + + +@ResourceRoute({ + path: '/api/mussels/blow-bys/#', + description: 'API route controller for Blow Bys', + dataController: BlowByController.shared, + // validators: CreateTreatmentValidator, + secure: true +}) +@CreateMiddleware(() => [ inspectAppEditorRoute() ]) +@UpdateMiddleware(() => [ inspectAppEditorRoute() ]) +export class BlowByRouteController extends ResourceRouteController { + static get shared(): BlowByRouteController { + return this.sharedInstance() as BlowByRouteController; + } + + /** + * @description Create New Object + * @param Request req + * @param any data + */ + public async createResource(req: any, data: any): Promise<[number, any]> { + // Get Proper data mapping + return [201, await this.dataController.createNewObject(data, req.user)]; + } + + @Get({ + path: '/export', + secure: true, + middleware: () => [ inspectAppAdminRoute() ] + }) + public async export() { + return [200, await this.dataController.export()]; + } +} diff --git a/api/api_sources/sources/server/modules/blowBy/index.ts b/api/api_sources/sources/server/modules/blowBy/index.ts new file mode 100644 index 00000000..35c9aa4f --- /dev/null +++ b/api/api_sources/sources/server/modules/blowBy/index.ts @@ -0,0 +1,24 @@ +/* + * Copyright © 2019 Province of British Columbia + * Licensed under the Apache License, Version 2.0 (the "License") + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * ** + * http://www.apache.org/licenses/LICENSE-2.0 + * ** + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * File: index.ts + * Project: lucy + * File Created: Thursday, 23rd April 2020 1:36:03 pm + * Author: Sustainment Team (you@you.you) + * ----- + * Last Modified: Thursday, 23rd April 2020 1:36:31 pm + * Modified By: Sustainment Team (you@you.you>) + * ----- + */ + +export * from './blowBy.route';