gemcap-be-common 1.1.93 → 1.1.95

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.
@@ -60,6 +60,17 @@ export declare const COLLATERALS_LOOKUP: ({
60
60
  };
61
61
  $lookup?: undefined;
62
62
  })[];
63
+ export declare const createQuery: (groupFields: {
64
+ [groupFields: string]: string;
65
+ }, collateralType: string) => {
66
+ groupQueries: {
67
+ [x: string]: {
68
+ $first: string;
69
+ };
70
+ };
71
+ itemQueries: any;
72
+ enumKey: "INVENTORY" | "RECEIVABLE" | "ACCOUNT_PAYABLE" | "OTHER" | "CASH" | "BANKS" | "QUICKBOOKS";
73
+ };
63
74
  export declare const ITEMS_PAGINATION: (paginatorOptions: IPaginatorOptions) => ({
64
75
  $skip: number;
65
76
  $limit?: undefined;
@@ -74,3 +85,4 @@ export declare const getPreviousCollateralDocument: (borrowerId: string, date: D
74
85
  _id: mongoose.Types.ObjectId;
75
86
  bbcDate: Date;
76
87
  }[]>;
88
+ export declare const getCollateralDocsByBBC: (bbcIds: string[], collateralType: ECollaterals) => Promise<any>;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getPreviousCollateralDocument = exports.getCollateralListBySheet = exports.getBBCSheetsForBorrowerAndType = exports.findCollateralsWithSheets = exports.ITEMS_PAGINATION = exports.COLLATERALS_LOOKUP = exports.collateralMap = void 0;
6
+ exports.getCollateralDocsByBBC = exports.getPreviousCollateralDocument = exports.getCollateralListBySheet = exports.getBBCSheetsForBorrowerAndType = exports.findCollateralsWithSheets = exports.ITEMS_PAGINATION = exports.createQuery = exports.COLLATERALS_LOOKUP = exports.collateralMap = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
8
  const _models_1 = require("../models/_models");
9
9
  const enums_1 = require("../enums");
@@ -16,6 +16,11 @@ exports.collateralMap = {
16
16
  [enums_1.ECollaterals.RECEIVABLE]: models_1.ReceivableItemModel,
17
17
  [enums_1.ECollaterals.ACCOUNT_PAYABLE]: models_1.AccountPayableItemModel,
18
18
  };
19
+ const collateralFields = {
20
+ [enums_1.ECollaterals.INVENTORY]: models_1.INVENTORY_FIELDS,
21
+ [enums_1.ECollaterals.RECEIVABLE]: models_1.RECEIVABLE_FIELDS,
22
+ [enums_1.ECollaterals.ACCOUNT_PAYABLE]: models_1.ACCOUNT_PAYABLE_FIELDS,
23
+ };
19
24
  exports.COLLATERALS_LOOKUP = [
20
25
  {
21
26
  $lookup: {
@@ -44,6 +49,19 @@ exports.COLLATERALS_LOOKUP = [
44
49
  },
45
50
  },
46
51
  ];
52
+ const createQuery = (groupFields, collateralType) => {
53
+ const enumKey = (0, helpers_1.getEnumKeyByEnumValue)(enums_1.ECollaterals, collateralType);
54
+ const itemFields = collateralFields[enumKey];
55
+ const groupQueries = Object
56
+ .entries(groupFields)
57
+ .map(([fieldKey, fieldValue]) => ({ [fieldKey]: { $first: `$${fieldValue}` } }))
58
+ .reduce((acc, group) => ({ ...acc, ...group }), {});
59
+ const itemQueries = itemFields
60
+ .map((fieldKey) => ({ [fieldKey]: `$${fieldKey}` }))
61
+ .reduce((acc, group) => ({ ...acc, ...group }), {});
62
+ return { groupQueries, itemQueries, enumKey };
63
+ };
64
+ exports.createQuery = createQuery;
47
65
  const ITEMS_PAGINATION = (paginatorOptions) => {
48
66
  if (!paginatorOptions) {
49
67
  return [];
@@ -216,3 +234,39 @@ const getPreviousCollateralDocument = async (borrowerId, date, collateralType, l
216
234
  ]);
217
235
  };
218
236
  exports.getPreviousCollateralDocument = getPreviousCollateralDocument;
237
+ const getCollateralDocsByBBC = async (bbcIds, collateralType) => {
238
+ const bbcSheets = await (0, bbcSheets_db_1.getBBCSheetsByType)(bbcIds, collateralType);
239
+ const groupFields = {
240
+ bbcDate: 'bbc.bbcDate',
241
+ borrowerId: 'bbc.borrowerId',
242
+ };
243
+ const { groupQueries, itemQueries, enumKey } = (0, exports.createQuery)(groupFields, collateralType);
244
+ const groupingQuery = { _id: '$bbc._id', ...groupQueries, items: { $push: itemQueries } };
245
+ const aggregationResult = await exports.collateralMap[enumKey].aggregate([
246
+ {
247
+ $match: {
248
+ 'bbcSheetId': { $in: bbcSheets.map((bbcSheet) => bbcSheet._id) },
249
+ },
250
+ },
251
+ ...exports.COLLATERALS_LOOKUP,
252
+ {
253
+ $sort: {
254
+ order: 1,
255
+ },
256
+ },
257
+ {
258
+ $group: groupingQuery,
259
+ },
260
+ {
261
+ $sort: {
262
+ bbcDate: 1,
263
+ },
264
+ },
265
+ ]);
266
+ if (!aggregationResult.length) {
267
+ const BBCs = await models_1.BBCDateModel.find({ _id: { $in: bbcIds } }).lean();
268
+ return BBCs.map((BBC) => ({ ...BBC, items: [] }));
269
+ }
270
+ return aggregationResult;
271
+ };
272
+ exports.getCollateralDocsByBBC = getCollateralDocsByBBC;
@@ -3,7 +3,14 @@ import mongoose from 'mongoose';
3
3
  import { MODEL_NAMES } from '../models/_models';
4
4
  import { IPaginatorOptions } from '../interfaces';
5
5
  import { ECollaterals } from '../enums';
6
- import { AccountPayableItemModel, BBCSheetModel, InventoryItemModel, ReceivableItemModel } from '../models';
6
+ import {
7
+ ACCOUNT_PAYABLE_FIELDS,
8
+ AccountPayableItemModel,
9
+ BBCDateModel,
10
+ BBCSheetModel, INVENTORY_FIELDS,
11
+ InventoryItemModel, RECEIVABLE_FIELDS,
12
+ ReceivableItemModel,
13
+ } from '../models';
7
14
  import { getEnumKeyByEnumValue } from '../helpers';
8
15
  import { getBBCSheetsByType } from './bbcSheets.db';
9
16
  import { getBBCDatesByBorrower } from './bbcDates.db';
@@ -22,6 +29,12 @@ export const collateralMap = {
22
29
  [ECollaterals.ACCOUNT_PAYABLE]: AccountPayableItemModel,
23
30
  };
24
31
 
32
+ const collateralFields = {
33
+ [ECollaterals.INVENTORY]: INVENTORY_FIELDS,
34
+ [ECollaterals.RECEIVABLE]: RECEIVABLE_FIELDS,
35
+ [ECollaterals.ACCOUNT_PAYABLE]: ACCOUNT_PAYABLE_FIELDS,
36
+ };
37
+
25
38
  export const COLLATERALS_LOOKUP = [
26
39
  {
27
40
  $lookup: {
@@ -51,6 +64,19 @@ export const COLLATERALS_LOOKUP = [
51
64
  },
52
65
  ];
53
66
 
67
+ export const createQuery = (groupFields: { [groupFields: string]: string }, collateralType: string) => {
68
+ const enumKey = getEnumKeyByEnumValue(ECollaterals, collateralType);
69
+ const itemFields = collateralFields[enumKey];
70
+ const groupQueries = Object
71
+ .entries(groupFields)
72
+ .map(([fieldKey, fieldValue]) => ({ [fieldKey]: { $first: `$${fieldValue}` } }))
73
+ .reduce((acc, group) => ({ ...acc, ...group }), {});
74
+ const itemQueries = itemFields
75
+ .map((fieldKey) => ({ [fieldKey]: `$${fieldKey}` }))
76
+ .reduce((acc, group) => ({ ...acc, ...group }), {});
77
+ return { groupQueries, itemQueries, enumKey };
78
+ }
79
+
54
80
  export const ITEMS_PAGINATION = (paginatorOptions: IPaginatorOptions) => {
55
81
  if (!paginatorOptions) {
56
82
  return [];
@@ -225,3 +251,42 @@ export const getPreviousCollateralDocument = async (borrowerId: string, date: Da
225
251
  },
226
252
  ]);
227
253
  };
254
+
255
+ export const getCollateralDocsByBBC = async (bbcIds: string[], collateralType: ECollaterals) => {
256
+ const bbcSheets = await getBBCSheetsByType(bbcIds, collateralType);
257
+
258
+ const groupFields = {
259
+ bbcDate: 'bbc.bbcDate',
260
+ borrowerId: 'bbc.borrowerId',
261
+ };
262
+
263
+ const { groupQueries, itemQueries, enumKey } = createQuery(groupFields, collateralType);
264
+ const groupingQuery = { _id: '$bbc._id', ...groupQueries, items: { $push: itemQueries } };
265
+
266
+ const aggregationResult = await collateralMap[enumKey].aggregate([
267
+ {
268
+ $match: {
269
+ 'bbcSheetId': { $in: bbcSheets.map((bbcSheet) => bbcSheet._id) },
270
+ },
271
+ },
272
+ ...COLLATERALS_LOOKUP,
273
+ {
274
+ $sort: {
275
+ order: 1,
276
+ },
277
+ },
278
+ {
279
+ $group: groupingQuery,
280
+ },
281
+ {
282
+ $sort: {
283
+ bbcDate: 1,
284
+ },
285
+ },
286
+ ]);
287
+ if (!aggregationResult.length) {
288
+ const BBCs = await BBCDateModel.find({ _id: { $in: bbcIds } }).lean();
289
+ return BBCs.map((BBC) => ({ ...BBC, items: [] }));
290
+ }
291
+ return aggregationResult;
292
+ }
package/db/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './inventory-availability.db';
6
6
  export * from './inventory-manual-entry.db';
7
7
  export * from './inventory-seasonal-rates.db';
8
8
  export * from './microservice-tasks.db';
9
+ export * from './receivables';
package/db/index.js CHANGED
@@ -22,3 +22,4 @@ __exportStar(require("./inventory-availability.db"), exports);
22
22
  __exportStar(require("./inventory-manual-entry.db"), exports);
23
23
  __exportStar(require("./inventory-seasonal-rates.db"), exports);
24
24
  __exportStar(require("./microservice-tasks.db"), exports);
25
+ __exportStar(require("./receivables"), exports);
package/db/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './inventory-availability.db';
6
6
  export * from './inventory-manual-entry.db';
7
7
  export * from './inventory-seasonal-rates.db';
8
8
  export * from './microservice-tasks.db';
9
+ export * from './receivables';
@@ -0,0 +1,72 @@
1
+ /// <reference types="mongoose/types/aggregate" />
2
+ /// <reference types="mongoose/types/callback" />
3
+ /// <reference types="mongoose/types/collection" />
4
+ /// <reference types="mongoose/types/connection" />
5
+ /// <reference types="mongoose/types/cursor" />
6
+ /// <reference types="mongoose/types/document" />
7
+ /// <reference types="mongoose/types/error" />
8
+ /// <reference types="mongoose/types/expressions" />
9
+ /// <reference types="mongoose/types/helpers" />
10
+ /// <reference types="mongoose/types/middlewares" />
11
+ /// <reference types="mongoose/types/indexes" />
12
+ /// <reference types="mongoose/types/models" />
13
+ /// <reference types="mongoose/types/mongooseoptions" />
14
+ /// <reference types="mongoose/types/pipelinestage" />
15
+ /// <reference types="mongoose/types/populate" />
16
+ /// <reference types="mongoose/types/query" />
17
+ /// <reference types="mongoose/types/schemaoptions" />
18
+ /// <reference types="mongoose/types/schematypes" />
19
+ /// <reference types="mongoose/types/session" />
20
+ /// <reference types="mongoose/types/types" />
21
+ /// <reference types="mongoose/types/utility" />
22
+ /// <reference types="mongoose/types/validation" />
23
+ /// <reference types="mongoose/types/virtuals" />
24
+ /// <reference types="mongoose/types/inferschematype" />
25
+ import mongoose from 'mongoose';
26
+ import { IReceivableItemDocPopulated } from '../models';
27
+ export declare const CUSTOMER_NUMBER_SEPARATOR = "~@~";
28
+ export declare const getLastDigitFromAllValues: (allBbcDates: {
29
+ bbcDate: Date;
30
+ ids: mongoose.Types.ObjectId[];
31
+ }[]) => Promise<any[]>;
32
+ export declare const getReceivableItemsSKUAndValue: (bbcDate: string, borrowerId: string) => Promise<{
33
+ _id: string;
34
+ qty: number;
35
+ }[]>;
36
+ export declare const getReceivablesAndTheirQtyByBorrowerId: (borrowerId: string, allBbcDates: {
37
+ bbcDate: Date;
38
+ }[]) => Promise<{
39
+ _id: string;
40
+ qty: number;
41
+ }[]>;
42
+ export declare const aggregateSKUDateAgingPlot: (bbcSheetIds: mongoose.Types.ObjectId[]) => Promise<{
43
+ '0-30': number;
44
+ '30-60': number;
45
+ '60-90': number;
46
+ '90+': number;
47
+ }>;
48
+ export declare const aggregateAllValuesForBbc: (sheetIds: mongoose.Types.ObjectId[]) => Promise<any[]>;
49
+ export declare const aggregateBbcExposures: (bbcSheetIds: mongoose.Types.ObjectId[]) => Promise<{
50
+ receivableTotal: any;
51
+ topItemsForBbc: any[];
52
+ }>;
53
+ export declare const aggregateBbcExposuresGrouped: (bbcSheetIds: mongoose.Types.ObjectId[]) => Promise<{
54
+ receivableTotal: any;
55
+ topItemsForBbc: any[];
56
+ }>;
57
+ export declare const getAllCustomers: (borrowerId: any) => Promise<{
58
+ customers: string[];
59
+ }[]>;
60
+ export declare const getAverageInvoiceSize: (borrowerId: string) => Promise<any[]>;
61
+ export declare const getAverageOverInvoiceSize: (borrowerId: string, averageInvoiceSize: any) => Promise<any[]>;
62
+ export declare const getPartialPaidTable: (bbcSheetIds: mongoose.Types.ObjectId[]) => Promise<IReceivableItemDocPopulated[]>;
63
+ export declare const getFirstLastBBCInvoiceWithNumberOccurrence: (bbcSheetIds: mongoose.Types.ObjectId[], { customerTitle, invoiceNumber, }: {
64
+ customerTitle: any;
65
+ invoiceNumber: any;
66
+ }, firstLast: 'first' | 'last') => Promise<any[]>;
67
+ export declare const getUniqInvoices: (bbcSheetIds: mongoose.Types.ObjectId[]) => Promise<any[]>;
68
+ export declare const setReceivableManualInputsToggle: (bbcDateId: string, useManualInputs: boolean) => Promise<void>;
69
+ export declare const getReceivableItemsCustomerAndValue: (sheetIds: string[]) => Promise<{
70
+ _id: string;
71
+ totalAmount: number;
72
+ }[]>;