Merge branch 'main' into feature-fixStats

This commit is contained in:
Dobromir Popov
2024-05-11 16:33:41 +03:00
67 changed files with 2703 additions and 881 deletions

View File

@ -45,15 +45,22 @@ export const authOptions: NextAuthOptions = {
}
}
}),
// AppleProvider({
// clientId: process.env.APPLE_APP_ID,
// clientSecret: process.env.APPLE_SECRET
// }),
// AzureADProvider({
// clientId: process.env.AZURE_AD_CLIENT_ID,
// clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
// tenantId: process.env.AZURE_AD_TENANT_ID,
// }),
AppleProvider({
// clientId: process.env.APPLE_APP_ID,
// clientSecret: process.env.APPLE_SECRET
clientId: process.env.APPLE_APP_ID,
clientSecret: {
appleId: process.env.APPLE_APP_ID,
teamId: process.env.APPLE_TEAM_ID,
privateKey: process.env.APPLE_PK,
keyId: process.env.APPLE_KEY_ID,
}
}),
AzureADProvider({
clientId: process.env.AZURE_AD_CLIENT_ID,
clientSecret: process.env.AZURE_AD_CLIENT_SECRET,
tenantId: process.env.AZURE_AD_TENANT_ID,
}),
CredentialsProvider({
id: 'credentials',
// The name to display on the sign in form (e.g. 'Sign in with...')
@ -79,9 +86,9 @@ export const authOptions: NextAuthOptions = {
// // Return null if user data could not be retrieved
// return null
const users = [
{ id: "1", name: "admin", email: "admin@example.com", password: "admin123", role: "ADMIN" },
{ id: "2", name: "krasi", email: "krasi@example.com", password: "krasi123", role: "ADMIN" },
{ id: "3", name: "popov", email: "popov@example.com", password: "popov123", role: "ADMIN" }
{ id: "1", name: "admin", email: "admin@example.com", password: "admin123", role: "ADMIN", static: true },
{ id: "2", name: "krasi", email: "krasi@example.com", password: "krasi123", role: "ADMIN", static: true },
{ id: "3", name: "popov", email: "popov@example.com", password: "popov123", role: "ADMIN", static: true }
];
const user = users.find(user =>
@ -167,6 +174,10 @@ export const authOptions: NextAuthOptions = {
callbacks: {
// https://codevoweb.com/implement-authentication-with-nextauth-in-nextjs-14/
async signIn({ user, account, profile }) {
if (account.provider === 'credentials' && user?.static) {
return true;
}
var prisma = common.getPrismaClient();
console.log("[nextauth] signIn:", account.provider, user.email)
@ -240,7 +251,10 @@ export const authOptions: NextAuthOptions = {
session.user.role = token.role;
session.user.name = token.name || token.email;
}
if (user?.impersonating) {
// Add flag to session if user is being impersonated
session.user.impersonating = true;
}
// if (session?.user) {
// session.user.id = user.id; //duplicate
// }

View File

@ -27,6 +27,8 @@ export default async function handler(req, res) {
impersonating: true, // flag to indicate impersonation
originalUser: session.user // save the original user for later
};
// Log the event (simplified example)
console.log(`Admin ${session.user} impersonated user ${userToImpersonate.email} on ${new Date().toISOString()}`);
// Here you would typically use some method to create a session server-side
// For this example, we'll just send the impersonated session as a response

View File

@ -1,12 +1,77 @@
import path from 'path';
import { promises as fs } from 'fs';
import express from 'express';
import type { NextApiRequest, NextApiResponse } from 'next';
import nc from 'next-connect';
import { promises as fs } from 'fs';
import path from 'path';
import multer from 'multer';
import sharp from 'sharp';
import { createRouter } from 'next-connect';
const handler = nc({
onError: (err, req, res, next) => {
// Generalized Multer configuration
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const subfolder = req.query.subfolder ? decodeURIComponent(req.query.subfolder as string) : 'default';
const uploadPath = path.join(process.cwd(), 'public/content', subfolder);
fs.mkdir(uploadPath, { recursive: true })
.then(() => cb(null, uploadPath))
.catch(cb);
},
filename: (req, file, cb) => {
const filename = decodeURIComponent(file.originalname); // Ensure the filename is correctly decoded
const prefix = req.body.prefix ? decodeURIComponent(req.body.prefix) : path.parse(filename).name;
cb(null, `${prefix}${path.extname(filename)}`);
}
});
const fileFilter = (req, file, cb) => {
// Accept PDFs only
if (file.mimetype === 'application/pdf') {
cb(null, true);
} else {
cb(new Error('Only PDF files are allowed!'), false);
}
};
const upload = multer({ storage, fileFilter });
const router = createRouter<NextApiRequest, NextApiResponse>();
router.use(upload.array('file'));
router.post((req, res) => {
console.log(req.files); // Log files to see if PDFs are included
if (req.files.length === 0) {
return res.status(400).json({ error: 'No files were uploaded.' });
}
// Process uploaded files, assume images are being resized and saved
res.json({ message: 'Files uploaded successfully', files: req.files });
});
router.get(async (req, res) => {
// Implement functionality to list files
const directory = path.join(process.cwd(), 'public/content', req.query.subfolder as string);
try {
const files = await fs.readdir(directory);
const imageUrls = files.map(file => `/content/${req.query.subfolder}/${file}`);
res.json({ imageUrls });
} catch (err) {
res.status(500).json({ error: 'Internal Server Error', details: err.message });
}
});
router.delete(async (req, res) => {
// Implement functionality to delete a file
const filePath = path.join(process.cwd(), 'public/content', req.query.subfolder as string, req.query.file as string);
try {
await fs.unlink(filePath);
res.send('File deleted successfully.');
} catch (error) {
res.status(500).send('Failed to delete the file.');
}
});
export default router.handler({
onError: (err, req, res) => {
console.error(err.stack);
res.status(500).end('Something broke!');
},
@ -15,131 +80,8 @@ const handler = nc({
}
});
handler.use((req: NextApiRequest, res: NextApiResponse, next) => {
const subfolder = req.query.subfolder as string;
const upload = createUploadMiddleware(subfolder).array('image');
upload(req, res, (err) => {
if (err) {
return res.status(500).json({ error: 'Failed to upload files.', details: err.message });
}
next();
});
});
handler.post((req: NextApiRequest, res: NextApiResponse) => {
// Process uploaded files
// Example response
res.json({ message: 'Files uploaded successfully', files: req.files });
});
handler.get((req: NextApiRequest, res: NextApiResponse) => {
// Handle listing files
//listFiles(req, res, req.subfolder);
listFiles(req, res, req.query.subfolder as string);
});
handler.delete((req: NextApiRequest, res: NextApiResponse) => {
// Handle deleting files
deleteFile(req, res, req.query.subfolder as string);
});
export const config = {
api: {
bodyParser: false,
},
};
export default handler;
// ------------------------------------------------------------
//handling file uploads
import multer from 'multer';
import sharp from 'sharp';
// Generalized Multer configuration
export const createUploadMiddleware = (folder: string) => {
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadPath = path.join(process.cwd(), 'public/content', folder);
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath, { recursive: true });
}
cb(null, uploadPath);
},
filename: (req, file, cb) => {
const prefix = req.body.prefix || path.parse(file.originalname).name;
cb(null, `${prefix}${path.extname(file.originalname)}`);
}
});
return multer({ storage });
};
async function processFiles(req, res, folder) {
if (!req.files || req.files.length === 0) {
return res.status(400).json({ error: 'No files uploaded.' });
}
const uploadDir = path.join(process.cwd(), 'public/content', folder);
const thumbDir = path.join(uploadDir, "thumb");
if (!fs.existsSync(thumbDir)) {
fs.mkdirSync(thumbDir, { recursive: true });
}
try {
const processedFiles = await Promise.all(req.files.map(async (file) => {
const originalPath = path.join(uploadDir, file.filename);
const thumbPath = path.join(thumbDir, file.filename);
await sharp(file.path)
.resize({ width: 1920, fit: sharp.fit.inside, withoutEnlargement: true })
.jpeg({ quality: 80 })
.toFile(originalPath);
await sharp(file.path)
.resize(320, 320, { fit: sharp.fit.inside, withoutEnlargement: true })
.toFile(thumbPath);
fs.unlinkSync(file.path); // Remove temp file
return {
originalUrl: `/content/${folder}/${file.filename}`,
thumbUrl: `/content/${folder}/thumb/${file.filename}`
};
}));
res.json(processedFiles);
} catch (error) {
console.error('Error processing files:', error);
res.status(500).json({ error: 'Error processing files.' });
}
}
// List files in a directory
async function listFiles(req, res, folder) {
const directory = path.join(process.cwd(), 'public/content', folder);
try {
const files = await fs.promises.readdir(directory);
const imageUrls = files.map(file => `${req.protocol}://${req.get('host')}/content/${folder}/${file}`);
res.json({ imageUrls });
} catch (err) {
console.error('Error reading uploads directory:', err);
res.status(500).json({ error: 'Internal Server Error' });
}
}
// Delete a file
async function deleteFile(req, res, folder) {
const filename = req.query.file;
if (!filename) {
return res.status(400).send('Filename is required.');
}
try {
const filePath = path.join(process.cwd(), 'public/content', folder, filename);
await fs.unlink(filePath);
res.status(200).send('File deleted successfully.');
} catch (error) {
res.status(500).send('Failed to delete the file.');
}
}

View File

@ -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
}
@ -161,7 +162,7 @@ export default async function handler(req, res) {
newPubs: newPubs,
placeName: assignment.shift.cartEvent.location.name,
dateStr: common.getDateFormated(assignment.shift.startTime),
time: common.formatTimeHHmm(assignment.shift.startTime),
time: common.getTimeFormatted(assignment.shift.startTime),
sentDate: common.getDateFormated(new Date())
};
@ -383,7 +384,7 @@ export default async function handler(req, res) {
email: pubsToSend[i].email,
placeName: assignment.shift.cartEvent.location.name,
dateStr: common.getDateFormated(assignment.shift.startTime),
time: common.formatTimeHHmm(assignment.shift.startTime),
time: common.getTimeFormatted(assignment.shift.startTime),
sentDate: common.getDateFormated(new Date())
};
let results = emailHelper.SendEmailHandlebars(

View File

@ -1,6 +1,8 @@
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 } 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');
@ -9,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";
/**
*
@ -46,6 +49,9 @@ export default async function handler(req, res) {
let monthInfo = common.getMonthDatesInfo(day);
const searchText = req.query.searchText?.normalize('NFC');
const sessionServer = await getServerSession(req, res, authOptions)
var isAdmin = sessionServer?.user.role == UserRole.ADMIN
try {
switch (action) {
case "initDb":
@ -137,7 +143,7 @@ export default async function handler(req, res) {
break;
case "getCalendarEvents":
let events = await dataHelper.getCalendarEvents(req.query.publisherId, day);
let events = await dataHelper.getCalendarEvents(req.query.publisherId, true, true, isAdmin);
res.status(200).json(events);
case "getPublisherInfo":
@ -817,10 +823,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;
}

View File

@ -1,33 +1,139 @@
const webPush = require('web-push')
import common from '../../src/helpers/common';
//generate and store VAPID keys in .env.local if not already done
if (!process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY || !process.env.WEB_PUSH_PRIVATE_KEY) {
const { publicKey, privateKey } = webPush.generateVAPIDKeys()
console.log('VAPID keys generated:')
console.log('Public key:', publicKey)
console.log('Private key:', privateKey)
console.log('Store these keys in your .env.local file:')
console.log('NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY=', publicKey)
console.log('WEB_PUSH_PRIVATE_KEY=', privateKey)
process.exit(0)
}
webPush.setVapidDetails(
`mailto:${process.env.WEB_PUSH_EMAIL}`,
process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY,
process.env.WEB_PUSH_PRIVATE_KEY
)
const Notification = (req, res) => {
if (req.method == 'POST') {
const { subscription } = req.body
const Notification = async (req, res) => {
if (req.method == 'GET') {
res.statusCode = 200
res.setHeader('Allow', 'POST')
let subs = 0
if (req.query && req.query.id) {
const prisma = common.getPrismaClient();
const publisher = await prisma.publisher.findUnique({
where: { id: req.query.id },
select: { pushSubscription: true }
});
subs = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription.length : (publisher.pushSubscription ? 1 : 0);
res.end()
return
}
// send the public key in the response headers
//res.setHeader('Content-Type', 'text/plain')
res.send({ pk: process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY, subs })
res.end()
}
if (req.method == 'PUT') {
// store the subscription object in the database
// publisher.pushSubscription = subscription
const prisma = common.getPrismaClient();
const { subscription, id } = req.body
const publisher = await prisma.publisher.findUnique({
where: { id },
select: { pushSubscription: true }
});
webPush
.sendNotification(
subscription,
JSON.stringify({ title: 'Hello Web Push', message: 'Your web push notification is here!' })
)
.then(response => {
res.writeHead(response.statusCode, response.headers).end(response.body)
})
.catch(err => {
if ('statusCode' in err) {
res.writeHead(err.statusCode, err.headers).end(err.body)
} else {
console.error(err)
res.statusCode = 500
res.end()
}
})
let subscriptions = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription : (publisher.pushSubscription ? [publisher.pushSubscription] : []);
const index = subscriptions.findIndex(sub => sub.endpoint === subscription.endpoint);
if (index !== -1) {
subscriptions[index] = subscription; // Update existing subscription
} else {
subscriptions.push(subscription); // Add new subscription
}
await prisma.publisher.update({
where: { id },
data: { pushSubscription: subscriptions }
});
console.log('Subscription for publisher', id, 'updated:', subscription)
res.send({ subs: subscriptions.length })
res.statusCode = 200
res.end()
}
if (req.method == 'DELETE') {
// remove the subscription object from the database
// publisher.pushSubscription = null
const prisma = common.getPrismaClient();
const { subscriptionId, id } = req.body;
const publisher = await prisma.publisher.findUnique({
where: { id },
select: { pushSubscription: true }
});
let subscriptions = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription : (publisher.pushSubscription ? [publisher.pushSubscription] : []);
try {
subscriptions = subscriptionId ? subscriptions.filter(sub => sub.endpoint !== subscriptionId) : [];
await prisma.publisher.update({
where: { id },
data: { pushSubscription: subscriptions }
});
} catch (e) {
console.log(e)
await prisma.publisher.update({
where: { id },
data: { pushSubscription: null }
});
}
console.log('Subscription for publisher', id, 'deleted')
res.send({ subs: subscriptions.length })
res.statusCode = 200
res.end()
}
if (req.method == 'POST') {//title = "ССС", message = "Ще получите уведомление по този начин.")
const { subscription, id, broadcast, title = 'ССОМ', message = 'Ще получавате уведомления така.', actions } = req.body
if (broadcast) {
await broadcastPush(title, message, actions)
res.statusCode = 200
res.end()
return
}
else if (id) {
await sendPush(id, title, message.actions)
res.statusCode = 200
res.end()
return
} else if (subscription) {
await webPush
.sendNotification(
subscription,
JSON.stringify({ title, message, actions })
)
.then(response => {
res.writeHead(response.statusCode, response.headers).end(response.body)
})
.catch(err => {
if ('statusCode' in err) {
res.writeHead(err.statusCode, err.headers).end(err.body)
} else {
console.error(err)
res.statusCode = 500
res.end()
}
})
}
} else {
res.statusCode = 405
res.end()
@ -35,3 +141,54 @@ const Notification = (req, res) => {
}
export default Notification
//export pushNotification(userId or email) for use in other files
export const sendPush = async (id, title, message, actions) => {
const prisma = common.getPrismaClient();
const publisher = await prisma.publisher.findUnique({
where: { id }
})
if (!publisher.pushSubscription) {
console.log('No push subscription found for publisher', id)
return
}
await webPush
.sendNotification(
publisher.pushSubscription,
JSON.stringify({ title, message, actions })
)
.then(response => {
console.log('Push notification sent to publisher', id)
})
.catch(err => {
console.error('Error sending push notification to publisher', id, ':', err)
})
}
//export breoadcastNotification for use in other files
export const broadcastPush = async (title, message, actions) => {
const prisma = common.getPrismaClient();
const publishers = await prisma.publisher.findMany({
where: { pushSubscription: { not: null } }
})
for (const publisher of publishers) {
if (Array.isArray(publisher.pushSubscription) && publisher.pushSubscription.length) {
for (const subscription of publisher.pushSubscription) {
await webPush.sendNotification(
subscription, // Here subscription is each individual subscription object
JSON.stringify({ title, message, actions })
)
.then(response => {
console.log('Push notification sent to device', subscription.endpoint, 'of publisher', publisher.id);
})
.catch(err => {
console.error('Error sending push notification to device', subscription.endpoint, 'of publisher', publisher.id, ':', err);
// Optionally handle failed subscriptions, e.g., remove outdated or invalid subscriptions
});
}
} else {
console.log('No valid subscriptions found for publisher', publisher.id);
}
}
}