-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enquiry form, submission, drafts & note creation for relations
- Loading branch information
1 parent
cdf13ee
commit 56d6e60
Showing
25 changed files
with
902 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { NIL, v4 as uuidv4 } from 'uuid'; | ||
|
||
import { Initiatives, NOTE_TYPE_LIST } from '../components/constants'; | ||
import { getCurrentIdentity } from '../components/utils'; | ||
import { activityService, enquiryService, noteService, userService } from '../services'; | ||
|
||
import type { NextFunction, Request, Response } from '../interfaces/IExpress'; | ||
import type { Enquiry } from '../types'; | ||
|
||
const controller = { | ||
createRelatedNote: async (req: Request, data: Enquiry) => { | ||
if (data.relatedActivityId) { | ||
const activity = await activityService.getActivity(data.relatedActivityId); | ||
if (activity) { | ||
const userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, NIL), NIL); | ||
|
||
await noteService.createNote({ | ||
activityId: data.relatedActivityId, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, max-len | ||
note: `Added by ${(req.currentUser?.tokenPayload as any)?.idir_username}\nEnquiry #${data.activityId}\n${data.enquiryDescription}`, | ||
noteType: NOTE_TYPE_LIST.ENQUIRY, | ||
title: 'Enquiry', | ||
bringForwardDate: null, | ||
bringForwardState: null, | ||
createdAt: new Date().toISOString(), | ||
createdBy: userId, | ||
isDeleted: false | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
generateEnquiryData: async (req: Request) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const data: any = req.body; | ||
|
||
const activityId = data.activityId ?? (await activityService.createActivity(Initiatives.HOUSING))?.activityId; | ||
|
||
let applicant, basic; | ||
|
||
// Create applicant information | ||
if (data.applicant) { | ||
applicant = { | ||
contactFirstName: data.applicant.firstName, | ||
contactLastName: data.applicant.lastName, | ||
contactPhoneNumber: data.applicant.phoneNumber, | ||
contactEmail: data.applicant.email, | ||
contactApplicantRelationship: data.applicant.relationshipToProject, | ||
contactPreference: data.applicant.contactPreference | ||
}; | ||
} | ||
|
||
if (data.basic) { | ||
basic = { | ||
enquiryType: data.basic.enquiryType, | ||
isRelated: data.basic.isRelated, | ||
relatedActivityId: data.basic.relatedActivityId, | ||
enquiryDescription: data.basic.enquiryDescription, | ||
applyForPermitConnect: data.basic.applyForPermitConnect | ||
}; | ||
} | ||
|
||
// Put new enquiry together | ||
return { | ||
...applicant, | ||
...basic, | ||
enquiryId: data.enquiryId ?? uuidv4(), | ||
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 | ||
}; | ||
}, | ||
|
||
createDraft: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const data: any = req.body; | ||
|
||
const enquiry = await controller.generateEnquiryData(req); | ||
|
||
// Create new enquiry | ||
const result = await enquiryService.createEnquiry(enquiry); | ||
|
||
// On submit attempt to create note if enquiry is associated with an existing activity | ||
if (data.submit) { | ||
await controller.createRelatedNote(req, result); | ||
} | ||
|
||
res.status(201).json({ activityId: result.activityId, enquiryId: result.enquiryId }); | ||
} catch (e: unknown) { | ||
next(e); | ||
} | ||
}, | ||
|
||
updateDraft: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const data: any = req.body; | ||
|
||
const enquiry = await controller.generateEnquiryData(req); | ||
|
||
// Update enquiry | ||
const result = await enquiryService.updateEnquiry(enquiry as Enquiry); | ||
|
||
// On submit, attempt to create note if enquiry is associated with an existing activity | ||
if (data.submit) { | ||
await controller.createRelatedNote(req, result); | ||
} | ||
|
||
res.status(200).json({ activityId: result.activityId, enquiryId: result.enquiryId }); | ||
} catch (e: unknown) { | ||
next(e); | ||
} | ||
} | ||
}; | ||
|
||
export default controller; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import stamps from '../stamps'; | ||
|
||
import type { Knex } from 'knex'; | ||
|
||
/* | ||
* Not included in this migration is a manual destructive operation | ||
* submission.contact_name will be split into two new parts | ||
* submission.contact_first_name | ||
* submission.contact_last_name | ||
* submission.contact_name will then be dropped | ||
*/ | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Create public schema tables | ||
.then(() => | ||
knex.schema.createTable('enquiry', (table) => { | ||
table.uuid('enquiry_id').primary(); | ||
table | ||
.text('activity_id') | ||
.notNullable() | ||
.references('activity_id') | ||
.inTable('activity') | ||
.onUpdate('CASCADE') | ||
.onDelete('CASCADE'); | ||
table.uuid('assigned_user_id').references('user_id').inTable('user').onUpdate('CASCADE').onDelete('CASCADE'); | ||
table.timestamp('submitted_at', { useTz: true }).notNullable(); | ||
table.text('submitted_by').notNullable(); | ||
table.text('contact_first_name'); | ||
table.text('contact_last_name'); | ||
table.text('contact_phone_number'); | ||
table.text('contact_email'); | ||
table.text('contact_preference'); | ||
table.text('contact_applicant_relationship'); | ||
table.text('is_related'); | ||
table.text('related_activity_id'); | ||
table.text('enquiry_description'); | ||
table.text('apply_for_permit_connect'); | ||
stamps(knex, table); | ||
}) | ||
) | ||
|
||
.then(() => | ||
knex.schema.alterTable('submission', (table) => { | ||
table.text('contact_first_name'); | ||
table.text('contact_last_name'); | ||
}) | ||
) | ||
|
||
// Create public schema table triggers | ||
.then(() => | ||
knex.schema.raw(`create trigger before_update_enquiry_trigger | ||
before update on "enquiry" | ||
for each row execute procedure public.set_updated_at();`) | ||
) | ||
|
||
// Create audit triggers | ||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER audit_enquiry_trigger | ||
AFTER UPDATE OR DELETE ON enquiry | ||
FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func();`) | ||
) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Drop audit triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_enquiry_trigger ON permit')) | ||
|
||
// 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'); | ||
}) | ||
) | ||
|
||
// Drop public schema tables | ||
.then(() => knex.schema.dropTableIfExists('enquiry')) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Prisma } from '@prisma/client'; | ||
|
||
import user from './user'; | ||
|
||
import type { Stamps } from '../stamps'; | ||
import type { Enquiry } from '../../types'; | ||
|
||
// Define types | ||
const _enquiry = Prisma.validator<Prisma.enquiryDefaultArgs>()({}); | ||
const _enquiryWithGraph = Prisma.validator<Prisma.enquiryDefaultArgs>()({}); | ||
const _enquiryWithUserGraph = Prisma.validator<Prisma.enquiryDefaultArgs>()({ include: { user: true } }); | ||
|
||
type PrismaRelationEnquiry = Omit<Prisma.enquiryGetPayload<typeof _enquiry>, keyof Stamps>; | ||
type PrismaGraphEnquiry = Prisma.enquiryGetPayload<typeof _enquiryWithGraph>; | ||
type PrismaGraphEnquiryUser = Prisma.enquiryGetPayload<typeof _enquiryWithUserGraph>; | ||
|
||
export default { | ||
toPrismaModel(input: Enquiry): PrismaRelationEnquiry { | ||
return { | ||
enquiry_id: input.enquiryId, | ||
activity_id: input.activityId, | ||
assigned_user_id: input.assignedUserId, | ||
submitted_at: new Date(input.submittedAt ?? Date.now()), | ||
submitted_by: input.submittedBy, | ||
contact_first_name: input.contactFirstName, | ||
contact_last_name: input.contactLastName, | ||
contact_phone_number: input.contactPhoneNumber, | ||
contact_email: input.contactEmail, | ||
contact_preference: input.contactPreference, | ||
contact_applicant_relationship: input.contactApplicantRelationship, | ||
is_related: input.isRelated, | ||
related_activity_id: input.relatedActivityId, | ||
enquiry_description: input.enquiryDescription, | ||
apply_for_permit_connect: input.applyForPermitConnect | ||
}; | ||
}, | ||
|
||
fromPrismaModel(input: PrismaGraphEnquiry): Enquiry { | ||
return { | ||
enquiryId: input.enquiry_id, | ||
activityId: input.activity_id, | ||
assignedUserId: input.assigned_user_id, | ||
submittedAt: input.submitted_at?.toISOString() as string, | ||
submittedBy: input.submitted_by, | ||
contactFirstName: input.contact_first_name, | ||
contactLastName: input.contact_last_name, | ||
contactPhoneNumber: input.contact_phone_number, | ||
contactEmail: input.contact_email, | ||
contactPreference: input.contact_preference, | ||
contactApplicantRelationship: input.contact_applicant_relationship, | ||
isRelated: input.is_related, | ||
relatedActivityId: input.related_activity_id, | ||
enquiryDescription: input.enquiry_description, | ||
applyForPermitConnect: input.apply_for_permit_connect, | ||
user: null | ||
}; | ||
}, | ||
|
||
fromPrismaModelWithUser(input: PrismaGraphEnquiryUser | null): Enquiry | null { | ||
if (!input) return null; | ||
|
||
const enquiry = this.fromPrismaModel(input); | ||
if (enquiry && input.user) { | ||
enquiry.user = user.fromPrismaModel(input.user); | ||
} | ||
|
||
return enquiry; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import express from 'express'; | ||
import { enquiryController } from '../../controllers'; | ||
import { requireSomeAuth } from '../../middleware/requireSomeAuth'; | ||
|
||
import type { NextFunction, Request, Response } from '../../interfaces/IExpress'; | ||
|
||
const router = express.Router(); | ||
router.use(requireSomeAuth); | ||
|
||
// Submission create draft endpoint | ||
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 => { | ||
enquiryController.updateDraft(req, res, next); | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.