/* Account is a User's single Circuit */ 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: String, default: function () { return new ObjectID().toString() } }, idapp: { type: String, }, username: { type: String, }, circuitId: { // ----- REF TO Circuit type: String, }, 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) { this._id = new ObjectID().toString() } 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.methods.addtoSaldo = async function(amount) { const account = this; if (account) { account.saldo = account.saldo + amount; account.date_updated = new Date(); return await account.save(); } return null; }; AccountSchema.pre('save', async function(next) { if (this.isNew) { this.date_created = new Date(); } next(); }); AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) { const Account = this; try { const {Circuit} = require('../models/circuit'); if (username === undefined) return false; let myquery = { 'idapp': idapp, 'username': username, }; if (circuitId) { myquery.circuitId = circuitId; } if (circuitName) { myquery.circuitName = circuitName; } let mycircuit; if (circuitId) mycircuit = await Circuit.getCircuitById(circuitId); else mycircuit = await Circuit.findOne({name: circuitName}); if (mycircuit) { let myaccount = await Account.findOne(myquery); 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; } return null; } catch (e) { console.error('error', e); } }; AccountSchema.statics.createAccount = async function(idapp, username, circuitName) { return await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitName}, true); }; 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); } }; const Account = mongoose.model('Account', AccountSchema); module.exports = {Account};