diff --git a/pages/api/email.ts b/pages/api/email.ts index cb84196..1988183 100644 --- a/pages/api/email.ts +++ b/pages/api/email.ts @@ -6,6 +6,7 @@ import { createRouter, expressWrapper } from "next-connect"; const common = require('../../src/helpers/common'); const emailHelper = require('../../src/helpers/email'); const { v4: uuidv4 } = require('uuid'); +const CON = require("../../src/helpers/const"); import fs from 'fs'; import path from 'path'; @@ -36,13 +37,13 @@ export default async function handler(req, res) { //validate shiftId and assignmentId let shiftId = req.query.shiftId; let userId = req.query.userId; - let user = await prisma.publisher.findUnique({ + let publisher = await prisma.publisher.findUnique({ where: { id: userId } }); // Update the user status to accepted - console.log("User: " + user.firstName + " " + user.lastName + " accepted the CoverMe request"); + console.log("User: " + publisher.firstName + " " + publisher.lastName + " accepted the CoverMe request"); let assignmentPID = req.query.assignmentPID; if (!shiftId) { @@ -65,9 +66,22 @@ export default async function handler(req, res) { include: { location: true } + }, + assignments: { + include: { + publisher: true + // { + // include: { + // email: true, + // firstName: true, + // lastName: true + // } + // } + } } } - } + }, + publisher: true } }); @@ -77,7 +91,64 @@ export default async function handler(req, res) { return; } - emailHelper.SendEmail_NewShifts(user, [assignment.shift]); + let to = assignment.shift.assignments.map(a => a.publisher.email); + to.push(publisher.email); + + // update the assignment. clear the guid, isConfirmed to true + await prisma.assignment.update({ + where: { + id: assignment.id + }, + data: { + publisherId: userId, + publicGuid: null, // if this exists, we consider the request open + isConfirmed: true + } + }); + const newAssignment = await prisma.assignment.findFirst({ + where: { + shiftId: parseInt(shiftId), + isConfirmed: true + }, + include: { + shift: { + include: { + cartEvent: { + include: { + location: true + } + }, + assignments: { + include: { + publisher: true + } + } + } + } + } + }); + + + const shiftStr = `${CON.weekdaysBG[assignment.shift.startTime.getDay()]} ${CON.GetDateFormat(assignment.shift.startTime)} at ${assignment.shift.cartEvent.location.name} from ${CON.GetTimeFormat(assignment.shift.startTime)} to ${CON.GetTimeFormat(assignment.shift.endTime)}`; + + const allNewPubNames = newAssignment.shift.assignments.map(a => a.publisher.firstName + " " + a.publisher.lastName + "\t\t" + a.publisher.phone + ).join(""); + + let model = { + user: publisher, + shiftStr: shiftStr, + shiftId: assignment.shiftId, + prefix: publisher.isMale ? "Брат" : "Сестра", + oldPubName: assignment.publisher.firstName + " " + assignment.publisher.lastName, + firstName: publisher.firstName, + lastName: publisher.lastName, + newPubNames: allNewPubNames, + placeName: assignment.shift.cartEvent.location.name, + dateStr: common.getDateFormated(assignment.shift.startTime), + time: common.formatTimeHHmm(assignment.shift.startTime), + sentDate: common.getDateFormated(new Date()) + }; + + emailHelper.SendEmailHandlebars(to, "coverMeAccepted", model); // await prisma.user.update({ // where: { @@ -200,6 +271,7 @@ export default async function handler(req, res) { prefix: user.isMale ? "Брат" : "Сестра", firstName: subscribedPublishers[i].firstName, lastName: subscribedPublishers[i].lastName, + email: subscribedPublishers[i].email, placeName: assignment.shift.cartEvent.location.name, dateStr: common.getDateFormated(assignment.shift.startTime), time: common.formatTimeHHmm(assignment.shift.startTime), diff --git a/pages/cart/publishers/myschedule.tsx b/pages/cart/publishers/myschedule.tsx index ad93c97..c7c4ee2 100644 --- a/pages/cart/publishers/myschedule.tsx +++ b/pages/cart/publishers/myschedule.tsx @@ -92,8 +92,9 @@ export default function MySchedulePage({ assignments }) {
Здравей {{firstName}},
{{prefix}} {{user.firstName}} {{user.lastName}} търси заместник.
{{!--Shift Details:
--}} @@ -20,5 +22,5 @@Best regards,
{{name}}
Здравейте {{firstName}},
+{{firstName}} {{lastName}} ще замести {{oldPubName}} на смяната ви на {{dateStr}}
+Новаия списък с участници за тази смяна е:
+ {{{newPubNames}}} +