From edb582ae7b2b0838d82b61ffed30ef733effa90c Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Mon, 29 Jul 2024 08:59:44 -0700 Subject: [PATCH] More offline fixes --- api/src/routes/offline-report-router.ts | 48 ++++++++++------- api/src/services/user-service.ts | 2 +- web/src/components/DirectorySelector.vue | 1 + web/src/components/incident/CreatePage.vue | 37 ++++++++------ .../components/incident/CreatePageOffline.vue | 51 ++++++++++--------- web/src/routes.ts | 16 ++---- web/src/utils/validation.ts | 10 ++++ 7 files changed, 96 insertions(+), 69 deletions(-) create mode 100644 web/src/utils/validation.ts diff --git a/api/src/routes/offline-report-router.ts b/api/src/routes/offline-report-router.ts index 397ae76..03d0831 100644 --- a/api/src/routes/offline-report-router.ts +++ b/api/src/routes/offline-report-router.ts @@ -2,7 +2,7 @@ import express, { Request, Response } from "express"; import { isArray } from "lodash"; import { db as knex } from "../data"; -import { DepartmentService, DirectoryService, EmailService, IncidentService } from "../services"; +import { DepartmentService, DirectoryService, EmailService, IncidentService, UserService } from "../services"; import { Hazard, HazardStatuses, @@ -21,30 +21,45 @@ export const offlineReportRouter = express.Router(); const db = new IncidentService(); const emailService = new EmailService(); const directoryService = new DirectoryService(); +const userService = new UserService(); offlineReportRouter.post("/", async (req: Request, res: Response) => { const {} = req.body; - let currentUserEmail = "offline-user@yukon.ca"; + let currentUserEmail = req.body.email; let currentUserId = 1; - let currentUserName = "Offline User Testing"; + let currentUserName = req.body.email; + + const existingUser = await userService.getByEmail(currentUserEmail); + + if (existingUser) { + currentUserId = existingUser.id; + currentUserEmail = existingUser.email; + currentUserName = existingUser.display_name; + } else { + const createdUser = await userService.create({ + department: "", + division: "", + branch: "", + unit: "", + display_name: currentUserEmail, + email: currentUserEmail, + first_name: "Offline", + last_name: "Submission", + title: "", + is_active: true, + auth_subject: "SUB_MISSING", + }); + + currentUserId = createdUser[0].id; + } req.body.email = currentUserEmail; req.body.status = "Initial Report"; - let { - date, - eventType, - description, - location_code, - location_detail, - supervisor_email, - on_behalf, - on_behalf_email, - urgency, - } = req.body; + let { date, eventType, description, location_code, location_detail, supervisor_email, urgency } = req.body; - const reporting_person_email = on_behalf == "Yes" ? on_behalf_email : currentUserEmail; + const reporting_person_email = req.body.email; const defaultHazardType = await knex("hazard_types").orderBy("id").select("id").first(); const defaultIncidentType = await knex("incident_types").where({ name: eventType }).select("id").first(); @@ -89,7 +104,6 @@ offlineReportRouter.post("/", async (req: Request, res: Response) => { supervisor_email, incident_type_id: defaultIncidentType.id, reporting_person_email, - proxy_user_id: currentUserId, urgency_code: urgency, } as Incident; @@ -181,7 +195,7 @@ offlineReportRouter.post("/", async (req: Request, res: Response) => { } await trx.commit(); - console.log("ALL GOOD!") + console.log("ALL GOOD!"); return res.status(200).json({ data: {} }); } catch (error) { trx.rollback(); diff --git a/api/src/services/user-service.ts b/api/src/services/user-service.ts index 4b27489..f47ed39 100644 --- a/api/src/services/user-service.ts +++ b/api/src/services/user-service.ts @@ -34,7 +34,7 @@ export class UserService { } async create(item: User_Create): Promise { - return db("users").insert(item); + return db("users").insert(item).returning("*"); } async update(id: number | string, item: User_Update): Promise { diff --git a/web/src/components/DirectorySelector.vue b/web/src/components/DirectorySelector.vue index 1d64f0c..a7f8b0f 100644 --- a/web/src/components/DirectorySelector.vue +++ b/web/src/components/DirectorySelector.vue @@ -89,6 +89,7 @@ watch(search, async (nv) => { watch(model, async (nv) => { if (!nv) { + doSelect(); return; } diff --git a/web/src/components/incident/CreatePage.vue b/web/src/components/incident/CreatePage.vue index a66870e..7a14918 100644 --- a/web/src/components/incident/CreatePage.vue +++ b/web/src/components/incident/CreatePage.vue @@ -6,7 +6,7 @@ Wellbeing at 867-332-5974

- +
@@ -100,36 +100,31 @@ Urgency level - + - + General location where the event occurred + :rules="[requiredRule]" /> - + Specific location where the event occurred (such as a spot in a building) - + - + Describe the event in your own words. Please include any details or thoughts that may be helpful to know such as weather or time of day - + @@ -182,12 +177,14 @@ diff --git a/web/src/components/incident/CreatePageOffline.vue b/web/src/components/incident/CreatePageOffline.vue index a317d69..7ffdded 100644 --- a/web/src/components/incident/CreatePageOffline.vue +++ b/web/src/components/incident/CreatePageOffline.vue @@ -6,7 +6,7 @@ Wellbeing at 867-332-5974.

- +
@@ -52,7 +52,7 @@

What Happened?

- + @@ -62,36 +62,31 @@ Urgency level - + - + General location where the event occurred + :rules="[requiredRule]" /> - + Specific location where the event occurred (such as a spot in a building) - + - + Describe the event in your own words. Please include any details or thoughts that may be helpful to know such as weather or time of day - +
@@ -105,17 +100,24 @@

Submit Report

- Supervisor's email - - - - You are submitting this report offline. When your device reconnects to the internet, this report will be - uploaded. - + + + Your email + + + + Supervisor's email + + +
Submit
+ + You are submitting this report offline. When your device reconnects to the internet, this report will be + uploaded. +
@@ -128,24 +130,25 @@ import { storeToRefs } from "pinia"; import { router } from "@/routes"; import { useReportStore } from "@/store/ReportStore"; import DateTimeSelector from "@/components/DateTimeSelector.vue"; +import { requiredRule, emailRule } from "@/utils/validation"; const reportStore = useReportStore(); const { initialize, addReportOffline } = reportStore; const { locations, urgencies } = storeToRefs(reportStore); +const isValid = ref(false); + await initialize(); const report = ref({ eventType: null, date: new Date(), urgency: "Medium" }); const canSave = computed(() => { - return report.value.eventType; + return report.value.eventType && isValid.value; }); async function saveReport() { report.value.createDate = new Date(); - console.log("SAVING OFFLINE REPORT", report.value); - await addReportOffline(report.value).then(() => { router.push("/report-an-incident-offline/complete"); }); diff --git a/web/src/routes.ts b/web/src/routes.ts index b706614..7e6b949 100644 --- a/web/src/routes.ts +++ b/web/src/routes.ts @@ -70,11 +70,11 @@ const routes: RouteRecordRaw[] = [ import { useUserStore } from "@/store/UserStore"; -/* export async function waitForUserToLoad(): Promise { +export async function waitForUserToLoad(): Promise { let u = useUserStore(); await u.initialize(); return u; -} */ +} export const router = createRouter({ history: createWebHistory(), @@ -91,24 +91,18 @@ router.beforeEach(async (to) => { } console.log("Await authGuard"); - const isAuthenticated = await authGuard(to); if (isAuthenticated) { console.log("You are authenticated"); if (to.meta.requireSystemAdmin) { - /* const u = await waitForUserToLoad(); - - console.log("User Is", u.isSystemAdmin); - - console.log("requires Admin"); - return u.isSystemAdmin; */ - return false; + const u = await waitForUserToLoad(); + console.log("User Is Admin", u.isSystemAdmin); + return u.isSystemAdmin; } console.log(" route allowed"); - return true; } diff --git a/web/src/utils/validation.ts b/web/src/utils/validation.ts new file mode 100644 index 0000000..78503af --- /dev/null +++ b/web/src/utils/validation.ts @@ -0,0 +1,10 @@ +export const requiredRule = (value: string) => { + if (value) return true; + return "This field is required."; +}; + +export const emailRule = (value: string) => { + if (/.+@.+\..+/.test(value)) return true; + + return "E-mail must be valid."; +};