/* Account is a User's single Circuit */ const mongoose = require('mongoose').set('debug', process.env.DEBUG); 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, }, idapp: { type: String, }, username: { type: String, }, circuitId: { // ----- REF TO Circuit type: Number, }, name: { type: String, }, deperibile: { type: Boolean, }, fidoConcesso: { type: Number, }, qta_maxConcessa: { type: Number, }, importo_iniziale: { type: Number, }, saldo: { type: Number, }, deleted: { type: Boolean, default: false, }, date_created: { type: Date, default: Date.now, }, date_updated: { type: Date, }, }); AccountSchema.statics.findAllIdApp = async function(idapp) { const Account = this; const myfind = {idapp, deleted: false}; return await Account.find(myfind, (err, arrrec) => { 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}, ]; }; AccountSchema.statics.executeQueryTable = function(idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; 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(); }; AccountSchema.statics.getUserAccounts = async function(idapp, username) { try { let aggr1 = [ { $match: {idapp, username}, }, { $lookup: { from: 'circuits', localField: 'circuitId', foreignField: '_id', as: 'circuit', }, }, { '$replaceRoot': { 'newRoot': { '$mergeObjects': [ { '$arrayElemAt': [ '$circuit', 0, ], }, '$$ROOT', ], }, }, }, /* { $project: { "circuit.name": 1, }, }, */ ]; ris = await this.aggregate(aggr1); return ris; }catch (e) { console.error('e', e); } }; AccountSchema.statics.addtoSaldo = async function(id, amount) { const Account = this; if (!id) return false; const myaccount = await Account.findById(id); if (myaccount) { myaccount.saldo = myaccount.saldo + amount; myaccount.date_updated = new Date(); return await myaccount.save(); } return null; }; AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, circuitId, createifnotexist) { const Account = this; const {Circuit} = require('../models/circuit'); if (username === undefined) return false; const myquery = { 'idapp': idapp, 'username': username, circuitId, }; const mycircuit = await Circuit.getCircuitById(circuitId); let myaccount = await Account.findOne(myquery).lean(); if (!myaccount && createifnotexist) { myaccount = new Account({ idapp, username, circuitId, deperibile: false, fidoConcesso: mycircuit.fido_scoperto_default, qta_maxConcessa: mycircuit.qta_max_default, importo_iniziale: 0, saldo: 0, }); return await myaccount.save(); } return myaccount; }; const Account = mongoose.model('Account', AccountSchema); module.exports = {Account};