Merge branch 'main' into production

This commit is contained in:
Dobromir Popov
2024-04-28 01:43:27 +03:00
17 changed files with 354 additions and 242 deletions

View File

@ -17,8 +17,8 @@ AUTH0_ISSUER=https://dev-wkzi658ckibr1amv.us.auth0.com
# GOOGLE_ID=926212607479-d3m8hm8f8esp3rf1639prskn445sa01v.apps.googleusercontent.com
# GOOGLE_SECRET=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
EMAIL_SERVICE=mailtrap
MAILTRAP_HOST_BULK=bulk.smtp.mailtrap.io
MAILTRAP_HOST=live.smtp.mailtrap.io
MAILTRAP_USER=api
MAILTRAP_PASS=1cfe82e747b8dc3390ed08bb16e0f48d
# EMAIL_SERVICE=mailtrap
# MAILTRAP_HOST_BULK=bulk.smtp.mailtrap.io
# MAILTRAP_HOST=live.smtp.mailtrap.io
# MAILTRAP_USER=api
# MAILTRAP_PASS=1cfe82e747b8dc3390ed08bb16e0f48d

View File

@ -224,12 +224,12 @@ in schedule admin - if a publisher is always pair & family is not in the shift -
лог ако е изрит потребител.
last login. pubs.
otchet - za denq
делете цонфирм
статистика - фкс (янка) + posledno vlizane
[x] OK last login. pubs.
[x] OK otchet - za denq
[x] OK - делете цонфирм
[x] статистика - фкс (янка) + posledno vlizane
заместник, предпочитание в миналото - да не може.
Да иска потвърждение преди да заместиш някой
да не се виждат непубликуваните смени в Моите смени
да не се виждат старите предпочитания ако не си админ в публишерс
[x] OK заместник, предпочитание в миналото - да не може.
[] Да иска потвърждение преди да заместиш някой
[] да не се виждат непубликуваните смени в Моите смени
[] да не се виждат старите предпочитания ако не си админ в публишерс

View File

@ -2,6 +2,8 @@ import React, { useState, useEffect } from 'react';
import { Calendar, momentLocalizer, dateFnsLocalizer } from 'react-big-calendar';
import 'react-big-calendar/lib/css/react-big-calendar.css';
import AvailabilityForm from '../availability/AvailabilityForm';
import ProtectedRoute from '../protectedRoute';
import { UserRole } from "@prisma/client";
import common from '../../src/helpers/common';
import { toast } from 'react-toastify';
@ -16,6 +18,7 @@ import { MdToday } from 'react-icons/md';
import { useSwipeable } from 'react-swipeable';
import axiosInstance from '../../src/axiosSecure';
// import { set, format, addDays } from 'date-fns';
// import { isEqual, isSameDay, getHours, getMinutes } from 'date-fns';
import { filter } from 'jszip';
@ -45,6 +48,8 @@ const messages = {
const AvCalendar = ({ publisherId, events, selectedDate }) => {
const isAdmin = ProtectedRoute.IsInRole(UserRole.ADMIN);
const [date, setDate] = useState(new Date());
//ToDo: see if we can optimize this
const [evts, setEvents] = useState(events); // Existing events
@ -211,8 +216,9 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
if (!start || !end) return;
//readonly for past dates (ToDo: if not admin)
if (startdate < new Date() || end < new Date() || startdate > end) return;
if (!isAdmin) {
if (startdate < new Date() || end < new Date() || startdate > end) return;
}
// Check if start and end are on the same day
if (startdate.toDateString() !== enddate.toDateString()) {
end = common.setTimeHHmm(startdate, "23:59");
@ -299,22 +305,22 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
}
if (event.isActive) {
switch (event.type) {
case 'assignment':
// backgroundColor = '#48bb78' // always green-500 as we don't pass isConfirmed correctly
//backgroundColor = event.isConfirmed ? '#48bb78' : '#f6e05e'; // green-500 and yellow-300 from Tailwind CSS
break;
case 'recurring':
backgroundColor = '#63b3ed'; // blue-300 from Tailwind CSS
break;
default: // availability
//backgroundColor = '#a0aec0'; // gray-400 from Tailwind CSS
break;
}
} else {
backgroundColor = '#a0aec0'; // Default color for inactive events
// if (event.isActive) {
switch (event.type) {
case 'assignment':
backgroundColor = '#48bb78' // always green-500 as we don't pass isConfirmed correctly
//backgroundColor = event.isConfirmed ? '#48bb78' : '#f6e05e'; // green-500 and yellow-300 from Tailwind CSS
break;
case 'recurring':
backgroundColor = '#63b3ed'; // blue-300 from Tailwind CSS
break;
default: // availability
//backgroundColor = '#a0aec0'; // gray-400 from Tailwind CSS
break;
}
// } else {
// backgroundColor = '#a0aec0'; // Default color for inactive events
// }
return {
style: {
@ -337,9 +343,7 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
const handleMouseLeave = () => setIsHovered(false);
if (currentView !== 'agenda') {
//if event.type is availability show in blue. if it is schedule - green if confirmed, yellow if not confirmed
//if event is not active - show in gray
let bgColorClass = 'bg-gray-500'; // Default color for inactive events
var bgColor = event.isActive ? "" : "bg-gray-500";
var bgColor = "";
//ToDo: fix this. maybe we're missing some properties
// if (event.isFromPreviousMonth) {
// // set opacity to 0.5
@ -354,7 +358,6 @@ const AvCalendar = ({ publisherId, events, selectedDate }) => {
if (event.start !== undefined && event.end !== undefined && event.startTime !== null && event.endTime !== null) {
try {
if (event.type === "recurring") {
//bgColor = "bg-blue-300";
event.title = common.getTimeFomatted(event.startTime) + " - " + common.getTimeFomatted(event.endTime);
}
else {

View File

@ -68,3 +68,8 @@ export async function serverSideAuth({ req, allowedRoles }) {
// Return the session if the user is authenticated and has the required role
return { session };
}
// Static method to check if the user has a specific role
ProtectedRoute.IsInRole = async (roleName) => {
const session = await getSession();
return session && session.user && session.user.role === roleName;
};

View File

@ -114,7 +114,7 @@ function ConfirmationModal({ isOpen, onClose, onConfirm, subscribedPublishers, a
checked={selectedGroups.includes('availablePublishers')}
onChange={() => handleToggleGroup('availablePublishers')}
/>
<span className="text-sm font-medium">На разположение:</span>
<span className="text-sm font-medium">На разположение :</span>
</div>
<div className="flex flex-wrap">
{availablePublishers.map(pub => (

View File

@ -9,6 +9,7 @@ const emailHelper = require('../../src/helpers/email');
const { v4: uuidv4 } = require('uuid');
const CON = require("../../src/helpers/const");
import { EventLogType } from "@prisma/client";
const logger = require('../../src/logger');
import fs from 'fs';
import path from 'path';
@ -46,6 +47,7 @@ export default async function handler(req, res) {
});
// Update the user status to accepted
console.log("User: " + publisher.firstName + " " + publisher.lastName + " accepted the CoverMe request");
logger.info("" + publisher.firstName + " " + publisher.lastName + " accepted the CoverMe request for shift " + shiftId + " PID: " + req.query.assignmentPID + "");
let assignmentPID = req.query.assignmentPID;
if (!shiftId) {
@ -245,7 +247,6 @@ export default async function handler(req, res) {
}
}
});
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
@ -260,23 +261,35 @@ export default async function handler(req, res) {
}
});
let subscribedPublishers = [], availablePublishers = [];
if (toSubscribed) {
//get all subscribed publisers
subscribedPublishers = await prisma.publisher.findMany({
where: {
isSubscribedToCoverMe: true
}
});
let targetEmails = await data.getCoverMePublisherEmails(assignment.shift.id);
if (!toSubscribed) {
targetEmails.subscribedPublishers = [];
}
if (!toAvailable) {
targetEmails.availablePublishers = [];
}
// let subscribedPublishers = targetEmails.subscribedPublishers, availablePublishers = [];
// if (toSubscribed) {
// //get all subscribed publisers
// subscribedPublishers = await prisma.publisher.findMany({
// where: {
// isSubscribedToCoverMe: true
// }
// });
// }
// if (toAvailable) {
// availablePublishers = await data.filterPublishersNew("id,firstName,lastName,email", new Date(assignment.shift.startTime),
// true, false);
// }
// use
if (toAvailable) {
availablePublishers = await data.filterPublishersNew("id,firstName,lastName,email", new Date(assignment.shift.startTime), true, false);
}
//concat and remove duplicate emails
let pubsToSend = subscribedPublishers.concat(availablePublishers).
let pubsToSend = targetEmails.subscribedPublishers.concat(targetEmails.availablePublishers).
filter((item, index, self) =>
index === self.findIndex((t) => (
t.email === item.email && item.email !== publisher.email//and exclude the user himself
@ -284,7 +297,7 @@ export default async function handler(req, res) {
);
console.log("Sending CoverMe request to " + pubsToSend.length + " publishers");
await prisma.eventLog.create({
let eventLog = await prisma.eventLog.create({
data: {
date: new Date(),
publisher: { connect: { id: publisher.id } },
@ -294,6 +307,7 @@ export default async function handler(req, res) {
+ "до: " + pubsToSend.map(p => p.firstName + " " + p.lastName + "<" + p.email + ">").join(", "),
}
});
logger.info("User: " + publisher.email + " sent a 'CoverMe' request for his assignment " + assignmentId + " - " + assignment.shift.cartEvent.location.name + " " + assignment.shift.startTime.toISOString() + " to " + pubsToSend.map(p => p.firstName + " " + p.lastName + "<" + p.email + ">").join(", ") + ". EventLogId: " + eventLog.id + "");
//send email to all subscribed publishers
for (let i = 0; i < pubsToSend.length; i++) {

View File

@ -2,8 +2,9 @@ import { getToken } from "next-auth/jwt";
import { NextApiRequest, NextApiResponse } from 'next'
import { DayOfWeek, AvailabilityType } from '@prisma/client';
const common = require('../../src/helpers/common');
const data = require('../../src/helpers/data');
const dataHelper = require('../../src/helpers/data');
const subq = require('../../prisma/bl/subqueries');
import { addMinutes } from 'date-fns';
import fs from 'fs';
import path from 'path';
@ -171,7 +172,7 @@ export default async function handler(req, res) {
// find publisher by full name or email
case "findPublisher":
const getAll = common.parseBool(req.query.all) || false;
let publisher = await data.findPublisher(filter, req.query.email, req.query.select, getAll);
let publisher = await dataHelper.findPublisher(filter, req.query.email, req.query.select, getAll);
res.status(200).json(publisher);
break;
@ -350,41 +351,8 @@ export default async function handler(req, res) {
break;
case "getPossibleShiftPublisherEmails":
const subscribedPublishers = await prisma.publisher.findMany({
where: {
isSubscribedToCoverMe: true
},
select: {
id: true,
firstName: true,
lastName: true,
email: true
}
}).then(pubs => {
return pubs.map(pub => {
return {
id: pub.id,
name: pub.firstName + " " + pub.lastName,
email: pub.email
}
});
});
let shift = await prisma.shift.findUnique({
where: {
id: parseInt(req.query.shiftId)
}
});
let availablePublishers = await filterPublishersNew_Available("id,firstName,lastName,email", new Date(shift.startTime), true, false);
//return names and email info only
availablePublishers = availablePublishers.map(pub => {
return {
id: pub.id,
name: pub.firstName + " " + pub.lastName,
email: pub.email
}
});
res.status(200).json({ shift, availablePublishers: availablePublishers, subscribedPublishers });
let data = await dataHelper.getCoverMePublisherEmails(parseInt(req.query.shiftId));
res.status(200).json(data);
break;
default:
@ -464,7 +432,7 @@ export async function getMonthlyStatistics(selectFields, filterDate) {
export async function filterPublishersNew_Available(selectFields, filterDate, isExactTime = false, isForTheMonth = false, isWithStats = true, includeOldAvailabilities = false) {
return data.filterPublishersNew(selectFields, filterDate, isExactTime, isForTheMonth, isWithStats, includeOldAvailabilities);
return dataHelper.filterPublishersNew(selectFields, filterDate, isExactTime, isForTheMonth, false, isWithStats, includeOldAvailabilities);
}
// availabilites filter:
@ -837,8 +805,48 @@ function matchesAvailability(avail, filterDate) {
async function getCalendarEvents(publisherId, date, availabilities = true, assignments = true) {
const result = [];
let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", date, assignments, availabilities, date ? true : false, publisherId);
let publisher = pubs[0];
// let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", date, assignments, availabilities, date ? true : false, publisherId);
const prisma = common.getPrismaClient();
let publisher = await prisma.publisher.findUnique({
where: {
id: publisherId
},
select: {
id: true,
firstName: true,
lastName: true,
email: true,
availabilities: {
select: {
id: true,
dayOfMonth: true,
dayofweek: true,
weekOfMonth: true,
startTime: true,
endTime: true,
name: true,
isFromPreviousAssignment: true,
isFromPreviousMonth: true,
repeatWeekly: true,
}
},
assignments: {
select: {
id: true,
shift: {
select: {
id: true,
startTime: true,
endTime: true,
isPublished: true
}
}
}
}
}
});
if (publisher) {
if (availabilities) {
publisher.availabilities?.forEach(item => {

View File

@ -603,7 +603,7 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
message="Това ще изпрати имейли до всички участници за смените им през избрания месец. Сигурни ли сте?"
/>
<button
className={`button btn m-2 ${isPublished ? 'hover:bg-gray-100 bg-yellow-500' : 'hover:bg-red-300 bg-blue-400'}`}
className={`button btn m-2 ${isPublished ? 'hover:bg-gray-500 bg-yellow-500' : 'hover:bg-red-300 bg-blue-400'}`}
onClick={togglePublished}>
<i className={`fas ${isPublished ? 'fa-check' : 'fa-close'} mr-2`}></i>
{isPublished ? "Скрий" : "Публикувай"} графика (м.)

View File

@ -3,6 +3,8 @@ import NewPubPage from "../new";
export default NewPubPage;
import { Assignment, Shift, UserRole, AvailabilityType } from "prisma/prisma-client";
//import ProtectedRoute from '../../../../components/protectedRoute';
// import { monthNamesBG } from "~/src/helpers/const"
import { monthNamesBG } from "src/helpers/const";
@ -38,6 +40,7 @@ function getShiftGroups(shifts: [Shift]) {
export const getServerSideProps = async (context) => {
const axios = await axiosServer(context);
// const isAdmin = await ProtectedRoute.IsInRole(UserRole.ADMIN); does not work on server side
context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate");
if (!context.query || !context.query.id) {
return {
@ -50,11 +53,9 @@ export const getServerSideProps = async (context) => {
// item.allShifts = item.assignments.map((a: Assignment[]) => a.shift);
//group shifts by month, remove duplicates
//sort availabilities by start time
// item.availabilities = item.availabilities
// .sort((a, b) => b.startTime - a.startTime);
// ============================================================
// don't show past availabilities (if user not admin) ToDo: add admin check
// ============================================================
item.availabilities = item.availabilities.filter((a) => new Date(a.startTime) >= new Date() || a.type == AvailabilityType.Weekly);
item.assignments = item.assignments
.sort((a, b) => b.startTime - a.startTime)

View File

@ -127,7 +127,7 @@ export const getServerSideProps = async (context) => {
const prisma = common.getPrismaClient();
const dateStr = new Date().toISOString().split('T')[0];
let publishers = await data.filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth,lastLogin', dateStr, false, true, true, true);
let publishers = await data.filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth,lastLogin', dateStr, false, true, true, true, true);
// const axios = await axiosServer(context);
// const { data: publishers } = await axios.get(`api/?action=filterPublishers&assignments=true&availabilities=true&date=${dateStr}&select=id,firstName,lastName,isActive,desiredShiftsPerMonth`);

View File

@ -3,9 +3,8 @@ import Layout from "../components/layout"
import AvCalendar from '../components/calendar/avcalendar';
import { getSession } from "next-auth/react";
import common from '../src/helpers/common';
import { Availability } from "@prisma/client";
import { Availability, UserRole } from "@prisma/client";
import ProtectedRoute, { serverSideAuth } from "../components/protectedRoute";
import { UserRole } from "@prisma/client";
import React, { useState, useEffect } from 'react';
import axiosInstance from '../src/axiosSecure';
@ -56,6 +55,15 @@ export default function IndexPage({ initialItems, initialUserId }: IProps) {
}
};
// EXAMPLE USAGE OF ProtectedRoute
ProtectedRoute.IsInRole(UserRole.ADMIN).then(isAdmin => {
if (isAdmin) {
console.log("User is an admin.");
} else {
console.log("User is not an admin.");
}
});
return (
<Layout>
<ProtectedRoute deniedMessage="">

View File

@ -8,6 +8,8 @@ const data = require("./src/helpers/data");
const sharp = require('sharp');
const https = require('https');
const logger = require('./src/logger');
//const getPort = require("get-port");
let getPort;
@ -16,6 +18,7 @@ import('get-port').then(module => {
// Initialize your server or other logic that relies on getPort here
});
process.env.TZ = 'Europe/Sofia';
// Global variable to store the base URL
// let baseUrlGlobal;
@ -42,6 +45,12 @@ console.log("process.env.DATABASE_URL = ", process.env.DATABASE_URL);
console.log("process.env.DATABASE = ", process.env.DATABASE);
console.log("process.env.APPLE_APP_ID = ", process.env.APPLE_APP_ID);
logger.info("App started on " + process.env.PROTOCOL + "://" + process.env.HOST + ":" + process.env.PORT + "");
logger.info("process.env.GIT_COMMIT_ID = " + process.env.GIT_COMMIT_ID);
logger.info("process.env.APP_ENV = " + process.env.APP_ENV);
logger.info("process.env.NODE_ENV = " + process.env.NODE_ENV);
logger.info("process.env.APPLE_APP_ID = " + process.env.APPLE_APP_ID);
logger.info("process.env.EMAIL_SERVICE = " + process.env.EMAIL_SERVICE);
// update GIT_COMMIT_ID
const { exec } = require("child_process");

View File

@ -78,7 +78,7 @@ exports.getPrismaClient = function getPrismaClient() {
if (!prisma) {
logger.debug("getPrismaClient: process.env.DATABASE = ", process.env.DATABASE);
prisma = new PrismaClient({
// Optional: Enable logging
// Optional: Enable Prisma logging
//log: ['query', 'info', 'warn', 'error'],
datasources: { db: { url: process.env.DATABASE } },
});
@ -757,6 +757,11 @@ exports.getInitials = function (names) {
const parts = names.split(' ');
return parts.map(part => part[0] + ".").join('');
}
// ? import { addMinutes } from 'date-fns';
exports.addMinutes = function (date, minutes) {
return new Date(date.getTime() + minutes * 60000); // 60000 milliseconds in a minute
}
// exports.getInitials = function (names) {
// const parts = names.split(' '); // Split the full name into parts
// if (parts.length === 0) {

View File

@ -230,8 +230,14 @@ async function getAvailabilities(userId) {
async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, noEndDateFilter = false, isWithStats = true, includeOldAvailabilities = false) {
const prisma = common.getPrismaClient();
filterDate = new Date(filterDate); // Convert to date object if not already
const monthInfo = common.getMonthDatesInfo(filterDate);
let prevMnt = new Date(filterDate)
prevMnt.setMonth(prevMnt.getMonth() - 1);
const prevMonthInfo = common.getMonthDatesInfo(prevMnt);
// Only attempt to split if selectFields is a string; otherwise, use it as it is.
selectFields = typeof selectFields === 'string' ? selectFields.split(",") : selectFields;
@ -240,6 +246,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
return acc;
}, {});
selectBase.assignments = {
select: {
id: true,
@ -254,119 +261,91 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
where: {
shift: {
startTime: {
gte: filterDate,
gte: prevMnt,
}
}
}
};
var monthInfo = common.getMonthDatesInfo(filterDate);
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
let dayOfWeekEnum = common.getDayOfWeekNameEnEnumForDate(filterDate);
if (!isExactTime) {
filterDate.setHours(0, 0, 0, 0); // Set to midnight
}
const filterDateEnd = new Date(filterDate);
filterDateEnd.setHours(23, 59, 59, 999);
let filterTimeFrom = new Date(filterDate)
let filterTimeTo = new Date(filterDate);
let isDayFilter = true;
let whereClause = {};
//if full day, match by date only
if (!isExactTime) { // Check only by date without considering time ( Assignments on specific days without time)
whereClause["availabilities"] = {
some: {
OR: [
{
startTime: { gte: filterDate },
endTime: { lte: filterDateEnd },
}
,
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
// This includes availabilities from previous assignments but not with preference
{
dayOfMonth: null, // includes monthly and weekly repeats
dayofweek: dayOfWeekEnum,
// ToDo: and weekOfMonth
startTime: { lte: filterDate },
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
}
};
}
//if not full day, match by date and time
else {
//match exact time (should be same as data.findPublisherAvailability())
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is set and filterDate is between start and end dates (Assignments on specific days AND time)
{
// dayOfMonth: filterDate.getDate(),
startTime: { lte: filterDate },
endTime: { gte: filterDate }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
dayofweek: dayOfWeekEnum,
startTime: { gte: filterDate },
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
}
};
}
if (isForTheMonth) {
// If no filter date, return all publishers's availabilities for currentMonthStart
var weekNr = common.getWeekOfMonth(filterDate); //getWeekNumber
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
{
dayOfMonth: { not: null },
startTime: { gte: monthInfo.firstMonday },
// endTime: { lte: monthInfo.lastSunday }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterDate } },
{ endDate: null }
]
}
]
}
]
filterTimeFrom = monthInfo.firstMonday;
filterTimeTo = monthInfo.lastSunday;
isDayFilter = false;
}
if (isExactTime) {
//add +- 90 minutes to the filterDate ToDo: should be "shift duration"
// filterTimeFrom.setMinutes(filterTimeFrom.getMinutes() - 90);
filterTimeTo.setMinutes(filterTimeTo.getMinutes() + 90);
}
else {
filterTimeFrom.setHours(0, 0, 0, 0);
filterTimeTo.setHours(23, 59, 59, 999);
}
whereClause["availabilities"] = {
some: {
OR: [
// Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
{
//dayOfMonth: { not: null },
startTime: { gte: filterTimeFrom },
// endTime: { lte: monthInfo.lastSunday }
},
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{
dayOfMonth: null,
AND: [
{
OR: [ // OR condition for repeatUntil to handle events that either end after filterDate or repeat forever
{ endDate: { gte: filterTimeFrom } }, // endDate included
{ endDate: null }
]
}
]
}
]
}
};
/* FILTERS
1. exact time
2. exact date
3. the month
4. from start date only
*/
if (noEndDateFilter) {
isDayFilter = false;
}
else {
whereClause["availabilities"].some.OR[0].endTime = { lte: filterTimeTo };
if (isForTheMonth) {
// no dayofweek or time filters here
}
else {
let dayOfWeekEnum = common.getDayOfWeekNameEnEnumForDate(filterDate);
whereClause["availabilities"].some.OR[1].dayofweek = dayOfWeekEnum;
//NOTE: we filter by date after we calculate the correct dates post query
if (isExactTime) {
//if exact time we need the availability to be starting on or before start of the shift and ending on or after the end of the shift
whereClause["availabilities"].some.OR[0].startTime = { lte: filterTimeFrom };
whereClause["availabilities"].some.OR[0].endTime = { gte: filterTimeTo };
}
else {
}
};
if (!noEndDateFilter) { // Check if we need to apply the endTime filter
whereClause["availabilities"].some.OR[0].endTime = { lte: monthInfo.lastSunday };
}
}
console.log(`getting publishers for date: ${filterDate}, isExactTime: ${isExactTime}, isForTheMonth: ${isForTheMonth}`);
//include availabilities if flag is true
const prisma = common.getPrismaClient(); //why we need to get it again?
let publishers = await prisma.publisher.findMany({
where: whereClause,
select: {
@ -377,7 +356,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
console.log(`publishers: ${publishers.length}, WhereClause: ${JSON.stringify(whereClause)}`);
// convert matching weekly availabilities to availabilities for the day to make furter processing easier on the client.
// convert matching weekly availabilities to availabilities for the day to make further processing easier on the client.
// we trust that the filtering was OK, so we use the dateFilter as date.
publishers.forEach(pub => {
pub.availabilities = pub.availabilities.map(avail => {
@ -397,28 +376,20 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
});
let currentWeekStart, currentWeekEnd,
currentMonthStart, currentMonthEnd,
previousMonthStart, previousMonthEnd;
let currentWeekStart, currentWeekEnd;
if (isWithStats) {
currentWeekStart = common.getStartOfWeek(filterDate);
currentWeekEnd = common.getEndOfWeek(filterDate);
currentMonthStart = monthInfo.firstMonday;
currentMonthEnd = monthInfo.lastSunday;
let prevMnt = new Date(filterDate)
prevMnt.setMonth(prevMnt.getMonth() - 1);
monthInfo = common.getMonthDatesInfo(prevMnt);
previousMonthStart = monthInfo.firstMonday;
previousMonthEnd = monthInfo.lastSunday;
//get if publisher has assignments for current weekday, week, current month, previous month
publishers.forEach(pub => {
// Filter assignments for current day
pub.currentDayAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= filterDate && assignment.shift.startTime <= filterDateEnd;
}).length;
if (isDayFilter) {
pub.currentDayAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= filterDate && assignment.shift.startTime <= filterTimeTo;
}).length;
}
// Filter assignments for current week
pub.currentWeekAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= currentWeekStart && assignment.shift.startTime <= currentWeekEnd;
@ -426,26 +397,26 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
// Filter assignments for current month
pub.currentMonthAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= currentMonthStart && assignment.shift.startTime <= currentMonthEnd;
return assignment.shift.startTime >= monthInfo.firstMonday && (noEndDateFilter || assignment.shift.startTime <= monthInfo.lastSunday);
}).length;
// Filter assignments for previous month
pub.previousMonthAssignments = pub.assignments?.filter(assignment => {
return assignment.shift.startTime >= previousMonthStart && assignment.shift.startTime <= previousMonthEnd;
return assignment.shift.startTime >= prevMonthInfo.firstMonday && assignment.shift.startTime <= prevMonthInfo.lastSunday;
}).length;
});
}
//get the availabilities for the day. Calcullate:
//get the availabilities for the day. Calculate:
//1. how many days the publisher is available for the current month - only with dayOfMonth
//2. how many days the publisher is available without dayOfMonth (previous months count)
//3. how many hours in total the publisher is available for the current month
publishers.forEach(pub => {
if (isWithStats) {
pub.currentMonthAvailability = pub.availabilities?.filter(avail => {
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= currentMonthEnd;
return avail.startTime >= currentMonthStart && avail.startTime <= currentMonthEnd;
// return avail.dayOfMonth != null && avail.startTime >= currentMonthStart && avail.startTime <= monthInfo.lastSunday;
return avail.startTime >= monthInfo.firstMonday && (noEndDateFilter || avail.startTime <= monthInfo.lastSunday);
})
pub.currentMonthAvailabilityDaysCount = pub.currentMonthAvailability.length || 0;
// pub.currentMonthAvailabilityDaysCount += pub.availabilities.filter(avail => {
@ -456,7 +427,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
}, 0);
//if pub has up-to-date availabilities (with dayOfMonth) for the current month
pub.hasUpToDateAvailabilities = pub.availabilities?.some(avail => {
return avail.dayOfMonth != null && avail.startTime >= currentMonthStart; // && avail.startTime <= currentMonthEnd;
return avail.dayOfMonth != null && avail.startTime >= monthInfo.firstMonday; // && avail.startTime <= monthInfo.lastSunday;
});
}
@ -465,38 +436,118 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
pub.hasEverFilledForm = pub.availabilities?.some(avail => {
return avail.isFromPreviousAssignments == false;
});
//if pub has availabilities for the current day
pub.hasAvailabilityForCurrentDay = pub.availabilities?.some(avail => {
return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
});
if (isDayFilter) {
//if pub has availabilities for the current day
pub.hasAvailabilityForCurrentDay = pub.availabilities?.some(avail => {
return avail.startTime >= filterDate && avail.startTime <= filterTimeTo;
});
}
});
if (isExactTime) {
// Post filter for time if dayOfMonth is null as we can't only by time for multiple dates in SQL
// Modify the availabilities array of the filtered publishers
//HERE WE FILTER by time for repeating availabilities. We can't do that if we don't have
// whereClause["availabilities"].some.OR[1].startTime = { gte: filterTimeFrom };
// whereClause["availabilities"].some.OR[1].endTime = { gte: filterTimeTo }
publishers.forEach(pub => {
pub.availabilities = pub.availabilities?.filter(avail => matchesAvailability(avail, filterDate));
pub.availabilities.filter(a => a.startTime > filterTimeFrom && a.endTime < filterTimeTo)
});
publishers.filter(pub => pub.availabilities.length > 0);
}
// if (isExactTime) {
// // Post filter for time if dayOfMonth is null as we can't only by time for multiple dates in SQL
// // Modify the availabilities array of the filtered publishers
// publishers.forEach(pub => {
// pub.availabilities = pub.availabilities?.filter(avail => matchesAvailability(avail, filterDate));
// });
// }
return publishers;
}
// exports.getCoverMePublisherEmails = async function (shiftId) {
async function getCoverMePublisherEmails(shiftId) {
const prisma = common.getPrismaClient();
let subscribedPublishers = await prisma.publisher.findMany({
where: {
isSubscribedToCoverMe: true
},
select: {
id: true,
firstName: true,
lastName: true,
email: true
}
}).then(pubs => {
return pubs.map(pub => {
return {
id: pub.id,
firstName: pub.firstName,
lastName: pub.lastName,
name: pub.firstName + " " + pub.lastName,
email: pub.email
}
});
});
function matchesAvailability(avail, filterDate) {
// Setting the start and end time of the filterDate
filterDate.setHours(0, 0, 0, 0);
const filterDateEnd = new Date(filterDate);
filterDateEnd.setHours(23, 59, 59, 999);
let shift = await prisma.shift.findUnique({
where: {
id: shiftId
},
include: {
assignments: {
include: {
publisher: {
select: {
id: true,
firstName: true,
lastName: true,
email: true
}
}
}
}
}
});
let availableIn = new Date(shift.startTime)
let availablePublishers = await filterPublishersNew("id,firstName,lastName,email", availableIn,
true, false, false, false, false);
// Return true if avail.startTime is between filterDate and filterDateEnd
return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
//filter out publishers that are already assigned to the shift
availablePublishers = availablePublishers.filter(pub => {
return shift.assignments.findIndex(assignment => assignment.publisher.id == pub.id) == -1;
});
//subscribed list includes only publishers that are not already assigned to the shift
subscribedPublishers = subscribedPublishers.filter(pub => {
return availablePublishers.findIndex(availablePub => availablePub.id == pub.id) == -1
&& shift.assignments.findIndex(assignment => assignment.publisher.id == pub.id) == -1;
});
//return names and email info only
availablePublishers = availablePublishers.map(pub => {
return {
id: pub.id,
firstName: pub.firstName,
lastName: pub.lastName,
name: pub.firstName + " " + pub.lastName,
email: pub.email
}
});
return { shift, availablePublishers: availablePublishers, subscribedPublishers };
}
// function matchesAvailability(avail, filterDate) {
// // Setting the start and end time of the filterDate
// filterDate.setHours(0, 0, 0, 0);
// const filterDateEnd = new Date(filterDate);
// filterDateEnd.setHours(23, 59, 59, 999);
// // Return true if avail.startTime is between filterDate and filterDateEnd
// return avail.startTime >= filterDate && avail.startTime <= filterDateEnd;
// }
const fs = require('fs');
const path = require('path');
@ -527,5 +578,6 @@ module.exports = {
findPublisherAvailability,
runSqlFile,
getAvailabilities,
filterPublishersNew
filterPublishersNew,
getCoverMePublisherEmails
};

View File

@ -27,8 +27,7 @@ if (process.env.EMAIL_SERVICE.toLowerCase() === "mailtrap") {
}
});
}
if (process.env.EMAIL_SERVICE.toLowerCase() === "gmail") {
else if (process.env.EMAIL_SERVICE.toLowerCase() === "gmail") {
transporter = nodemailer.createTransport({
service: "gmail",
auth: {
@ -71,7 +70,15 @@ function normalizeEmailAddresses(to) {
return emails; // Always returns an array
}
/// <summary>
/// Final email sending function.
/// </summary>
/// <param name="to">Email address or array of email addresses</param>
/// <param name="subject">Email subject</param>
/// <param name="text">Plain text version of the email</param>
/// <param name="html">HTML version of the email</param>
/// <param name="attachments">Array of attachment objects</param>
/// <returns>Promise</returns>
exports.SendEmail = async function (to, subject, text, html, attachments = []) {
let sender = process.env.EMAIL_SENDER || '"Специално Свидетелстване София" <sofia@mwitnessing.com>';

View File

@ -5,7 +5,7 @@ const logConfiguration = {
'transports': [
new winston.transports.DailyRotateFile({
filename: './logs/application-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
datePattern: 'YYYY-MM-DD', // new file is created every hour: 'YYYY-MM-DD-HH'
zippedArchive: true,
maxSize: '20m',
maxFiles: '90d',

View File

@ -1,9 +1,9 @@
{{!--Subject: ССОМ: Нужен е заместник --}}
<section>
<h3>Търси се зместник:
{{!-- <h3>Търси се зместник: --}}
{{!-- за смяна на {{placeName}} за {{dateStr}}! --}}
</h3>
{{!-- </h3> --}}
<p>Здравей {{firstName}},</p>
<p>{{user.prefix}} {{user.firstName}} {{user.lastName}} търси заместник.</p>
{{!-- <p><strong>Shift Details:</strong></p> --}}