- Cataloghi
- Import ed Export Pagine - ObjectID sostituita con ObjectId
This commit is contained in:
@@ -12,7 +12,8 @@ const sharp = require('sharp');
|
||||
const { authenticate, authenticate_noerror } = require(
|
||||
'../middleware/authenticate');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
const { ObjectId } = require('mongodb');
|
||||
|
||||
// const {ListaIngresso} = require('../models/listaingresso');
|
||||
const { Graduatoria } = require('../models/graduatoria');
|
||||
|
||||
@@ -418,8 +419,8 @@ router.post('/settable', authenticate, async (req, res) => {
|
||||
|
||||
} else {
|
||||
if ((mydata['_id'] === undefined || mydata['_id'] === '' || (mytablerec.isNew && mydata['_id'] === 0)) && (mytablerec._id === undefined || mytablerec._id === '0')) {
|
||||
mytablerec._id = new ObjectID();
|
||||
mydata._id = new ObjectID();
|
||||
mytablerec._id = new ObjectId();
|
||||
mydata._id = new ObjectId();
|
||||
mytablerec.isNew = true;
|
||||
}
|
||||
}
|
||||
@@ -911,25 +912,62 @@ async function exportPage(idapp, pageId) {
|
||||
}
|
||||
};
|
||||
|
||||
async function upsertRecord(table, record, appId) {
|
||||
async function upsertRecord(table, record, appId, newIdPage = null) {
|
||||
|
||||
let newId = null;
|
||||
|
||||
const existingRecord = await table.findOne({ idapp: appId, _id: record._id }); // Assumendo che `record` ha un ID
|
||||
if (existingRecord) {
|
||||
if (newIdPage && record.idPage) {
|
||||
record.idPage = newIdPage;
|
||||
}
|
||||
const modif = await table.updateOne({ _id: record._id }, { $set: { ...record, idapp: appId } });
|
||||
wasModified = modif.nModified > 0;
|
||||
} else {
|
||||
const ris = await table.create({
|
||||
// Se sono sulla tabella mypages
|
||||
if (table.modelName === 'MyPage') {
|
||||
// Controlla se esiste già la pagina in questione
|
||||
const existingRecPage = await table.findOne({ idapp: appId, path: record.path });
|
||||
if (existingRecPage) {
|
||||
// Esiste già ! quindi aggiorno questo record, e cancello tutti gli elementi precedenti !
|
||||
delete record._id;
|
||||
const modif = await table.updateOne({ _id: existingRecPage._id }, { $set: { ...record, idapp: appId } });
|
||||
|
||||
const tableElems = globalTables.getTableByTableName('myelems');
|
||||
if (tableElems)
|
||||
risdel = await tableElems.deleteMany({ idapp: appId, idPage: existingRecPage._id });
|
||||
|
||||
newId = existingRecPage._id.toString();
|
||||
return { ImportedRecords: false, newId }; // Torna il numero di record importati (1 in questo caso)
|
||||
}
|
||||
}
|
||||
// Se non esiste, allora la creo, con un id Nuovo !
|
||||
|
||||
const existingRecSameID = await table.findOne({ _id: record._id }); // Assumendo che `record` ha un ID
|
||||
if (existingRecSameID) {
|
||||
// Se
|
||||
newId = new ObjectId();
|
||||
record._id = newId;
|
||||
}
|
||||
if (newIdPage && record.idPage) {
|
||||
record.idPage = newIdPage;
|
||||
}
|
||||
const newrec = {
|
||||
...record,
|
||||
idapp: appId,
|
||||
});
|
||||
};
|
||||
|
||||
const ris = await table.create(newrec);
|
||||
wasModified = !!ris;
|
||||
}
|
||||
|
||||
return wasModified; // Torna il numero di record importati (1 in questo caso)
|
||||
return { ImportedRecords: wasModified, newId }; // Torna il numero di record importati (1 in questo caso)
|
||||
}
|
||||
|
||||
async function importPage(req, idapp, jsonString) {
|
||||
try {
|
||||
console.log('INIZIO importPage');
|
||||
|
||||
// Parsing dei dati JSON
|
||||
const myexp = JSON.parse(jsonString);
|
||||
|
||||
@@ -939,12 +977,15 @@ async function importPage(req, idapp, jsonString) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let totalImportedRecords = 0;
|
||||
let newIdPage = null;
|
||||
|
||||
// Ciclo su ogni proprietà di myexp
|
||||
const importCounts = []; // Array per memorizzare i conteggi di importazione
|
||||
|
||||
// Cicla prima sulle pagine
|
||||
for (const key in myexp) {
|
||||
if (myexp.hasOwnProperty(key)) {
|
||||
let ImportedRecordstemp = 0;
|
||||
const tableName = key;
|
||||
|
||||
// Verifica se la tabella esiste
|
||||
@@ -954,24 +995,61 @@ async function importPage(req, idapp, jsonString) {
|
||||
if (tableName === 'mypages') {
|
||||
if (User.isEditor(req.user.perm)) {
|
||||
for (const page of myexp.mypages) {
|
||||
totalImportedRecords += await upsertRecord(table, page, idapp) ? 1 : 0;
|
||||
const { ImportedRecords, newId } = await upsertRecord(table, page, idapp);
|
||||
if (!newIdPage && newId) {
|
||||
newIdPage = newId;
|
||||
}
|
||||
ImportedRecordstemp += ImportedRecords ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
totalImportedRecords += ImportedRecordstemp;
|
||||
|
||||
} else if (tableName === 'myelems') {
|
||||
}
|
||||
if (ImportedRecordstemp > 0)
|
||||
importCounts.push({ tableName, count: ImportedRecordstemp });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Ciclo su ogni proprietà di myexp
|
||||
for (const key in myexp) {
|
||||
if (myexp.hasOwnProperty(key)) {
|
||||
let ImportedRecordstemp = 0;
|
||||
const tableName = key;
|
||||
|
||||
// Verifica se la tabella esiste
|
||||
if (tableName) {
|
||||
const table = globalTables.getTableByTableName(tableName);
|
||||
|
||||
if (tableName === 'myelems') {
|
||||
if (User.isEditor(req.user.perm)) {
|
||||
for (const elem of myexp.myelems) {
|
||||
totalImportedRecords += await upsertRecord(table, elem, idapp) ? 1 : 0;
|
||||
const { ImportedRecords, newId } = await upsertRecord(table, elem, idapp, newIdPage);
|
||||
ImportedRecordstemp += ImportedRecords ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (tableName === 'myusers') {
|
||||
if (User.isManager(req.user.perm)) {
|
||||
for (const user of myexp.myusers) {
|
||||
totalImportedRecords += await upsertRecord(table, user, idapp) ? 1 : 0;
|
||||
const { ImportedRecords, newId } = await upsertRecord(table, user, idapp);
|
||||
ImportedRecordstemp += ImportedRecords ? 1 : 0;
|
||||
}
|
||||
}
|
||||
} else if (table && (tableName !== 'mypages')) {
|
||||
// Tutte le altre tabelle
|
||||
if (User.isManager(req.user.perm)) {
|
||||
for (const rec of myexp[key]) {
|
||||
const { ImportedRecords, newId } = await upsertRecord(table, rec, idapp);
|
||||
ImportedRecordstemp += ImportedRecords ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
totalImportedRecords += ImportedRecordstemp;
|
||||
if (ImportedRecordstemp > 0)
|
||||
importCounts.push({ tableName, count: ImportedRecordstemp });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -980,7 +1058,7 @@ async function importPage(req, idapp, jsonString) {
|
||||
console.log(`Importazione completata con successo. Totale record importati: ${totalImportedRecords}`);
|
||||
}
|
||||
|
||||
return { imported: totalImportedRecords };
|
||||
return { imported: totalImportedRecords, importCounts };
|
||||
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'importazione:', error);
|
||||
@@ -1387,9 +1465,9 @@ router.patch('/askfunz', authenticate, async (req, res) => {
|
||||
const coltoshow = {
|
||||
[mydata.coltoshow]: 1
|
||||
};
|
||||
|
||||
|
||||
const ris = await mytable.findOne({ _id: id }, coltoshow);
|
||||
|
||||
|
||||
return ris;
|
||||
} else if (mydata.myfunc === shared_consts.CallFunz.SET_VALBYTABLE) {
|
||||
const mytable = globalTables.getTableByTableName(mydata.table);
|
||||
@@ -1397,7 +1475,7 @@ router.patch('/askfunz', authenticate, async (req, res) => {
|
||||
const coltoset = {
|
||||
[mydata.coltoshow]: value
|
||||
};
|
||||
|
||||
|
||||
const ris = await mytable.findOneAndUpdate({ _id: id }, { $set: coltoset }, { new: false });
|
||||
if (!!ris)
|
||||
return res.send({ code: server_constants.RIS_CODE_OK });*/
|
||||
@@ -2009,7 +2087,7 @@ router.get(process.env.LINK_CHECK_UPDATES, authenticate_noerror, async (req, res
|
||||
if (req.user) {
|
||||
|
||||
const userId = req.user._id;
|
||||
if (!ObjectID.isValid(userId)) {
|
||||
if (!ObjectId.isValid(userId)) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
@@ -2059,29 +2137,29 @@ router.post('/upload_from_other_server/:dir', authenticate, (req, res) => {
|
||||
|
||||
/*
|
||||
const form = new formidable.IncomingForm();
|
||||
|
||||
|
||||
form.parse(req);
|
||||
|
||||
|
||||
const client = new ftp(process.env.FTPSERVER_HOST, process.env.FTPSERVER_PORT, process.env.FTPSERVER_USER + idapp + '@associazioneshen.it', process.env.FTPSERVER_PWD + idapp, false, 134217728);
|
||||
|
||||
|
||||
// SSL_OP_NO_TLSv1_2 = 134217728
|
||||
|
||||
|
||||
// console.log('client', client);
|
||||
|
||||
|
||||
form.uploadDir = folder + '/' + dir;
|
||||
try {
|
||||
|
||||
|
||||
form.on('fileBegin', async function (name, file){
|
||||
file.path = folder + '/' + file.name;
|
||||
});
|
||||
|
||||
|
||||
form.on('file', async function (name, file){
|
||||
try {
|
||||
// Create directory remote
|
||||
|
||||
|
||||
if (!!dir)
|
||||
await client.createDir(dir);
|
||||
|
||||
|
||||
const miofile = (dir) ? dir + ` / ` + file.name : file.name;
|
||||
console.log('Upload...');
|
||||
const ret = await client.upload(file.path, miofile, 755);
|
||||
@@ -2098,17 +2176,17 @@ router.post('/upload_from_other_server/:dir', authenticate, (req, res) => {
|
||||
res.status(400).send();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
form.on('aborted', () => {
|
||||
console.error('Request aborted by the user');
|
||||
res.status(400).send();
|
||||
});
|
||||
|
||||
|
||||
form.on('error', (err) => {
|
||||
console.error('Error Uploading', err);
|
||||
res.status(400).send();
|
||||
});
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.log('Error', e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user