Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id": "5d0118f0f57a282f89bc5f71",
- "product": {
- "_id": "5cfed37375a13067dd01ddb7",
- "name": "My product",
- "description": "My description",
- "purchased_amount": 15,
- "unit_price_mex": "45",
- "unit_price_to_sell": "5",
- "travel": "5cf58713d6f7f1657e2d8302",
- "__v": 0,
- "id": "5cfed37375a13067dd01ddb7"
- },
- "client": {
- "_id": "5cf1778efffb651fad89d8b6",
- "name": "Client name",
- "description": "",
- "__v": 0
- },
- "purchased_amount": 3,
- "fch": "13/6/2019",
- "__v": 0
- },
- {
- "_id": "5d0151afda1a446008f1817b",
- "product": {
- "_id": "5cfed1995eaf2665c45efd82",
- "name": "Camisa",
- "description": "Camisas buenas",
- "purchased_amount": 10,
- "unit_price_mex": "100",
- "unit_price_to_sell": "15",
- "travel": "5cf56b04462a865264fabb9d",
- "__v": 0,
- "id": "5cfed1995eaf2665c45efd82"
- },
- "client": {
- "_id": "5cf1778efffb651fad89d8b6",
- "name": "Randy",
- "description": "El que trabaja aqui",
- "__v": 0
- },
- "purchased_amount": 34,
- "fch": "12/6/2019",
- "__v": 0
- },
- var mongoose = require('mongoose');
- const mongoosePaginate = require('mongoose-paginate-v2');
- var clientSchema = new mongoose.Schema({
- name: String,
- description: String
- }).plugin(mongoosePaginate);
- var Client = mongoose.model('Client', clientSchema);
- module.exports = Client;
- var mongoose = require('mongoose');
- const mongoosePaginate = require('mongoose-paginate-v2');
- var productSchema = new mongoose.Schema({
- name: String,
- description: String,
- purchased_amount: Number,
- unit_price_mex: mongoose.Schema.Types.Decimal128,
- unit_price_to_sell: mongoose.Schema.Types.Decimal128,
- travel: { type: mongoose.Schema.Types.ObjectId, ref: 'Travel' }
- }).plugin(mongoosePaginate);
- productSchema.set('toJSON', {
- getters: true,
- transform: (doc, ret) => {
- if (ret.unit_price_mex) {
- ret.unit_price_mex = ret.unit_price_mex.toString();
- }
- if ( ret.unit_price_to_sell ) {
- ret.unit_price_to_sell = ret.unit_price_to_sell.toString();
- }
- }
- })
- var Product = mongoose.model('Product', productSchema);
- module.exports = Product;
- var aggregate = InvoiceModel.aggregate([
- { $match: { client: mongoose.Types.ObjectId( id ) } },
- { $group: { _id: null, total: { $sum: { $multiply: [ "$purchased_amount", "$product.unit_price_to_sell" ] } } } }
- ]);
- InvoiceModel.aggregatePaginate(aggregate, {}, (error, aggs) => {
- InvoiceModel.paginate({ client: id },{ page, limit, populate: 'client product' }, (err, value) => {
- return res.status(200).send({
- results: value.docs,
- totalPages: value.totalPages,
- totalDocs: value.totalDocs,
- purchase_amount_total : aggs.docs[0].total
- })
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement