Skip to content

Commit

Permalink
Merge pull request #181 from bcgov/chore/draft-endpoints
Browse files Browse the repository at this point in the history
Changes to Submission/Enquiry draft saving endpoints
  • Loading branch information
kyle1morel authored Oct 7, 2024
2 parents 0fae423 + 6e2ff06 commit 4279c4f
Show file tree
Hide file tree
Showing 14 changed files with 550 additions and 490 deletions.
62 changes: 41 additions & 21 deletions app/src/controllers/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,6 @@ const controller = {
};
},

createDraft: async (req: Request<never, never, EnquiryIntake>, res: Response, next: NextFunction) => {
try {
const enquiry = await controller.generateEnquiryData(req);

// Create new enquiry
const result = await enquiryService.createEnquiry({
...enquiry,
...generateCreateStamps(req.currentContext)
});

res.status(201).json({ activityId: result.activityId, enquiryId: result.enquiryId });
} catch (e: unknown) {
next(e);
}
},

deleteEnquiry: async (req: Request<{ enquiryId: string }>, res: Response, next: NextFunction) => {
try {
const response = await enquiryService.deleteEnquiry(req.params.enquiryId);
Expand Down Expand Up @@ -137,6 +121,32 @@ const controller = {
}
},

submitDraft: async (req: Request<never, never, EnquiryIntake>, res: Response, next: NextFunction) => {
try {
const update = req.body.activityId && req.body.enquiryId;

const enquiry = await controller.generateEnquiryData(req);

let result;
if (update) {
result = await enquiryService.updateEnquiry({
...enquiry,
...generateUpdateStamps(req.currentContext)
} as Enquiry);
} else {
// Create new enquiry
result = await enquiryService.createEnquiry({
...enquiry,
...generateCreateStamps(req.currentContext)
});
}

res.status(201).json({ activityId: result.activityId, enquiryId: result.enquiryId });
} catch (e: unknown) {
next(e);
}
},

updateEnquiry: async (req: Request<never, never, Enquiry>, res: Response, next: NextFunction) => {
try {
const result = await enquiryService.updateEnquiry({
Expand All @@ -152,13 +162,23 @@ const controller = {

updateDraft: async (req: Request<never, never, EnquiryIntake>, res: Response, next: NextFunction) => {
try {
const update = req.body.activityId && req.body.enquiryId;

const enquiry = await controller.generateEnquiryData(req);

// Update enquiry
const result = await enquiryService.updateEnquiry({
...(enquiry as Enquiry),
...generateUpdateStamps(req.currentContext)
});
let result;
if (update) {
result = await enquiryService.updateEnquiry({
...enquiry,
...generateUpdateStamps(req.currentContext)
} as Enquiry);
} else {
// Create new enquiry
result = await enquiryService.createEnquiry({
...enquiry,
...generateCreateStamps(req.currentContext)
});
}

res.status(200).json({ activityId: result.activityId, enquiryId: result.enquiryId });
} catch (e: unknown) {
Expand Down
182 changes: 105 additions & 77 deletions app/src/controllers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ import type {
} from '../types';

const controller = {
/**
* @function assignPriority
* Assigns a priority level to a submission based on given criteria
* Criteria defined below
*/
assignPriority: (submission: Partial<Submission>) => {
const matchesPriorityOneCriteria = // Priority 1 Criteria:
submission.singleFamilyUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED || // 1. More than 50 units (any)
submission.singleFamilyUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.multiFamilyUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED ||
submission.multiFamilyUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.otherUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED ||
submission.otherUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.hasRentalUnits === 'Yes' || // 2. Supports Rental Units
submission.financiallySupportedBC === 'Yes' || // 3. Social Housing
submission.financiallySupportedIndigenous === 'Yes'; // 4. Indigenous Led

const matchesPriorityTwoCriteria = // Priority 2 Criteria:
submission.singleFamilyUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 1. Single Family >= 10 Units
submission.multiFamilyUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 2. Has 1 or more MultiFamily Units
submission.multiFamilyUnits === NumResidentialUnits.ONE_TO_NINE ||
submission.otherUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 3. Has 1 or more Other Units
submission.otherUnits === NumResidentialUnits.ONE_TO_NINE;

if (matchesPriorityOneCriteria) {
submission.queuePriority = 1;
} else if (matchesPriorityTwoCriteria) {
submission.queuePriority = 2;
} else {
// Prioriy 3 Criteria:
submission.queuePriority = 3; // Everything Else
}
},

checkAndStoreNewSubmissions: async () => {
const cfg = config.get('server.chefs.forms') as ChefsFormConfig;

Expand Down Expand Up @@ -308,35 +342,26 @@ const controller = {
return submissionData;
},

getActivityIds: async (req: Request, res: Response, next: NextFunction) => {
/**
* @function emailConfirmation
* Send an email with the confirmation of submission
*/
emailConfirmation: async (req: Request<never, never, Email>, res: Response, next: NextFunction) => {
try {
let response = await submissionService.getSubmissions();
if (req.currentAuthorization?.attributes.includes('scope:self')) {
response = response.filter((x: Submission) => x?.submittedBy === getCurrentUsername(req.currentContext));
}
res.status(200).json(response.map((x) => x.activityId));
const { data, status } = await emailService.email(req.body);
res.status(status).json(data);
} catch (e: unknown) {
next(e);
}
},

createDraft: async (req: Request<never, never, SubmissionIntake>, res: Response, next: NextFunction) => {
getActivityIds: async (req: Request, res: Response, next: NextFunction) => {
try {
const { submission, appliedPermits, investigatePermits } = await controller.generateSubmissionData(
req,
req.body.submit ? IntakeStatus.SUBMITTED : IntakeStatus.DRAFT
);

// Create new submission
const result = await submissionService.createSubmission({
...submission,
...generateCreateStamps(req.currentContext)
});

// Create each permit
await Promise.all(appliedPermits.map(async (x: Permit) => await permitService.createPermit(x)));
await Promise.all(investigatePermits.map(async (x: Permit) => await permitService.createPermit(x)));
res.status(201).json({ activityId: result.activityId, submissionId: result.submissionId });
let response = await submissionService.getSubmissions();
if (req.currentAuthorization?.attributes.includes('scope:self')) {
response = response.filter((x: Submission) => x?.submittedBy === getCurrentUsername(req.currentContext));
}
res.status(200).json(response.map((x) => x.activityId));
} catch (e: unknown) {
next(e);
}
Expand Down Expand Up @@ -443,25 +468,75 @@ const controller = {
}
},

submitDraft: async (req: Request<never, never, SubmissionIntake>, res: Response, next: NextFunction) => {
try {
const update = req.body.activityId && req.body.submissionId;

const { submission, appliedPermits, investigatePermits } = await controller.generateSubmissionData(
req,
IntakeStatus.SUBMITTED
);

let result;

if (update) {
// Update submission
result = await submissionService.updateSubmission({
...submission,
...generateUpdateStamps(req.currentContext)
});
} else {
// Create new submission
result = await submissionService.createSubmission({
...submission,
...generateCreateStamps(req.currentContext)
});
}

// Remove already existing permits for this activity
await permitService.deletePermitsByActivity(submission.activityId);

// Create each permit
await Promise.all(appliedPermits.map((x: Permit) => permitService.createPermit(x)));
await Promise.all(investigatePermits.map((x: Permit) => permitService.createPermit(x)));

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

updateDraft: async (req: Request<never, never, SubmissionIntake>, res: Response, next: NextFunction) => {
try {
const update = req.body.activityId && req.body.submissionId;

const { submission, appliedPermits, investigatePermits } = await controller.generateSubmissionData(
req,
req.body.submit ? IntakeStatus.SUBMITTED : IntakeStatus.DRAFT
IntakeStatus.DRAFT
);

// Update submission
const result = await submissionService.updateSubmission({
...submission,
...generateUpdateStamps(req.currentContext)
});
let result;

if (update) {
// Update submission
result = await submissionService.updateSubmission({
...submission,
...generateUpdateStamps(req.currentContext)
});
} else {
// Create new submission
result = await submissionService.createSubmission({
...submission,
...generateCreateStamps(req.currentContext)
});
}

// Remove already existing permits for this activity
await permitService.deletePermitsByActivity(submission.activityId);

// Create each permit
await Promise.all(appliedPermits.map(async (x: Permit) => await permitService.createPermit(x)));
await Promise.all(investigatePermits.map(async (x: Permit) => await permitService.createPermit(x)));
await Promise.all(appliedPermits.map((x: Permit) => permitService.createPermit(x)));
await Promise.all(investigatePermits.map((x: Permit) => permitService.createPermit(x)));

res.status(200).json({ activityId: result.activityId, submissionId: result.submissionId });
} catch (e: unknown) {
Expand Down Expand Up @@ -496,53 +571,6 @@ const controller = {
} catch (e: unknown) {
next(e);
}
},

/**
* @function emailConfirmation
* Send an email with the confirmation of submission
*/
emailConfirmation: async (req: Request<never, never, Email>, res: Response, next: NextFunction) => {
try {
const { data, status } = await emailService.email(req.body);
res.status(status).json(data);
} catch (e: unknown) {
next(e);
}
},

/**
* @function assignPriority
* Assigns a priority level to a submission based on given criteria
* Criteria defined below
*/
assignPriority: (submission: Partial<Submission>) => {
const matchesPriorityOneCriteria = // Priority 1 Criteria:
submission.singleFamilyUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED || // 1. More than 50 units (any)
submission.singleFamilyUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.multiFamilyUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED ||
submission.multiFamilyUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.otherUnits === NumResidentialUnits.GREATER_THAN_FIVE_HUNDRED ||
submission.otherUnits === NumResidentialUnits.FIFTY_TO_FIVE_HUNDRED ||
submission.hasRentalUnits === 'Yes' || // 2. Supports Rental Units
submission.financiallySupportedBC === 'Yes' || // 3. Social Housing
submission.financiallySupportedIndigenous === 'Yes'; // 4. Indigenous Led

const matchesPriorityTwoCriteria = // Priority 2 Criteria:
submission.singleFamilyUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 1. Single Family >= 10 Units
submission.multiFamilyUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 2. Has 1 or more MultiFamily Units
submission.multiFamilyUnits === NumResidentialUnits.ONE_TO_NINE ||
submission.otherUnits === NumResidentialUnits.TEN_TO_FOURTY_NINE || // 3. Has 1 or more Other Units
submission.otherUnits === NumResidentialUnits.ONE_TO_NINE;

if (matchesPriorityOneCriteria) {
submission.queuePriority = 1;
} else if (matchesPriorityTwoCriteria) {
submission.queuePriority = 2;
} else {
// Prioriy 3 Criteria:
submission.queuePriority = 3; // Everything Else
}
}
};

Expand Down
34 changes: 10 additions & 24 deletions app/src/routes/v1/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,12 @@ import { Action, Resource } from '../../utils/enums/application';
import { enquiryValidator } from '../../validators';

import type { NextFunction, Request, Response } from 'express';
import type { Enquiry, EnquiryIntake, Middleware } from '../../types';
import type { Enquiry, EnquiryIntake } from '../../types';

const router = express.Router();
router.use(requireSomeAuth);
router.use(requireSomeGroup);

const decideValidation = (validator: Middleware) => {
return (req: Request, _res: Response, next: NextFunction) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const body: any = req.body;

if (body.submit) {
return validator(req, _res, next);
} else {
return next();
}
};
};

/** Gets a list of enquiries */
router.get(
'/',
Expand Down Expand Up @@ -61,24 +48,23 @@ router.delete(
}
);

/** Creates an enquiry with Draft status */
/** Creates or updates an intake and set status to Draft */
router.put(
'/draft',
'/draft/',
hasAuthorization(Resource.ENQUIRY, Action.CREATE),
decideValidation(enquiryValidator.createDraft),
enquiryValidator.createOrUpdateDraft,
(req: Request<never, never, EnquiryIntake>, res: Response, next: NextFunction): void => {
enquiryController.createDraft(req, res, next);
enquiryController.updateDraft(req, res, next);
}
);

/** Updates an enquiry with Draft status */
/** Creates or updates an intake and set status to Submitted */
router.put(
'/draft/:enquiryId',
hasAuthorization(Resource.ENQUIRY, Action.UPDATE),
hasAccess('enquiryId'),
decideValidation(enquiryValidator.updateDraft),
'/draft/submit',
hasAuthorization(Resource.ENQUIRY, Action.CREATE),
enquiryValidator.createOrUpdateDraft,
(req: Request<never, never, EnquiryIntake>, res: Response, next: NextFunction): void => {
enquiryController.updateDraft(req, res, next);
enquiryController.submitDraft(req, res, next);
}
);

Expand Down
Loading

0 comments on commit 4279c4f

Please sign in to comment.