From 70ee0e4b8893345c024c1cf3016e347c01ac3d95 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 25 Jun 2024 13:08:07 -0700 Subject: [PATCH 1/4] Fixing dates for user_roles --- api/src/routes/role-router.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/api/src/routes/role-router.ts b/api/src/routes/role-router.ts index cd5edd0..7097217 100644 --- a/api/src/routes/role-router.ts +++ b/api/src/routes/role-router.ts @@ -2,6 +2,8 @@ import express, { Request, Response } from "express"; import { db as knex } from "../data"; import { isEmpty } from "lodash"; import { UserRole } from "src/data/models"; +import { DB_CLIENT } from "src/config"; +import { DateTime } from "luxon"; export const roleRouter = express.Router(); @@ -24,6 +26,25 @@ roleRouter.post("/user/:user_id", async (req: Request, res: Response) => { for (const role of roles) { role.create_user_id = req.user.id; + + if (DB_CLIENT == "oracledb") { + if (role.start_date) { + (role as any).start_date = knex.raw( + `TO_TIMESTAMP('${DateTime.fromISO(role.start_date).toFormat( + "yyyy-MM-dd HH:mm:ss" + )}', 'YYYY-MM-DD HH24:MI:SS')` + ); + } + + if (role.end_date) { + (role as any).end_date = knex.raw( + `TO_TIMESTAMP('${DateTime.fromISO(role.end_date).toFormat( + "yyyy-MM-dd HH:mm:ss" + )}', 'YYYY-MM-DD HH24:MI:SS')` + ); + } + } + if (isEmpty(role.department_code)) role.department_code = null; await trx("user_roles").insert(roleForInsert(role)); } From 5f01104c74492f30b10169f79e3ad7fbbaac2186 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 25 Jun 2024 14:02:22 -0700 Subject: [PATCH 2/4] Allowing workbox to load --- api/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/index.ts b/api/src/index.ts index 3ea79ed..4e330bc 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -29,7 +29,7 @@ app.use( "frame-ancestors": ["'self'"], "img-src": ["'self'", "data:"], "object-src": ["'none'"], - "script-src": ["'self'", "'unsafe-eval'"], + "script-src": ["'self'", "'unsafe-eval'", "https://storage.googleapis.com"], "script-src-attr": ["'none'"], "style-src": ["'self'", "https:", "'unsafe-inline'"], "worker-src": ["'self'", "blob:"], From b41f8d3d51e9687e8ef973d47cd7bd64d7681060 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 25 Jun 2024 14:29:45 -0700 Subject: [PATCH 3/4] Making dates save --- .../data/models/incident-attachment-model.ts | 6 +++-- api/src/data/models/incident-model.ts | 4 ++- api/src/routes/location-router.ts | 5 ++-- api/src/routes/report-router.ts | 24 ++++++----------- api/src/routes/role-router.ts | 27 +++---------------- api/src/routes/user-router.ts | 5 ++-- api/src/utils/formatters.ts | 20 +++++++++++++- 7 files changed, 44 insertions(+), 47 deletions(-) diff --git a/api/src/data/models/incident-attachment-model.ts b/api/src/data/models/incident-attachment-model.ts index eed93d7..8b47bb6 100644 --- a/api/src/data/models/incident-attachment-model.ts +++ b/api/src/data/models/incident-attachment-model.ts @@ -1,3 +1,5 @@ +import { Knex } from "knex"; + export interface IncidentAttachment { id: number; incident_id: number; @@ -8,6 +10,6 @@ export interface IncidentAttachment { file_size?: number; file?: Buffer; deleted_by_user_id?: string; - added_date: Date; - deleted_date: Date; + added_date: Date | Knex.Raw; + deleted_date: Date | Knex.Raw; } diff --git a/api/src/data/models/incident-model.ts b/api/src/data/models/incident-model.ts index 36ef9ae..80e4fde 100644 --- a/api/src/data/models/incident-model.ts +++ b/api/src/data/models/incident-model.ts @@ -1,3 +1,5 @@ +import { Knex } from "knex"; + export interface Incident { id: number; proxy_role_type_id?: number; @@ -9,7 +11,7 @@ export interface Incident { supervisor_email?: string; proxy_user_id?: string; description: string; - created_at: Date; + created_at: Date | Knex.Raw; attachments?: any[]; } diff --git a/api/src/routes/location-router.ts b/api/src/routes/location-router.ts index 2bb721c..54bd4ee 100644 --- a/api/src/routes/location-router.ts +++ b/api/src/routes/location-router.ts @@ -1,9 +1,10 @@ import express, { Request, Response } from "express"; + import { db as knex } from "../data"; export const locationRouter = express.Router(); -locationRouter.get("/", async (req: Request, res: Response) => { +locationRouter.get("/", async (_req: Request, res: Response) => { const list = await knex("locations"); - return res.json({ data: list }); + res.json({ data: list }); }); diff --git a/api/src/routes/report-router.ts b/api/src/routes/report-router.ts index 323a877..98eced2 100644 --- a/api/src/routes/report-router.ts +++ b/api/src/routes/report-router.ts @@ -1,10 +1,11 @@ import express, { Request, Response } from "express"; -import { IncidentService } from "../services"; import { DateTime } from "luxon"; +import { isArray } from "lodash"; + import { db as knex } from "../data"; -import { DB_CLIENT } from "../config"; +import { IncidentService } from "../services"; import { Incident, IncidentAttachment } from "../data/models"; -import { isArray } from "lodash"; +import { InsertableDate } from "../utils/formatters"; export const reportRouter = express.Router(); const db = new IncidentService(); @@ -37,7 +38,7 @@ reportRouter.post("/", async (req: Request, res: Response) => { const incident_type = await knex("incident_types").where({ name: eventType }).select("id").first(); const report = { - created_at: new Date(), + created_at: InsertableDate(new Date().toISOString()), description, department_code: "PSC", // TODO: make this lookup based on submitting person status_code: "REP", // Initial Report @@ -63,21 +64,13 @@ reportRouter.post("/", async (req: Request, res: Response) => { proxy_user_id: req.user.id, } as Incident; - if (DB_CLIENT == "oracledb") { - (report as any).created_at = knex.raw( - `TO_TIMESTAMP('${cVal.toFormat("yyyy-MM-dd HH:mm:ss")}', 'YYYY-MM-DD HH24:MI:SS')` - ); - //report.date = knex.raw(`TO_TIMESTAMP('${dVal.toFormat("yyyy-MM-dd HH:mm:ss")}', 'YYYY-MM-DD HH24:MI:SS')`); - } - console.log("INSERTING REPORT", report); const insertedReports = await db.create(report); let insertedId = insertedReports[0].id; -console.log("req.file", req.files) - + console.log("req.file", req.files); if (req.files && req.files.files) { let files = req.files.files; @@ -92,11 +85,10 @@ console.log("req.file", req.files) file_type: file.mimetype, file_size: file.size, file: file.data, - added_date: new Date(), + added_date: InsertableDate(new Date().toISOString()), } as IncidentAttachment; - - console.log("INSERT ATTACH", attachment) + console.log("INSERT ATTACH", attachment); await knex("incident_attachments").insert(attachment); } } diff --git a/api/src/routes/role-router.ts b/api/src/routes/role-router.ts index 7097217..b7d2c32 100644 --- a/api/src/routes/role-router.ts +++ b/api/src/routes/role-router.ts @@ -1,9 +1,8 @@ import express, { Request, Response } from "express"; import { db as knex } from "../data"; import { isEmpty } from "lodash"; -import { UserRole } from "src/data/models"; -import { DB_CLIENT } from "src/config"; -import { DateTime } from "luxon"; +import { UserRole } from "../data/models"; +import { InsertableDate } from "../utils/formatters"; export const roleRouter = express.Router(); @@ -16,8 +15,6 @@ roleRouter.post("/user/:user_id", async (req: Request, res: Response) => { const { user_id } = req.params; const { roles } = req.body; - console.log("SET ROLES", user_id, roles); - knex .transaction(async (trx) => { await trx("user_roles") @@ -26,24 +23,8 @@ roleRouter.post("/user/:user_id", async (req: Request, res: Response) => { for (const role of roles) { role.create_user_id = req.user.id; - - if (DB_CLIENT == "oracledb") { - if (role.start_date) { - (role as any).start_date = knex.raw( - `TO_TIMESTAMP('${DateTime.fromISO(role.start_date).toFormat( - "yyyy-MM-dd HH:mm:ss" - )}', 'YYYY-MM-DD HH24:MI:SS')` - ); - } - - if (role.end_date) { - (role as any).end_date = knex.raw( - `TO_TIMESTAMP('${DateTime.fromISO(role.end_date).toFormat( - "yyyy-MM-dd HH:mm:ss" - )}', 'YYYY-MM-DD HH24:MI:SS')` - ); - } - } + role.start_date = InsertableDate(role.start_date); + role.end_date = InsertableDate(role.end_date); if (isEmpty(role.department_code)) role.department_code = null; await trx("user_roles").insert(roleForInsert(role)); diff --git a/api/src/routes/user-router.ts b/api/src/routes/user-router.ts index 3d1d710..e96b3b0 100644 --- a/api/src/routes/user-router.ts +++ b/api/src/routes/user-router.ts @@ -1,7 +1,8 @@ import express, { Request, Response } from "express"; -import { DirectoryService, RoleService, UserService } from "../services"; -import { ReturnValidationErrors } from "../middleware"; import { param } from "express-validator"; + +import { RoleService, UserService } from "../services"; +import { ReturnValidationErrors } from "../middleware"; import { User } from "../data/models"; export const userRouter = express.Router(); diff --git a/api/src/utils/formatters.ts b/api/src/utils/formatters.ts index 9bc43b8..93ee4c4 100644 --- a/api/src/utils/formatters.ts +++ b/api/src/utils/formatters.ts @@ -1,7 +1,11 @@ import moment from "moment"; import _ from "lodash"; +import { DateTime } from "luxon"; import markdownit from "markdown-it"; +import { DB_CLIENT } from "../config"; +import { db as knex } from "../data"; + export function FormatDate(input: Date): string { return moment(input).format("YYYY-MM-DD"); } @@ -23,7 +27,7 @@ export function CleanInteger(input: any): number { } export function RenderMarkdown(input: string): { output: string; isMarkdown: boolean } { - let containsNewlines = RegExp(/.*\n/g).test(input) + let containsNewlines = RegExp(/.*\n/g).test(input); let containsHash = input.includes("#"); if (containsNewlines || containsHash) { @@ -39,3 +43,17 @@ export function RenderMarkdown(input: string): { output: string; isMarkdown: boo return { output: input, isMarkdown: false }; } + +export function InsertableDate(input: string | null) { + if (input) { + if (DB_CLIENT == "oracledb") { + return knex.raw( + `TO_TIMESTAMP('${DateTime.fromISO(input).toFormat("yyyy-MM-dd HH:mm:ss")}', 'YYYY-MM-DD HH24:MI:SS')` + ); + } + + return input; + } + + return null; +} From cd65929ece2792d825e2a23bee94dff57bc5e79f Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Tue, 25 Jun 2024 14:46:37 -0700 Subject: [PATCH 4/4] Fixing for local --- api/src/config.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/src/config.ts b/api/src/config.ts index 5b5ac02..20d70b2 100644 --- a/api/src/config.ts +++ b/api/src/config.ts @@ -18,8 +18,8 @@ dotenv.config({ path: path, override: true }); console.log(`LOADING ${NODE_ENV} CONFIG FROM ${path}`); export const API_PORT = process.env.API_PORT || "3000"; -// TODO: Remove this value once it's setup in the environment -export const FRONTEND_URL ="https://safety.gov.yk.ca" // process.env.FRONTEND_URL || ""; +// TODO: Remove this value once it's setup in the environment +export const FRONTEND_URL = process.env.FRONTEND_URL || "https://safety.gov.yk.ca"; export const AUTH_REDIRECT = process.env.AUTH_REDIRECT || ""; export const AUTH0_DOMAIN = (`${process.env.AUTH0_DOMAIN}` || "").replace(/\/$/, ""); export const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE; @@ -76,7 +76,7 @@ export const APPLICATION_NAME = process.env.APPLICATION_NAME || "YG Safety Porta export const DB_SCHEMA = process.env.DB_SCHEMA || ""; export const DB_USER_TABLE = process.env.DB_USER_TABLE || ""; -// TODO: Remove this value once it's setup in the environment -export const AD_CLIENT_ID = "15a9a9d8-2dcd-4cd8-965e-e2a7d044c7de"// process.env.AD_CLIENT_ID || ""; +// TODO: Remove this value once it's setup in the environment +export const AD_CLIENT_ID = "15a9a9d8-2dcd-4cd8-965e-e2a7d044c7de"; // process.env.AD_CLIENT_ID || ""; export const AD_CLIENT_SECRET = process.env.AD_CLIENT_SECRET || ""; export const AD_TENANT_ID = process.env.AD_TENANT_ID || "";