Files
freeplanet_serverside/src/server/models/myelem.js

540 lines
12 KiB
JavaScript
Executable File

const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
const { ObjectId } = require('mongodb');
const { MySchedaSchema, IDimensioni, IImg, IText, IAreaDiStampa } = require('../models/myscheda');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const IElementiPagina = new Schema({
pagina: IDimensioni,
});
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 catalogo = new Schema(
{
//++AddCATALOGO_FIELDS
productTypes: [{ type: Number }],
excludeproductTypes: [{ type: Number }],
editore: [{ type: String }],
argomenti: [{ type: String }],
idCollane: [{ type: Number }],
sort_field: { type: String },
sort_dir: { type: Number },
pdf: { type: Boolean },
pdf_filename: { type: String },
printable: { type: Boolean },
indebug: { type: Boolean },
maxnumlibri: { type: Number },
first_page: IDimensioni,
last_page: IDimensioni,
areadistampa: IAreaDiStampa,
dimensioni_def: IElementiPagina,
// -------------------
arrSchede: [
{
scheda: MySchedaSchema,
order: { type: Number },
numPagineMax: { 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,
},
parambool4: {
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,
},
heightcard: {
type: String,
},
widthcard: {
type: String,
},
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
}
}
],
date_created: {
type: Date,
default: Date.now
},
date_updated: {
type: Date,
},
});
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)) {
if (path) {
await MyElem.updateMany(
{ idapp },
{ 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
);
}
}
const pathToIdMap2 = {};
pages.forEach(page => {
pathToIdMap2[page.path] = page._id.toString(); // Mappa il path all'ID del documento MyPage
});
for (const [oldpath, id] of Object.entries(pathToIdMap2)) {
await MyElem.updateMany(
{ idapp },
{ 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 };
const aggiorna = false;
let arrrec = null;
if (aggiorna) {
arrrec = await MyElem.find(myfind).sort({ order: 1 });
for (const elem of arrrec) {
if (elem.heightimg === 'NaNpx') {
elem.heightimg = '';
await elem.save();
}
}
} else {
arrrec = await MyElem.find(myfind).lean().sort({ order: 1 });
}
return arrrec;
};
async function deleteOldMyElems(idapp) {
try {
const { MyPage } = require('../models/mypage');
// 1. Recupera tutti gli _id dalle pagine
const existingPages = await MyPage.find({idapp}).select('_id').lean();
const existingPageIds = existingPages.map(page => page._id.toString());
// 2. Trova gli MyElem che hanno idPage non esistenti in MyPage
const elemsToDelete = await MyElem.find({
idapp,
idPage: { $nin: existingPageIds }
});
if (elemsToDelete.length > 0) {
// 3. Esegui la cancellazione
const result = await MyElem.deleteMany({ idPage: { $nin: existingPageIds } });
console.log(`Cancellati ${result.deletedCount} documenti di MyElem.`);
} else {
console.log('Nessun documento da cancellare.');
}
} catch (error) {
console.error('Errore durante la cancellazione dei documenti:', error);
}
}
MyElemSchema.statics.findallSchedeTemplate = async function (idapp) {
const MyElem = this;
try {
const { MyPage } = require('../models/mypage');
const ris = await MyElem.find({ idapp }).lean();
const schedeTemplate = ris.flatMap(elem =>
elem.catalogo && elem.catalogo.arrSchede ?
elem.catalogo.arrSchede
.filter(scheda => scheda.scheda?.isTemplate)
.map(scheda => ({
...scheda, // mantieni i dati originali della scheda
idPageOrig: elem.idPage // aggiungi l'idPage
}))
: []
);
if (idapp === '18') {
const duplicateIds = schedeTemplate.reduce((acc, scheda) => {
const id = scheda.scheda._id; // Ottieni l'ID della scheda
if (!acc[id]) {
acc[id] = []; // Inizializza un array per tenere traccia delle pagine
}
acc[id].push(scheda.idPageOrig); // Aggiungi l'idPage all'array
return acc;
}, {});
// Filtra i duplicati
const duplicates = Object.entries(duplicateIds)
.filter(([_, pages]) => pages.length > 1) // Mantieni solo gli ID con più di un'istanza
.map(([id, pages]) => ({ id, pages })); // Ottieni ID e pagine corrispondenti
// Recupera i dettagli delle pagine
const pageIds = duplicates.flatMap(dup => dup.pages); // Estrai tutti gli idPage
const pages = await MyPage.find({ idapp, _id: { $in: pageIds } }).lean();
// Crea una mappatura tra idPage e title
const pageMap = pages.reduce((acc, page) => {
acc[page._id] = page.title; // Mappa idPage a title
return acc;
}, {});
// Associa i titoli delle pagine agli ID duplicati
const resultWithTitles = duplicates.map(dup => ({
id: dup.id,
pages: dup.pages.map(_id => ({
_id,
title: pageMap[_id] || 'Titolo non trovato' // Usa la mappatura per trovare il titolo
}))
}));
if (resultWithTitles.length > 0) {
console.log('Duplicati e titoli delle pagine:', JSON.stringify(resultWithTitles, null, 2));
await deleteOldMyElems(idapp);
}
}
return schedeTemplate;
} catch (e) {
console.error('Err', e);
}
};
// Ricerca tra tutte le schede, contenute in catalogo, se esiste un nome di template uguale,
// se non lo trova allora è quello giusto per crearne uno nuovo
MyElemSchema.statics.getNewFreeNameTemplate = async function (idapp, idPageOrig, nomeTemplate) {
const MyElem = this;
try {
const ris = await MyElem.find(
{
idapp,
'catalogo.arrSchede.scheda.isTemplate': true,
'catalogo.arrSchede.scheda.idPage': { $ne: idPageOrig }
},
{
'catalogo.arrSchede.scheda.name': 1,
'catalogo.arrSchede.scheda.isTemplate': 1,
'catalogo.arrSchede.scheda.isPagIntro': 1,
'catalogo.arrSchede.scheda.idPage': 1
});
const existingNames = new Set(
ris.flatMap(elem =>
elem.catalogo?.arrSchede?.filter(scheda =>
scheda.scheda?.isTemplate &&
scheda.scheda?.idPage !== idPageOrig
)
.map(scheda => scheda.scheda?.name) || []
)
);
let ind = 2;
let newNameTemplate;
do {
newNameTemplate = `${nomeTemplate}_copia_${ind}`;
ind++;
} while (existingNames.has(newNameTemplate) && ind <= 1000);
return newNameTemplate;
} catch (e) {
console.error('Err', e);
throw e; // Propagate the error
}
};
const MyElem = mongoose.model('MyElem', MyElemSchema);
MyElem.createIndexes((err) => {
if (err) throw err;
});
module.exports = { MyElem };