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

feat: CE-1059-database-changes #83

Merged
merged 6 commits into from
Sep 17, 2024
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
56 changes: 29 additions & 27 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
98 changes: 86 additions & 12 deletions backend/src/case_file/case_file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -2464,7 +2465,16 @@ export class CaseFileService {
userId: string,
): Promise<any> => {
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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2726,7 +2743,8 @@ export class CaseFileService {
PrismaClient<Prisma.PrismaClientOptions, never, DefaultArgs>,
"$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends"
>,
id: string,
caseIdentifier: string,
decisionId: string,
decision: DecisionInput,
updateUserId: string,
current: Date,
Expand All @@ -2740,6 +2758,7 @@ export class CaseFileService {
const source = await db.action.findFirst({
where: {
case_guid: caseIdentifier,
decision_guid: decisionId,
},
select: {
action_guid: true,
Expand Down Expand Up @@ -2791,15 +2810,30 @@ 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);
}
}
});

result = await this.findOne(caseIdentifier);

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", {});
}
};
Expand Down Expand Up @@ -3033,6 +3067,46 @@ export class CaseFileService {
}
};

private _addAction = async (
db: Omit<
PrismaClient<Prisma.PrismaClientOptions, never, DefaultArgs>,
"$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends"
>,
caseFileId: string,
decisionId: string,
input: ActionInput,
userId: string,
): Promise<any> => {
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
//--
Expand Down
5 changes: 5 additions & 0 deletions backend/src/case_file/dto/action-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ActionInput {
actionTaken: string;
actor: string;
date: Date;
}
4 changes: 2 additions & 2 deletions backend/src/case_file/dto/ceeb/decision/decision-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions backend/src/case_file/entities/decision-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions migrations/sql/V1.27.1__CE-1059.sql
Original file line number Diff line number Diff line change
@@ -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);
Loading