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

225 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-08-26 03:33:13 +02:00
/*
Account is a User's single Circuit
*/
2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
2022-04-07 08:19:40 +02:00
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: {
2022-09-14 11:32:04 +02:00
type: String,
default: function () {
return new ObjectID().toString()
}
2022-04-07 08:19:40 +02:00
},
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
2022-09-14 11:32:04 +02:00
type: String,
2022-08-26 03:33:13 +02:00
},
name: {
2022-04-07 08:19:40 +02:00
type: String,
},
deperibile: {
type: Boolean,
},
fidoConcesso: {
type: Number,
},
qta_maxConcessa: {
type: Number,
},
2022-04-07 08:19:40 +02:00
importo_iniziale: {
type: Number,
},
saldo: {
type: Number,
},
deleted: {
type: Boolean,
default: false,
},
2022-09-03 13:06:58 +02:00
date_created: {
type: Date,
default: Date.now,
},
date_updated: {
type: Date,
},
2022-04-07 08:19:40 +02:00
});
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, deleted: false};
2022-04-07 08:19:40 +02:00
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) {
2022-09-14 11:32:04 +02:00
this._id = new ObjectID().toString()
2022-04-07 08:19:40 +02:00
}
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, idapp, params);
2022-04-07 08:19:40 +02:00
};
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).lean();
2022-08-26 03:33:13 +02:00
2022-09-03 13:06:58 +02:00
};
AccountSchema.methods.addtoSaldo = async function(amount) {
const account = this;
2022-09-03 13:06:58 +02:00
if (account) {
account.saldo = account.saldo + amount;
account.date_updated = new Date();
return await account.save();
2022-09-03 13:06:58 +02:00
}
return null;
};
2022-09-14 11:32:04 +02:00
AccountSchema.pre('save', async function(next) {
if (this.isNew) {
this.date_created = new Date();
}
next();
});
2022-09-12 18:37:08 +02:00
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) {
2022-09-03 13:06:58 +02:00
const Account = this;
2022-09-12 18:37:08 +02:00
try {
2022-09-12 18:37:08 +02:00
const {Circuit} = require('../models/circuit');
2022-09-03 13:06:58 +02:00
2022-09-12 18:37:08 +02:00
if (username === undefined)
return false;
2022-09-03 13:06:58 +02:00
2022-09-12 18:37:08 +02:00
let myquery = {
'idapp': idapp,
'username': username,
};
2022-09-12 18:37:08 +02:00
if (circuitId) {
myquery.circuitId = circuitId;
}
if (circuitName) {
myquery.circuitName = circuitName;
}
2022-09-03 13:06:58 +02:00
2022-09-14 11:32:04 +02:00
let mycircuit;
if (circuitId)
mycircuit = await Circuit.getCircuitById(circuitId);
else
mycircuit = await Circuit.findOne({name: circuitName});
2022-09-14 11:32:04 +02:00
if (mycircuit) {
let myaccount = await Account.findOne(myquery);
2022-09-14 11:32:04 +02:00
if (!myaccount && createifnotexist) {
myaccount = new Account({
_id: new ObjectID().toString(),
idapp,
username,
circuitId: mycircuit._id,
deperibile: false,
fidoConcesso: mycircuit.fido_scoperto_default,
qta_maxConcessa: mycircuit.qta_max_default,
importo_iniziale: 0,
saldo: 0,
});
return await myaccount.save();
}
return myaccount;
2022-09-12 18:37:08 +02:00
}
2022-09-14 11:32:04 +02:00
return null;
2022-09-12 18:37:08 +02:00
} catch (e) {
console.error('error', e);
2022-09-03 13:06:58 +02:00
}
2022-09-12 18:37:08 +02:00
};
AccountSchema.statics.createAccount = async function(idapp, username, circuitName) {
return await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitName}, true);
2022-09-03 13:06:58 +02:00
};
2022-08-26 03:33:13 +02:00
2022-09-12 18:37:08 +02:00
AccountSchema.statics.getUserAccounts = async function(idapp, username) {
try {
let aggr1 = [
{
$match: {idapp, username},
},
{
$lookup: {
from: 'circuits',
localField: 'circuitId',
foreignField: '_id',
as: 'circuit',
},
},
{$unwind: '$circuit'},
];
ris = await this.aggregate(aggr1);
return ris;
} catch (e) {
console.error('e', e);
}
};
2022-08-26 03:33:13 +02:00
2022-04-07 08:19:40 +02:00
const Account = mongoose.model('Account', AccountSchema);
module.exports = {Account};