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

198 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-12-27 02:58:15 +01:00
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 productInfoSchema = new Schema({
idapp: {
type: String,
},
department: {
type: String, ref: 'Department'
},
code: {
type: String,
unique: true,
required: true,
},
2024-04-29 14:58:45 +02:00
id_wp: { // id in wordpress
type: String,
},
2023-12-27 02:58:15 +01:00
codice_EAN: {
type: String,
},
barcode: {
type: String,
},
name: {
type: String,
},
description: {
type: String,
},
idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd' }],
2024-01-12 13:02:59 +01:00
idSubCatProds: [{ type: Schema.Types.ObjectId, ref: 'SubCatProd' }],
2023-12-27 02:58:15 +01:00
color: {
type: String
},
size: {
2024-02-06 20:12:54 +01:00
type: String // 11x4x3
2023-12-27 02:58:15 +01:00
},
weight: {
type: Number
},
2024-02-06 20:12:54 +01:00
weight_lordo: {
type: Number
2023-12-27 02:58:15 +01:00
},
unit: {
type: Number,
default: 0,
2023-12-27 02:58:15 +01:00
},
2024-02-06 20:12:54 +01:00
unit_lordo: {
type: Number,
default: 0,
},
sfuso: { // serve se moltiplicare le qta (es: 12 kg) oppure fare (2 x 20 ml)
type: Boolean
},
2024-02-06 20:12:54 +01:00
vegan: {
type: Boolean
},
2023-12-27 02:58:15 +01:00
icon: {
type: String,
},
2024-04-29 14:58:45 +02:00
img: { // Se esiste img (sul server) visualizza questa, altrimenti vedi se esiste image_link
type: String,
},
image_link: {
2023-12-27 02:58:15 +01:00
type: String,
},
2024-02-06 20:12:54 +01:00
link_scheda: {
type: String,
},
2023-12-27 02:58:15 +01:00
link: {
type: String,
},
img2: {
type: String,
},
img3: {
type: String,
},
ingredienti: {
type: String,
},
valori_nutrizionali: {
type: String,
},
note: {
type: String,
},
2024-04-29 14:58:45 +02:00
author: {
type: String,
},
collezione: {
type: String,
},
publisher: { //editore
type: String,
},
numpages: {
type: Number,
},
2023-12-27 02:58:15 +01:00
});
var productInfo = module.exports = mongoose.model('ProductInfo', productInfoSchema);
module.exports.getFieldsForSearch = function () {
return [{ field: 'name', type: tools.FieldType.string }]
};
module.exports.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
module.exports.findAllIdApp = async function (idapp, code, id) {
let myfind = {};
let myqueryadd = {};
let query = [];
try {
if (idapp)
myfind = { idapp };
if (code) {
myfind = { ...myfind, code }
}
if (id) {
myqueryadd = {
$addFields: {
myId1: {
$toObjectId: id,
},
},
}
myfind = {
$expr: {
$eq: ["$_id", "$myId1"],
},
}
query.push(myqueryadd);
}
query.push(
{ $match: myfind },
{
$lookup: {
from: 'catprods',
localField: 'idCatProds',
foreignField: '_id',
as: 'catprods'
}
},
2024-01-12 13:02:59 +01:00
{
$lookup: {
from: 'subcatprods',
localField: 'idSubCatProds',
foreignField: '_id',
as: 'subcatprods'
}
},
2023-12-27 02:58:15 +01:00
{
$sort: {
name: 1 // 1 for ascending order, -1 for descending order
}
},
);
let ris = await productInfo.aggregate(query)
return ris;
} catch (e) {
console.error('E', e);
}
};
module.exports.getProductByCode = function (idapp, code) {
return productInfo.findAllIdApp(idapp, code);
}
module.exports.createIndexes((err) => {
if (err) throw err;
});