email template fo rreplacements

This commit is contained in:
Dobromir Popov
2024-04-05 23:44:27 +03:00
parent bac4f4c7d5
commit fa5d3f4f99
4 changed files with 207 additions and 72 deletions

View File

@ -4,8 +4,10 @@ import { getToken } from "next-auth/jwt";
import type { NextApiRequest, NextApiResponse } from 'next';
import { createRouter, expressWrapper } from "next-connect";
const common = require('../../src/helpers/common');
const email = require('../../src/helpers/');
const emailHelper = require('../../src/helpers/email');
import fs from 'fs';
import path from 'path';
const handlebars = require("handlebars");
const router = createRouter<NextApiRequest, NextApiResponse>();
@ -51,7 +53,7 @@ export default async function handler(req, res) {
if (!user) {
return res.status(404).json({ message: "User not found" });
}
var emailaction = req.query.emailaction;
switch (action) {
case "send_coverme_request":
// Send CoverMe request to the user
@ -72,84 +74,121 @@ export default async function handler(req, res) {
//send email to all subscribed publishers
for (let i = 0; i < subscribedPublishers.length; i++) {
//send email to subscribed publisher
//send email to subscribed publisher
const emailResponse = await common.sendEmail(subscribedPublishers[i].email, "CoverMe Request",
"User: " + user.email + " sent a CoverMe request: " +
shiftId + " " + assignmentId + " " + date);
}
break;
case "coverme_accept":
// Update the user status to accepted
console.log("User: " + user.firstName + " " + user.lastName + " accepted the CoverMe request");
//validate shiftId and assignmentId
let shiftId = req.query.shiftId;
let assignmentId = req.query.assignmentId;
if (!shiftId || !assignmentId) {
return res.status(400).json({ message: "Shift ID or Assignment ID is not provided" });
}
//get the assignment
const assignment = await prisma.assignment.findUnique({
where: {
id: parseInt(assignmentId)
let shift = await prisma.shift.findUnique({
where: {
id: parseInt(shiftId)
},
include: {
cartEvent: {
include: {
location: true
}
},
}
}
});
if (!assignment) {
return res.status(404).json({ message: "Assignment not found" });
}
if (assignment.shiftId != parseInt(shiftId)) {
return res.status(400).json({ message: "Shift ID does not match" });
);
let acceptUrl = process.env.NEXTAUTH_URL + "/api/emailActions?action=coverme_accept&userId=" + user.id + "&shiftId=" + shiftId + "&assignmentId=" + assignmentId;
let model = {
user: user,
shiftId: shiftId,
assignmentId: assignmentId,
acceptUrl: acceptUrl,
prefix: subscribedPublishers[i].isMale ? "Брат" : "Сестра",
firstName: subscribedPublishers[i].firstName,
lastName: subscribedPublishers[i].lastName,
placeName: shift.cartEvent.location.name,
dateStr: date.toLocaleDateString(),
sentDate: date.toLocaleTimeString()
};
emailHelper.SendEmailHandlebars(subscribedPublishers[i].email, "coverMe", model);
}
// await prisma.user.update({
// where: {
// id: parseInt(userId)
// },
// data: {
// status: "accepted",
// acceptedAt: date
// }
// });
break;
//POST
case "send_report": //we can send report form in the emails to the user. process the POSTED data here
// Send report form to the user
//get from POST data: locationId, date, placementCount, videoCount, returnVisitInfoCount, conversationCount
let locationId = req.body.locationId;
let date = req.body.date;
let placementCount = req.body.placementCount;
let videoCount = req.body.videoCount;
let returnVisitInfoCount = req.body.returnVisitInfoCount;
let conversationCount = req.body.conversationCount;
console.log("User: " + user.email + " sent a report: " +
locationId + " " + date + " " +
placementCount + " " + videoCount + " " +
returnVisitInfoCount + " " + conversationCount);
//save the report in the database
await prisma.report.create({
data: {
userId: parseInt(userId),
locationId: parseInt(locationId),
date: date,
placementCount: parseInt(placementCount),
videoCount: parseInt(videoCount),
returnVisitInfoCount: parseInt(returnVisitInfoCount),
conversationCount: parseInt(conversationCount)
}
});
case "email_response":
//get email action
if (!emailaction) {
return res.status(400).json({ message: "Email action is not provided" });
}
break;
default:
return res.status(400).json({ message: "Invalid action" });
}
if (action !== "email_response") {
switch (emailaction) {
case "coverme_accept":
// Update the user status to accepted
console.log("User: " + user.firstName + " " + user.lastName + " accepted the CoverMe request");
//validate shiftId and assignmentId
let shiftId = req.query.shiftId;
let assignmentId = req.query.assignmentId;
if (!shiftId || !assignmentId) {
return res.status(400).json({ message: "Shift ID or Assignment ID is not provided" });
}
//get the assignment
const assignment = await prisma.assignment.findUnique({
where: {
id: parseInt(assignmentId)
}
});
if (!assignment) {
return res.status(404).json({ message: "Assignment not found" });
}
if (assignment.shiftId != parseInt(shiftId)) {
return res.status(400).json({ message: "Shift ID does not match" });
}
// await prisma.user.update({
// where: {
// id: parseInt(userId)
// },
// data: {
// status: "accepted",
// acceptedAt: date
// }
// });
break;
//POST
case "send_report": //we can send report form in the emails to the user. process the POSTED data here
// Send report form to the user
//get from POST data: locationId, date, placementCount, videoCount, returnVisitInfoCount, conversationCount
let locationId = req.body.locationId;
let date = req.body.date;
let placementCount = req.body.placementCount;
let videoCount = req.body.videoCount;
let returnVisitInfoCount = req.body.returnVisitInfoCount;
let conversationCount = req.body.conversationCount;
console.log("User: " + user.email + " sent a report: " +
locationId + " " + date + " " +
placementCount + " " + videoCount + " " +
returnVisitInfoCount + " " + conversationCount);
//save the report in the database
await prisma.report.create({
data: {
userId: parseInt(userId),
locationId: parseInt(locationId),
date: date,
placementCount: parseInt(placementCount),
videoCount: parseInt(videoCount),
returnVisitInfoCount: parseInt(returnVisitInfoCount),
conversationCount: parseInt(conversationCount)
}
});
break;
}
// //send email response to the user
// const emailResponse = await common.sendEmail(user.email, "Email Action Processed",
// "Your email action was processed successfully");
}
return res.status(200).json({ message: "User action processed" });
}