fix statistics;
rewrite availability filters; fix availability filters; ProtectedRoute.IsInRole helper
This commit is contained in:
@@ -272,7 +272,8 @@ export default async function handler(req, res) {
|
||||
|
||||
|
||||
if (toAvailable) {
|
||||
availablePublishers = await data.filterPublishersNew("id,firstName,lastName,email", new Date(assignment.shift.startTime), true, false);
|
||||
availablePublishers = await data.filterPublishersNew("id,firstName,lastName,email", new Date(assignment.shift.startTime),
|
||||
true, false);
|
||||
|
||||
}
|
||||
//concat and remove duplicate emails
|
||||
|
@@ -4,6 +4,7 @@ import { DayOfWeek, AvailabilityType } from '@prisma/client';
|
||||
const common = require('../../src/helpers/common');
|
||||
const data = require('../../src/helpers/data');
|
||||
const subq = require('../../prisma/bl/subqueries');
|
||||
import { addMinutes } from 'date-fns';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
@@ -350,7 +351,7 @@ export default async function handler(req, res) {
|
||||
|
||||
break;
|
||||
case "getPossibleShiftPublisherEmails":
|
||||
const subscribedPublishers = await prisma.publisher.findMany({
|
||||
let subscribedPublishers = await prisma.publisher.findMany({
|
||||
where: {
|
||||
isSubscribedToCoverMe: true
|
||||
},
|
||||
@@ -373,10 +374,37 @@ export default async function handler(req, res) {
|
||||
let shift = await prisma.shift.findUnique({
|
||||
where: {
|
||||
id: parseInt(req.query.shiftId)
|
||||
},
|
||||
include: {
|
||||
assignments: {
|
||||
include: {
|
||||
publisher: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
email: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let availablePublishers = await filterPublishersNew_Available("id,firstName,lastName,email", new Date(shift.startTime), true, false);
|
||||
let availableIn = new Date(addMinutes(shift.startTime, 0))
|
||||
let availablePublishers = await filterPublishersNew_Available("id,firstName,lastName,email", availableIn,
|
||||
true, false, false);
|
||||
|
||||
//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,
|
||||
|
@@ -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 ? "Скрий" : "Публикувай"} графика (м.)
|
||||
|
@@ -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)
|
||||
|
@@ -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`);
|
||||
|
@@ -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="">
|
||||
|
Reference in New Issue
Block a user