refactor stats page API

This commit is contained in:
Dobromir Popov
2024-04-13 19:48:58 +03:00
parent 3dcc1862cd
commit 7eeb55af88
2 changed files with 12 additions and 5 deletions

View File

@ -4,7 +4,10 @@ import ProtectedRoute from '../../../components/protectedRoute';
import { UserRole } from '@prisma/client'; import { UserRole } from '@prisma/client';
import axiosServer from '../../../src/axiosServer'; import axiosServer from '../../../src/axiosServer';
import common from '../../../src/helpers/common'; import common from '../../../src/helpers/common';
import { filterPublishers, /* other functions */ } from '../../api/index'; // import { filterPublishers, /* other functions */ } from '../../api/index';
import data from '../../../src/helpers/data';
// const data = require('../../src/helpers/data');
function ContactsPage({ publishers }) { function ContactsPage({ publishers }) {
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
@ -74,7 +77,7 @@ export default ContactsPage;
export const getServerSideProps = async (context) => { export const getServerSideProps = async (context) => {
const dateStr = new Date().toISOString().split('T')[0]; const dateStr = new Date().toISOString().split('T')[0];
let publishers = await filterPublishers('id,firstName,lastName,email,isActive,desiredShiftsPerMonth', "", new Date(), true, true, false); let publishers = await data.filterPublishersNew('id,firstName,lastName,email,isActive,desiredShiftsPerMonth', dateStr, false, true, true);
// const axios = await axiosServer(context); // 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`); // const { data: publishers } = await axios.get(`api/?action=filterPublishers&assignments=true&availabilities=true&date=${dateStr}&select=id,firstName,lastName,isActive,desiredShiftsPerMonth`);

View File

@ -227,8 +227,11 @@ async function getAvailabilities(userId) {
} }
async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, isWithStats = true) { async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, isWithStats = true) {
filterDate = new Date(filterDate); // Convert to date object if not already
// Only attempt to split if selectFields is a string; otherwise, use it as it is. // Only attempt to split if selectFields is a string; otherwise, use it as it is.
selectFields = typeof selectFields === 'string' ? selectFields.split(",") : selectFields; selectFields = typeof selectFields === 'string' ? selectFields.split(",") : selectFields;
@ -330,14 +333,15 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
} }
if (isForTheMonth) { if (isForTheMonth) {
// If no filter date, return all publishers's availabilities for currentMonthStart // If no filter date, return all publishers's availabilities for currentMonthStart
whereClause["availabilities"] = { whereClause["availabilities"] = {
some: { some: {
OR: [ OR: [
// Check if dayOfMonth is not null and startTime is after currentMonthStart (Assignments on specific days AND time) // Check if dayOfMonth is not null and startTime is after monthInfo.firstMonday (Assignments on specific days AND time)
{ {
dayOfMonth: { not: null }, dayOfMonth: { not: null },
startTime: { gte: currentMonthStart }, startTime: { gte: monthInfo.firstMonday },
endTime: { lte: currentMonthEnd } endTime: { lte: monthInfo.lastSunday }
}, },
// Check if dayOfMonth is null and match by day of week using the enum (Assigments every week) // Check if dayOfMonth is null and match by day of week using the enum (Assigments every week)
{ {