Skip to content

Commit 26aaf4e

Browse files
authored
Merge branch 'ytgov:main' into main
2 parents 7050e6b + 449af37 commit 26aaf4e

18 files changed

+277
-232
lines changed

api/src/routes/directory-router.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ directoryRouter.post(
1414
const results = await unifiedDirectory.search(terms);
1515

1616
return res.json({ data: results });
17-
}
17+
},
1818
);
1919

2020
directoryRouter.post(
@@ -33,16 +33,16 @@ directoryRouter.post(
3333

3434
for (const part of parts) {
3535
allUsersQuery.whereRaw(
36-
`(LOWER("email") LIKE '${part}%' OR LOWER("first_name") LIKE '${part}%' OR LOWER("last_name") LIKE '${part}%')`
36+
`(LOWER("email") LIKE '${part}%' OR LOWER("first_name") LIKE '${part}%' OR LOWER("last_name") LIKE '${part}%')`,
3737
);
3838
}
3939

4040
let allUsers = await allUsersQuery;
4141
data.map(
42-
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id)
42+
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id),
4343
);
4444
yesnetResults.map(
45-
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id)
45+
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id),
4646
);
4747

4848
const combinedData = [...data, ...yesnetResults];
@@ -68,7 +68,7 @@ directoryRouter.post(
6868
}
6969

7070
return res.json({ data: combinedData });
71-
}
71+
},
7272
);
7373

7474
directoryRouter.post(
@@ -104,7 +104,7 @@ directoryRouter.post(
104104

105105
if (data.length > 0) {
106106
data.map(
107-
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id)
107+
(d: any) => (d.user_id = allUsers.find((u) => u.email == d.email)?.id),
108108
);
109109
} else {
110110
const nonDirectoryUser = allUsers.find((u) => u.email == terms);
@@ -127,5 +127,5 @@ directoryRouter.post(
127127
}
128128

129129
return res.json({ data });
130-
}
130+
},
131131
);

api/src/routes/offline-report-router.ts

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import express, { Request, Response } from "express";
22
import { isArray } from "lodash";
33

44
import { db as knex } from "../data/db-client";
5-
import { DepartmentService, UnifiedDirectoryService, EmailService, IncidentService, UserService } from "../services";
5+
import {
6+
DepartmentService,
7+
UnifiedDirectoryService,
8+
EmailService,
9+
IncidentService,
10+
UserService,
11+
} from "../services";
612
import {
713
Hazard,
814
HazardStatuses,
@@ -28,7 +34,15 @@ const userService = new UserService();
2834
offlineReportRouter.post("/", async (req: Request, res: Response) => {
2935
req.body.status = "Initial Report";
3036

31-
let { date, eventType, description, location_code, location_detail, supervisor_email, urgency } = req.body;
37+
let {
38+
date,
39+
eventType,
40+
description,
41+
location_code,
42+
location_detail,
43+
supervisor_email,
44+
urgency,
45+
} = req.body;
3246

3347
let currentUserEmail = req.body.on_behalf_email;
3448
let currentUserId = 1;
@@ -66,20 +80,30 @@ offlineReportRouter.post("/", async (req: Request, res: Response) => {
6680

6781
const reporting_person_email = req.body.email;
6882

69-
const defaultHazardType = await knex("hazard_types").orderBy("id").select("id").first();
70-
const defaultIncidentType = await knex("incident_types").where({ name: eventType }).select("id").first();
83+
const defaultHazardType = await knex("hazard_types")
84+
.orderBy("id")
85+
.select("id")
86+
.first();
87+
const defaultIncidentType = await knex("incident_types")
88+
.where({ name: eventType })
89+
.select("id")
90+
.first();
7191
const department = await new DepartmentService().determineDepartment(
72-
reporting_person_email,
73-
supervisor_email,
74-
location_code
92+
[reporting_person_email, supervisor_email],
93+
location_code,
7594
);
7695

7796
await directoryService.connect();
78-
const directorySubmitter = await directoryService.searchByEmail(reporting_person_email);
79-
const directorySupervisor = await directoryService.searchByEmail(supervisor_email);
97+
const directorySubmitter = await directoryService.searchByEmail(
98+
reporting_person_email,
99+
);
100+
const directorySupervisor =
101+
await directoryService.searchByEmail(supervisor_email);
80102

81103
const employeeName =
82-
directorySubmitter && directorySubmitter[0] ? directorySubmitter[0].display_name : reporting_person_email;
104+
directorySubmitter && directorySubmitter[0]
105+
? directorySubmitter[0].display_name
106+
: reporting_person_email;
83107

84108
const trx = await knex.transaction();
85109

@@ -102,15 +126,28 @@ offlineReportRouter.post("/", async (req: Request, res: Response) => {
102126
source: "offline",
103127
} as Incident;
104128

105-
const insertedIncidents = await trx("incidents").insert(incident).returning("*");
129+
const insertedIncidents = await trx("incidents")
130+
.insert(incident)
131+
.returning("*");
106132
let insertedIncidentId = insertedIncidents[0].id;
107133

108134
let steps = new Array<string>();
109135

110136
if (eventType == "hazard") {
111-
steps = ["Hazard Identified", "Assessment of Hazard", "Control the Hazard", "Employee Notification"];
137+
steps = [
138+
"Hazard Identified",
139+
"Assessment of Hazard",
140+
"Control the Hazard",
141+
"Employee Notification",
142+
];
112143
} else {
113-
steps = ["Incident Reported", "Investigation", "Control Plan", "Controls Implemented", "Employee Notification"];
144+
steps = [
145+
"Incident Reported",
146+
"Investigation",
147+
"Control Plan",
148+
"Controls Implemented",
149+
"Employee Notification",
150+
];
114151
}
115152

116153
for (let i = 1; i <= steps.length; i++) {
@@ -134,25 +171,31 @@ offlineReportRouter.post("/", async (req: Request, res: Response) => {
134171

135172
if (directorySubmitter && directorySubmitter.length > 0) {
136173
await emailService.sendIncidentEmployeeNotification(
137-
{ fullName: directorySubmitter[0].display_name, email: reporting_person_email },
174+
{
175+
fullName: directorySubmitter[0].display_name,
176+
email: reporting_person_email,
177+
},
138178
employeeName,
139-
insertedIncidents[0]
179+
insertedIncidents[0],
140180
);
141181

142182
if (currentUserEmail != reporting_person_email) {
143183
await emailService.sendIncidentReporterNotification(
144184
{ fullName: currentUserName, email: currentUserEmail },
145185
employeeName,
146-
insertedIncidents[0]
186+
insertedIncidents[0],
147187
);
148188
}
149189
}
150190

151191
if (directorySupervisor && directorySupervisor.length > 0) {
152192
await emailService.sendIncidentSupervisorNotification(
153-
{ fullName: directorySupervisor[0].display_name, email: supervisor_email },
193+
{
194+
fullName: directorySupervisor[0].display_name,
195+
email: supervisor_email,
196+
},
154197
employeeName,
155-
insertedIncidents[0]
198+
insertedIncidents[0],
156199
);
157200
}
158201

api/src/routes/report-router.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,34 @@ reportRouter.post("/", async (req: Request, res: Response) => {
319319
additional_people,
320320
} = req.body;
321321

322-
const reporting_person_email =
323-
on_behalf == "Yes" ? on_behalf_email : req.user.email;
322+
let reporting_person_email = req.user.email;
323+
324+
await directoryService.connect();
325+
326+
if (on_behalf == "Yes") {
327+
const directoryOnBehalf = await directoryService.search(on_behalf_email);
328+
329+
if (directoryOnBehalf && directoryOnBehalf.length == 1) {
330+
reporting_person_email = directoryOnBehalf[0].email;
331+
} else {
332+
reporting_person_email = on_behalf_email;
333+
}
334+
}
324335

325336
const defaultIncidentType = await knex("incident_types")
326337
.where({ name: eventType })
327338
.select("id")
328339
.first();
340+
329341
const department = await new DepartmentService().determineDepartment(
330-
reporting_person_email,
331-
supervisor_email,
342+
[reporting_person_email, req.user.email, supervisor_email],
332343
location_code,
333344
);
334345

335-
await directoryService.connect();
336346
const directorySubmitter = await directoryService.searchByEmail(
337347
reporting_person_email,
338348
);
349+
339350
const directorySupervisor =
340351
await directoryService.searchByEmail(supervisor_email);
341352

@@ -732,12 +743,21 @@ reportRouter.post(
732743
.orderBy("order");
733744

734745
const implementedStep = incidentSteps.find(
735-
(step: IncidentStep) => step.step_title == "Controls Implemented",
746+
(step: IncidentStep) =>
747+
step.step_title == "Controls Implemented" ||
748+
step.step_title == "Control the Hazard",
736749
);
737750
const requestStep = incidentSteps.find(
738751
(step: IncidentStep) => step.step_title == "Committee Review",
739752
);
740753

754+
if (isNil(implementedStep)) {
755+
console.log("No implemented step found, cannot send to committee");
756+
return res
757+
.status(400)
758+
.send("No implemented step found, cannot send to committee");
759+
}
760+
741761
if (!isNil(requestStep)) return res.status(400).send("Step already exists");
742762

743763
const newStep = {

api/src/services/department-service.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,56 @@ import { db } from "../data/db-client";
33
import { UnifiedDirectoryService } from "./unified-directory-service";
44

55
export class DepartmentService {
6-
async determineDepartment(userEmail: string, supervisorEmail: string, location_code: string): Promise<Department> {
6+
async determineDepartment(
7+
emailArray: string[],
8+
location_code: string,
9+
): Promise<Department> {
10+
for (const email of emailArray) {
11+
const department = await this.findMatchByEmail(email);
12+
if (department) return department;
13+
}
14+
15+
let department = await db("departments")
16+
.where({ code: "UNK" })
17+
.first<Department>();
18+
19+
return department;
20+
}
21+
22+
async findMatchByEmail(email: string): Promise<Department | null> {
723
const directory = new UnifiedDirectoryService();
24+
const userResults = await directory.searchByEmail(email);
825

9-
const userResults = await directory.searchByEmail(userEmail);
10-
const superResults = await directory.searchByEmail(supervisorEmail);
26+
if (userResults && userResults.length == 1) {
27+
await db("users")
28+
.where({ email })
29+
.update({ department: userResults[0].department });
1130

12-
if (userResults && userResults.length > 0) {
13-
await db("users").where({ email: userEmail }).update({ department: userResults[0].department });
14-
const userDepart = await db("departments").where({ name: userResults[0].department }).first<Department>();
31+
const userDepart = await db("departments")
32+
.where({ name: userResults[0].department })
33+
.first<Department>();
1534
if (userDepart) return userDepart;
16-
}
1735

18-
if (superResults && superResults.length > 0) {
19-
await db("users").where({ email: supervisorEmail }).update({ department: superResults[0].department });
20-
const superDepart = await db("departments").where({ name: superResults[0].department }).first<Department>();
21-
if (superDepart) return superDepart;
36+
const depart = await db("departments")
37+
.where({ name: userResults[0].department })
38+
.first<Department>();
39+
if (depart) return depart;
2240
}
2341

24-
if (userEmail.endsWith("@wcb.yk.ca") || supervisorEmail.endsWith("@wcb.yk.ca")) {
25-
const wcbDepart = await db("departments").where({ code: "WSCB" }).first<Department>();
42+
if (email.endsWith("@wcb.yk.ca")) {
43+
const wcbDepart = await db("departments")
44+
.where({ code: "WSCB" })
45+
.first<Department>();
2646
if (wcbDepart) return wcbDepart;
2747
}
2848

29-
if (userEmail.endsWith("@yesnet.yk.ca") || supervisorEmail.endsWith("@yesnet.yk.ca")) {
30-
const eduDepart = await db("departments").where({ code: "EDU" }).first<Department>();
49+
if (email.endsWith("@yesnet.yk.ca")) {
50+
const eduDepart = await db("departments")
51+
.where({ code: "EDU" })
52+
.first<Department>();
3153
if (eduDepart) return eduDepart;
3254
}
3355

34-
let department = await db("departments").where({ code: "UNK" }).first();
35-
return department;
56+
return null;
3657
}
3758
}

0 commit comments

Comments
 (0)