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

652 lines
15 KiB
JavaScript
Raw Normal View History

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');
const {Account} = require('../models/account');
2022-04-07 08:19:40 +02:00
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const MovementSchema = 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
},
idapp: {
type: String,
},
notifId: {
type: String,
},
2022-04-07 08:19:40 +02:00
transactionDate: {
type: Date,
2022-04-07 08:19:40 +02:00
},
accountFromId: {
2022-09-14 11:32:04 +02:00
type: String,
2022-04-07 08:19:40 +02:00
},
accountToId: {
2022-09-14 11:32:04 +02:00
type: String,
2022-04-07 08:19:40 +02:00
},
causal_table: {
type: String,
},
causal_IdRec: {
type: String,
},
2022-04-07 08:19:40 +02:00
amount: {
type: Number,
},
causal: {
type: String,
},
residual: {
type: Number,
},
expiringDate: {
type: Date,
2022-04-07 08:19:40 +02:00
},
});
MovementSchema.statics.findAllIdApp = async function(idapp) {
const MyMovement = this;
const myfind = {idapp};
return await MyMovement.find(myfind, (err, arrrec) => {
2022-04-07 08:19:40 +02:00
return arrrec;
});
};
MovementSchema.pre('save', async function(next) {
if (this.isNew) {
2022-09-14 11:32:04 +02:00
this.transactionDate = new Date();
2022-04-07 08:19:40 +02:00
}
next();
});
MovementSchema.statics.getFieldsForSearch = function() {
return [
{field: 'causal', type: tools.FieldType.string},
{field: 'amount', type: tools.FieldType.number},
2022-04-07 08:19:40 +02:00
];
};
MovementSchema.statics.executeQueryTable = function(idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId) {
try {
// Only positive values
amount = Math.abs(amount);
let mymov = await Movement.create(
{
2022-09-14 11:32:04 +02:00
_id: new ObjectID().toString(),
idapp,
transactionDate: new Date(),
accountFromId: accountFromIdTable._id,
accountToId: accountToIdTable._id,
amount,
causal,
residual: 0,
notifId,
// expiringDate:
},
);
if (mymov) {
2022-09-14 11:32:04 +02:00
// Update saldo dell'Account
await Account.addtoSaldo(accountToIdTable, amount);
2022-10-22 15:38:15 +02:00
await Account.addtoSaldo(accountFromIdTable, -amount);
return mymov;
2022-09-14 11:32:04 +02:00
}
} catch (e) {
console.error('Error in addMov', e.message);
}
};
MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) {
2022-09-12 18:37:08 +02:00
try {
if (!circuitId) {
return [];
}
2023-02-01 16:36:10 +01:00
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, '');
2022-09-12 18:37:08 +02:00
if (myaccount) {
let aggr1 = [
{
$match: {
idapp,
$or: [
{accountFromId: myaccount._id},
{accountToId: myaccount._id}],
},
},
{
$lookup: {
from: 'accounts',
localField: 'accountFromId',
foreignField: '_id',
as: 'accfrom',
},
},
{$unwind: '$accfrom'},
{
$lookup: {
from: 'users',
let: {username: '$accfrom.username', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$username', '$username']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'userfrom',
},
},
{$unwind: '$userfrom'},
{
$lookup: {
from: 'mygroups',
let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$groupname', '$groupname']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'groupfrom',
},
},
{$unwind: '$groupfrom'},
2023-02-01 16:36:10 +01:00
{
$lookup: {
from: 'circuits',
let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$contocom', '$path']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'contocomfrom',
},
},
{$unwind: '$contocomfrom'},
{
$lookup: {
from: 'accounts',
localField: 'accountToId',
foreignField: '_id',
as: 'accto',
},
},
{$unwind: '$accto'},
2022-09-12 18:37:08 +02:00
{
$lookup: {
from: 'circuits',
localField: 'accfrom.circuitId',
foreignField: '_id',
as: 'circuitfrom',
},
2022-09-12 18:37:08 +02:00
},
{
$unwind: '$circuitfrom',
2022-09-12 18:37:08 +02:00
},
{
$lookup: {
from: 'circuits',
localField: 'accto.circuitId',
foreignField: '_id',
as: 'circuitto',
},
2022-09-12 18:37:08 +02:00
},
{
$unwind: '$circuitto',
2022-09-12 18:37:08 +02:00
},
{
$lookup: {
from: 'users',
let: {username: '$accto.username', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$username', '$username']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'userto',
},
},
{$unwind: '$userto'},
{
$lookup: {
from: 'mygroups',
let: {groupname: '$accto.groupname', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$groupname', '$groupname']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'groupto',
},
},
{$unwind: '$groupto'},
2023-02-01 16:36:10 +01:00
{
$lookup: {
from: 'circuits',
let: {contocom: '$accto.contocom', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$contocom', '$path']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'contocomto',
},
},
{$unwind: '$contocomto'},
{
$project:
{
transactionDate: 1,
amount: 1,
causal: 1,
notifId: 1,
2022-09-12 18:37:08 +02:00
'circuitfrom.symbol': 1,
'circuitto.symbol': 1,
'userfrom.username': 1,
'userfrom.profile.img': 1,
'userto.username': 1,
'userto.profile.img': 1,
'groupfrom.groupname': 1,
'groupto.groupname': 1,
2023-02-01 16:36:10 +01:00
'contocomfrom.path': 1,
'contocomto.path': 1,
},
},
];
return aggr1;
}
2022-09-12 18:37:08 +02:00
} catch (e) {
return [];
}
return [];
};
2022-09-03 13:06:58 +02:00
MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, circuitId) {
try {
if (!circuitId) {
return [];
}
let aggr1 = [
{
$match: {
idapp,
},
},
{
$lookup: {
from: 'accounts',
localField: 'accountFromId',
foreignField: '_id',
as: 'accfrom',
},
},
{$unwind: '$accfrom'},
{
$lookup: {
from: 'users',
let: {username: '$accfrom.username', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$username', '$username']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'userfrom',
},
},
{
$unwind: {
path: '$userfrom',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'mygroups',
let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$groupname', '$groupname']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'groupfrom',
},
},
{
$unwind: {
path: '$groupfrom',
preserveNullAndEmptyArrays: true,
},
},
2023-02-01 16:36:10 +01:00
{
$lookup: {
from: 'circuits',
let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$contocom', '$path']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'contocomfrom',
},
},
{
$unwind: {
path: '$contocomfrom',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'accounts',
localField: 'accountToId',
foreignField: '_id',
as: 'accto',
},
},
{
$unwind: {
path: '$accto',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {'accto.circuitId': circuitId},
},
{
'$lookup': {
'from': 'circuits',
'localField': 'accfrom.circuitId',
'foreignField': '_id',
'as': 'circuitfrom',
},
},
{
$unwind: {
path: '$circuitfrom',
preserveNullAndEmptyArrays: true,
},
},
{
'$lookup': {
'from': 'circuits',
'localField': 'accto.circuitId',
'foreignField': '_id',
'as': 'circuitto',
},
},
{
$unwind: {
path: '$circuitto',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'users',
let: {username: '$accto.username', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$username', '$username']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'userto',
},
},
{
$unwind: {
path: '$userto',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'mygroups',
let: {groupname: '$accto.groupname', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$groupname', '$groupname']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'groupto',
},
},
{
$unwind: {
path: '$groupto',
preserveNullAndEmptyArrays: true,
},
},
2023-02-01 16:36:10 +01:00
{
$lookup: {
from: 'circuits',
let: {contocom: '$accto.contocom', idapp: '$accto.idapp'},
pipeline: [
{
$match:
{
$expr:
{
$and:
[
{$eq: ['$$contocom', '$path']},
{$eq: ['$$idapp', '$idapp']},
],
},
},
},
],
as: 'contocomto',
},
},
{
$unwind: {
path: '$contocomto',
preserveNullAndEmptyArrays: true,
},
},
{
$project:
{
transactionDate: 1,
amount: 1,
causal: 1,
notifId: 1,
'circuitfrom.symbol': 1,
'circuitto.symbol': 1,
'userfrom.username': 1,
'userfrom.profile.img': 1,
'userto.username': 1,
'userto.profile.img': 1,
'groupfrom.groupname': 1,
'groupto.groupname': 1,
2023-02-01 16:36:10 +01:00
'contocomfrom.path': 1,
'contocomto.path': 1,
},
},
];
return aggr1;
} catch (e) {
return [];
}
return [];
};
MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circuitId) {
const MyMovement = this;
2022-09-03 13:06:58 +02:00
const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId);
2022-09-03 13:06:58 +02:00
2022-09-12 18:37:08 +02:00
if (myquery && myquery.length > 0) {
ris = await MyMovement.aggregate(myquery);
2022-09-03 13:06:58 +02:00
return ris;
}
2022-09-03 13:06:58 +02:00
return [];
2022-09-03 13:06:58 +02:00
};
MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) {
const MyMovement = this;
try {
const rec = await MyMovement.findOne({notifId}, {_id: 1});
return !!rec;
} catch (e) {
// If Error, don't send the coins
return true;
}
};
2022-04-07 08:19:40 +02:00
const Movement = mongoose.model('Movement', MovementSchema);
module.exports = {Movement};