Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. {
  2. "_id": "5d0118f0f57a282f89bc5f71",
  3. "product": {
  4. "_id": "5cfed37375a13067dd01ddb7",
  5. "name": "My product",
  6. "description": "My description",
  7. "purchased_amount": 15,
  8. "unit_price_mex": "45",
  9. "unit_price_to_sell": "5",
  10. "travel": "5cf58713d6f7f1657e2d8302",
  11. "__v": 0,
  12. "id": "5cfed37375a13067dd01ddb7"
  13. },
  14. "client": {
  15. "_id": "5cf1778efffb651fad89d8b6",
  16. "name": "Client name",
  17. "description": "",
  18. "__v": 0
  19. },
  20. "purchased_amount": 3,
  21. "fch": "13/6/2019",
  22. "__v": 0
  23. },
  24. {
  25. "_id": "5d0151afda1a446008f1817b",
  26. "product": {
  27. "_id": "5cfed1995eaf2665c45efd82",
  28. "name": "Camisa",
  29. "description": "Camisas buenas",
  30. "purchased_amount": 10,
  31. "unit_price_mex": "100",
  32. "unit_price_to_sell": "15",
  33. "travel": "5cf56b04462a865264fabb9d",
  34. "__v": 0,
  35. "id": "5cfed1995eaf2665c45efd82"
  36. },
  37. "client": {
  38. "_id": "5cf1778efffb651fad89d8b6",
  39. "name": "Randy",
  40. "description": "El que trabaja aqui",
  41. "__v": 0
  42. },
  43. "purchased_amount": 34,
  44. "fch": "12/6/2019",
  45. "__v": 0
  46. },
  47.  
  48. var mongoose = require('mongoose');
  49. const mongoosePaginate = require('mongoose-paginate-v2');
  50.  
  51.  
  52. var clientSchema = new mongoose.Schema({
  53. name: String,
  54. description: String
  55. }).plugin(mongoosePaginate);
  56.  
  57. var Client = mongoose.model('Client', clientSchema);
  58.  
  59. module.exports = Client;
  60.  
  61. var mongoose = require('mongoose');
  62. const mongoosePaginate = require('mongoose-paginate-v2');
  63.  
  64. var productSchema = new mongoose.Schema({
  65. name: String,
  66. description: String,
  67. purchased_amount: Number,
  68. unit_price_mex: mongoose.Schema.Types.Decimal128,
  69. unit_price_to_sell: mongoose.Schema.Types.Decimal128,
  70. travel: { type: mongoose.Schema.Types.ObjectId, ref: 'Travel' }
  71. }).plugin(mongoosePaginate);
  72.  
  73. productSchema.set('toJSON', {
  74. getters: true,
  75. transform: (doc, ret) => {
  76. if (ret.unit_price_mex) {
  77. ret.unit_price_mex = ret.unit_price_mex.toString();
  78. }
  79. if ( ret.unit_price_to_sell ) {
  80. ret.unit_price_to_sell = ret.unit_price_to_sell.toString();
  81. }
  82. }
  83. })
  84.  
  85. var Product = mongoose.model('Product', productSchema);
  86.  
  87. module.exports = Product;
  88.  
  89. var aggregate = InvoiceModel.aggregate([
  90. { $match: { client: mongoose.Types.ObjectId( id ) } },
  91. { $group: { _id: null, total: { $sum: { $multiply: [ "$purchased_amount", "$product.unit_price_to_sell" ] } } } }
  92. ]);
  93. InvoiceModel.aggregatePaginate(aggregate, {}, (error, aggs) => {
  94. InvoiceModel.paginate({ client: id },{ page, limit, populate: 'client product' }, (err, value) => {
  95. return res.status(200).send({
  96. results: value.docs,
  97. totalPages: value.totalPages,
  98. totalDocs: value.totalDocs,
  99. purchase_amount_total : aggs.docs[0].total
  100. })
  101. })
  102. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement