@go-mailer/jarvis 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/query.js +28 -13
  2. package/package.json +1 -1
package/lib/query.js CHANGED
@@ -2,7 +2,10 @@ const buildQuery = (options) => {
2
2
  const sort_condition = options.sort_by ? buildSortOrderString(options.sort_by) : ''
3
3
  const fields_to_return = options.return_only ? buildReturnFieldsString(options.return_only) : ''
4
4
  const count = options.count || false
5
- const group_by = options.group_by || null
5
+
6
+ const group_by = options.group_by
7
+ const sum = options.sum
8
+ const is_pipeline = group_by || sum
6
9
 
7
10
  let seek_conditions = {}
8
11
  if (options.bool) {
@@ -30,6 +33,7 @@ const buildQuery = (options) => {
30
33
  delete options.population
31
34
  delete options.return_only
32
35
  delete options.sort_by
36
+ delete options.sum
33
37
 
34
38
  Object.keys(options).forEach((field) => {
35
39
  const field_value = options[field] ? options[field].toString().toLowerCase() : ''
@@ -49,10 +53,11 @@ const buildQuery = (options) => {
49
53
  })
50
54
 
51
55
  let pipeline = []
52
- if (group_by) {
53
- pipeline = generateGroupPipeline({
56
+ if (is_pipeline) {
57
+ pipeline = generatePipeline({
54
58
  count,
55
59
  group_by,
60
+ sum,
56
61
  seek_conditions,
57
62
  fields_to_return,
58
63
  sort_condition
@@ -166,11 +171,8 @@ const determinePagination = (page = 0, population) => {
166
171
  return pagination
167
172
  }
168
173
 
169
- const generateGroupPipeline = ({ group_by, seek_conditions, sort_condition, fields_to_return, count }) => {
170
- if (!group_by || typeof group_by !== 'string') return null
171
-
174
+ const generatePipeline = ({ group_by, seek_conditions, sort_condition, sum, fields_to_return, count }) => {
172
175
  const pipeline = []
173
- const group_field = group_by.split(',')[0]
174
176
 
175
177
  pipeline.push({
176
178
  $match: { ...seek_conditions }
@@ -201,11 +203,24 @@ const generateGroupPipeline = ({ group_by, seek_conditions, sort_condition, fiel
201
203
  })
202
204
  }
203
205
 
204
- pipeline.push({
205
- $group: {
206
- _id: { group_field: `$${group_field}` }
207
- }
208
- })
206
+ if (group_by) {
207
+ const group_field = group_by.split(',')[0]
208
+ pipeline.push({
209
+ $group: {
210
+ _id: { group_field: `$${group_field}` }
211
+ }
212
+ })
213
+ }
214
+
215
+ if (sum) {
216
+ const sum_field = sum.split(',')[0]
217
+ pipeline.push({
218
+ $group: {
219
+ _id: { tenant_id: "$tenant_id" },
220
+ [sum_field]: { $sum: `$${sum_field}` }
221
+ }
222
+ })
223
+ }
209
224
 
210
225
  if (count) {
211
226
  pipeline.push({
@@ -226,5 +241,5 @@ module.exports = {
226
241
  buildSortOrderString,
227
242
  buildWildcardOptions,
228
243
  determinePagination,
229
- generateGroupPipeline
244
+ generatePipeline
230
245
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",