Skip to content

Commit

Permalink
Merge pull request #5 from icefoganalytics/main
Browse files Browse the repository at this point in the history
Demo bomb fixes
  • Loading branch information
datajohnson authored Jun 25, 2024
2 parents bbef5b8 + cd65929 commit ae77bfd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 32 deletions.
8 changes: 4 additions & 4 deletions api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 || "";
6 changes: 4 additions & 2 deletions api/src/data/models/incident-attachment-model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Knex } from "knex";

export interface IncidentAttachment {
id: number;
incident_id: number;
Expand All @@ -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<any>;
deleted_date: Date | Knex.Raw<any>;
}
4 changes: 3 additions & 1 deletion api/src/data/models/incident-model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Knex } from "knex";

export interface Incident {
id: number;
proxy_role_type_id?: number;
Expand All @@ -9,7 +11,7 @@ export interface Incident {
supervisor_email?: string;
proxy_user_id?: string;
description: string;
created_at: Date;
created_at: Date | Knex.Raw<any>;

attachments?: any[];
}
2 changes: 1 addition & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:"],
Expand Down
5 changes: 3 additions & 2 deletions api/src/routes/location-router.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
24 changes: 8 additions & 16 deletions api/src/routes/report-router.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down
8 changes: 5 additions & 3 deletions api/src/routes/role-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +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 { UserRole } from "../data/models";
import { InsertableDate } from "../utils/formatters";

export const roleRouter = express.Router();

Expand All @@ -14,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")
Expand All @@ -24,6 +23,9 @@ roleRouter.post("/user/:user_id", async (req: Request, res: Response) => {

for (const role of roles) {
role.create_user_id = req.user.id;
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));
}
Expand Down
5 changes: 3 additions & 2 deletions api/src/routes/user-router.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
20 changes: 19 additions & 1 deletion api/src/utils/formatters.ts
Original file line number Diff line number Diff line change
@@ -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");
}
Expand All @@ -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) {
Expand All @@ -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;
}

0 comments on commit ae77bfd

Please sign in to comment.