Skip to content

Commit

Permalink
Updated navigational flow for all user roles
Browse files Browse the repository at this point in the history
Folder structure updated
  • Loading branch information
kyle1morel committed May 28, 2024
1 parent 62b6bce commit 09ed184
Show file tree
Hide file tree
Showing 53 changed files with 1,031 additions and 563 deletions.
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<{ activityId: string }>, res: Response, next: NextFunction) => {
try {
const response = await enquiryService.getEnquiry(req.params.activityId);
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
25 changes: 20 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 {
YesNo,
YesNoUnsure
} 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 @@ -325,6 +325,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 +347,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
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
1 change: 1 addition & 0 deletions app/src/db/models/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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
1 change: 1 addition & 0 deletions app/src/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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
21 changes: 18 additions & 3 deletions app/src/routes/v1/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ import type { NextFunction, Request, Response } from '../../interfaces/IExpress'
const router = express.Router();
router.use(requireSomeAuth);

// Submission create draft endpoint
/** Gets a list of enquiries */
router.get('/', (req: Request, res: Response, next: NextFunction): void => {
enquiryController.getEnquiries(req, res, next);
});

/** Gets a specific enquiry */
router.get('/:enquiryId', (req: Request, res: Response, next: NextFunction): void => {
enquiryController.getEnquiry(req, res, next);
});

/** Deletes an enquiry */
router.delete('/:enquiryId', (req: Request, res: Response, next: NextFunction): void => {
enquiryController.deleteEnquiry(req, res, next);
});

/** Creates an enquiry with Draft status */
router.put('/draft', (req: Request, res: Response, next: NextFunction): void => {
enquiryController.createDraft(req, res, next);
});

// Submission update draft endpoint
router.put('/draft/:activityId', (req: Request, res: Response, next: NextFunction): void => {
/** Updates an enquiry with Draft status */
router.put('/draft/:enquiryId', (req: Request, res: Response, next: NextFunction): void => {
enquiryController.updateDraft(req, res, next);
});

Expand Down
3 changes: 1 addition & 2 deletions app/src/routes/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { currentUser } from '../../middleware/authentication';
import { hasAccess } from '../../middleware/authorization';

import express from 'express';
import submission from './submission';
import document from './document';
Expand All @@ -11,7 +11,6 @@ import user from './user';

const router = express.Router();
router.use(currentUser);
router.use(hasAccess);

// Base v1 Responder
router.get('/', (_req, res) => {
Expand Down
26 changes: 18 additions & 8 deletions app/src/routes/v1/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { NextFunction, Request, Response } from '../../interfaces/IExpress'
const router = express.Router();
router.use(requireSomeAuth);

// Submissions endpoint
/** Gets a list of submissions */
router.get('/', (req: Request, res: Response, next: NextFunction): void => {
submissionController.getSubmissions(req, res, next);
});

// Statistics endpoint
/** Gets submission statistics*/
router.get(
'/statistics',
submissionValidator.getStatistics,
Expand All @@ -22,30 +22,40 @@ router.get(
}
);

// Submission create draft endpoint
/** Creates a submission with Draft status */
router.put('/draft', (req: Request, res: Response, next: NextFunction): void => {
submissionController.createDraft(req, res, next);
});

// Submission update draft endpoint
router.put('/draft/:activityId', (req: Request, res: Response, next: NextFunction): void => {
/** Updates a submission with Draft status */
router.put('/draft/:submissionId', (req: Request, res: Response, next: NextFunction): void => {
submissionController.updateDraft(req, res, next);
});

// Submission create endpoint
/** Creates a submission */
router.put('/', submissionValidator.createSubmission, (req: Request, res: Response, next: NextFunction): void => {
submissionController.createSubmission(req, res, next);
});

/** Deletes a submission */
router.delete(
'/:submissionId',
submissionValidator.deleteSubmission,
(req: Request, res: Response, next: NextFunction): void => {
submissionController.deleteSubmission(req, res, next);
}
);

/** Gets a specific submission */
router.get(
'/:activityId',
'/:submissionId',
submissionValidator.getSubmission,
(req: Request, res: Response, next: NextFunction): void => {
submissionController.getSubmission(req, res, next);
}
);

// Submission update endpoint
/** Updates a submission*/
router.put(
'/:submissionId',
submissionValidator.updateSubmission,
Expand Down
17 changes: 17 additions & 0 deletions app/src/services/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ const service = {
return activity.fromPrismaModel(response);
},

/**
* @function deleteActivity
* Delete an activity
* This action will cascade delete across all linked items
* @param {string} activityId Unique activity ID
* @returns {Promise<Activity | null>} The result of running the findFirst operation
*/
deleteActivity: async (activityId: string) => {
const response = await prisma.activity.delete({
where: {
activity_id: activityId
}
});

return activity.fromPrismaModel(response);
},

/**
* @function getActivity
* Get an activity
Expand Down
62 changes: 62 additions & 0 deletions app/src/services/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,68 @@ const service = {
return enquiry.fromPrismaModel(response);
},

/**
* @function deleteEnquiry
* Deletes the enquiry, followed by the associated activity
* This action will cascade delete across all linked items
* @param {string} enquiryId Enquiry ID
* @returns {Promise<Enquiry>} The result of running the delete operation
*/
deleteEnquiry: async (enquiryId: string) => {
const response = await prisma.$transaction(async (trx) => {
const del = await trx.enquiry.delete({
where: {
enquiry_id: enquiryId
}
});

await trx.activity.delete({
where: {
activity_id: del.activity_id
}
});

return del;
});

return enquiry.fromPrismaModel(response);
},

/**
* @function getEnquiries
* Gets a list of enquiries
* @returns {Promise<(Enquiry | null)[]>} The result of running the findMany operation
*/
getEnquiries: async () => {
try {
const result = await prisma.enquiry.findMany({ include: { user: true } });

return result.map((x) => enquiry.fromPrismaModelWithUser(x));
} catch (e: unknown) {
throw e;
}
},

/**
* @function getEnquiry
* Gets a specific enquiry from the PCNS database
* @param {string} activityId PCNS Activity ID
* @returns {Promise<Enquiry | null>} The result of running the findFirst operation
*/
getEnquiry: async (activityId: string) => {
try {
const result = await prisma.enquiry.findFirst({
where: {
activity_id: activityId
}
});

return result ? enquiry.fromPrismaModel(result) : null;
} catch (e: unknown) {
throw e;
}
},

/**
* @function updateEnquiry
* Updates a specific enquiry
Expand Down
Loading

0 comments on commit 09ed184

Please sign in to comment.