diff --git a/back/methods/chat/sendCheckingPresence.js b/back/methods/chat/sendCheckingPresence.js index 85b66e94b..12cadec04 100644 --- a/back/methods/chat/sendCheckingPresence.js +++ b/back/methods/chat/sendCheckingPresence.js @@ -37,7 +37,7 @@ module.exports = Self => { if (!recipient) throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`); - if (process.env.NODE_ENV == 'test') + if (!Self.app.models.Application.isProduction()) message = `[Test:Environment to user ${userId}] ` + message; const chat = await models.Chat.create({ diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js index 9a23af379..1ab4cb6e0 100644 --- a/back/methods/chat/sendQueued.js +++ b/back/methods/chat/sendQueued.js @@ -94,7 +94,7 @@ module.exports = Self => { * @return {Promise} - The request promise */ Self.sendMessage = async function sendMessage(senderFk, recipient, message) { - if (process.env.NODE_ENV !== 'production') { + if (!Self.app.models.Application.isProduction()) { return new Promise(resolve => { return resolve({ statusCode: 200, @@ -149,7 +149,7 @@ module.exports = Self => { * @return {Promise} - The request promise */ Self.getUserStatus = async function getUserStatus(username) { - if (process.env.NODE_ENV !== 'production') { + if (!Self.app.models.Application.isProduction()) { return new Promise(resolve => { return resolve({ data: { diff --git a/back/methods/dms/deleteTrashFiles.js b/back/methods/dms/deleteTrashFiles.js index 239d654ef..77e33929a 100644 --- a/back/methods/dms/deleteTrashFiles.js +++ b/back/methods/dms/deleteTrashFiles.js @@ -22,7 +22,7 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - if (process.env.NODE_ENV == 'test') + if (!Self.app.models.Application.isProduction()) throw new UserError(`Action not allowed on the test environment`); const models = Self.app.models; diff --git a/back/methods/docuware/upload.js b/back/methods/docuware/upload.js index 27be72295..ae3989def 100644 --- a/back/methods/docuware/upload.js +++ b/back/methods/docuware/upload.js @@ -119,7 +119,7 @@ module.exports = Self => { ] }; - if (process.env.NODE_ENV != 'production') + if (!Self.app.models.Application.isProduction(false)) throw new UserError('Action not allowed on the test environment'); // delete old diff --git a/back/methods/image/scrub.js b/back/methods/image/scrub.js index 99c6bcbf3..4af2d8d49 100644 --- a/back/methods/image/scrub.js +++ b/back/methods/image/scrub.js @@ -43,8 +43,7 @@ module.exports = Self => { Self.scrub = async function(collection, remove, limit, dryRun, skipLock) { const $ = Self.app.models; - const env = process.env.NODE_ENV; - dryRun = dryRun || (env && env !== 'production'); + dryRun = dryRun || !Self.app.models.Application.isProduction(false); const instance = await $.ImageCollection.findOne({ fields: ['id'], diff --git a/back/methods/image/upload.js b/back/methods/image/upload.js index 51da327f6..81d9759e6 100644 --- a/back/methods/image/upload.js +++ b/back/methods/image/upload.js @@ -41,7 +41,7 @@ module.exports = Self => { if (!hasWriteRole) throw new UserError(`You don't have enough privileges`); - if (process.env.NODE_ENV == 'test') + if (!Self.app.models.Application.isProduction()) throw new UserError(`Action not allowed on the test environment`); // Upload file to temporary path diff --git a/back/methods/notification/send.js b/back/methods/notification/send.js index b2748477d..4f8f436da 100644 --- a/back/methods/notification/send.js +++ b/back/methods/notification/send.js @@ -70,7 +70,7 @@ module.exports = Self => { const newParams = Object.assign({}, queueParams, sendParams); const email = new Email(queueName, newParams); - if (process.env.NODE_ENV != 'test') + if (Self.app.models.Application.isProduction()) await email.send(); await queue.updateAttribute('status', statusSent); diff --git a/loopback/common/methods/application/isProduction.js b/loopback/common/methods/application/isProduction.js new file mode 100644 index 000000000..9479b67af --- /dev/null +++ b/loopback/common/methods/application/isProduction.js @@ -0,0 +1,3 @@ +module.exports = (localAsProduction = true) => { + if ((!process.env.NODE_ENV && localAsProduction) || process.env.NODE_ENV == 'production') return true; +}; diff --git a/loopback/common/models/application.js b/loopback/common/models/application.js index 6bdc2c13a..70db17de2 100644 --- a/loopback/common/models/application.js +++ b/loopback/common/models/application.js @@ -1,3 +1,4 @@ +const isProductionFn = require('../methods/application/isProduction'); module.exports = function(Self) { require('../methods/application/status')(Self); @@ -6,4 +7,8 @@ module.exports = function(Self) { require('../methods/application/executeProc')(Self); require('../methods/application/executeFunc')(Self); require('../methods/application/getEnumValues')(Self); + + Self.isProduction = (localAsProduction = true) => { + return isProductionFn(localAsProduction); + }; }; diff --git a/modules/account/back/models/ldap-config.js b/modules/account/back/models/ldap-config.js index 89f0add48..4f5c0218d 100644 --- a/modules/account/back/models/ldap-config.js +++ b/modules/account/back/models/ldap-config.js @@ -3,9 +3,10 @@ const app = require('vn-loopback/server/server'); const ldap = require('../util/ldapjs-extra'); const crypto = require('crypto'); const nthash = require('smbhash').nthash; +const isProduction = require('vn-loopback/common/methods/application/isProduction'); module.exports = Self => { - const shouldSync = process.env.NODE_ENV !== 'test'; + const shouldSync = isProduction(); Self.getLinker = async function() { return await Self.findOne({ diff --git a/modules/account/back/models/samba-config.js b/modules/account/back/models/samba-config.js index 927510a29..aa68af35d 100644 --- a/modules/account/back/models/samba-config.js +++ b/modules/account/back/models/samba-config.js @@ -1,6 +1,7 @@ const ldap = require('../util/ldapjs-extra'); const execFile = require('child_process').execFile; +const isProduction = require('vn-loopback/common/methods/application/isProduction'); /** * Summary of userAccountControl flags: @@ -12,7 +13,7 @@ const UserAccountControlFlags = { }; module.exports = Self => { - const shouldSync = process.env.NODE_ENV !== 'test'; + const shouldSync = isProduction(); Self.getLinker = async function() { return await Self.findOne({ diff --git a/modules/client/back/methods/sms/send.js b/modules/client/back/methods/sms/send.js index 94b2b6c27..269dedacb 100644 --- a/modules/client/back/methods/sms/send.js +++ b/modules/client/back/methods/sms/send.js @@ -47,7 +47,7 @@ module.exports = Self => { let response; try { - if (process.env.NODE_ENV !== 'production') + if (!Self.app.models.Application.isProduction(false)) response = {result: [{status: 'ok'}]}; else { const jsonTest = { diff --git a/modules/invoiceOut/back/methods/invoiceOut/download.js b/modules/invoiceOut/back/methods/invoiceOut/download.js index 748e2df17..5a9d5bc51 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/download.js +++ b/modules/invoiceOut/back/methods/invoiceOut/download.js @@ -66,7 +66,7 @@ module.exports = Self => { console.error(err); }); - if (process.env.NODE_ENV == 'test') { + if (!Self.app.models.Application.isProduction()) { try { await fs.access(file.path); } catch (error) { diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index e4fcc1a69..166565fe2 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -59,7 +59,7 @@ module.exports = Self => { hasPdf: true }, options); - if (process.env.NODE_ENV !== 'test') { + if (Self.app.models.Application.isProduction()) { await print.storage.write(buffer, { type: 'invoice', path: pdfFile.path, diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 58fc46abb..9edaf454f 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -111,7 +111,7 @@ module.exports = Self => { const destinationFile = path.join( accessContainer.client.root, accessContainer.name, appName, `${toVersion}.7z`); - if (process.env.NODE_ENV == 'test') + if (!Self.app.models.Application.isProduction()) await fs.unlink(srcFile); else { await fs.move(srcFile, destinationFile, {