From 644ce13e019f6655b0727410db384f1af7268501 Mon Sep 17 00:00:00 2001 From: Mike <100624415+marqueone-ps@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:34:59 -0700 Subject: [PATCH] feat: CE-1002 Allow CEEB users to capture authorization number (#84) Co-authored-by: Mike Sears Co-authored-by: afwilcox --- backend/prisma/schema.prisma | 66 ++++- backend/src/case_file/case_file.resolver.ts | 21 ++ backend/src/case_file/case_file.service.ts | 256 ++++++++++++++++++ .../src/case_file/case_file_functions.graphql | 4 + .../src/case_file/case_file_inputs.graphql | 28 ++ backend/src/case_file/case_file_types.graphql | 7 + .../authorization-outcome-search-results.ts | 4 + .../create-authorization-outcome-input.ts | 9 + .../delete-authorization-outcome-input.ts | 5 + .../permit-site-input.ts | 5 + .../update-authorization-outcome-input.ts | 9 + .../dto/ceeb/decision/update-decsion-input.ts | 1 - .../entities/authorization-outcome.entity.ts | 5 + .../case_file/entities/case_file.entity.ts | 2 + migrations/sql/V1.27.0__CE-1002.sql | 157 +++++++++++ 15 files changed, 567 insertions(+), 12 deletions(-) create mode 100644 backend/src/case_file/dto/ceeb/authorization-outcome/authorization-outcome-search-results.ts create mode 100644 backend/src/case_file/dto/ceeb/authorization-outcome/create-authorization-outcome-input.ts create mode 100644 backend/src/case_file/dto/ceeb/authorization-outcome/delete-authorization-outcome-input.ts create mode 100644 backend/src/case_file/dto/ceeb/authorization-outcome/permit-site-input.ts create mode 100644 backend/src/case_file/dto/ceeb/authorization-outcome/update-authorization-outcome-input.ts create mode 100644 backend/src/case_file/entities/authorization-outcome.entity.ts create mode 100644 migrations/sql/V1.27.0__CE-1002.sql diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 8f34ca40..bea625ba 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -270,23 +270,25 @@ model case_code { /// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments model case_file { - case_file_guid String @id(map: "PK_case_file_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid - case_code String @db.VarChar(10) - owned_by_agency_code String @db.VarChar(10) - inaction_reason_code String? @db.VarChar(10) + case_file_guid String @id(map: "PK_case_file_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + case_code String @db.VarChar(10) + owned_by_agency_code String @db.VarChar(10) + inaction_reason_code String? @db.VarChar(10) action_not_required_ind Boolean? note_text String? review_required_ind Boolean? - create_user_id String @db.VarChar(32) - create_utc_timestamp DateTime @db.Timestamp(6) - update_user_id String? @db.VarChar(32) - update_utc_timestamp DateTime? @db.Timestamp(6) + create_user_id String @db.VarChar(32) + create_utc_timestamp DateTime @db.Timestamp(6) + update_user_id String? @db.VarChar(32) + update_utc_timestamp DateTime? @db.Timestamp(6) action action[] - case_code_case_file_case_codeTocase_code case_code @relation("case_file_case_codeTocase_code", fields: [case_code], references: [case_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__case_code") - inaction_reason_code_case_file_inaction_reason_codeToinaction_reason_code inaction_reason_code? @relation("case_file_inaction_reason_codeToinaction_reason_code", fields: [inaction_reason_code], references: [inaction_reason_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__inaction_reason_code") - agency_code agency_code @relation(fields: [owned_by_agency_code], references: [agency_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__owned_by_agency_code") + authorization_permit authorization_permit[] + case_code_case_file_case_codeTocase_code case_code @relation("case_file_case_codeTocase_code", fields: [case_code], references: [case_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__case_code") + inaction_reason_code_case_file_inaction_reason_codeToinaction_reason_code inaction_reason_code? @relation("case_file_inaction_reason_codeToinaction_reason_code", fields: [inaction_reason_code], references: [inaction_reason_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__inaction_reason_code") + agency_code agency_code @relation(fields: [owned_by_agency_code], references: [agency_code], onDelete: NoAction, onUpdate: NoAction, map: "FK_case_file__owned_by_agency_code") decision decision[] lead lead[] + site site[] wildlife wildlife[] } @@ -570,3 +572,45 @@ model sector_code { update_utc_timestamp DateTime @db.Timestamp(6) schedule_sector_xref_schedule_sector_xref_sector_codeTosector_code schedule_sector_xref[] @relation("schedule_sector_xref_sector_codeTosector_code") } + +model authorization_permit { + authorization_permit_guid String @id(map: "PK_authorization_permit_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + case_file_guid String @db.Uuid + authorization_permit_id String @db.VarChar(50) + active_ind Boolean? @default(true) + create_user_id String @db.VarChar(32) + create_utc_timestamp DateTime @db.Timestamp(6) + update_user_id String? @db.VarChar(32) + update_utc_timestamp DateTime? @db.Timestamp(6) + case_file case_file @relation(fields: [case_file_guid], references: [case_file_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_authorization_permit__case_file_guid") +} + +model authorization_permit_h { + h_authorization_permit_guid String @id(map: "PK_h_authorization_permit") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + target_row_id String @db.Uuid + operation_type String @db.Char(1) + operation_user_id String @default(dbgenerated("CURRENT_USER")) @db.VarChar(32) + operation_executed_at DateTime @default(now()) @db.Timestamp(6) + data_after_executed_operation Json? +} + +model site { + site_guid String @id(map: "PK_site_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + case_file_guid String @db.Uuid + site_id String @db.VarChar(50) + active_ind Boolean? @default(true) + create_user_id String @db.VarChar(32) + create_utc_timestamp DateTime @db.Timestamp(6) + update_user_id String? @db.VarChar(32) + update_utc_timestamp DateTime? @db.Timestamp(6) + case_file case_file @relation(fields: [case_file_guid], references: [case_file_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_site__case_file_guid") +} + +model site_h { + h_site_guid String @id(map: "PK_h_site") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + target_row_id String @db.Uuid + operation_type String @db.Char(1) + operation_user_id String @default(dbgenerated("CURRENT_USER")) @db.VarChar(32) + operation_executed_at DateTime @default(now()) @db.Timestamp(6) + data_after_executed_operation Json? +} diff --git a/backend/src/case_file/case_file.resolver.ts b/backend/src/case_file/case_file.resolver.ts index 7bcde230..eb3fcb09 100644 --- a/backend/src/case_file/case_file.resolver.ts +++ b/backend/src/case_file/case_file.resolver.ts @@ -16,6 +16,9 @@ import { DeleteWildlifeInput } from "./dto/wildlife/delete-wildlife-input"; import { UpdateWildlifeInput } from "./dto/wildlife/update-wildlife-input"; import { CreateDecisionInput } from "./dto/ceeb/decision/create-decsion-input"; import { UpdateDecisionInput } from "./dto/ceeb/decision/update-decsion-input"; +import { CreateAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/create-authorization-outcome-input"; +import { UpdateAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/update-authorization-outcome-input"; +import { DeleteAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/delete-authorization-outcome-input"; @UseGuards(JwtRoleGuard) @Resolver("CaseFile") @@ -135,4 +138,22 @@ export class CaseFileResolver { updateDecision(@Args("input") input: UpdateDecisionInput) { return this.caseFileService.updateDecision(input); } + + @Mutation("createAuthorizationOutcome") + @Roles(Role.CEEB) + createAuthorizationOutcome(@Args("input") input: CreateAuthorizationOutcomeInput) { + return this.caseFileService.createAuthorizationOutcome(input); + } + + @Mutation("updateAuthorizationOutcome") + @Roles(Role.CEEB) + updateAuthorizationOutcome(@Args("input") input: UpdateAuthorizationOutcomeInput) { + return this.caseFileService.updateAuthorizationOutcome(input); + } + + @Mutation("deleteAuthorizationOutcome") + @Roles(Role.CEEB) + deleteAuthorizationOutcome(@Args("input") input: DeleteAuthorizationOutcomeInput) { + return this.caseFileService.deleteAuthorizationOutcome(input); + } } diff --git a/backend/src/case_file/case_file.service.ts b/backend/src/case_file/case_file.service.ts index 64e1ac53..6f1114b2 100644 --- a/backend/src/case_file/case_file.service.ts +++ b/backend/src/case_file/case_file.service.ts @@ -29,6 +29,11 @@ import { DecisionInput } from "./dto/ceeb/decision/decision-input"; import { randomUUID } from "crypto"; import { Decision } from "./entities/decision-entity"; import { UpdateDecisionInput } from "./dto/ceeb/decision/update-decsion-input"; +import { CreateAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/create-authorization-outcome-input"; +import { AuthorizationOutcomeSearchResults } from "./dto/ceeb/authorization-outcome/authorization-outcome-search-results"; +import { AuthorizationOutcome } from "./entities/authorization-outcome.entity"; +import { UpdateAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/update-authorization-outcome-input"; +import { DeleteAuthorizationOutcomeInput } from "./dto/ceeb/authorization-outcome/delete-authorization-outcome-input"; @Injectable() export class CaseFileService { @@ -309,6 +314,24 @@ export class CaseFileService { }, }, }, + authorization_permit: { + where: { + active_ind: true, + }, + select: { + authorization_permit_guid: true, + authorization_permit_id: true, + }, + }, + site: { + where: { + active_ind: true, + }, + select: { + site_guid: true, + site_id: true, + }, + }, }, }); @@ -404,6 +427,10 @@ export class CaseFileService { caseFile.decision = record; } + //-- add the authorization if there's either authorized_permit or site + //-- but there can be only one + caseFile.authorization = this._getAuthorizationOutcome(queryResult as AuthorizationOutcomeSearchResults); + return caseFile; }; @@ -2777,6 +2804,235 @@ export class CaseFileService { } }; + //-- + //-- authorization outcome + //-- + private _addAuthorizationOutcome = async ( + db: Omit< + PrismaClient, + "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends" + >, + caseId: string, + type: "permit" | "site", + value: string, + userId: string, + ): Promise => { + try { + let record: any = { + case_file_guid: caseId, + create_user_id: userId, + update_user_id: userId, + create_utc_timestamp: new Date(), + update_utc_timestamp: new Date(), + }; + + if (type === "permit") { + record = { ...record, authorization_permit_id: value }; + + const result = await db.authorization_permit.create({ + data: record, + }); + + return result?.authorization_permit_guid; + } else { + record = { ...record, site_id: value }; + + const result = await db.site.create({ + data: record, + }); + + return result.site_guid; + } + } catch (exception) { + let { message } = exception; + this.logger.error( + `Unable to create new ${type === "permit" ? "authorization_permit" : "site"} record: `, + message, + ); + throw new GraphQLError("Exception occurred. See server log for details", exception); + } + }; + + private _removeAuthorizationOutcome = async ( + db: Omit< + PrismaClient, + "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends" + >, + id: string, + type: "permit" | "site", + userId: string, + current: Date, + ): Promise => { + const softDeleteFragment = { active_ind: false, update_user_id: userId, update_utc_timestamp: current }; + + try { + if (type === "permit") { + const result = await db.authorization_permit.update({ + where: { authorization_permit_guid: id }, + data: softDeleteFragment, + }); + } else { + const result = await db.site.update({ + where: { site_guid: id }, + data: softDeleteFragment, + }); + } + } catch (exception) { + let { message } = exception; + this.logger.error( + `Unable to create new ${type === "permit" ? "authorization_permit" : "site"} record: `, + message, + ); + throw new GraphQLError("Exception occurred. See server log for details", exception); + } + }; + + private _updateAuthorizationOutcome = async ( + db: Omit< + PrismaClient, + "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends" + >, + id: string, + type: "permit" | "site", + value: string, + userId: string, + current: Date, + ): Promise => { + const record = { + update_user_id: userId, + update_utc_timestamp: current, + ...(type === "permit" && { authorization_permit_id: value }), + ...(type === "site" && { site_id: value }), + }; + + try { + if (type === "permit") { + const result = await db.authorization_permit.update({ + where: { authorization_permit_guid: id }, + data: record, + }); + } else { + const result = await db.site.update({ + where: { site_guid: id }, + data: record, + }); + } + } catch (exception) { + let { message } = exception; + this.logger.error( + `Unable to update existing ${type === "permit" ? "authorization_permit" : "site"} record: `, + message, + ); + throw new GraphQLError("Exception occurred. See server log for details", exception); + } + }; + + private _getAuthorizationOutcome = (query: AuthorizationOutcomeSearchResults): AuthorizationOutcome => { + const { authorization_permit: permit, site } = query; + + if (permit.length !== 0) { + return { id: permit[0].authorization_permit_guid, type: "permit", value: permit[0].authorization_permit_id }; + } + + if (site.length !== 0) { + return { id: site[0].site_guid, type: "site", value: site[0].site_id }; + } + + return null; + }; + + createAuthorizationOutcome = async (model: CreateAuthorizationOutcomeInput): Promise => { + let caseFileId = ""; + + try { + let result: CaseFile; + + await this.prisma.$transaction(async (db) => { + const { leadIdentifier, createUserId, input } = model; + + const caseFile = await this.findOneByLeadId(leadIdentifier); + + if (caseFile && caseFile?.caseIdentifier) { + caseFileId = caseFile.caseIdentifier; + } else { + const caseInput: CreateCaseInput = { ...model }; + caseFileId = await this.createCase(db, caseInput); + } + + //-- create a new authorized_permit or site record depending on the type + //-- of authorization is provided + const { type, value } = input; + + const outcome = await this._addAuthorizationOutcome(db, caseFileId, type, value, createUserId); + }); + + return await this.findOne(caseFileId); + } catch (error) { + console.log("exception: unable to create authorization outcome ", error); + throw new GraphQLError("Exception occurred. See server log for details", {}); + } + }; + + updateAuthorizationOutcome = async (model: UpdateAuthorizationOutcomeInput): Promise => { + const { caseIdentifier, updateUserId, input } = model; + const timestamp = new Date(); + + try { + let result: CaseFile; + + await this.prisma.$transaction(async (db) => { + //-- get the current case file and compare the current + //-- authorization outcome to the submited outcome if the + //-- outcome is a different type, remove the old outcome + //-- and add a new one or update the type if the same + const caseFile = await this.findOne(caseIdentifier); + const { authorization: current } = caseFile; + + const { type, value } = input; + if (current.type === type) { + await this._updateAuthorizationOutcome(db, current.id, type, value, updateUserId, timestamp); + } else { + await this._removeAuthorizationOutcome(db, current.id, current.type, updateUserId, timestamp); + + if (current.type === "permit") { + await this._addAuthorizationOutcome(db, caseIdentifier, type, value, updateUserId); + } else { + await this._addAuthorizationOutcome(db, caseIdentifier, type, value, updateUserId); + } + } + }); + + return await this.findOne(caseIdentifier); + } catch (error) { + console.log("exception: unable to create authorization outcome ", error); + throw new GraphQLError("Exception occurred. See server log for details", {}); + } + }; + + deleteAuthorizationOutcome = async (model: DeleteAuthorizationOutcomeInput): Promise => { + const { caseIdentifier, updateUserId, id } = model; + const timestamp = new Date(); + + try { + await this.prisma.$transaction(async (db) => { + const caseFile = await this.findOne(caseIdentifier); + + if (caseFile) { + const { + authorization: { type }, + } = caseFile; + + await this._removeAuthorizationOutcome(db, id, type, updateUserId, timestamp); + } + }); + + return await this.findOne(caseIdentifier); + } catch (error) { + console.log("exception: unable to delete authorization outcome ", error); + throw new GraphQLError("Exception occurred. See server log for details", {}); + } + }; + //-- //-- not implemented //-- diff --git a/backend/src/case_file/case_file_functions.graphql b/backend/src/case_file/case_file_functions.graphql index ef18169b..b6ffd5cf 100644 --- a/backend/src/case_file/case_file_functions.graphql +++ b/backend/src/case_file/case_file_functions.graphql @@ -27,4 +27,8 @@ type Mutation { createDecision(input: CreateDecisionInput!): CaseFile! updateDecision(input: UpdateDecisionInput!): CaseFile! + + createAuthorizationOutcome(input: CreateAuthorizationOutcomeInput!): CaseFile! + updateAuthorizationOutcome(input: UpdateAuthorizationOutcomeInput!): CaseFile! + deleteAuthorizationOutcome(input: DeleteAuthorizationOutcomeInput!): CaseFile! } diff --git a/backend/src/case_file/case_file_inputs.graphql b/backend/src/case_file/case_file_inputs.graphql index abe48160..28d0ea6f 100644 --- a/backend/src/case_file/case_file_inputs.graphql +++ b/backend/src/case_file/case_file_inputs.graphql @@ -237,3 +237,31 @@ input DecisionInput { actionTaken: String actionTakenDate: Date } + +input CreateAuthorizationOutcomeInput { + leadIdentifier: String! + agencyCode: String! + caseCode: String! + createUserId: String! + input: PermitSiteInput! +} + +input UpdateAuthorizationOutcomeInput { + caseIdentifier: String + agencyCode: String + caseCode: String + updateUserId: String + input: PermitSiteInput +} + +input DeleteAuthorizationOutcomeInput { + caseIdentifier: String! + updateUserId: String! + id: String! +} + +input PermitSiteInput { + id: String + type: String! + value: String! +} diff --git a/backend/src/case_file/case_file_types.graphql b/backend/src/case_file/case_file_types.graphql index 5b2f1fec..92b757a5 100644 --- a/backend/src/case_file/case_file_types.graphql +++ b/backend/src/case_file/case_file_types.graphql @@ -11,6 +11,7 @@ type CaseFile { reviewComplete: CaseFileAction subject: [Wildlife] decision: Decision + authorization: PermitSite } type Assessment { @@ -90,3 +91,9 @@ type Decision { actionTaken: String actionTakenDate: Date } + +type PermitSite { + id: String + type: String + value: String +} diff --git a/backend/src/case_file/dto/ceeb/authorization-outcome/authorization-outcome-search-results.ts b/backend/src/case_file/dto/ceeb/authorization-outcome/authorization-outcome-search-results.ts new file mode 100644 index 00000000..090276ea --- /dev/null +++ b/backend/src/case_file/dto/ceeb/authorization-outcome/authorization-outcome-search-results.ts @@ -0,0 +1,4 @@ +export interface AuthorizationOutcomeSearchResults { + authorization_permit: { authorization_permit_guid: string; authorization_permit_id: string }[]; + site: { site_guid: string; site_id: string }[]; +} diff --git a/backend/src/case_file/dto/ceeb/authorization-outcome/create-authorization-outcome-input.ts b/backend/src/case_file/dto/ceeb/authorization-outcome/create-authorization-outcome-input.ts new file mode 100644 index 00000000..d8ab789f --- /dev/null +++ b/backend/src/case_file/dto/ceeb/authorization-outcome/create-authorization-outcome-input.ts @@ -0,0 +1,9 @@ +import { PermitSiteInput } from "./permit-site-input"; + +export interface CreateAuthorizationOutcomeInput { + leadIdentifier: string; + agencyCode: string; + caseCode: string; + createUserId: string; + input: PermitSiteInput; +} diff --git a/backend/src/case_file/dto/ceeb/authorization-outcome/delete-authorization-outcome-input.ts b/backend/src/case_file/dto/ceeb/authorization-outcome/delete-authorization-outcome-input.ts new file mode 100644 index 00000000..bede7545 --- /dev/null +++ b/backend/src/case_file/dto/ceeb/authorization-outcome/delete-authorization-outcome-input.ts @@ -0,0 +1,5 @@ +export interface DeleteAuthorizationOutcomeInput { + caseIdentifier: string; + updateUserId: string; + id: string; +} diff --git a/backend/src/case_file/dto/ceeb/authorization-outcome/permit-site-input.ts b/backend/src/case_file/dto/ceeb/authorization-outcome/permit-site-input.ts new file mode 100644 index 00000000..a3d9d9c6 --- /dev/null +++ b/backend/src/case_file/dto/ceeb/authorization-outcome/permit-site-input.ts @@ -0,0 +1,5 @@ +export interface PermitSiteInput { + id?: string; + type: "permit" | "site"; + value: string; +} diff --git a/backend/src/case_file/dto/ceeb/authorization-outcome/update-authorization-outcome-input.ts b/backend/src/case_file/dto/ceeb/authorization-outcome/update-authorization-outcome-input.ts new file mode 100644 index 00000000..a0a35b3d --- /dev/null +++ b/backend/src/case_file/dto/ceeb/authorization-outcome/update-authorization-outcome-input.ts @@ -0,0 +1,9 @@ +import { PermitSiteInput } from "./permit-site-input"; + +export interface UpdateAuthorizationOutcomeInput { + caseIdentifier: string; + agencyCode: string; + caseCode: string; + updateUserId: string; + input: PermitSiteInput; +} diff --git a/backend/src/case_file/dto/ceeb/decision/update-decsion-input.ts b/backend/src/case_file/dto/ceeb/decision/update-decsion-input.ts index 6c288399..40fa157c 100644 --- a/backend/src/case_file/dto/ceeb/decision/update-decsion-input.ts +++ b/backend/src/case_file/dto/ceeb/decision/update-decsion-input.ts @@ -7,4 +7,3 @@ export class UpdateDecisionInput { updateUserId: string; decision: DecisionInput; } -// diff --git a/backend/src/case_file/entities/authorization-outcome.entity.ts b/backend/src/case_file/entities/authorization-outcome.entity.ts new file mode 100644 index 00000000..21ab7701 --- /dev/null +++ b/backend/src/case_file/entities/authorization-outcome.entity.ts @@ -0,0 +1,5 @@ +export interface AuthorizationOutcome { + id: string; + type: "permit" | "site"; + value: string; +} diff --git a/backend/src/case_file/entities/case_file.entity.ts b/backend/src/case_file/entities/case_file.entity.ts index 9b03d092..35e5dc9e 100644 --- a/backend/src/case_file/entities/case_file.entity.ts +++ b/backend/src/case_file/entities/case_file.entity.ts @@ -1,4 +1,5 @@ import { Assessment } from "./assessment.entity"; +import { AuthorizationOutcome } from "./authorization-outcome.entity"; import { Decision } from "./decision-entity"; import { Equipment } from "./equipment.entity"; import { Prevention } from "./prevention.entity"; @@ -17,4 +18,5 @@ export class CaseFile { reviewComplete?: ReviewComplete; subject?: Array; decision?: Decision; + authorization?: AuthorizationOutcome; } diff --git a/migrations/sql/V1.27.0__CE-1002.sql b/migrations/sql/V1.27.0__CE-1002.sql new file mode 100644 index 00000000..d5be3dde --- /dev/null +++ b/migrations/sql/V1.27.0__CE-1002.sql @@ -0,0 +1,157 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +-- +-- creates new tables to support authroization outcomes +-- +-- +-- create authorization_permit table +-- +create table + case_management.authorization_permit ( + authorization_permit_guid uuid NULL DEFAULT uuid_generate_v4 (), + case_file_guid uuid NOT NULL, + authorization_permit_id varchar(50) NOT NULL, + active_ind boolean default TRUE, + create_user_id varchar(32) NOT NULL, + create_utc_timestamp timestamp NOT NULL, + update_user_id varchar(32) NULL, + update_utc_timestamp timestamp NULL, + constraint "PK_authorization_permit_guid" PRIMARY KEY (authorization_permit_guid) + ); + +comment on table case_management.authorization_permit is 'Contains the authroized site id for the Authroization Outcome'; + +comment on column case_management.authorization_permit.authorization_permit_guid is 'System generated unique key for an authorized_permit record'; + +comment on column case_management.authorization_permit.case_file_guid is 'System generated unique key for a case. This key should never be exposed to users via any system utilizing the tables.'; + +comment on column case_management.authorization_permit.authorization_permit_id is 'The value used for an authorized site id'; + +comment on column case_management.authorization_permit.active_ind is 'A boolean indicator to determine if the has been soft deleted.'; + +comment on column case_management.authorization_permit.create_user_id is 'The id of the user that created the authroized site id.'; + +comment on column case_management.authorization_permit.create_utc_timestamp is 'The timestamp when the authroized site id was created. The timestamp is stored in UTC with no Offset.'; + +comment on column case_management.authorization_permit.update_user_id is 'The id of the user that updated the authroized site id'; + +comment on column case_management.authorization_permit.update_utc_timestamp is 'The timestamp when the authroized site id was updated. The timestamp is stored in UTC with no Offset.'; + +-- +-- create authorized_permit_h history table +-- +CREATE TABLE + case_management.authorization_permit_h ( + h_authorization_permit_guid uuid NOT NULL DEFAULT uuid_generate_v4 (), + target_row_id uuid NOT NULL, + operation_type char(1) NOT NULL, + operation_user_id varchar(32) NOT NULL DEFAULT current_user, + operation_executed_at timestamp NOT NULL DEFAULT now(), + data_after_executed_operation jsonb, + CONSTRAINT "PK_h_authorization_permit" PRIMARY KEY (h_authorization_permit_guid) + ); + +COMMENT on table case_management.authorization_permit_h is 'History table for authorization_permit table'; + +COMMENT on column case_management.authorization_permit_h.h_authorization_permit_guid is 'System generated unique key for authorization permit history. This key should never be exposed to users via any system utilizing the tables.'; + +COMMENT on column case_management.authorization_permit_h.target_row_id is 'The unique key for the authorization permit that has been created or modified.'; + +COMMENT on column case_management.authorization_permit_h.operation_type is 'The operation performed: I = Insert, U = Update, D = Delete'; + +COMMENT on column case_management.authorization_permit_h.operation_user_id is 'The id of the user that created or modified the data in the authorization permit table. Defaults to the logged in user if not passed in by the application.'; + +COMMENT on column case_management.authorization_permit_h.operation_executed_at is 'The timestamp when the data in the authorization permit table was created or modified. The timestamp is stored in UTC with no Offset.'; + +COMMENT on column case_management.authorization_permit_h.data_after_executed_operation is 'A JSON representation of the row in the table after the operation was completed successfully. This implies that the latest row in the audit table will always match with the current row in the live table.'; + +-- +-- create site table +-- +create table + case_management.site ( + site_guid uuid NULL DEFAULT uuid_generate_v4 (), + case_file_guid uuid NOT NULL, + site_id varchar(50) NOT NULL, + active_ind boolean default TRUE, + create_user_id varchar(32) NOT NULL, + create_utc_timestamp timestamp NOT NULL, + update_user_id varchar(32) NULL, + update_utc_timestamp timestamp NULL, + constraint "PK_site_guid" PRIMARY KEY (site_guid) + ); + +comment on table case_management.site is 'Contains the unauthorized site id for the Authroization Outcome'; + +comment on column case_management.site.site_guid is 'System generated unique key for a site record'; + +comment on column case_management.site.case_file_guid is 'System generated unique key for a case. This key should never be exposed to users via any system utilizing the tables.'; + +comment on column case_management.site.site_id is 'The value used for an unauthorized site id'; + +comment on column case_management.site.active_ind is 'A boolean indicator to determine if the record has been soft deleted.'; + +comment on column case_management.site.create_user_id is 'The id of the user that created the unauthroized site id.'; + +comment on column case_management.site.create_utc_timestamp is 'The timestamp when the unauthroized site id was created. The timestamp is stored in UTC with no Offset.'; + +comment on column case_management.site.update_user_id is 'The id of the user that updated the unauthroized site id'; + +comment on column case_management.site.update_utc_timestamp is 'The timestamp when the unauthroized site id was updated. The timestamp is stored in UTC with no Offset.'; + +-- +-- create site_h history table +-- +CREATE TABLE + case_management.site_h ( + h_site_guid uuid NOT NULL DEFAULT uuid_generate_v4 (), + target_row_id uuid NOT NULL, + operation_type char(1) NOT NULL, + operation_user_id varchar(32) NOT NULL DEFAULT current_user, + operation_executed_at timestamp NOT NULL DEFAULT now(), + data_after_executed_operation jsonb, + CONSTRAINT "PK_h_site" PRIMARY KEY (h_site_guid) + ); + +COMMENT on table case_management.site_h is 'History table for site table'; + +COMMENT on column case_management.site_h.h_site_guid is 'System generated unique key for site history. This key should never be exposed to users via any system utilizing the tables.'; + +COMMENT on column case_management.site_h.target_row_id is 'The unique key for the site that has been created or modified.'; + +COMMENT on column case_management.site_h.operation_type is 'The operation performed: I = Insert, U = Update, D = Delete'; + +COMMENT on column case_management.site_h.operation_user_id is 'The id of the user that created or modified the data in the site table. Defaults to the logged in user if not passed in by the application.'; + +COMMENT on column case_management.site_h.operation_executed_at is 'The timestamp when the data in the site table was created or modified. The timestamp is stored in UTC with no Offset.'; + +COMMENT on column case_management.site_h.data_after_executed_operation is 'A JSON representation of the row in the table after the operation was completed successfully. This implies that the latest row in the audit table will always match with the current row in the live table.'; + +-- +-- add history triggers to authorization_permit and site tables +-- +CREATE +OR REPLACE TRIGGER auth_permit_history_trigger BEFORE INSERT +OR DELETE +OR +UPDATE ON case_management.authorization_permit FOR EACH ROW +EXECUTE PROCEDURE audit_history ( + 'authorization_permit_h', + 'authorization_permit_guid' +); + +CREATE +OR REPLACE TRIGGER site_history_trigger BEFORE INSERT +OR DELETE +OR +UPDATE ON case_management.site FOR EACH ROW +EXECUTE PROCEDURE audit_history ('site_h', 'site_guid'); + +-- +-- add foreign keys to case_file table +-- +ALTER TABLE case_management.authorization_permit +ADD CONSTRAINT FK_authorization_permit__case_file_guid FOREIGN KEY (case_file_guid) REFERENCES case_management.case_file (case_file_guid); + +ALTER TABLE case_management.site +ADD CONSTRAINT FK_site__case_file_guid FOREIGN KEY (case_file_guid) REFERENCES case_management.case_file (case_file_guid); \ No newline at end of file