From aa4e607866bbfa9c603864ff3fe982e5cdbc397e Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 6 Apr 2024 12:12:01 +0300 Subject: [PATCH] coverMeRourine completed :) --- pages/api/email.ts | 80 ++++++++++++++++++- pages/cart/publishers/myschedule.tsx | 5 +- src/helpers/email.js | 9 +-- src/templates/emails/coverMe.hbs | 6 +- src/templates/emails/coverMeAccepted.hbs | 15 ++++ .../emails/{coverMe copy.hbs => example.hbs} | 0 6 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 src/templates/emails/coverMeAccepted.hbs rename src/templates/emails/{coverMe copy.hbs => example.hbs} (100%) 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 }) {
{assignment.shift.assignments.map((a, index) => { return ( - - {a.publisher.firstName} {a.publisher.lastName}{a.isWithTransport && } + + {a.publisher.firstName} {a.publisher.lastName} + {a.isWithTransport && } ) } diff --git a/src/helpers/email.js b/src/helpers/email.js index 0dff56a..a69db91 100644 --- a/src/helpers/email.js +++ b/src/helpers/email.js @@ -184,11 +184,7 @@ exports.SendEmail_NewShifts = async function (publisher, shifts) { sentDate: new Date().toLocaleDateString() // Assuming you want to include the sent date in the email }; - // Call the refactored function to send the email with Handlebars template rendering - await exports.SendEmailHandlebars( - publisher.email, // Assuming the publisher's email is to be used - "newShifts", // The name of your Handlebars template for new shifts notification - model, + await exports.SendEmailHandlebars(publisher.email, "newShifts", model, [{ filename: "calendar.ics", content: icsLink, @@ -200,9 +196,6 @@ exports.SendEmail_NewShifts = async function (publisher, shifts) { - - - //----------------------- OLD ----------------------------- // exports.SendEmail_NewShifts = async function (publisher, shifts) { diff --git a/src/templates/emails/coverMe.hbs b/src/templates/emails/coverMe.hbs index f95ecd1..2a954f8 100644 --- a/src/templates/emails/coverMe.hbs +++ b/src/templates/emails/coverMe.hbs @@ -1,7 +1,9 @@ {{!--Subject: ССС: Нужен е заместник --}}
-

Търси се зместник за смяна на {{placeName}} за {{dateStr}}!

+

Търси се зместник + {{!-- за смяна на {{placeName}} за {{dateStr}}! --}} +

Здравей {{firstName}},

{{prefix}} {{user.firstName}} {{user.lastName}} търси заместник.

{{!--

Shift Details:

--}} @@ -20,5 +22,5 @@

Best regards,
{{name}}

--}}
-

Изпратено до {{firstName}} {{lastName}} на {{sentDate}}

+

Изпратено до {{firstName}} {{lastName}} {{email}} {{sentDate}}

\ No newline at end of file diff --git a/src/templates/emails/coverMeAccepted.hbs b/src/templates/emails/coverMeAccepted.hbs new file mode 100644 index 0000000..a7fc71f --- /dev/null +++ b/src/templates/emails/coverMeAccepted.hbs @@ -0,0 +1,15 @@ +{{!-- Subject: ССС: Нови назначени смени--}} + +
+

Промяна твоята смяна на {{placeName}} {{dateStr}}

+

Здравейте {{firstName}},

+

{{firstName}} {{lastName}} ще замести {{oldPubName}} на смяната ви на {{dateStr}}

+

Новаия списък с участници за тази смяна е:

+ {{{newPubNames}}} +
+
+
+ + \ No newline at end of file diff --git a/src/templates/emails/coverMe copy.hbs b/src/templates/emails/example.hbs similarity index 100% rename from src/templates/emails/coverMe copy.hbs rename to src/templates/emails/example.hbs