From 73cf5fbe89625f9aeeced16a1dd1038b37c7212c Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 27 Feb 2024 01:17:48 +0200 Subject: [PATCH] setBaseUrl dynamic; PubTypeEnu mapping --- src/helpers/common.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/helpers/common.js b/src/helpers/common.js index 98720e3..3960ad4 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -62,34 +62,31 @@ exports.isValidPhoneNumber = function (phone) { // If neither condition is met, the phone number is invalid return false; } - exports.setBaseUrl = function (req) { const protocol = req.headers['x-forwarded-proto'] || 'http'; const host = req.headers.host; const baseUrl = `${protocol}://${host}`; - fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrlGlobal, 'utf8'); + // Write the baseUrl to the file + if (req != null) { + fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrl, 'utf8'); + } return baseUrl; -} +}; exports.getBaseUrl = function (relative = "", req = null) { const filePath = path.join(__dirname, 'baseUrl.txt'); try { - // Check if the base URL file exists if (fs.existsSync(filePath)) { - // Read the base URL from the file const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); - // If a relative path is provided, append it to the base URL const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; return fullUrl; } else { - if (req) { - const baseUrl = exports.setBaseUrl(req); - return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}/`; + const baseUrl = exports.setBaseUrl(req); // Correctly reference setBaseUrl + return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`; } - console.log('Base URL file does not exist.'); return null; } @@ -97,7 +94,6 @@ exports.getBaseUrl = function (relative = "", req = null) { console.error('Error reading the base URL file:', error); return null; } - // 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" @@ -112,7 +108,8 @@ exports.getBaseUrl = function (relative = "", req = null) { // logger.debug("getBaseURL = ", url); // return url; -} +}; + let prisma; @@ -202,6 +199,18 @@ exports.getDayOfWeekNameEnEnum = function (date) { return dayOfWeekNames[dayOfWeekIndex]; } +exports.getPubTypeEnum = function (text) { + const input = text.trim(); + const mapping = { + 'Вестител': 'Publisher', + 'Бетелит': 'Bethelite', + 'Редовен Пионер': 'RegularPioneer', + 'Специален Пионер/Мисионер': 'SpecialPioneer_Missionary', + }; + + // Default to 'Publisher' if the input does not match any key + return mapping[input] || 'Publisher'; +} /*