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

104 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-08-26 03:33:13 +02:00
/*
Account is a User's single Circuit
*/
2022-04-07 08:19:40 +02:00
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 AccountSchema = new Schema({
_id: {
type: Number,
},
2022-08-26 03:33:13 +02:00
idapp: {
type: String,
2022-04-07 08:19:40 +02:00
},
2022-08-26 03:33:13 +02:00
username: {
2022-04-07 08:19:40 +02:00
type: String,
},
2022-08-26 03:33:13 +02:00
circuitId: { // ----- REF TO Circuit
type: Number,
},
name: {
2022-04-07 08:19:40 +02:00
type: String,
},
deperibile: {
type: Boolean,
},
importo_iniziale: {
type: Number,
},
saldo: {
type: Number,
},
});
AccountSchema.statics.findAllIdApp = async function(idapp) {
2022-08-26 03:33:13 +02:00
const Account = this;
2022-04-07 08:19:40 +02:00
const myfind = {idapp};
2022-08-26 03:33:13 +02:00
return await Account.find(myfind, (err, arrrec) => {
2022-04-07 08:19:40 +02:00
return arrrec;
});
};
AccountSchema.pre('save', async function(next) {
if (this.isNew) {
const myrec = await Account.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();
});
AccountSchema.statics.getFieldsForSearch = function() {
return [
{field: 'name', type: tools.FieldType.string},
2022-04-07 08:19:40 +02:00
];
};
AccountSchema.statics.executeQueryTable = function(idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
2022-08-26 03:33:13 +02:00
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
const Account = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
};
return await Account.find(myquery);
};
2022-04-07 08:19:40 +02:00
const Account = mongoose.model('Account', AccountSchema);
module.exports = {Account};