const mongoose = require('mongoose').set('debug', false) const Schema = mongoose.Schema; const tools = require('../tools/general'); mongoose.Promise = global.Promise; mongoose.level = "F"; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const MsgTemplateSchema = new Schema({ idapp: { type: String, }, typemsg: { type: Number, }, title: { type: String, }, title_it: { type: String, }, msg_it: { type: String, }, title_enUs: { type: String, }, msg_enUs: { type: String, }, title_si: { type: String, }, msg_si: { type: String, }, title_fr: { type: String, }, msg_fr: { type: String, }, title_es: { type: String, }, msg_es: { type: String, }, title_pt: { type: String, }, msg_pt: { type: String, }, }); MsgTemplateSchema.statics.getFieldsForSearch = function () { return [{ field: 'title', type: tools.FieldType.string }, ] }; MsgTemplateSchema.statics.executeQueryTable = function (idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; MsgTemplateSchema.statics.getMsgByLang = async function (idapp, myuser, typemsg, lang) { const MsgTemplate = this; try { const mymsg = await MsgTemplate.findOne({ idapp, typemsg }); if (!!mymsg) { if ((!!mymsg["msg_" + lang]) && (!!mymsg["title_" + lang])) { let body = mymsg["msg_" + lang] let title = mymsg["title_" + lang] if (!body) { body = mymsg["msg_it"] title = mymsg["title_it"] } body = await tools.convertSpecialTags(myuser, body); title = await tools.convertSpecialTags(myuser, title); return { body, title } } } } catch (e) { return { body: '', title: '' }; } return { body: '', title: '' }; }; MsgTemplateSchema.statics.getMsgByTitleAndLang = async function (idapp, title, lang) { const MsgTemplate = this; try { const mymsg = await MsgTemplate.findOne({ idapp, title }); if (!!mymsg) { if ((!!mymsg["msg_" + lang]) && (!!mymsg["title_" + lang])) { return { body: mymsg["msg_" + lang], title: mymsg["title_" + lang] } } } } catch (e) { return { body: '', title: '' }; } return { body: '', title: '' }; }; MsgTemplateSchema.statics.findAllIdApp = async function (idapp) { const MsgTemplate = this; const myfind = { idapp }; return await tools.findAllQueryIdApp(this, myfind); }; const MsgTemplate = mongoose.model('MsgTemplate', MsgTemplateSchema); MsgTemplate.createIndexes() .then(() => { }) .catch((err) => { throw err; }); module.exports = { MsgTemplate };