From a6b7c62768e606dbf6cc56cf0d77d36c57dbf359 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 28 Mar 2024 18:12:16 +0200 Subject: [PATCH] js error log fix --- src/helpers/common.js | 96 ++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/src/helpers/common.js b/src/helpers/common.js index 4cc84e7..d706006 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -4,8 +4,9 @@ const levenshtein = require('fastest-levenshtein'); -const fs = require("fs"); -const path = require("path"); +const fs = typeof window === 'undefined' ? require('fs') : undefined; +const path = typeof window === 'undefined' ? require('path') : undefined; + const { PrismaClient } = require('@prisma/client'); const DayOfWeek = require("@prisma/client").DayOfWeek; @@ -75,43 +76,42 @@ exports.setBaseUrl = function (req) { }; 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 { - if (fs.existsSync(filePath)) { - const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); - const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; - return fullUrl; - } else { - if (req) { - const baseUrl = exports.setBaseUrl(req); // Correctly reference setBaseUrl - return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`; - } - console.log('Base URL file does not exist.'); - return null; - } - } catch (error) { - console.error('Error reading the base URL file:', error); - return null; + // const filePath = path.join(__dirname, 'baseUrl.txt'); + + // try { + // if (fs.existsSync(filePath)) { + // const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); + // const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; + // return fullUrl; + // } else { + // if (req) { + // // Assuming setBaseUrl is defined somewhere in this file + // const baseUrl = exports.setBaseUrl(req); + // return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`; + // } + // console.log('Base URL file does not exist.'); + // 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; exports.getPrismaClient = function getPrismaClient() { if (!prisma) { @@ -228,15 +228,25 @@ exports.getDayOfWeekDate = function (dayOfWeekName, date = new Date()) { return date; }; //common.getWeekOfMonth(date) -// exports.getWeekOfMonth = function (date) { -// // Copy date so don't modify original -// date = new Date(date); -// // Adjust to Monday of this week -// date.setDate(date.getDate() + 3 - (date.getDayEuropean() + 6) % 7); -// // Return week number -// const weekNumber = Math.floor((date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000 / 7); -// return weekNumber; -// } +exports.getWeekOfMonth = function (inputDate) { + let date = new Date(inputDate); + let firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1); + let firstMonday = new Date(firstDayOfMonth); + + // Adjust firstDayOfMonth to the first Monday of the month + if (firstDayOfMonth.getDay() === 0) { // Sunday + 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) { // get first day of the month