From 91adc27d29fd736b5bf228088d4f669d41fbb38e Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 18 Apr 2024 13:34:17 +0300 Subject: [PATCH] CoverMe requests logged --- package.json | 2 +- pages/api/email.ts | 32 +++++++++++++++---- .../migration.sql | 2 +- prisma/schema.prisma | 4 +-- src/helpers/email.js | 2 +- src/templates/emails/coverMeAccepted.hbs | 2 +- src/templates/emails/example.hbs | 2 +- src/templates/emails/newShifts.hbs | 2 +- 8 files changed, 34 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index d602472..a9e5e94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pwwa", - "version": "1.1.2", + "version": "1.2.0", "private": true, "description": "JW PW Web App", "repository": "http://git.d-popov.com/popov/next-cart-app.git", diff --git a/pages/api/email.ts b/pages/api/email.ts index 4e9fa3d..049895a 100644 --- a/pages/api/email.ts +++ b/pages/api/email.ts @@ -8,6 +8,7 @@ const data = require('../../src/helpers/data'); const emailHelper = require('../../src/helpers/email'); const { v4: uuidv4 } = require('uuid'); const CON = require("../../src/helpers/const"); +import { EventLogType } from "@prisma/client"; import fs from 'fs'; import path from 'path'; @@ -128,6 +129,15 @@ export default async function handler(req, res) { } } }); + await prisma.eventLog.create({ + data: { + date: new Date(), + publisher: { connect: { id: publisher.id } }, + shift: { connect: { id: assignment.shiftId } }, + type: EventLogType.AssignmentReplacementAccepted, + content: "Заявка за заместване приета от " + publisher.firstName + " " + publisher.lastName + } + }); 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)}`; @@ -202,7 +212,7 @@ export default async function handler(req, res) { return res.status(401).json({ message: "Unauthorized to call this API endpoint" }); } - const user = await prisma.publisher.findUnique({ + const publisher = await prisma.publisher.findUnique({ where: { email: token.email } @@ -210,7 +220,7 @@ export default async function handler(req, res) { switch (action) { case "sendCoverMeRequestByEmail": - // Send CoverMe request to the user + // Send CoverMe request to the users //get from POST data: shiftId, assignmentId, date //let shiftId = req.body.shiftId; let assignmentId = req.body.assignmentId; @@ -235,7 +245,7 @@ export default async function handler(req, res) { } } }); - console.log("User: " + user.email + " sent a 'CoverMe' request for his assignment " + assignmentId + " - " + assignment.shift.cartEvent.location.name + " " + assignment.shift.startTime.toISOString()); + console.log("User: " + publisher.email + " sent a 'CoverMe' request for his assignment " + assignmentId + " - " + assignment.shift.cartEvent.location.name + " " + assignment.shift.startTime.toISOString()); // update the assignment. generate new publicGuid, isConfirmed to false @@ -270,10 +280,20 @@ export default async function handler(req, res) { filter((item, index, self) => index === self.findIndex((t) => ( t.email === item.email //and exclude the user himself - )) //&& item.email !== user.email + )) //&& item.email !== publisher.email ); console.log("Sending CoverMe request to " + pubsToSend.length + " publishers"); + await prisma.eventLog.create({ + data: { + date: new Date(), + publisher: { connect: { id: publisher.id } }, + shift: { connect: { id: assignment.shiftId } }, + type: EventLogType.AssignmentReplacementRequested, + content: "Заявка за заместване от " + publisher.firstName + " " + publisher.lastName + + "до: " + pubsToSend.map(p => p.firstName + " " + p.lastName + "<" + p.email + ">").join(", "), + } + }); //send email to all subscribed publishers for (let i = 0; i < pubsToSend.length; i++) { @@ -281,10 +301,10 @@ export default async function handler(req, res) { let acceptUrl = process.env.NEXTAUTH_URL + "/api/email?action=email_response&emailaction=coverMeAccept&userId=" + pubsToSend[i].id + "&shiftId=" + assignment.shiftId + "&assignmentPID=" + newPublicGuid; let model = { - user: user, + user: publisher, shiftId: assignment.shiftId, acceptUrl: acceptUrl, - prefix: user.isMale ? "Брат" : "Сестра", + prefix: publisher.isMale ? "Брат" : "Сестра", firstName: pubsToSend[i].firstName, lastName: pubsToSend[i].lastName, email: pubsToSend[i].email, diff --git a/prisma/migrations/20240418092928_add_event_log_table/migration.sql b/prisma/migrations/20240418092928_add_event_log_table/migration.sql index 52dd62f..bd811c8 100644 --- a/prisma/migrations/20240418092928_add_event_log_table/migration.sql +++ b/prisma/migrations/20240418092928_add_event_log_table/migration.sql @@ -5,7 +5,7 @@ CREATE TABLE `EventLog` ( `publisherId` VARCHAR(191) NULL, `shiftId` INTEGER NULL, `content` VARCHAR(191) NOT NULL, - `type` ENUM('AssignnementReplacementRequested', 'AssignnementReplacement', 'SentEmail') NOT NULL, + `type` ENUM('AssignmentReplacementRequested', 'AssignmentReplacementAccepted', 'SentEmail') NOT NULL, PRIMARY KEY (`id`) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cbb1d77..445d589 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -259,8 +259,8 @@ model Message { } enum EventLogType { - AssignnementReplacementRequested - AssignnementReplacement + AssignmentReplacementRequested + AssignmentReplacementAccepted SentEmail } diff --git a/src/helpers/email.js b/src/helpers/email.js index 64efd24..cc6b39b 100644 --- a/src/helpers/email.js +++ b/src/helpers/email.js @@ -266,7 +266,7 @@ exports.SendEmail_NewShifts = async function (publisher, shifts) { // ], // subject: "[CCC]: вашите смени през " + CON.monthNamesBG[date.getMonth()], // text: -// "Здравейте, " + publisher.firstName + " " + publisher.lastName + "!\n\n" + +// "Здравей, " + publisher.firstName + " " + publisher.lastName + "!\n\n" + // "Ти регистриран да получавате известия за нови смени на количка.\n" + // `За месец ${CON.monthNamesBG[date.getMonth()]} имате следните смени:\n` + // ` ${shftStr} \n\n\n` + diff --git a/src/templates/emails/coverMeAccepted.hbs b/src/templates/emails/coverMeAccepted.hbs index 16bb198..a29a086 100644 --- a/src/templates/emails/coverMeAccepted.hbs +++ b/src/templates/emails/coverMeAccepted.hbs @@ -2,7 +2,7 @@

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

-

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

+

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

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

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