diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index bea625ba..7b4d2f76 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -461,24 +461,25 @@ model wildlife_h { /// 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 decision { - decision_guid String @id(map: "PK_decision_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid - case_file_guid String @db.Uuid - schedule_sector_xref_guid String @db.Uuid - discharge_code String @db.VarChar(10) - rationale_code String? @db.VarChar(10) - inspection_number Int? - lead_agency String? @db.VarChar(10) - non_compliance_decision_matrix_code String @db.VarChar(10) - active_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) - action action[] - case_file case_file @relation(fields: [case_file_guid], references: [case_file_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__case_file_guid") - discharge_code_decision_discharge_codeTodischarge_code discharge_code @relation("decision_discharge_codeTodischarge_code", fields: [discharge_code], references: [discharge_code], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__discharge_code") - rationale_code_decision_rationale_codeTorationale_code rationale_code? @relation("decision_rationale_codeTorationale_code", fields: [rationale_code], references: [rationale_code], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__rationale_code") - schedule_sector_xref schedule_sector_xref @relation(fields: [schedule_sector_xref_guid], references: [schedule_sector_xref_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__schedule_sector_xref_guid") + decision_guid String @id(map: "PK_decision_guid") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid + case_file_guid String @db.Uuid + schedule_sector_xref_guid String @db.Uuid + discharge_code String @db.VarChar(10) + rationale_code String? @db.VarChar(10) + inspection_number Int? + lead_agency String? @db.VarChar(10) + non_compliance_decision_matrix_code String? @db.VarChar(10) + active_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) + action action[] + case_file case_file @relation(fields: [case_file_guid], references: [case_file_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__case_file_guid") + discharge_code_decision_discharge_codeTodischarge_code discharge_code @relation("decision_discharge_codeTodischarge_code", fields: [discharge_code], references: [discharge_code], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__discharge_code") + non_compliance_decision_matrix_code_decision_non_compliance_decision_matrix_codeTonon_compliance_decision_matrix_code non_compliance_decision_matrix_code? @relation("decision_non_compliance_decision_matrix_codeTonon_compliance_decision_matrix_code", fields: [non_compliance_decision_matrix_code], references: [non_compliance_decision_matrix_code], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__ncdm_code") + rationale_code_decision_rationale_codeTorationale_code rationale_code? @relation("decision_rationale_codeTorationale_code", fields: [rationale_code], references: [rationale_code], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__rationale_code") + schedule_sector_xref schedule_sector_xref @relation(fields: [schedule_sector_xref_guid], references: [schedule_sector_xref_guid], onDelete: NoAction, onUpdate: NoAction, map: "fk_decision__schedule_sector_xref_guid") } model decision_h { @@ -506,15 +507,16 @@ model discharge_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 non_compliance_decision_matrix_code { - non_compliance_decision_matrix_code String @id(map: "PK_non_compliance_decision_matrix_code") @db.VarChar(10) - short_description String @db.VarChar(50) - long_description String? @db.VarChar(250) - display_order Int - active_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) + non_compliance_decision_matrix_code String @id(map: "PK_non_compliance_decision_matrix_code") @db.VarChar(10) + short_description String @db.VarChar(50) + long_description String? @db.VarChar(250) + display_order Int + active_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) + decision_decision_non_compliance_decision_matrix_codeTonon_compliance_decision_matrix_code decision[] @relation("decision_non_compliance_decision_matrix_codeTonon_compliance_decision_matrix_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 diff --git a/backend/src/case_file/case_file.service.ts b/backend/src/case_file/case_file.service.ts index 6f1114b2..a9c9f7e0 100644 --- a/backend/src/case_file/case_file.service.ts +++ b/backend/src/case_file/case_file.service.ts @@ -34,6 +34,7 @@ import { AuthorizationOutcomeSearchResults } from "./dto/ceeb/authorization-outc 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"; +import { ActionInput } from "./dto/action-input"; @Injectable() export class CaseFileService { @@ -410,11 +411,11 @@ export class CaseFileService { schedule: decision[0].schedule_sector_xref.schedule_code, sector: decision[0].schedule_sector_xref.sector_code, discharge: decision[0].discharge_code, - nonCompliance: decision[0].non_compliance_decision_matrix_code, - rationale: decision[0].rationale_code, - assignedTo: action[0].actor, - actionTaken: action[0].actionCode, - actionTakenDate: action[0].date, + nonCompliance: decision[0]?.non_compliance_decision_matrix_code, + rationale: decision[0]?.rationale_code, + assignedTo: action[0]?.actor, + actionTaken: action[0]?.actionCode, + actionTakenDate: action[0]?.date, }; if (decision[0].inspection_number) { @@ -2464,7 +2465,16 @@ export class CaseFileService { userId: string, ): Promise => { try { - const { discharge, nonCompliance, rationale } = decision; + let { discharge, nonCompliance, rationale } = decision; + + //don't try and insert empty into the code tables. + if (rationale === "" || rationale === undefined) { + rationale = null; + } + + if (nonCompliance === "" || nonCompliance === undefined) { + nonCompliance = null; + } let record: any = { decision_guid: randomUUID(), @@ -2592,11 +2602,18 @@ export class CaseFileService { //-- create sector/schedule xref const xref = await _addWdrXref(db, decision, createUserId); - //-- apply action - const actionId = await _applyAction(db, caseFileId, decision, createUserId); - //-- add decision const decsionId = await _addDecision(db, caseFileId, decision, xref, createUserId); + + //-- apply action + if (decision.actionTaken && decision.assignedTo) { + let action: ActionInput = { + actionTaken: decision.actionTaken, + actor: decision.assignedTo, + date: decision.actionTakenDate, + }; + await this._addAction(db, caseFileId, decsionId, action, createUserId); + } }); result = await this.findOne(caseFileId); @@ -2726,7 +2743,8 @@ export class CaseFileService { PrismaClient, "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends" >, - id: string, + caseIdentifier: string, + decisionId: string, decision: DecisionInput, updateUserId: string, current: Date, @@ -2740,6 +2758,7 @@ export class CaseFileService { const source = await db.action.findFirst({ where: { case_guid: caseIdentifier, + decision_guid: decisionId, }, select: { action_guid: true, @@ -2791,7 +2810,22 @@ export class CaseFileService { current, ); - const actionResult = await _updateAction(db, caseIdentifier, decision, updateUserId, current); + //-- make sure that there is an action to update first + //-- otherwise create a new action + const currentAction = await db.action.findFirst({ + where: { case_guid: caseIdentifier, decision_guid: decisonId }, + }); + + if (!currentAction && decision.actionTaken && decision.assignedTo && decision.actionTakenDate) { + const action: ActionInput = { + actionTaken: decision.actionTaken, + actor: decision.assignedTo, + date: decision.actionTakenDate, + }; + await this._addAction(db, caseIdentifier, decisonId, action, updateUserId); + } else if (currentAction && decision.actionTaken && decision.assignedTo && decision.actionTakenDate) { + await _updateAction(db, caseIdentifier, decisonId, decision, updateUserId, current); + } } }); @@ -2799,7 +2833,7 @@ export class CaseFileService { return result; } catch (error) { - console.log("exception: unable to create wildlife ", error); + console.log("exception: unable to update decision", error); throw new GraphQLError("Exception occurred. See server log for details", {}); } }; @@ -3033,6 +3067,46 @@ export class CaseFileService { } }; + private _addAction = async ( + db: Omit< + PrismaClient, + "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends" + >, + caseFileId: string, + decisionId: string, + input: ActionInput, + userId: string, + ): Promise => { + try { + const { actionTaken, actor, date } = input; + + //-- get the action_type_action xref + const xref = await this._getActionXref(db, actionTaken, ACTION_TYPE_CODES.CEEBACTION); + + let record: any = { + action_guid: randomUUID(), + case_guid: caseFileId, + action_type_action_xref_guid: xref, + decision_guid: decisionId, + actor_guid: actor, + action_date: date, + active_ind: true, + create_user_id: userId, + update_user_id: userId, + create_utc_timestamp: new Date(), + update_utc_timestamp: new Date(), + }; + const test = 0; + const result = await db.action.create({ + data: record, + }); + + return result?.action_guid; + } catch (exception) { + throw new GraphQLError("Exception occurred. See server log for details", exception); + } + }; + //-- //-- not implemented //-- diff --git a/backend/src/case_file/dto/action-input.ts b/backend/src/case_file/dto/action-input.ts new file mode 100644 index 00000000..d354970b --- /dev/null +++ b/backend/src/case_file/dto/action-input.ts @@ -0,0 +1,5 @@ +export interface ActionInput { + actionTaken: string; + actor: string; + date: Date; +} diff --git a/backend/src/case_file/dto/ceeb/decision/decision-input.ts b/backend/src/case_file/dto/ceeb/decision/decision-input.ts index 8d948020..b06693b4 100644 --- a/backend/src/case_file/dto/ceeb/decision/decision-input.ts +++ b/backend/src/case_file/dto/ceeb/decision/decision-input.ts @@ -3,8 +3,8 @@ export interface DecisionInput { schedule: string; sector: string; discharge: string; - nonCompliance: string; - rationale: string; + nonCompliance?: string; + rationale?: string; inspectionNumber?: string; leadAgency?: string; assignedTo: string; diff --git a/backend/src/case_file/entities/decision-entity.ts b/backend/src/case_file/entities/decision-entity.ts index a354be29..74b617f5 100644 --- a/backend/src/case_file/entities/decision-entity.ts +++ b/backend/src/case_file/entities/decision-entity.ts @@ -3,11 +3,11 @@ export interface Decision { schedule: string; sector: string; discharge: string; - nonCompliance: string; - rationale: string; + nonCompliance?: string; + rationale?: string; inspectionNumber?: string; leadAgency?: string; - assignedTo: string; - actionTaken: string; - actionTakenDate: Date; + assignedTo?: string; + actionTaken?: string; + actionTakenDate?: Date; } diff --git a/migrations/sql/V1.27.1__CE-1059.sql b/migrations/sql/V1.27.1__CE-1059.sql new file mode 100644 index 00000000..09c2f590 --- /dev/null +++ b/migrations/sql/V1.27.1__CE-1059.sql @@ -0,0 +1,4 @@ +ALTER TABLE case_management.decision ALTER COLUMN non_compliance_decision_matrix_code DROP NOT NULL; + +ALTER TABLE case_management.decision +ADD CONSTRAINT FK_decision__ncdm_code FOREIGN KEY (non_compliance_decision_matrix_code) REFERENCES case_management.non_compliance_decision_matrix_code (non_compliance_decision_matrix_code); \ No newline at end of file