Merge commit '1936a9cb78a25629d8e4f3b4a107d2ad42e87a60' into production

This commit is contained in:
Dobromir Popov
2024-06-25 17:54:56 +03:00
12 changed files with 272 additions and 102 deletions

11
.vscode/launch.json vendored
View File

@ -60,6 +60,17 @@
"APP_ENV": "development.devserver" "APP_ENV": "development.devserver"
} }
}, },
{
"name": "!Run npm DEV (UI REDESIGN)",
"request": "launch",
"type": "node-terminal",
"cwd": "${workspaceFolder}",
"command": "npm run start-env",
"env": {
// "NODE_ENV": "test",
"APP_ENV": "development.devserver"
}
},
{ {
"name": "Run conda npm TEST", "name": "Run conda npm TEST",
"request": "launch", "request": "launch",

View File

@ -14,6 +14,15 @@ docker build -t dev-next-cart-app-img .devcontainer
docker run -d -v /path/to/your/project:/workspace --name dev-next-cart-app dev-next-cart-app-img docker run -d -v /path/to/your/project:/workspace --name dev-next-cart-app dev-next-cart-app-img
docker exec -it dev-next-cart-app /bin/bash docker exec -it dev-next-cart-app /bin/bash
##### ----------- setup on new linux macine ----------- ####
sudo apt remove nodejs libnode-dev
sudo apt purge nodejs libnode-dev
sudo apt autoremove -y
#
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
apt install nodejs -y
##### ----------------- compose/deploy ----------------- ### ##### ----------------- compose/deploy ----------------- ###
# install docker if inside docker (vscode-server)# apt-get update && apt-get install -y docker.io # install docker if inside docker (vscode-server)# apt-get update && apt-get install -y docker.io
# .10 > /mnt/apps/DEV/SSS/next-cart-app/next-cart-app/ # .10 > /mnt/apps/DEV/SSS/next-cart-app/next-cart-app/

View File

@ -0,0 +1,37 @@
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
this.logger = null;
if (typeof window === 'undefined') {
this.logger = require('../src/logger');
}
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, info) {
// Log the error to an error reporting service
console.error(error, info);
if (this.logger) {
this.logger.error(`${error}: ${info.componentStack}`);
}
}
render() {
if (this.state.hasError) {
// Render any custom fallback UI
return <h1>Нещо се обърка при изтриването. Моля, опитай отново и се свържете с нас ако проблема продължи. </h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;

View File

@ -35,6 +35,7 @@ function PwaManager({ subs }) {
useEffect(() => { useEffect(() => {
if (isSupported()) { if (isSupported()) {
setNotificationPermission(Notification.permission); setNotificationPermission(Notification.permission);
getSubscriptionCount();
} }
// Handle Push Notification Subscription // Handle Push Notification Subscription
@ -77,6 +78,10 @@ function PwaManager({ subs }) {
window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt); window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
window.removeEventListener('appinstalled', handleAppInstalled); window.removeEventListener('appinstalled', handleAppInstalled);
}; };
}, []); }, []);
@ -127,6 +132,7 @@ function PwaManager({ subs }) {
throw new Error("Failed to fetch VAPID public key from server."); throw new Error("Failed to fetch VAPID public key from server.");
} }
} }
const sub = await registration.pushManager.subscribe({ const sub = await registration.pushManager.subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: common.base64ToUint8Array(vapidPublicKey) applicationServerKey: common.base64ToUint8Array(vapidPublicKey)
@ -197,6 +203,19 @@ function PwaManager({ subs }) {
} }
}; };
const getSubscriptionCount = async () => {
try {
const response = await fetch('/api/notify?id=' + session.user.id, { method: 'GET' });
if (!response.ok) {
throw new Error('Failed to fetch subscription data.');
}
const result = await response.json();
setSubs(result.subs);
} catch (error) {
console.error('Error fetching subscription data:', error);
}
};
// Function to request push notification permission // Function to request push notification permission
const requestNotificationPermission = async (e) => { const requestNotificationPermission = async (e) => {
e.preventDefault(); e.preventDefault();
@ -243,48 +262,56 @@ function PwaManager({ subs }) {
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
//sends test notification to the current subscription body: JSON.stringify(
// body: JSON.stringify({ subscription }) {
//sends test notification to all subscriptions of this user id: session.user.id,
body: JSON.stringify({ id: session.user.id, title: "Тестово уведомление", message: "Това е тестово уведомление" }) title: "Тестово уведомление",
message: "Това е тестово уведомление",
actions: [{ action: 'test', title: 'Тест', icon: '✅' },
{ action: 'close', title: 'Затвори', icon: '❌' }]
})
}); });
}; };
async function sendTestReminder(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> { // async function sendTestReminder(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> {
event.preventDefault(); // event.preventDefault();
if (!subscription) { // if (!subscription) {
console.error('Web push not subscribed'); // console.error('Web push not subscribed');
return; // return;
} // }
await fetch('/api/notify', { // await fetch('/api/notify', {
method: 'POST', // method: 'POST',
headers: { // headers: {
'Content-Type': 'application/json' // 'Content-Type': 'application/json'
}, // },
body: JSON.stringify({ broadcast: true, message: "Мили братя, искаме да ви напомним да ни изпратите вашите предпочитания за юни до 25-то число като използвате меню 'Възможности'. Ако имате проблем, моля пишете ни на 'specialnosvidetelstvanesofia@gmail.com'" }) // body: JSON.stringify({
}); // broadcast: true,
} // message: "Мили братя, искаме да ви напомним да ни изпратите вашите предпочитания за юни до 25-то число като използвате меню 'Възможности'. Ако имате проблем, моля пишете ни на 'specialnosvidetelstvanesofia@gmail.com'"
// })
// });
// }
async function sendTestCoverMe(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> { // async function sendTestCoverMe(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> {
event.preventDefault(); // event.preventDefault();
if (!subscription) { // if (!subscription) {
console.error('Web push not subscribed'); // console.error('Web push not subscribed');
return; // return;
} // }
await fetch('/api/notify', { // await fetch('/api/notify', {
method: 'POST', // method: 'POST',
headers: { // headers: {
'Content-Type': 'application/json' // 'Content-Type': 'application/json'
}, // },
body: JSON.stringify({ // body: JSON.stringify({
broadcast: true, message: "Брат ТЕСТ търси заместник за 24-ти май от 10:00 ч. Можеш ли да го покриеш?", // id: session.user.id,
//use fontawesome icons for actions // message: "Брат ТЕСТ търси заместник за 24-ти май от 10:00 ч. Можеш ли да го покриеш?",
actions: [{ action: 'covermeaccepted', title: 'Да ', icon: '✅' }] // //use fontawesome icons for actions
}) // actions: [{ action: 'covermeaccepted', title: 'Да ', icon: '✅' }]
}); // })
} // });
// }
async function deleteAllSubscriptions(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> { async function deleteAllSubscriptions(event: MouseEvent<HTMLButtonElement, MouseEvent>): Promise<void> {
event.preventDefault(); event.preventDefault();
@ -358,22 +385,22 @@ function PwaManager({ subs }) {
</div> </div>
{isAdmin && {isAdmin &&
<div> <div>
<button {/* <button
onClick={sendTestReminder} onClick={sendTestReminder}
disabled={!isSubscribed} disabled={!isSubscribed}
className={`text-xs py-1 px-2 rounded-full focus:outline-none transition duration-150 ease-in-out ${!isSubscribed ? 'cursor-not-allowed bg-gray-300 text-gray-500' : 'bg-yellow-500 hover:bg-yellow-600 text-white' className={`text-xs py-1 px-2 rounded-full focus:outline-none transition duration-150 ease-in-out ${!isSubscribed ? 'cursor-not-allowed bg-gray-300 text-gray-500' : 'bg-yellow-500 hover:bg-yellow-600 text-white'
}`} }`}
> >
Broadcast Reminder Broadcast Reminder
</button> </button> */}
<button {/* <button
onClick={sendTestCoverMe} onClick={sendTestCoverMe}
disabled={!isSubscribed} disabled={!isSubscribed}
className={`text-xs py-1 px-2 rounded-full focus:outline-none transition duration-150 ease-in-out ${!isSubscribed ? 'cursor-not-allowed bg-gray-300 text-gray-500' : 'bg-yellow-500 hover:bg-yellow-600 text-white' className={`text-xs py-1 px-2 rounded-full focus:outline-none transition duration-150 ease-in-out ${!isSubscribed ? 'cursor-not-allowed bg-gray-300 text-gray-500' : 'bg-yellow-500 hover:bg-yellow-600 text-white'
}`} }`}
> >
Broadcast CoverMe Broadcast CoverMe
</button> </button> */}
</div> </div>
} }
{notificationPermission !== "granted" && ( {notificationPermission !== "granted" && (

View File

@ -10,6 +10,7 @@ import Body from 'next/document'
import { ToastContainer } from 'react-toastify'; import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css'; import 'react-toastify/dist/ReactToastify.css';
import { set } from "date-fns" import { set } from "date-fns"
import ErrorBoundary from "./ErrorBoundary";
export default function Layout({ children }) { export default function Layout({ children }) {
const router = useRouter(); const router = useRouter();
@ -61,7 +62,9 @@ export default function Layout({ children }) {
<Sidebar isSidebarOpen={isSidebarOpen} toggleSidebar={toggleSidebar} /> <Sidebar isSidebarOpen={isSidebarOpen} toggleSidebar={toggleSidebar} />
<main className={`flex-1 transition-all duration-300 ${marginLeftClass}`}> <main className={`flex-1 transition-all duration-300 ${marginLeftClass}`}>
<div className=""> <div className="">
{children} <ErrorBoundary>
{children}
</ErrorBoundary>
</div> </div>
<div id="modal-root"></div> {/* Modal container */} <div id="modal-root"></div> {/* Modal container */}
</main> </main>

View File

@ -203,6 +203,29 @@ const SurveyForm: React.FC<SurveyFormProps> = ({ existingItem }) => {
(err) => alert('Не успяхме да копираме имената: ', err) (err) => alert('Не успяхме да копираме имената: ', err)
); );
}; };
const sendIndividualNotification = async (id, message) => {
const response = await fetch('/api/notify', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id,
title: 'Нямаме отговор',
message: `${message}`,
})
});
if (response.ok) {
console.log(`Notification sent successfully to ${name}`);
} else {
console.error(`Failed to send notification to ${name}`);
}
};
const handleSendNotificationsToAllUnanswered = async (message) => {
getIdsForUnanswered().forEach((id, index) => sendIndividualNotification(id, message));
};
return ( return (
<div className="w-full max-w-md mx-auto" > <div className="w-full max-w-md mx-auto" >
@ -284,6 +307,26 @@ const SurveyForm: React.FC<SurveyFormProps> = ({ existingItem }) => {
{item.messages ? item.messages.filter((message) => !message.answer).length : 0} {item.messages ? item.messages.filter((message) => !message.answer).length : 0}
</div> </div>
</div> </div>
<button
className="mt-2 px-4 py-2 bg-blue-500 text-white rounded"
onClick={() => { handleSendNotificationsToAllUnanswered(item?.content) }}>
Подсети ВСИЧКИ неотховорили с нотификация
</button>
<div className="mt-2">
{getIdsForUnanswered().map((id) => {
const pub = pubs.find((p) => p.id === id);
const name = pub ? `${pub.firstName} ${pub.lastName}` : '???';
return (
<button
key={id}
// className="block mt-1 px-1 py-0.5 bg-orange-500 text-white rounded"
className="mt-1 px-2 py-1 bg-green-500 text-white rounded text-xs"
onClick={() => sendIndividualNotification(id, item?.content)}>
{name}
</button>
);
})}
</div>
</div> </div>
</div> </div>
)} )}

View File

@ -511,6 +511,7 @@ export async function getMonthlyStatistics(selectFields, filterDate) {
export async function filterPublishersNew_Available(selectFields, filterDate, isExactTime = false, isForTheMonth = false, isWithStats = true, includeOldAvailabilities = false) { export async function filterPublishersNew_Available(selectFields, filterDate, isExactTime = false, isForTheMonth = false, isWithStats = true, includeOldAvailabilities = false) {
return dataHelper.filterPublishersNew(selectFields, filterDate, isExactTime, isForTheMonth, false, isWithStats, includeOldAvailabilities); return dataHelper.filterPublishersNew(selectFields, filterDate, isExactTime, isForTheMonth, false, isWithStats, includeOldAvailabilities);
// async function filterPublishersNew(selectFields, filterDate, isExactTime = false, isForTheMonth = false, noEndDateFilter = false, isWithStats = true, includeOldAvailabilities = false, id = null, filterAvailabilitiesByDate = true)
} }
// availabilites filter: // availabilites filter:

View File

@ -33,19 +33,21 @@ const Notification = async (req, res) => {
select: { pushSubscription: true } select: { pushSubscription: true }
}); });
subs = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription.length : (publisher.pushSubscription ? 1 : 0); subs = Array.isArray(publisher.pushSubscription) ? publisher.pushSubscription.length : (publisher.pushSubscription ? 1 : 0);
res.send({ subs })
res.end() res.end()
return return
} else {
// 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()
} }
// 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') { if (req.method == 'PUT') {
// store the subscription object in the database // store the subscription object in the database
// publisher.pushSubscription = subscription // publisher.pushSubscription = subscription
const prisma = common.getPrismaClient(); const prisma = common.getPrismaClient();
const { subscription, id } = req.body const { subscription, id, name } = req.body
const publisher = await prisma.publisher.findUnique({ const publisher = await prisma.publisher.findUnique({
where: { id }, where: { id },
select: { pushSubscription: true } select: { pushSubscription: true }
@ -105,14 +107,19 @@ const Notification = async (req, res) => {
if (req.method == 'POST') {//title = "ССС", message = "Ще получите уведомление по този начин.") if (req.method == 'POST') {//title = "ССС", message = "Ще получите уведомление по този начин.")
const { subscription, id, broadcast, title = 'ССОМ', message = 'Ще получавате уведомления така.', actions } = req.body const { subscription, id, ids, broadcast, title = 'ССОМ', message = 'Ще получавате уведомления така.', actions } = req.body
if (broadcast) { if (broadcast) {
await broadcastPush(title, message, actions) await broadcastPush(title, message, actions)
res.statusCode = 200 res.statusCode = 200
res.end() res.end()
return return
} } else if (ids && ids.length) {
else if (id) { console.log('Sending push notifications to publishers ', ids);
await Promise.all(ids.map(_id => sendPush(_id, title, message, actions)));
res.statusCode = 200;
res.end();
return;
} else if (id) {
console.log('Sending push notification to publisher ', id) console.log('Sending push notification to publisher ', id)
await sendPush(id, title, message, actions) await sendPush(id, title, message, actions)
res.statusCode = 200 res.statusCode = 200

View File

@ -834,18 +834,25 @@ export default function CalendarPage({ initialEvents, initialShifts }) {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ body: JSON.stringify({
id: pub.id, message: "Тестово съобщение", title: "Това е тестово съобщение от https://sofia.mwitnessing.com", actions: [ id: pub.id,
{ message: "Тестово съобщение",
action: 'open_url', title: "Това е тестово съобщение от https://sofia.mwitnessing.com",
title: 'Open URL', actions: [
icon: '/images/open-url.png' { action: 'OK', title: 'OK', icon: '✅' },
}, { action: 'close', title: 'Затвори', icon: '❌' }
{
action: 'dismiss',
title: 'Dismiss',
icon: '/images/dismiss.png'
}
] ]
// actions: [
// {
// title: 'Open URL',
// action: 'open_url',
// icon: '/images/open-url.png'
// },
// {
// title: 'Dismiss',
// action: 'dismiss',
// icon: '/images/dismiss.png'
// }
// ]
}) })
}) })
}} }}

View File

@ -324,10 +324,22 @@ export default ContactsPage;
export const getServerSideProps = async (context) => { export const getServerSideProps = async (context) => {
const allPublishers = await data.getAllPublishersWithStatisticsMonth(new Date()); const allPublishers = await data.getAllPublishersWithStatisticsMonth(new Date());
//merge first and last name // Merge first and last name and serialize Date objects
allPublishers.forEach(publisher => { allPublishers.forEach(publisher => {
publisher.name = `${publisher.firstName} ${publisher.lastName}`; publisher.name = `${publisher.firstName} ${publisher.lastName}`;
if (publisher.currentMonthAvailability) {
publisher.currentMonthAvailability = publisher.currentMonthAvailability.map(availability => {
return {
...availability,
startTime: availability.startTime instanceof Date ? availability.startTime.toISOString() : availability.startTime,
endTime: availability.endTime instanceof Date ? availability.endTime.toISOString() : availability.endTime,
dateOfEntry: availability.dateOfEntry instanceof Date ? availability.dateOfEntry.toISOString() : availability.dateOfEntry,
};
});
}
}); });
return { return {
props: { props: {
allPublishers allPublishers

View File

@ -390,43 +390,7 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
///console.log(`publishers: ${publishers.length}, WhereClause: ${JSON.stringify(whereClause)}`); ///console.log(`publishers: ${publishers.length}, WhereClause: ${JSON.stringify(whereClause)}`);
// include repeating weekly availabilities. generate occurrences for the month // ---------------------------------------------- statistics ----------------------------------------------
// convert matching weekly availabilities to availabilities for the day to make further processing easier on the client.
publishers.forEach(pub => {
pub.availabilities = pub.availabilities.map(avail => {
if (avail.dayOfMonth == null) {
if (filterAvailabilitiesByDate && !isForTheMonth) {
// filter out repeating availabilities when on other day of week
if (filterTimeFrom) {
if (avail.dayofweek != dayOfWeekEnum) {
return null;
}
}
}
let newStart = new Date(filterDate);
newStart.setHours(avail.startTime.getHours(), avail.startTime.getMinutes(), 0, 0);
let newEnd = new Date(filterDate);
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
return {
...avail,
startTime: newStart,
endTime: newEnd
}
}
else {
if (filterAvailabilitiesByDate && !isForTheMonth) {
if (avail.startTime >= filterTimeFrom && avail.startTime <= filterTimeTo) {
return avail;
}
return null;
}
return avail;
}
})
.filter(avail => avail !== null);
});
let currentWeekStart, currentWeekEnd; let currentWeekStart, currentWeekEnd;
if (isWithStats) { if (isWithStats) {
@ -494,8 +458,45 @@ async function filterPublishersNew(selectFields, filterDate, isExactTime = false
return avail.startTime >= filterDate && avail.startTime <= filterTimeTo; return avail.startTime >= filterDate && avail.startTime <= filterTimeTo;
}); });
} }
}); });
// ----------------------------------------------
// include repeating weekly availabilities. generate occurrences for the month
// convert matching weekly availabilities to availabilities for the day to make further processing easier on the client.
publishers.forEach(pub => {
pub.availabilities = pub.availabilities.map(avail => {
if (avail.dayOfMonth == null) {
if (filterAvailabilitiesByDate && !isForTheMonth) {
// filter out repeating availabilities when on other day of week
if (filterTimeFrom) {
if (avail.dayofweek != dayOfWeekEnum) {
return null;
}
}
}
let newStart = new Date(filterDate);
newStart.setHours(avail.startTime.getHours(), avail.startTime.getMinutes(), 0, 0);
let newEnd = new Date(filterDate);
newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0);
return {
...avail,
startTime: newStart,
endTime: newEnd
}
}
else {
if (filterAvailabilitiesByDate && !isForTheMonth) {
if (avail.startTime >= filterTimeFrom && avail.startTime <= filterTimeTo) {
return avail;
}
return null;
}
return avail;
}
})
.filter(avail => avail !== null);
});
// ToDo: test case/unit test // ToDo: test case/unit test
// ToDo: check and validate the filtering and calculations // ToDo: check and validate the filtering and calculations
if (isExactTime) { if (isExactTime) {

View File

@ -1,11 +1,22 @@
const winston = require('winston'); const winston = require('winston');
require('winston-daily-rotate-file'); require('winston-daily-rotate-file');
const fs = require('fs');
const path = require('path');
// Define the logs directory path
const logDirectory = path.join(__dirname, '../logs');
// Ensure the logs directory exists
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
// Define the log configuration
const logConfiguration = { const logConfiguration = {
'transports': [ transports: [
new winston.transports.DailyRotateFile({ new winston.transports.DailyRotateFile({
filename: './logs/application-%DATE%.log', filename: path.join(logDirectory, 'application-%DATE%.log'),
datePattern: 'YYYY-MM-DD', // new file is created every hour: 'YYYY-MM-DD-HH' datePattern: 'YYYY-MM-DD', // new file is created every day
zippedArchive: true, zippedArchive: true,
maxSize: '20m', maxSize: '20m',
maxFiles: '90d', maxFiles: '90d',
@ -20,6 +31,7 @@ const logConfiguration = {
) )
}; };
// Create the logger
const logger = winston.createLogger(logConfiguration); const logger = winston.createLogger(logConfiguration);
module.exports = logger; module.exports = logger;