const mongoose = require('mongoose').set('debug', false) const Schema = mongoose.Schema; const tools = require('../tools/general'); const { ObjectID, ObjectId } = require('mongodb'); const { MySchedaSchema, IDimensioni } = require('../models/myscheda'); mongoose.Promise = global.Promise; mongoose.level = "F"; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const myCard = new Schema( { imagefile: String, vers_img: Number, alt: String, description: String, style: String, size: String, color: String, content: String, colorsub: String, link: String, } ) const animation = new Schema( { name: String, clduration: String, cldelay: String, timingtype: String, } ); const elemText = new Schema( { text: String, color: String, class: String, size: String, anim: animation, } ); const IElementiPagina = new Schema( { pagina: IDimensioni, riga: IDimensioni, } ); const catalogo = new Schema( { //++AddCATALOGO_FIELDS productTypes: [{ type: Number }], excludeproductTypes: [{ type: Number }], Editore: [{ type: String }], pdf: { type: Boolean }, printable: { type: Boolean }, width: { type: String }, height: { type: String }, first_page_img: { type: String }, first_page_html: { type: String }, first_page_width: { type: Number }, first_page_height: { type: Number }, last_page_img: { type: String }, last_page_html: { type: String }, last_page_height: { type: Number }, last_page_width: { type: Number }, margine_pagina: { type: String }, margine_riga: { type: String }, margine_paginaPrintable: { type: String }, margine_rigaPrintable: { type: String }, backgroundimage: { type: String }, backgroundimage_printable: { type: String }, backgroundSize: { type: String }, backgroundSize_printable: { type: String }, widthpagPrintable: { type: Number }, // ------------------- arrSchede: [ { scheda: MySchedaSchema, order: { type: Number }, numSchede: { type: Number }, /*arrProdToShow: { type: [[mongoose.Schema.Types.Mixed]], // Definizione tipo select: false // Imposta il campo come non selezionabile },*/ } ], } ); const MyElemSchema = new Schema({ idapp: { type: String, }, path: { type: String, }, oldpath: { type: String, }, idPage: { type: String }, type: { type: Number, }, img: { type: String, }, container: { type: String, }, container2: { type: String, }, container3: { type: String, }, container4: { type: String, }, align: { type: Number, }, vertalign: { type: Number, }, speed: { type: Number, }, parambool: { type: Boolean, }, span: { type: Boolean, }, parambool2: { type: Boolean, }, parambool3: { type: Boolean, }, number: { type: Number, }, num2: { type: Number, }, imgback: { type: String, }, ratio: { type: String, }, containerHtml: { type: String, }, size: { type: String, }, order: { type: Number, default: 0, }, height: { type: Number, }, heightimg: { type: String, }, heightcarousel: { type: String, }, widthimg: { type: String, }, width: { type: Number, }, link: { type: String, }, fit: { type: String, }, onlyif_logged: { type: Boolean, }, color: { type: String, }, elemsText: [elemText], anim: animation, active: { type: Boolean, default: false, }, class: { type: String, }, class2: { type: String, }, class3: { type: String, }, class4: { type: String, }, styleadd: { type: String, }, image: { type: String, }, vers_img: { type: Number, }, titleBanner: String, classBanner: String, listcards: [myCard], catalogo: catalogo, list: [ { imagefile: { type: String }, vers_img: { type: Number, }, order: { type: Number }, alt: { type: String }, description: { type: String } } ], }); MyElemSchema.pre('save', async function (next) { if (this.isNew) { this._id = new ObjectID(); } next(); }); MyElemSchema.statics.getFieldsForSearch = function () { return [{ field: 'title', type: tools.FieldType.string }, { field: 'content', type: tools.FieldType.string }] }; MyElemSchema.statics.executeQueryTable = function (idapp, params, user) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params, user); }; MyElemSchema.statics.SetIdPageInsteadThePah = async function (idapp) { const MyElem = this; const { MyPage } = require('../models/mypage'); // Sostituisci path con IdPage try { // Recupera tutti i documenti di MyPage const pages = await MyPage.find({ idapp }); // Puoi anche specificare condizioni, se necessario // Utilizza una mappa per accoppiare i path con i loro Id const pathToIdMap = {}; pages.forEach(page => { pathToIdMap[page.path] = page._id; // Mappa il path all'ID del documento MyPage }); // Aggiorna MyElem utilizzando la mappa for (const [path, id] of Object.entries(pathToIdMap)) { await MyElem.updateMany( { path: path }, // Condizione per aggiornare dove il path corrisponde { $set: { idPage: id, oldpath: path, }, $unset: { path: "" } // Rimuove il campo path } // Imposta IdPage all'ID del documento corrispondente ); } if (false) { // Utilizza una mappa per accoppiare i path con i loro Id const pathToIdMap2 = {}; pages.forEach(page => { pathToIdMap2[page.path] = page._id.toString(); // Mappa il path all'ID del documento MyPage }); // Aggiorna MyElem utilizzando la mappa for (const [path, id] of Object.entries(pathToIdMap2)) { await MyElem.updateMany( { oldpath: path }, // Condizione per aggiornare dove il path corrisponde { $unset: { idPage: "" } // Rimuove il campo path } // Imposta IdPage all'ID del documento corrispondente ); } for (const [oldpath, id] of Object.entries(pathToIdMap2)) { await MyElem.updateMany( { oldpath: oldpath }, // Condizione per aggiornare dove il path corrisponde { $set: { idPage: id } } // Imposta IdPage all'ID del documento corrispondente ); } } console.log('Aggiornamenti effettuati con successo.'); return 'Aggiornamenti effettuati con successo.'; } catch (error) { console.error('Errore durante l\'aggiornamento:', error); return 'Errore durante l\'aggiornamento:', error; } }; MyElemSchema.statics.deleteAllFromThisPage = async function (id) { const MyElem = this; return MyElem.deleteMany({ idPage: id }); }; MyElemSchema.statics.findAllIdApp = async function (idapp) { const MyElem = this; const myfind = { idapp }; return await MyElem.find(myfind).sort({ order: 1 }); }; const MyElem = mongoose.model('MyElem', MyElemSchema); MyElem.createIndexes((err) => { if (err) throw err; }); module.exports = { MyElem };