email API and first templates
This commit is contained in:
162
pages/api/email.ts
Normal file
162
pages/api/email.ts
Normal file
@ -0,0 +1,162 @@
|
||||
// API endpoint to process email user actions - urls we send in emails to users
|
||||
|
||||
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 handlebars = require("handlebars");
|
||||
|
||||
const router = createRouter<NextApiRequest, NextApiResponse>();
|
||||
|
||||
|
||||
//action to accept coverme request from email
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param req import { NextApiRequest, NextApiResponse } from 'next'
|
||||
* @param res import { NextApiRequest, NextApiResponse } from 'next'
|
||||
*/
|
||||
export default async function handler(req, res) {
|
||||
const prisma = common.getPrismaClient();
|
||||
|
||||
var action = req.query.action;
|
||||
// Retrieve and validate the JWT token
|
||||
const token = await getToken({ req: req });
|
||||
|
||||
//response is a special action that does not require a token
|
||||
if (action !== "email_response") {
|
||||
if (!token) {
|
||||
// If no token or invalid token, return unauthorized status
|
||||
return res.status(401).json({ message: "Unauthorized to call this API endpoint" });
|
||||
}
|
||||
}
|
||||
|
||||
var userId = req.query.userId;
|
||||
var email = req.query.email;
|
||||
let date = new Date();
|
||||
|
||||
if (!userId && !email) {
|
||||
return res.status(400).json({ message: "User ID or email is not provided" });
|
||||
}
|
||||
// Retrieve the user
|
||||
const user = await prisma.publisher.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
email: email
|
||||
}
|
||||
});
|
||||
if (!user) {
|
||||
return res.status(404).json({ message: "User not found" });
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case "send_coverme_request":
|
||||
// Send CoverMe request to the user
|
||||
//get from POST data: shiftId, assignmentId, date
|
||||
let shiftId = req.body.shiftId;
|
||||
let assignmentId = req.body.assignmentId;
|
||||
let date = req.body.date;
|
||||
|
||||
console.log("User: " + user.email + " sent a CoverMe request: " +
|
||||
shiftId + " " + assignmentId + " " + date);
|
||||
|
||||
//get all subscribed publisers
|
||||
const subscribedPublishers = await prisma.publisher.findMany({
|
||||
where: {
|
||||
isSubscribedToCoverMe: true
|
||||
}
|
||||
});
|
||||
//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)
|
||||
}
|
||||
});
|
||||
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;
|
||||
|
||||
default:
|
||||
return res.status(400).json({ message: "Invalid action" });
|
||||
}
|
||||
return res.status(200).json({ message: "User action processed" });
|
||||
}
|
||||
|
||||
router.use(expressWrapper(handler));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user