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

Redesigned entry and login flow #83

Merged
merged 12 commits into from
Jun 5, 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
3 changes: 2 additions & 1 deletion app/src/components/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const AccessRoles = Object.freeze({
PCNS_ADMIN: 'PCNS_ADMIN',
PCNS_DEVELOPER: 'PCNS_DEVELOPER',
PCNS_NAVIGATOR: 'PCNS_NAVIGATOR',
PCNS_OTHER: 'PCNS_OTHER'
PCNS_PROPONENT: 'PCNS_PROPONENT',
PCNS_SUPERVISOR: 'PCNS_SUPERVISOR'
});

/** Current initiative types*/
Expand Down
41 changes: 38 additions & 3 deletions app/src/controllers/enquiry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NIL, v4 as uuidv4 } from 'uuid';

import { Initiatives, NOTE_TYPE_LIST } from '../components/constants';
import { getCurrentIdentity } from '../components/utils';
import { INTAKE_STATUS_LIST, Initiatives, NOTE_TYPE_LIST } from '../components/constants';
import { getCurrentIdentity, isTruthy } from '../components/utils';
import { activityService, enquiryService, noteService, userService } from '../services';

import type { NextFunction, Request, Response } from '../interfaces/IExpress';
Expand Down Expand Up @@ -68,7 +68,8 @@ const controller = {
activityId: activityId,
submittedAt: data.submittedAt ?? new Date().toISOString(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
submittedBy: (req.currentUser?.tokenPayload as any)?.idir_username
submittedBy: (req.currentUser?.tokenPayload as any)?.idir_username,
intakeStatus: data.submit ? INTAKE_STATUS_LIST.SUBMITTED : INTAKE_STATUS_LIST.DRAFT
};
},

Expand All @@ -93,6 +94,40 @@ const controller = {
}
},

deleteEnquiry: async (req: Request<{ enquiryId: string }>, res: Response, next: NextFunction) => {
try {
const response = await enquiryService.deleteEnquiry(req.params.enquiryId);
res.status(200).json(response);
} catch (e: unknown) {
next(e);
}
},

getEnquiries: async (req: Request<never, { self?: string }>, res: Response, next: NextFunction) => {
try {
// Pull from PCNS database
let response = await enquiryService.getEnquiries();

if (isTruthy(req.query.self)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response = response.filter((x) => x?.submittedBy === (req.currentUser?.tokenPayload as any)?.idir_username);
}

res.status(200).json(response);
} catch (e: unknown) {
next(e);
}
},

getEnquiry: async (req: Request<{ enquiryId: string }>, res: Response, next: NextFunction) => {
try {
const response = await enquiryService.getEnquiry(req.params.enquiryId);
res.status(200).json(response);
} catch (e: unknown) {
next(e);
}
},

updateDraft: async (req: Request, res: Response, next: NextFunction) => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
1 change: 1 addition & 0 deletions app/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { default as enquiryController } from './enquiry';
export { default as noteController } from './note';
export { default as permitController } from './permit';
export { default as roadmapController } from './roadmap';
export { default as ssoController } from './sso';
export { default as submissionController } from './submission';
export { default as userController } from './user';
26 changes: 26 additions & 0 deletions app/src/controllers/sso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ssoService } from '../services';

import type { NextFunction, Request, Response } from '../interfaces/IExpress';

const controller = {
requestBasicAccess: async (req: Request, res: Response, next: NextFunction) => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response = await ssoService.requestBasicAccess((req.currentUser?.tokenPayload as any).preferred_username);
res.status(response.status).json(response.data);
} catch (e: unknown) {
next(e);
}
},

getRoles: async (req: Request, res: Response, next: NextFunction) => {
try {
const response = await ssoService.getRoles();
res.status(response.status).json(response.data);
} catch (e: unknown) {
next(e);
}
}
};

export default controller;
26 changes: 21 additions & 5 deletions app/src/controllers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
YES_NO,
YES_NO_UNSURE
} from '../components/constants';
import { camelCaseToTitleCase, deDupeUnsure, getCurrentIdentity, toTitleCase } from '../components/utils';
import { camelCaseToTitleCase, deDupeUnsure, getCurrentIdentity, isTruthy, toTitleCase } from '../components/utils';
import { activityService, submissionService, permitService, userService } from '../services';

import type { NextFunction, Request, Response } from '../interfaces/IExpress';
Expand Down Expand Up @@ -223,6 +223,7 @@ const controller = {
location = {
naturalDisaster: data.location.naturalDisaster === YES_NO.YES,
projectLocation: data.location.projectLocation,
projectLocationDescription: data.location.projectLocationDescription,
locationPIDs: data.location.ltsaPIDLookup,
latitude: data.location.latitude,
longitude: data.location.longitude,
Expand Down Expand Up @@ -325,6 +326,15 @@ const controller = {
}
},

deleteSubmission: async (req: Request<{ submissionId: string }>, res: Response, next: NextFunction) => {
try {
const response = await submissionService.deleteSubmission(req.params.submissionId);
res.status(200).json(response);
} catch (e: unknown) {
next(e);
}
},

getStatistics: async (
req: Request<never, { dateFrom: string; dateTo: string; monthYear: string; userId: string }>,
res: Response,
Expand All @@ -338,22 +348,28 @@ const controller = {
}
},

getSubmission: async (req: Request<{ activityId: string }>, res: Response, next: NextFunction) => {
getSubmission: async (req: Request<{ submissionId: string }>, res: Response, next: NextFunction) => {
try {
const response = await submissionService.getSubmission(req.params.activityId);
const response = await submissionService.getSubmission(req.params.submissionId);
res.status(200).json(response);
} catch (e: unknown) {
next(e);
}
},

getSubmissions: async (req: Request, res: Response, next: NextFunction) => {
getSubmissions: async (req: Request<never, { self?: string }>, res: Response, next: NextFunction) => {
try {
// Check for and store new submissions in CHEFS
await controller.checkAndStoreNewSubmissions();

// Pull from PCNS database
const response = await submissionService.getSubmissions();
let response = await submissionService.getSubmissions();

if (isTruthy(req.query.self)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response = response.filter((x) => x?.submittedBy === (req.currentUser?.tokenPayload as any)?.idir_username);
}

res.status(200).json(response);
} catch (e: unknown) {
next(e);
Expand Down
1 change: 1 addition & 0 deletions app/src/db/migrations/20240506000000_004-shas-intake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function up(knex: Knex): Promise<void> {
table.text('other_units_description');
table.text('rental_units');
table.text('project_location');
table.text('project_location_description');
table.text('locality');
table.text('province');
table.text('has_applied_provincial_permits');
Expand Down
11 changes: 3 additions & 8 deletions app/src/db/migrations/20240516000000_005-shas-enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function up(knex: Knex): Promise<void> {
table.text('related_activity_id');
table.text('enquiry_description');
table.text('apply_for_permit_connect');
table.text('intake_status');
stamps(knex, table);
})
)
Expand Down Expand Up @@ -73,14 +74,8 @@ export async function down(knex: Knex): Promise<void> {
// Drop public schema table triggers
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_enquiry_trigger ON "enquiry"'))

// Revert table alters
.then(() =>
knex.schema.alterTable('submission', function (table) {
table.dropColumn('contact_first_name');
table.dropColumn('contact_last_name');
})
)

// Not reverting submission table alters
// This migration is destructive and would result in data loss
// Drop public schema tables
.then(() => knex.schema.dropTableIfExists('enquiry'))
);
Expand Down
5 changes: 4 additions & 1 deletion app/src/db/models/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default {
is_related: input.isRelated,
related_activity_id: input.relatedActivityId,
enquiry_description: input.enquiryDescription,
apply_for_permit_connect: input.applyForPermitConnect
apply_for_permit_connect: input.applyForPermitConnect,
intake_status: input.intakeStatus
};
},

Expand All @@ -52,6 +53,8 @@ export default {
relatedActivityId: input.related_activity_id,
enquiryDescription: input.enquiry_description,
applyForPermitConnect: input.apply_for_permit_connect,
intakeStatus: input.intake_status,
updatedAt: input.updated_at?.toISOString() as string,
user: null
};
},
Expand Down
3 changes: 3 additions & 0 deletions app/src/db/models/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default {
other_units_description: input.otherUnitsDescription,
rental_units: input.rentalUnits,
project_location: input.projectLocation,
project_location_description: input.projectLocationDescription,
locality: input.locality,
province: input.province,
has_applied_provincial_permits: input.hasAppliedProvincialPermits,
Expand Down Expand Up @@ -129,6 +130,7 @@ export default {
otherUnitsDescription: input.other_units_description,
rentalUnits: input.rental_units,
projectLocation: input.project_location,
projectLocationDescription: input.project_location_description,
locality: input.locality,
province: input.province,
hasAppliedProvincialPermits: input.has_applied_provincial_permits,
Expand All @@ -138,6 +140,7 @@ export default {
housingCoopDescription: input.housing_coop_description,
contactFirstName: input.contact_first_name,
contactLastName: input.contact_last_name,
updatedAt: input.updated_at?.toISOString() as string,
user: null
};
},
Expand Down
2 changes: 2 additions & 0 deletions app/src/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ model submission {
other_units_description String?
rental_units String?
project_location String?
project_location_description String?
locality String?
province String?
has_applied_provincial_permits String?
Expand Down Expand Up @@ -236,6 +237,7 @@ model enquiry {
related_activity_id String?
enquiry_description String?
apply_for_permit_connect String?
intake_status String?
created_by String? @default("00000000-0000-0000-0000-000000000000")
created_at DateTime? @default(now()) @db.Timestamptz(6)
updated_by String?
Expand Down
112 changes: 112 additions & 0 deletions app/src/routes/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable */
// @ts-nocheck

/*
* https://github.com/labithiotis/express-list-routes/blob/main/index.js
* Logs colour coded routes for given router
*/

import path from 'path';

const defaultOptions = {
prefix: '',
spacer: 7,
logger: console.info,
color: true
};

const COLORS = {
yellow: 33,
green: 32,
blue: 34,
red: 31,
grey: 90,
magenta: 35,
clear: 39
};

const spacer = (x) => (x > 0 ? [...new Array(x)].map(() => ' ').join('') : '');

const colorText = (color: number, string: string) => `\u001b[${color}m${string}\u001b[${COLORS.clear}m`;

function colorMethod(method: string) {
switch (method) {
case 'POST':
return colorText(COLORS.yellow, method);
case 'GET':
return colorText(COLORS.green, method);
case 'PUT':
return colorText(COLORS.blue, method);
case 'DELETE':
return colorText(COLORS.red, method);
case 'PATCH':
return colorText(COLORS.grey, method);
default:
return method;
}
}

function getPathFromRegex(regexp: string) {
return regexp
.toString()
.replace('/^', '')
.replace('?(?=\\/|$)/i', '')
.replace(/\\\//g, '/')
.replace('(?:/(?=$))', '');
}

function combineStacks(acc: any, stack: any) {
if (stack.handle.stack) {
const routerPath = getPathFromRegex(stack.regexp);
return [...acc, ...stack.handle.stack.map((s: any) => ({ routerPath, ...s }))];
}
return [...acc, stack];
}

function getStacks(app: any) {
// Express 4
if (app._router && app._router.stack) {
return app._router.stack.reduce(combineStacks, []);
}

// Express 4 Router
if (app.stack) {
return app.stack.reduce(combineStacks, []);
}

// Express 5
if (app.router && app.router.stack) {
return app.router.stack.reduce(combineStacks, []);
}

return [];
}

export function expressListRoutes(app: any, opts: any) {
const stacks = getStacks(app);
const options = { ...defaultOptions, ...opts };
const paths = [];

if (stacks) {
for (const stack of stacks) {
if (stack.route) {
const routeLogged: any = {};
for (const route of stack.route.stack) {
const method = route.method ? route.method.toUpperCase() : null;
if (!routeLogged[method] && method) {
const stackMethod = options.color ? colorMethod(method) : method;
const stackSpace = spacer(options.spacer - method.length);
const stackPath = path
.normalize([options.prefix, stack.routerPath, stack.route.path, route.path].filter((s) => !!s).join(''))
.trim();
options.logger(stackMethod, stackSpace, stackPath);
paths.push({ method, path: stackPath });
routeLogged[method] = true;
}
}
}
}
}

return paths;
}
Loading
Loading