Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // controller
- /**@type {import('express').RequestHandler} */
- async function createTicket(req, res, next) {
- const {
- title,
- userId,
- priority,
- departmentId,
- assigneTo,
- description,
- } = req.body;
- const adminId = req.payload.id;
- await ticketRepository.insert(
- title,
- userId,
- priority,
- departmentId,
- 'admin',
- {
- from: adminId,
- to: assigneTo,
- description,
- },
- );
- res.status(200).json({
- successful: true,
- message: 'TICKET_CREATED',
- });
- }
- // repository
- /**
- *
- * @param {string} title
- * @param {string} userId
- * @param {string} priority
- * @param {string} departmentId
- * @param {'admin'|'contractor'|'lead'} ticketInitializer
- * @param {object} [assignee]
- * @param {object} assignee.from
- * @param {object} assignee.to
- * @param {object} assignee.description
- * @throws {ForeignKeyConstraintsError}
- * @returns {Promise<{ticketId: string}>} created ticket's id
- */
- async function insert(
- title,
- userId,
- priority,
- departmentId,
- ticketInitializer,
- assignee = undefined,
- ) {
- const {
- ticketDepartment,
- } = await ticketDepartmentRepository.fetchDepartmentById(
- departmentId,
- );
- if (!ticketDepartment) {
- throw new ForeignKeyConstraintsError(COLLECTION_NAME, {
- fieldName: 'departmentId',
- fieldValue: departmentId,
- });
- }
- if (!ticketDepartment.members.includes(assignee.to)) {
- throw new MemberNotFound(assignee.to);
- }
- const newTicket = await new TicketModel({
- title,
- userId: userId,
- priority,
- departmentId,
- ...(assignee !== undefined
- ? { assigneeHistory: [assignee] }
- : {}),
- ticketInitializer,
- }).save();
- return {
- ticketId: newTicket.id,
- };
- }
- // current validator: express-validator
- // and other stuff is the same as my project schema
Add Comment
Please, Sign In to add comment