Skip to content

Commit

Permalink
Merge pull request #201 from bcgov/feature/draft-saving
Browse files Browse the repository at this point in the history
Improved draft saving
  • Loading branch information
kyle1morel authored Nov 27, 2024
2 parents 488db97 + 407d268 commit cf94292
Show file tree
Hide file tree
Showing 37 changed files with 995 additions and 826 deletions.
87 changes: 19 additions & 68 deletions app/src/controllers/enquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const controller = {

let basic;

// Create applicant information
if (data.basic) {
basic = {
enquiryType: data.basic.enquiryType,
Expand All @@ -66,6 +65,25 @@ const controller = {
};
},

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

// Create or update contacts
await contactService.upsertContacts(enquiry.activityId, req.body.contacts, req.currentContext);

// 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 @@ -119,39 +137,6 @@ 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, IntakeStatus.SUBMITTED);

let result;

await contactService.upsertContacts(enquiry.activityId, req.body.contacts, req.currentContext);

if (update) {
result = await enquiryService.updateEnquiry({
...enquiry,
...generateUpdateStamps(req.currentContext)
} as Enquiry);

if (!result) {
return res.status(404).json({ message: 'Enquiry not found' });
}
} 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 {
await contactService.upsertContacts(req.body.activityId, req.body.contacts, req.currentContext);
Expand All @@ -171,40 +156,6 @@ 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, IntakeStatus.DRAFT);

let result;
if (update) {
result = await enquiryService.updateEnquiry({
...enquiry,
...generateUpdateStamps(req.currentContext)
} as Enquiry);

if (!result) {
return res.status(404).json({ message: 'Enquiry not found' });
}
} else {
// Create new enquiry
result = await enquiryService.createEnquiry({
...enquiry,
...generateCreateStamps(req.currentContext)
});
}

if (!result) {
return res.status(404).json({ message: 'Enquiry not found' });
}

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

updateIsDeletedFlag: async (
req: Request<{ enquiryId: string }, never, { isDeleted: boolean }>,
res: Response,
Expand Down
164 changes: 95 additions & 69 deletions app/src/controllers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateCreateStamps, generateUpdateStamps } from '../db/utils/utils';
import {
activityService,
contactService,
draftService,
emailService,
enquiryService,
submissionService,
Expand All @@ -13,6 +14,7 @@ import {
import { BasicResponse, Initiative } from '../utils/enums/application';
import {
ApplicationStatus,
DraftCode,
IntakeStatus,
NumResidentialUnits,
PermitNeeded,
Expand All @@ -26,14 +28,15 @@ import type { NextFunction, Request, Response } from 'express';
import type {
ChefsFormConfig,
ChefsFormConfigData,
Submission,
ChefsSubmissionExport,
Permit,
CurrentContext,
Draft,
Email,
Permit,
StatisticsFilters,
Submission,
SubmissionIntake,
SubmissionSearchParameters,
CurrentContext
SubmissionSearchParameters
} from '../types';

const controller = {
Expand Down Expand Up @@ -228,12 +231,10 @@ const controller = {
notStored.map((x) => x.permits?.map(async (y) => await permitService.createPermit(y)));
},

generateSubmissionData: async (req: Request<never, never, SubmissionIntake>, intakeStatus: string) => {
const data = req.body;

generateSubmissionData: async (data: SubmissionIntake, intakeStatus: string, currentContext: CurrentContext) => {
const activityId =
data.activityId ??
(await activityService.createActivity(Initiative.HOUSING, generateCreateStamps(req.currentContext)))?.activityId;
(await activityService.createActivity(Initiative.HOUSING, generateCreateStamps(currentContext)))?.activityId;

let basic, housing, location, permits;
let appliedPermits: Array<Permit> = [],
Expand Down Expand Up @@ -327,11 +328,11 @@ const controller = {
...housing,
...location,
...permits,
submissionId: data.submissionId ?? uuidv4(),
submissionId: uuidv4(),
activityId: activityId,
submittedAt: data.submittedAt ?? new Date().toISOString(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
submittedBy: getCurrentUsername(req.currentContext),
submittedBy: getCurrentUsername(currentContext),
intakeStatus: intakeStatus,
applicationStatus: data.applicationStatus ?? ApplicationStatus.NEW,
submissionType: data?.submissionType ?? SubmissionType.GUIDANCE
Expand All @@ -340,9 +341,7 @@ const controller = {
investigatePermits
};

if (data.submit) {
controller.assignPriority(submissionData.submission);
}
controller.assignPriority(submissionData.submission);

return submissionData;
},
Expand Down Expand Up @@ -375,10 +374,15 @@ const controller = {
createSubmission: async (req: Request<never, never, SubmissionIntake>, res: Response, next: NextFunction) => {
try {
const { submission, appliedPermits, investigatePermits } = await controller.generateSubmissionData(
req,
IntakeStatus.SUBMITTED
req.body,
IntakeStatus.SUBMITTED,
req.currentContext
);

// Create contacts
if (req.body.contacts)
await contactService.upsertContacts(submission.activityId, req.body.contacts, req.currentContext);

// Create new submission
const result = await submissionService.createSubmission({
...submission,
Expand Down Expand Up @@ -409,6 +413,50 @@ const controller = {
}
},

deleteDraft: async (req: Request<{ draftId: string }>, res: Response, next: NextFunction) => {
try {
const response = await draftService.deleteDraft(req.params.draftId);

if (!response) {
return res.status(404).json({ message: 'Submission draft not found' });
}

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

getDraft: async (req: Request<{ draftId: string }>, res: Response, next: NextFunction) => {
try {
const response = await draftService.getDraft(req.params.draftId);

if (req.currentAuthorization?.attributes.includes('scope:self')) {
if (response?.createdBy !== getCurrentUsername(req.currentContext)) {
res.status(403).send();
}
}

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

getDrafts: async (req: Request, res: Response, next: NextFunction) => {
try {
let response = await draftService.getDrafts(DraftCode.SUBMISSION);

if (req.currentAuthorization?.attributes.includes('scope:self')) {
response = response.filter((x: Draft) => x?.createdBy === req.currentContext.userId);
}

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

getStatistics: async (req: Request<never, never, never, StatisticsFilters>, res: Response, next: NextFunction) => {
try {
const response = await submissionService.getStatistics(req.query);
Expand Down Expand Up @@ -484,85 +532,63 @@ 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
req.body,
IntakeStatus.SUBMITTED,
req.currentContext
);

let result;

await contactService.upsertContacts(submission.activityId, req.body.contacts, req.currentContext);

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

if (!result) {
return res.status(404).json({ message: 'Submission not found' });
}
} else {
// Create new submission
result = await submissionService.createSubmission({
...submission,
...generateCreateStamps(req.currentContext)
});
}
// Create contacts
if (req.body.contacts)
await contactService.upsertContacts(submission.activityId, req.body.contacts, req.currentContext);

// Remove already existing permits for this activity
await permitService.deletePermitsByActivity(submission.activityId);
// Create new submission
const result = await submissionService.createSubmission({
...submission,
...generateCreateStamps(req.currentContext)
});

// 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 });
// Delete old draft
if (req.body.draftId) await draftService.deleteDraft(req.body.draftId);

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

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

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

let result;
let response;

if (update) {
// Update submission
result = await submissionService.updateSubmission({
...submission,
// Update draft
response = await draftService.updateDraft({
...req.body,
...generateUpdateStamps(req.currentContext)
});

if (!result) {
return res.status(404).json({ message: 'Submission not found' });
}
} else {
// Create new submission
result = await submissionService.createSubmission({
...submission,
const activityId = (
await activityService.createActivity(Initiative.HOUSING, generateCreateStamps(req.currentContext))
)?.activityId;

// Create new draft
response = await draftService.createDraft({
draftId: uuidv4(),
activityId: activityId,
draftCode: DraftCode.SUBMISSION,
data: req.body.data,
...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 });
res.status(update ? 200 : 201).json({ draftId: response?.draftId, activityId: response?.activityId });
} catch (e: unknown) {
next(e);
}
Expand Down
Loading

0 comments on commit cf94292

Please sign in to comment.