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

155 lines
3.5 KiB
JavaScript
Raw Normal View History

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');
const { ObjectID } = require('mongodb');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const SettingsSchema = new Schema({
idapp: {
type: String,
},
key: {
type: String,
},
type: {
type: Number,
},
value_str: {
type: String,
},
value_date: {
type: Date,
},
value_num: {
type: Number,
},
value_bool: {
type: Boolean,
},
serv: {
type: Boolean,
default: false
}
});
SettingsSchema.statics.getFieldsForSearch = function () {
2020-03-21 10:28:26 +01:00
return [{ field: 'key', type: tools.FieldType.string },
{ field: 'value_str', type: tools.FieldType.string }, { field: 'value_num', type: tools.FieldType.number }]
};
SettingsSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
2019-12-04 18:20:50 +01:00
SettingsSchema.statics.getValDbSettings = function (idapp, key, def) {
return Settings.findOne({ idapp, key })
.then((myrec) => {
// console.log('getValDbSettings', myrec, 'idapp', idapp);
if (!!myrec) {
if (myrec.type === tools.FieldType.date)
return myrec.value_date;
2021-02-18 12:19:35 +01:00
else if ((myrec.type === tools.FieldType.number) || (myrec.type === tools.FieldType.hours))
return myrec.value_num;
else if (myrec.type === tools.FieldType.boolean)
return myrec.value_bool;
else
return myrec.value_str;
} else {
2019-12-04 18:20:50 +01:00
return def
}
}).catch((err) => {
console.error('getValDbSettings', err);
2019-12-04 18:20:50 +01:00
return def;
});
};
SettingsSchema.statics.DuplicateAllRecords = async function (idapporig, idappdest) {
return tools.DuplicateAllRecords(this, idapporig, idappdest);
};
SettingsSchema.statics.findAllIdApp = function (idapp, serv) {
const Settings = this;
let myfind = '';
if (serv)
myfind = { idapp, serv };
else
myfind = { idapp };
return Settings.find(myfind, (err, arrrec) => {
return arrrec
});
};
SettingsSchema.statics.setKeyNum = async function (idapp, key, value) {
const Settings = this;
let myrec = await Settings.findOne({ idapp, key });
if (!myrec) {
myrec = new Settings({ key });
myrec._id = new ObjectID();
myrec.idapp = idapp;
myrec.type = tools.FieldType.number;
myrec.value_num = value;
return await myrec.save();
} else {
2020-03-21 10:28:26 +01:00
myrec = await Settings.findOneAndUpdate({ idapp, key }, { $set: { value_num: value } }, { new: false });
}
return myrec
};
SettingsSchema.statics.setBool = async function (idapp, key, valbool) {
const Settings = this;
let myrec = await Settings.findOne({ idapp, key });
if (!myrec) {
myrec = new Settings({ key });
myrec._id = new ObjectID();
myrec.idapp = idapp;
myrec.type = tools.FieldType.boolean;
myrec.value_bool = valbool;
return await myrec.save();
} else {
myrec = await Settings.findOneAndUpdate({ idapp, key }, { $set: { value_bool: valbool } }, { new: false });
}
return myrec
};
SettingsSchema.statics.getKeyNum = async function (idapp, key, mydefault) {
const Settings = this;
2020-03-21 10:28:26 +01:00
const ret = await Settings.findOne({ idapp, key });
if (!!ret) {
return ret.value_num;
} else {
return mydefault;
}
};
2020-03-21 10:28:26 +01:00
const Settings = mongoose.model('Settings', SettingsSchema);
module.exports = { Settings };