- Aggiornate tutte le categorie ottimizzandole.
- Migrazione delle vecchie categ. con quelle nuove. - Create le Categorie e sottocategorie degli Eventi (a parte). - Aggiornato la card dell'Ospitalità
This commit is contained in:
88
src/models/bacheca.js
Executable file
88
src/models/bacheca.js
Executable file
@@ -0,0 +1,88 @@
|
||||
const mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const BachecaSchema = new Schema({
|
||||
_id: {
|
||||
type: Number,
|
||||
},
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
idSectorBacheca: [{
|
||||
type: Number
|
||||
}],
|
||||
icon: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
BachecaSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const Bacheca = this;
|
||||
|
||||
const query = [
|
||||
{ $sort: { descr: 1 } }
|
||||
];
|
||||
|
||||
const res = await Bacheca
|
||||
.aggregate(query)
|
||||
.then((arrrec) => {
|
||||
return arrrec
|
||||
})
|
||||
|
||||
return res;
|
||||
|
||||
};
|
||||
|
||||
BachecaSchema.pre('save', async function (next) {
|
||||
if (this.isNew) {
|
||||
const myrec = await Bacheca.findOne().limit(1).sort({_id:-1});
|
||||
if (!!myrec) {
|
||||
if (myrec._doc._id === 0)
|
||||
this._id = 1;
|
||||
else
|
||||
this._id = myrec._doc._id + 1;
|
||||
|
||||
} else {
|
||||
this._id = 1;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
|
||||
BachecaSchema.statics.getFieldsForSearch = function () {
|
||||
return [{ field: 'label', type: tools.FieldType.string },
|
||||
{ field: 'descr', type: tools.FieldType.string }]
|
||||
};
|
||||
|
||||
BachecaSchema.statics.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, 0, params);
|
||||
};
|
||||
|
||||
|
||||
const Bacheca = mongoose.model('Bacheca', BachecaSchema);
|
||||
|
||||
Bacheca.createIndexes()
|
||||
.then(() => { })
|
||||
.catch((err) => { throw err; });
|
||||
|
||||
|
||||
module.exports = { Bacheca };
|
||||
@@ -166,6 +166,7 @@ const CircuitSchema = new Schema({
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
|
||||
@@ -33,13 +33,24 @@ const MyBachecaSchema = new Schema({
|
||||
},
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||
groupname: { type: String },
|
||||
idSector: {
|
||||
idSectorBacheca: {
|
||||
type: Number,
|
||||
},
|
||||
idSkill: {
|
||||
},
|
||||
idBacheca: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
// ------------------
|
||||
idSector: { // VECCHIO
|
||||
type: Number,
|
||||
},
|
||||
|
||||
idSkill: { // VECCHIO
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
|
||||
// ------------------
|
||||
idStatusSkill: [
|
||||
{
|
||||
type: Number,
|
||||
@@ -123,6 +134,7 @@ const MyBachecaSchema = new Schema({
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
@@ -233,8 +245,8 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'skills',
|
||||
localField: 'idSkill',
|
||||
from: 'bachecas',
|
||||
localField: 'idBacheca',
|
||||
foreignField: '_id',
|
||||
as: 'recSkill',
|
||||
},
|
||||
@@ -256,10 +268,10 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'sectors',
|
||||
localField: 'idSector',
|
||||
from: 'sectorbachecas',
|
||||
localField: 'idSectorBacheca',
|
||||
foreignField: '_id',
|
||||
as: 'sector',
|
||||
as: 'sectorBacheca',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -267,7 +279,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
$arrayElemAt: ['$sector', 0],
|
||||
$arrayElemAt: ['$sectorBacheca', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
|
||||
@@ -16,7 +16,7 @@ const { ObjectId } = require('mongodb');
|
||||
const tableModel = shared_consts.TABLES_MYGOODS;
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
mongoose.plugin((schema) => {
|
||||
schema.options.usePushEach = true;
|
||||
});
|
||||
|
||||
@@ -41,18 +41,21 @@ const MyGoodSchema = new Schema({
|
||||
idShipping: [
|
||||
{
|
||||
type: Number,
|
||||
}],
|
||||
},
|
||||
],
|
||||
|
||||
idContribType: [
|
||||
{
|
||||
type: String,
|
||||
}],
|
||||
},
|
||||
],
|
||||
idCity: [
|
||||
{
|
||||
type: Number,
|
||||
}],
|
||||
},
|
||||
],
|
||||
pub_to_share: {
|
||||
type: Number, // PUB_TO_SHARE_ALL, PUB_TO_SHARE_ONLY_TABLE_FOLLOW
|
||||
type: Number, // PUB_TO_SHARE_ALL, PUB_TO_SHARE_ONLY_TABLE_FOLLOW
|
||||
},
|
||||
numLevel: {
|
||||
type: Number,
|
||||
@@ -61,9 +64,11 @@ const MyGoodSchema = new Schema({
|
||||
adType: {
|
||||
type: Number,
|
||||
},
|
||||
otherfilters: [{
|
||||
type: Number,
|
||||
}],
|
||||
otherfilters: [
|
||||
{
|
||||
type: Number,
|
||||
},
|
||||
],
|
||||
photos: [
|
||||
{
|
||||
imagefile: {
|
||||
@@ -75,7 +80,8 @@ const MyGoodSchema = new Schema({
|
||||
description: {
|
||||
type: String,
|
||||
},
|
||||
}],
|
||||
},
|
||||
],
|
||||
note: {
|
||||
type: String,
|
||||
default: '',
|
||||
@@ -89,19 +95,19 @@ const MyGoodSchema = new Schema({
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
},
|
||||
},
|
||||
},
|
||||
...Reaction.getFieldsForReactions(),
|
||||
...tools.getFieldsForAnnunci()
|
||||
...tools.getFieldsForAnnunci(),
|
||||
});
|
||||
|
||||
MyGoodSchema.pre('save', async function (next) {
|
||||
if (this.isNew) {
|
||||
if (!this.date_created)
|
||||
this.date_created = new Date();
|
||||
if (!this.date_created) this.date_created = new Date();
|
||||
}
|
||||
|
||||
next();
|
||||
@@ -110,15 +116,11 @@ MyGoodSchema.pre('save', async function (next) {
|
||||
MyGoodSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const MyGood = this;
|
||||
|
||||
const query = [
|
||||
{ $match: { idapp } },
|
||||
{ $sort: { descr: 1 } },
|
||||
];
|
||||
const query = [{ $match: { idapp } }, { $sort: { descr: 1 } }];
|
||||
|
||||
return await MyGood.aggregate(query).then((arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
MyGoodSchema.statics.getFieldsForSearch = function () {
|
||||
@@ -134,7 +136,6 @@ MyGoodSchema.statics.getFieldsLastForSearch = function () {
|
||||
];
|
||||
};
|
||||
|
||||
|
||||
MyGoodSchema.statics.executeQueryTable = function (idapp, params, user) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
params.fieldsearch_last = this.getFieldsLastForSearch();
|
||||
@@ -159,44 +160,40 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
const MyGood = this;
|
||||
|
||||
let myparsid = {
|
||||
'_id': idGood,
|
||||
_id: idGood,
|
||||
idapp,
|
||||
};
|
||||
|
||||
let query = [
|
||||
{
|
||||
'$match':
|
||||
myparsid,
|
||||
$match: myparsid,
|
||||
},
|
||||
{
|
||||
'$sort': {
|
||||
'desc': 1,
|
||||
$sort: {
|
||||
desc: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
'$addFields': {
|
||||
'myId1': {
|
||||
'$toObjectId': '$userId',
|
||||
$addFields: {
|
||||
myId1: {
|
||||
$toObjectId: '$userId',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
'$lookup': {
|
||||
'from': 'users',
|
||||
'localField': 'myId1',
|
||||
'foreignField': '_id',
|
||||
'as': 'user',
|
||||
$lookup: {
|
||||
from: 'users',
|
||||
localField: 'myId1',
|
||||
foreignField: '_id',
|
||||
as: 'user',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$user',
|
||||
0,
|
||||
],
|
||||
$arrayElemAt: ['$user', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
@@ -207,22 +204,19 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
$project: shared_consts.getProjectForAll({}, tableModel),
|
||||
},
|
||||
{
|
||||
'$lookup': {
|
||||
'from': 'goods',
|
||||
'localField': 'idGood',
|
||||
'foreignField': '_id',
|
||||
'as': 'recGood',
|
||||
$lookup: {
|
||||
from: 'goods',
|
||||
localField: 'idGood',
|
||||
foreignField: '_id',
|
||||
as: 'recGood',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$recGood',
|
||||
0,
|
||||
],
|
||||
$arrayElemAt: ['$recGood', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
@@ -233,23 +227,19 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
$project: shared_consts.getProjectForAll({}, tableModel),
|
||||
},
|
||||
{
|
||||
'$lookup': {
|
||||
'from': 'sectorgoods',
|
||||
'localField': 'idSectorGood',
|
||||
// 'localField': 'recGood.idSectorGood',
|
||||
'foreignField': '_id',
|
||||
'as': 'sectorGood',
|
||||
$lookup: {
|
||||
from: 'sectorgoods',
|
||||
localField: 'idSectorGood',
|
||||
foreignField: '_id',
|
||||
as: 'sectorGood',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$sectorgood',
|
||||
0,
|
||||
],
|
||||
$arrayElemAt: ['$sectorgood', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
@@ -258,10 +248,10 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
'from': 'mygroups',
|
||||
'localField': 'groupname',
|
||||
'foreignField': 'groupname',
|
||||
'as': 'mygrp',
|
||||
from: 'mygroups',
|
||||
localField: 'groupname',
|
||||
foreignField: 'groupname',
|
||||
as: 'mygrp',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -274,22 +264,19 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
$project: shared_consts.getProjectForAll({}, tableModel),
|
||||
},
|
||||
{
|
||||
'$lookup': {
|
||||
'from': 'subgoods',
|
||||
'localField': 'idShipping',
|
||||
'foreignField': '_id',
|
||||
'as': 'MyGood',
|
||||
$lookup: {
|
||||
from: 'subgoods',
|
||||
localField: 'idShipping',
|
||||
foreignField: '_id',
|
||||
as: 'MyGood',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$MyGood',
|
||||
0,
|
||||
],
|
||||
$arrayElemAt: ['$MyGood', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
@@ -300,22 +287,19 @@ MyGoodSchema.statics.getMyRecById = function (idapp, idGood) {
|
||||
$project: shared_consts.getProjectForAll({}, tableModel),
|
||||
},
|
||||
{
|
||||
'$lookup': {
|
||||
'from': 'cities',
|
||||
'localField': 'idCity',
|
||||
'foreignField': '_id',
|
||||
'as': 'mycities',
|
||||
$lookup: {
|
||||
from: 'cities',
|
||||
localField: 'idCity',
|
||||
foreignField: '_id',
|
||||
as: 'mycities',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
$replaceRoot: {
|
||||
newRoot: {
|
||||
$mergeObjects: [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$mycities',
|
||||
0,
|
||||
],
|
||||
$arrayElemAt: ['$mycities', 0],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
@@ -344,32 +328,30 @@ MyGoodSchema.statics.getCompleteRecord = function (idapp, id) {
|
||||
const MyGood = this;
|
||||
|
||||
return MyGood.getMyRecById(idapp, id);
|
||||
|
||||
};
|
||||
|
||||
MyGoodSchema.statics.getProject = function () {
|
||||
let proj = {
|
||||
'recGood': 1,
|
||||
'sectorGood': 1,
|
||||
'idSectorGood': 1,
|
||||
'idGood': 1,
|
||||
'idShipping': 1,
|
||||
'idStatusGood': 1,
|
||||
recGood: 1,
|
||||
sectorGood: 1,
|
||||
idSectorGood: 1,
|
||||
idGood: 1,
|
||||
idShipping: 1,
|
||||
idStatusGood: 1,
|
||||
//**ADDFIELD_MYGOOD
|
||||
|
||||
};
|
||||
|
||||
const proj_add = shared_consts.getProjectForAll()
|
||||
const proj_add = shared_consts.getProjectForAll();
|
||||
|
||||
return Object.assign({}, proj, proj_add);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
const MyGood = mongoose.model('MyGood', MyGoodSchema);
|
||||
|
||||
MyGood.createIndexes()
|
||||
.then(() => { })
|
||||
.catch((err) => { throw err; });
|
||||
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
module.exports = { MyGood };
|
||||
|
||||
@@ -96,6 +96,7 @@ const MyGroupSchema = new Schema({
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
|
||||
@@ -96,6 +96,7 @@ const MyHospSchema = new Schema({
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
@@ -359,7 +360,7 @@ MyHospSchema.statics.SettaAdTypeOffro_In_Hosps = async function () {
|
||||
}
|
||||
};
|
||||
|
||||
MyHospSchema.statics.getProject = function () {
|
||||
/*MyHospSchema.statics.getProject = function () {
|
||||
let proj = {
|
||||
visibile: 1,
|
||||
typeHosp: 1,
|
||||
@@ -378,7 +379,7 @@ MyHospSchema.statics.getProject = function () {
|
||||
return Object.assign({}, proj, proj_add);
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
const MyHosp = mongoose.model('MyHosp', MyHospSchema);
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ const MySkillSchema = new Schema(
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
@@ -347,7 +348,7 @@ MySkillSchema.statics.getMyRecById = function (idapp, idSkill) {
|
||||
});
|
||||
};
|
||||
|
||||
MySkillSchema.statics.getProject = function (proj_add2) {
|
||||
/*MySkillSchema.statics.getProject = function (proj_add2) {
|
||||
let proj = {
|
||||
recSkill: 1,
|
||||
sector: 1,
|
||||
@@ -364,6 +365,7 @@ MySkillSchema.statics.getProject = function (proj_add2) {
|
||||
return Object.assign({}, proj, proj_add);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
MySkillSchema.statics.getCompleteRecord = function (idapp, id) {
|
||||
const MySkill = this;
|
||||
|
||||
88
src/models/sectorbacheca.js
Executable file
88
src/models/sectorbacheca.js
Executable file
@@ -0,0 +1,88 @@
|
||||
const mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const SectorBachecaSchema = new Schema({
|
||||
_id: {
|
||||
type: Number,
|
||||
},
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
idSectorBacheca: {
|
||||
type: Number
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
},
|
||||
theme: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
SectorBachecaSchema.pre('save', async function (next) {
|
||||
if (this.isNew) {
|
||||
const myrec = await SectorBacheca.findOne().limit(1).sort({_id:-1});
|
||||
if (!!myrec) {
|
||||
if (myrec._doc._id === 0)
|
||||
this._id = 1;
|
||||
else
|
||||
this._id = myrec._doc._id + 1;
|
||||
} else {
|
||||
this._id = 1;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
SectorBachecaSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const SectorBacheca = this;
|
||||
|
||||
const query = [
|
||||
{ $sort: { descr: 1 } }
|
||||
];
|
||||
|
||||
return await SectorBacheca
|
||||
.aggregate(query)
|
||||
.then((arrrec) => {
|
||||
return arrrec
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
SectorBachecaSchema.statics.getFieldsForSearch = function () {
|
||||
return [{ field: 'descr', type: tools.FieldType.string }]
|
||||
};
|
||||
|
||||
SectorBachecaSchema.statics.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, 0, params);
|
||||
};
|
||||
|
||||
|
||||
const SectorBacheca = mongoose.model('SectorBacheca', SectorBachecaSchema);
|
||||
|
||||
SectorBacheca.createIndexes()
|
||||
.then(() => { })
|
||||
.catch((err) => { throw err; });
|
||||
|
||||
|
||||
module.exports = { SectorBacheca };
|
||||
@@ -1383,6 +1383,8 @@ sendNotifSchema.statics.getNotificationRecipients = async function (myrecnotifpa
|
||||
|
||||
if (myrecnotifpass.tablerec === shared_consts.TABLES_MYGOODS) {
|
||||
idSector = myrectableorig.idSectorGood;
|
||||
} else if (myrecnotifpass.tablerec === shared_consts.TABLES_MYBACHECAS) {
|
||||
idSector = myrectableorig.idSectorBacheca;
|
||||
} else {
|
||||
idSector = myrectableorig.idSector;
|
||||
}
|
||||
|
||||
163
src/models/version.js
Executable file
163
src/models/version.js
Executable file
@@ -0,0 +1,163 @@
|
||||
const mongoose = require('mongoose').set('debug', false);
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = 'F';
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
|
||||
const LASTVERSION = 'lastversion';
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin((schema) => {
|
||||
schema.options.usePushEach = true;
|
||||
});
|
||||
|
||||
const VersionSchema = new Schema({
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
executed: {
|
||||
type: Boolean,
|
||||
},
|
||||
table: {
|
||||
type: String,
|
||||
},
|
||||
version: {
|
||||
type: Number,
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
},
|
||||
});
|
||||
|
||||
VersionSchema.statics.isJobExecuted = async function (idapp, descr) {
|
||||
const Version = this;
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOne({ idapp, descr });
|
||||
return myrec.executed;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
VersionSchema.statics.setJobExecuted = async function (idapp, descr) {
|
||||
const Version = this;
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOneAndUpdate(
|
||||
{ idapp, descr },
|
||||
{
|
||||
$setOnInsert: { idapp, descr },
|
||||
$set: { executed: true },
|
||||
},
|
||||
{ new: true, upsert: true }
|
||||
);
|
||||
|
||||
if (!myrec) {
|
||||
throw new Error(`Error setting job as executed for idapp ${idapp} and descr ${descr}`);
|
||||
}
|
||||
|
||||
return myrec.executed;
|
||||
} catch (e) {
|
||||
console.error(`Error in setJobExecuted: ${e.message}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
VersionSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const Version = this;
|
||||
|
||||
const query = [{ $sort: { descr: 1 } }];
|
||||
|
||||
const res = await Version.aggregate(query).then((arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
VersionSchema.statics.getLastVersionRun = async function (idapp) {
|
||||
const Version = this;
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOne({ idapp, table: '', descr: LASTVERSION });
|
||||
return myrec ? myrec.version : 0;
|
||||
} catch (e) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
VersionSchema.statics.setLastVersionRun = async function (idapp, version) {
|
||||
const Version = this;
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOneAndUpdate(
|
||||
{ idapp, table: '', descr: LASTVERSION },
|
||||
{
|
||||
$setOnInsert: { idapp, table: '', descr: LASTVERSION },
|
||||
$set: { version },
|
||||
},
|
||||
{ new: true, upsert: true }
|
||||
);
|
||||
|
||||
return !!myrec;
|
||||
} catch (e) {
|
||||
console.error(`Error in setLastVersionTable: ${e.message}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
VersionSchema.statics.isTableVersionUpdated = async function (idapp, table) {
|
||||
const Version = this;
|
||||
|
||||
const lastversion = await Version.getLastVersionRun(idapp);
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOne({ idapp, table });
|
||||
return myrec.version === lastversion;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
VersionSchema.statics.updateTableVersion = async function (idapp, table, version) {
|
||||
const Version = this;
|
||||
|
||||
try {
|
||||
const myrec = await Version.findOneAndUpdate(
|
||||
{ idapp, table },
|
||||
{
|
||||
$setOnInsert: { idapp, table },
|
||||
$set: { version },
|
||||
},
|
||||
{ new: true, upsert: true }
|
||||
);
|
||||
|
||||
return !!myrec;
|
||||
} catch (e) {
|
||||
console.error(`Error in updateTableVersion: ${e.message}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const Version = mongoose.model('Version', VersionSchema);
|
||||
|
||||
Version.createIndexes()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
module.exports = { Version };
|
||||
Reference in New Issue
Block a user