diff --git a/pages/api/email.ts b/pages/api/email.ts index b8c2161..14ee1de 100644 --- a/pages/api/email.ts +++ b/pages/api/email.ts @@ -106,6 +106,7 @@ export default async function handler(req, res) { }, data: { publisherId: userId, + originalPublisherId: originalPublisher.id, publicGuid: null, // if this exists, we consider the request open isConfirmed: true } diff --git a/pages/api/index.ts b/pages/api/index.ts index 736fdda..f928d8c 100644 --- a/pages/api/index.ts +++ b/pages/api/index.ts @@ -2,7 +2,7 @@ import { getToken } from "next-auth/jwt"; import { authOptions } from './auth/[...nextauth]' import { getServerSession } from "next-auth/next" import { NextApiRequest, NextApiResponse } from 'next' -import { DayOfWeek, AvailabilityType, UserRole } from '@prisma/client'; +import { DayOfWeek, AvailabilityType, UserRole, EventLogType } from '@prisma/client'; const common = require('../../src/helpers/common'); const dataHelper = require('../../src/helpers/data'); const subq = require('../../prisma/bl/subqueries'); @@ -11,6 +11,7 @@ import { addMinutes } from 'date-fns'; import fs from 'fs'; import path from 'path'; import { all } from "axios"; +import { logger } from "src/helpers/common"; /** * @@ -821,10 +822,70 @@ async function replaceInAssignment(oldPublisherId, newPublisherId, shiftId) { }, data: { publisherId: newPublisherId, + originalPublisherId: oldPublisherId, isConfirmed: false, isBySystem: true, isMailSent: false } }); + + // log the event + let shift = await prisma.shift.findUnique({ + where: { + id: shiftId + }, + select: { + startTime: true, + cartEvent: { + select: { + location: { + select: { + name: true + } + } + } + } + }, + include: { + assignments: { + include: { + publisher: { + select: { + firstName: true, + lastName: true, + email: true + } + } + } + } + } + }); + let publishers = await prisma.publisher.findMany({ + where: { + id: { in: [oldPublisherId, newPublisherId] } + }, + select: { + id: true, + firstName: true, + lastName: true, + email: true + } + }); + let originalPublisher = publishers.find(p => p.id == oldPublisherId); + let newPublisher = publishers.find(p => p.id == newPublisherId); + let eventLog = await prisma.eventLog.create({ + data: { + date: new Date(), + publisher: { connect: { id: oldPublisherId } }, + shift: { connect: { id: shiftId } }, + type: EventLogType.AssignmentReplacementManual, + content: "Въведено заместване от " + originalPublisher.firstName + " " + originalPublisher.lastName + ". Ще го замества " + newPublisher.firstName + " " + newPublisher.lastName + "." + + } + }); + + logger.info("User: " + originalPublisher.email + " replaced his assignment for " + shift.cartEvent.location.name + " " + shift.startTime.toISOString() + " with " + newPublisher.firstName + " " + newPublisher.lastName + "<" + newPublisher.email + ">. EventLogId: " + eventLog.id + ""); + + return result; } \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8341d0d..25f0496 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -277,6 +277,7 @@ model Message { } enum EventLogType { + AssignmentReplacementManual AssignmentReplacementRequested AssignmentReplacementAccepted SentEmail