js error log fix

This commit is contained in:
Dobromir Popov
2024-03-28 18:12:16 +02:00
parent e0dfbc51ec
commit a6b7c62768

View File

@ -4,8 +4,9 @@
const levenshtein = require('fastest-levenshtein'); const levenshtein = require('fastest-levenshtein');
const fs = require("fs"); const fs = typeof window === 'undefined' ? require('fs') : undefined;
const path = require("path"); const path = typeof window === 'undefined' ? require('path') : undefined;
const { PrismaClient } = require('@prisma/client'); const { PrismaClient } = require('@prisma/client');
const DayOfWeek = require("@prisma/client").DayOfWeek; const DayOfWeek = require("@prisma/client").DayOfWeek;
@ -75,43 +76,42 @@ exports.setBaseUrl = function (req) {
}; };
exports.getBaseUrl = function (relative = "", req = null) { exports.getBaseUrl = function (relative = "", req = null) {
const filePath = path.join(__dirname, 'baseUrl.txt'); if (typeof window === 'undefined') {
// Server-side logic
// Read the base URL from env (NEXTAUTH_URL):
return process.env.NEXTAUTH_URL + relative;
try { // const filePath = path.join(__dirname, 'baseUrl.txt');
if (fs.existsSync(filePath)) {
const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); // try {
const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; // if (fs.existsSync(filePath)) {
return fullUrl; // const baseUrl = fs.readFileSync(filePath, 'utf8').trim();
} else { // const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl;
if (req) { // return fullUrl;
const baseUrl = exports.setBaseUrl(req); // Correctly reference setBaseUrl // } else {
return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`; // if (req) {
} // // Assuming setBaseUrl is defined somewhere in this file
console.log('Base URL file does not exist.'); // const baseUrl = exports.setBaseUrl(req);
return null; // return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`;
} // }
} catch (error) { // console.log('Base URL file does not exist.');
console.error('Error reading the base URL file:', error); // return null;
return null; // }
// } catch (error) {
// console.error('Error reading the base URL file:', error);
// return null;
// }
} else {
// Client-side logic
// Fetch the base URL from the server endpoint you've set up
const baseUrl = window.location.origin;
const fullUrl = relative ? `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}` : baseUrl;
//console.log("getBaseUrl()=", fullUrl);
return fullUrl.toString();
} }
// const host = process.env.NEXT_PUBLIC_HOST || '127.0.0.1';
// const port = process.env.NEXT_PUBLIC_PORT ? `:${ process.env.NEXT_PUBLIC_PORT } ` : '';
// const protocol = process.env.NEXT_PUBLIC_PROTOCOL || "https"
// //const url = `${ protocol }://${host}${port}/${relative.replace(/^\/|\/$/g, '')}/`;
// const isRelativeEmpty = !relative || relative.trim() === '';
// const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : '';
// const url = `${protocol}://${host}${port}${formattedRelative}`;
// logger.debug("NODE_ENV = ", process.env.NODE_ENV, "protocol:", protocol);
// logger.debug("getBaseURL = ", url);
// return url;
}; };
let prisma; let prisma;
exports.getPrismaClient = function getPrismaClient() { exports.getPrismaClient = function getPrismaClient() {
if (!prisma) { if (!prisma) {
@ -228,15 +228,25 @@ exports.getDayOfWeekDate = function (dayOfWeekName, date = new Date()) {
return date; return date;
}; };
//common.getWeekOfMonth(date) //common.getWeekOfMonth(date)
// exports.getWeekOfMonth = function (date) { exports.getWeekOfMonth = function (inputDate) {
// // Copy date so don't modify original let date = new Date(inputDate);
// date = new Date(date); let firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
// // Adjust to Monday of this week let firstMonday = new Date(firstDayOfMonth);
// date.setDate(date.getDate() + 3 - (date.getDayEuropean() + 6) % 7);
// // Return week number // Adjust firstDayOfMonth to the first Monday of the month
// const weekNumber = Math.floor((date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000 / 7); if (firstDayOfMonth.getDay() === 0) { // Sunday
// return weekNumber; firstMonday.setDate(2);
// } } else if (firstDayOfMonth.getDay() !== 1) { // Not Monday
firstMonday.setDate(9 - firstDayOfMonth.getDay());
}
// Calculate the difference in days
let diff = (date - firstMonday) / (1000 * 60 * 60 * 24);
// Calculate week number
let weekNumber = Math.ceil((diff + 1) / 7);
return weekNumber;
};
exports.getMonthDatesInfo = function (date) { exports.getMonthDatesInfo = function (date) {
// get first day of the month // get first day of the month