my-typescript-library-rahul52us 3.0.8 → 4.1.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.
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.deleteAllDocuments = exports.deleteSingleDocument = exports.getDashChartdata = exports.getPendingDash = exports.getDocumentByStatus = exports.getDocument = exports.updateAlternativeDocument = exports.updateFile = exports.updateStatus = exports.updateComment = exports.getPendingApprovalDocument = exports.updateDocument = exports.uploadAddtionalDocuments = exports.saveAsDraftDocument = exports.getPendingApproval = exports.getAllPendingApproval = exports.getDocumentCountDetails = exports.countDocumentsByLevelAndStatus = exports.getDocumentCountByLevelAndStatus = exports.getDocumentCount = exports.getAllDocument = exports.findOneByDocumentIdAndDelete = exports.withdrawalApprovalDocument = exports.createPendingApproval = exports.getCount = void 0;
15
+ exports.getDownloadExcelDocument = exports.deleteAllDocuments = exports.deleteSingleDocument = exports.getDashChartdata = exports.getPendingDash = exports.getDocumentByStatus = exports.getDocument = exports.updateAlternativeDocument = exports.updateFile = exports.updateStatus = exports.updateComment = exports.getPendingApprovalDocument = exports.updateDocument = exports.uploadAddtionalDocuments = exports.saveAsDraftDocument = exports.getPendingApproval = exports.getAllPendingApproval = exports.getDocumentCountDetails = exports.countDocumentsByLevelAndStatus = exports.getDocumentCountByLevelAndStatus = exports.getDocumentCount = exports.getAllDocument = exports.findOneByDocumentIdAndDelete = exports.withdrawalApprovalDocument = exports.createPendingApproval = exports.getCount = void 0;
16
16
  const document_schema_1 = __importDefault(require("../repository/schemas/document.schema"));
17
17
  const mongoose_1 = __importDefault(require("mongoose"));
18
18
  const status_constant_1 = require("../constants/status.constant");
@@ -134,17 +134,7 @@ function withdrawalApprovalDocument(data) {
134
134
  exports.withdrawalApprovalDocument = withdrawalApprovalDocument;
135
135
  const findOneByDocumentIdAndDelete = (data) => __awaiter(void 0, void 0, void 0, function* () {
136
136
  try {
137
- const doc = yield document_schema_1.default.findOne({ documentId: data.documentId });
138
- console.log(doc);
139
- if (!doc) {
140
- return {
141
- status: "error",
142
- data: "Document does not exist",
143
- };
144
- }
145
- doc.deleted_At = new Date();
146
- yield doc.save();
147
- console.log(doc);
137
+ yield document_schema_1.default.findOneAndDelete({ documentId: data.documentId });
148
138
  return {
149
139
  status: "success",
150
140
  data: "Document has been deleted successfully (soft delete)",
@@ -2115,4 +2105,712 @@ const deleteAllDocuments = () => __awaiter(void 0, void 0, void 0, function* ()
2115
2105
  }
2116
2106
  });
2117
2107
  exports.deleteAllDocuments = deleteAllDocuments;
2108
+ function getDownloadExcelDocument(noOfLevels, workflow, searchValue, status, company, userId, differentiateFirstLevel, level, sort = "1", limit = "10", page = "1", exchangeType, billType, manual, startDate, endDate, searchField, dateFilterKey, sortByKey, additionalFiles) {
2109
+ var _a;
2110
+ return __awaiter(this, void 0, void 0, function* () {
2111
+ try {
2112
+ let matchQuery = {};
2113
+ matchQuery.status = { $ne: "rejected" };
2114
+ if (searchField && (searchValue === null || searchValue === void 0 ? void 0 : searchValue.trim())) {
2115
+ matchQuery[`originalValues.${searchField}`] = {
2116
+ $regex: searchValue,
2117
+ $options: "i",
2118
+ };
2119
+ }
2120
+ if (startDate) {
2121
+ matchQuery[dateFilterKey] = { $gte: new Date(startDate) };
2122
+ }
2123
+ if (endDate) {
2124
+ matchQuery[dateFilterKey] = Object.assign(Object.assign({}, matchQuery[dateFilterKey]), { $lte: new Date(endDate) });
2125
+ }
2126
+ var matchConditions = Object.assign(Object.assign({}, matchQuery), {
2127
+ // company: new mongoose.Types.ObjectId(company),
2128
+ workflow: new mongoose_1.default.Types.ObjectId(workflow), deleted_At: { $exists: false } });
2129
+ if (exchangeType && Array.isArray(exchangeType)) {
2130
+ exchangeType.forEach((typeObj) => {
2131
+ Object.entries(typeObj).forEach(([field, value]) => {
2132
+ if (!matchConditions[`originalValues.${field}`]) {
2133
+ matchConditions[`originalValues.${field}`] = [];
2134
+ }
2135
+ matchConditions[`originalValues.${field}`].push(value);
2136
+ });
2137
+ });
2138
+ }
2139
+ Object.entries(matchConditions).forEach(([field, value]) => {
2140
+ if (Array.isArray(value)) {
2141
+ matchConditions[field] = { $in: value };
2142
+ }
2143
+ });
2144
+ let pipeline = [];
2145
+ let expr = {};
2146
+ let levels = (0, status_constant_1.generateLevels)(noOfLevels);
2147
+ switch (level) {
2148
+ case levels[0]:
2149
+ switch (status) {
2150
+ case "pending":
2151
+ expr = {
2152
+ $expr: {
2153
+ $and: [
2154
+ {
2155
+ $and: [
2156
+ {
2157
+ $not: [
2158
+ {
2159
+ $and: [
2160
+ {
2161
+ $in: [
2162
+ { $arrayElemAt: ["$approval.status", -1] },
2163
+ ["approved", "rejected"],
2164
+ ],
2165
+ },
2166
+ {
2167
+ $in: [
2168
+ { $arrayElemAt: ["$approval.level", -1] },
2169
+ [levels[0]],
2170
+ ],
2171
+ },
2172
+ ],
2173
+ },
2174
+ ],
2175
+ },
2176
+ {
2177
+ $not: [
2178
+ {
2179
+ $and: [
2180
+ {
2181
+ $in: [
2182
+ { $arrayElemAt: ["$approval.status", -1] },
2183
+ ["approved"],
2184
+ ],
2185
+ },
2186
+ {
2187
+ $in: [
2188
+ { $arrayElemAt: ["$approval.level", -1] },
2189
+ [levels[levels.length - 1]],
2190
+ ],
2191
+ },
2192
+ ],
2193
+ },
2194
+ ],
2195
+ },
2196
+ ],
2197
+ },
2198
+ ],
2199
+ },
2200
+ };
2201
+ break;
2202
+ case "approved":
2203
+ expr = {
2204
+ $expr: {
2205
+ $or: [
2206
+ {
2207
+ $and: [
2208
+ {
2209
+ $in: [
2210
+ { $arrayElemAt: ["$approval.status", -1] },
2211
+ [status],
2212
+ ],
2213
+ },
2214
+ {
2215
+ $in: [
2216
+ { $arrayElemAt: ["$approval.level", -1] },
2217
+ [levels[levels.length - 1], levels[0]],
2218
+ ],
2219
+ },
2220
+ {
2221
+ $not: [{ $eq: ["$approval", []] }],
2222
+ },
2223
+ ],
2224
+ },
2225
+ ],
2226
+ },
2227
+ };
2228
+ break;
2229
+ case "rejected":
2230
+ expr = {
2231
+ $expr: {
2232
+ $or: [
2233
+ {
2234
+ $and: [
2235
+ {
2236
+ $in: [
2237
+ { $arrayElemAt: ["$approval.status", -1] },
2238
+ [status],
2239
+ ],
2240
+ },
2241
+ {
2242
+ $in: [
2243
+ { $arrayElemAt: ["$approval.level", -1] },
2244
+ [levels[0]],
2245
+ ],
2246
+ },
2247
+ {
2248
+ $not: [{ $eq: ["$approval", []] }],
2249
+ },
2250
+ ],
2251
+ },
2252
+ ],
2253
+ },
2254
+ };
2255
+ break;
2256
+ }
2257
+ pipeline = [
2258
+ {
2259
+ $match: Object.assign(Object.assign({}, matchConditions), expr),
2260
+ },
2261
+ ];
2262
+ break;
2263
+ default:
2264
+ let { approvedRoles, rejectedRoles, approvedBy, rejectedBy } = getDynamicRoles(noOfLevels, level);
2265
+ switch (status) {
2266
+ case "pending":
2267
+ expr = {
2268
+ $expr: {
2269
+ $or: [
2270
+ {
2271
+ $and: [
2272
+ {
2273
+ $and: [
2274
+ {
2275
+ $not: [
2276
+ {
2277
+ $and: [
2278
+ {
2279
+ $in: [
2280
+ {
2281
+ $arrayElemAt: ["$approval.status", -1],
2282
+ },
2283
+ ["approved"],
2284
+ ],
2285
+ },
2286
+ {
2287
+ $in: [
2288
+ {
2289
+ $arrayElemAt: ["$approval.level", -1],
2290
+ },
2291
+ approvedRoles,
2292
+ ],
2293
+ },
2294
+ ],
2295
+ },
2296
+ ],
2297
+ },
2298
+ {
2299
+ $not: [
2300
+ {
2301
+ $and: [
2302
+ {
2303
+ $in: [
2304
+ {
2305
+ $arrayElemAt: ["$approval.status", -1],
2306
+ },
2307
+ ["rejected"],
2308
+ ],
2309
+ },
2310
+ {
2311
+ $in: [
2312
+ {
2313
+ $arrayElemAt: ["$approval.level", -1],
2314
+ },
2315
+ rejectedRoles,
2316
+ ],
2317
+ },
2318
+ ],
2319
+ },
2320
+ ],
2321
+ },
2322
+ level !== levels[1] && {
2323
+ $and: [
2324
+ {
2325
+ $in: [
2326
+ {
2327
+ $arrayElemAt: ["$approval.status", -1],
2328
+ },
2329
+ ["approved"],
2330
+ ],
2331
+ },
2332
+ {
2333
+ $in: [
2334
+ {
2335
+ $arrayElemAt: ["$approval.level", -1],
2336
+ },
2337
+ approvedBy,
2338
+ ],
2339
+ },
2340
+ ],
2341
+ },
2342
+ {
2343
+ $not: [{ $eq: ["$approval", []] }],
2344
+ },
2345
+ ],
2346
+ },
2347
+ ],
2348
+ },
2349
+ level !== levels[levels.length - 1] && {
2350
+ $and: [
2351
+ {
2352
+ $in: [
2353
+ {
2354
+ $arrayElemAt: ["$approval.status", -1],
2355
+ },
2356
+ ["rejected"],
2357
+ ],
2358
+ },
2359
+ {
2360
+ $in: [
2361
+ {
2362
+ $arrayElemAt: ["$approval.level", -1],
2363
+ },
2364
+ rejectedBy,
2365
+ ],
2366
+ },
2367
+ ],
2368
+ },
2369
+ level === levels[1] && {
2370
+ $and: [
2371
+ {
2372
+ $eq: ["$tags.extraction.type", "automatic"],
2373
+ },
2374
+ { $eq: ["$approval", []] },
2375
+ ],
2376
+ },
2377
+ ],
2378
+ },
2379
+ };
2380
+ // Only add the userId check for the first level if approval is not empty
2381
+ // let userIdCheck = {};
2382
+ // if (level === levels[1] && differentiateFirstLevel) {
2383
+ // userIdCheck = {
2384
+ // approval: {
2385
+ // $elemMatch: {
2386
+ // userId: { $in: [userId] },
2387
+ // level: levels[1],
2388
+ // },
2389
+ // },
2390
+ // };
2391
+ // }
2392
+ let userIdCheck = {};
2393
+ if (level === levels[1] && differentiateFirstLevel) {
2394
+ userIdCheck = {
2395
+ user: userId, // Only the creator can access
2396
+ };
2397
+ }
2398
+ pipeline = [
2399
+ {
2400
+ $match: Object.assign(Object.assign(Object.assign({}, matchConditions), expr), userIdCheck),
2401
+ },
2402
+ ];
2403
+ break;
2404
+ case "approved":
2405
+ pipeline = [
2406
+ {
2407
+ $addFields: {
2408
+ filteredApproval: {
2409
+ $filter: {
2410
+ input: "$approval",
2411
+ as: "item",
2412
+ cond: { $eq: ["$$item.level", level] },
2413
+ },
2414
+ },
2415
+ },
2416
+ },
2417
+ {
2418
+ $match: Object.assign(Object.assign({}, matchConditions), { $expr: {
2419
+ $and: [
2420
+ {
2421
+ $and: [
2422
+ {
2423
+ $eq: [
2424
+ {
2425
+ $arrayElemAt: ["$filteredApproval.status", -1],
2426
+ },
2427
+ status,
2428
+ ],
2429
+ },
2430
+ ],
2431
+ },
2432
+ {
2433
+ $cond: {
2434
+ if: differentiateFirstLevel,
2435
+ then: {
2436
+ $eq: [
2437
+ {
2438
+ $arrayElemAt: ["$filteredApproval.userId", -1],
2439
+ },
2440
+ userId,
2441
+ ],
2442
+ },
2443
+ else: true,
2444
+ },
2445
+ },
2446
+ {
2447
+ $not: [{ $eq: ["$approval", []] }],
2448
+ },
2449
+ {
2450
+ $not: [
2451
+ {
2452
+ $and: [
2453
+ {
2454
+ $in: [
2455
+ {
2456
+ $arrayElemAt: ["$approval.status", -1],
2457
+ },
2458
+ ["rejected"],
2459
+ ],
2460
+ },
2461
+ {
2462
+ $in: [
2463
+ {
2464
+ $arrayElemAt: ["$approval.level", -1],
2465
+ },
2466
+ rejectedBy,
2467
+ ],
2468
+ },
2469
+ ],
2470
+ },
2471
+ ],
2472
+ },
2473
+ // {
2474
+ // $not: [
2475
+ // {
2476
+ // $and: [
2477
+ // {
2478
+ // $in: [
2479
+ // {
2480
+ // $arrayElemAt: ["$approval.status", -1],
2481
+ // },
2482
+ // ["approved"],
2483
+ // ],
2484
+ // },
2485
+ // {
2486
+ // $in: [
2487
+ // {
2488
+ // $arrayElemAt: ["$approval.level", -1],
2489
+ // },
2490
+ // approvedBy,
2491
+ // ],
2492
+ // },
2493
+ // ],
2494
+ // },
2495
+ // ],
2496
+ // }
2497
+ ],
2498
+ } }),
2499
+ },
2500
+ ];
2501
+ break;
2502
+ case "rejected":
2503
+ pipeline = [
2504
+ {
2505
+ $addFields: {
2506
+ filteredApproval: {
2507
+ $filter: {
2508
+ input: "$approval",
2509
+ as: "item",
2510
+ cond: { $eq: ["$$item.level", level] },
2511
+ },
2512
+ },
2513
+ },
2514
+ },
2515
+ {
2516
+ $match: Object.assign(Object.assign({}, matchConditions), { $expr: {
2517
+ $and: [
2518
+ {
2519
+ $and: [
2520
+ {
2521
+ $eq: [
2522
+ {
2523
+ $arrayElemAt: ["$filteredApproval.status", -1],
2524
+ },
2525
+ status,
2526
+ ],
2527
+ },
2528
+ ],
2529
+ },
2530
+ {
2531
+ $cond: {
2532
+ if: differentiateFirstLevel,
2533
+ then: {
2534
+ $eq: [
2535
+ {
2536
+ $arrayElemAt: ["$filteredApproval.userId", -1],
2537
+ },
2538
+ userId,
2539
+ ],
2540
+ },
2541
+ else: true,
2542
+ },
2543
+ },
2544
+ {
2545
+ $not: [{ $eq: ["$approval", []] }],
2546
+ },
2547
+ // {
2548
+ // $not: [
2549
+ // {
2550
+ // $and: [
2551
+ // {
2552
+ // $in: [
2553
+ // {
2554
+ // $arrayElemAt: ["$approval.status", -1],
2555
+ // },
2556
+ // ["rejected"],
2557
+ // ],
2558
+ // },
2559
+ // {
2560
+ // $in: [
2561
+ // {
2562
+ // $arrayElemAt: ["$approval.level", -1],
2563
+ // },
2564
+ // rejectedBy,
2565
+ // ],
2566
+ // },
2567
+ // ],
2568
+ // },
2569
+ // ],
2570
+ // },
2571
+ {
2572
+ $not: [
2573
+ {
2574
+ $and: [
2575
+ {
2576
+ $in: [
2577
+ {
2578
+ $arrayElemAt: ["$approval.status", -1],
2579
+ },
2580
+ ["approved"],
2581
+ ],
2582
+ },
2583
+ {
2584
+ $in: [
2585
+ {
2586
+ $arrayElemAt: ["$approval.level", -1],
2587
+ },
2588
+ approvedBy,
2589
+ ],
2590
+ },
2591
+ ],
2592
+ },
2593
+ ],
2594
+ },
2595
+ ],
2596
+ } }),
2597
+ },
2598
+ ];
2599
+ }
2600
+ break;
2601
+ }
2602
+ if (manual && (level === levels[0] || level === levels[1])) {
2603
+ pipeline = [
2604
+ {
2605
+ $match: Object.assign(Object.assign(Object.assign({}, matchQuery), matchConditions), { "tags.extraction.type": "manual", approval: { $eq: [] } }),
2606
+ },
2607
+ ];
2608
+ }
2609
+ else if (manual) {
2610
+ return {
2611
+ data: [],
2612
+ totalPages: 0,
2613
+ };
2614
+ }
2615
+ const approvalStage = [
2616
+ {
2617
+ $unwind: {
2618
+ path: "$approval",
2619
+ preserveNullAndEmptyArrays: true,
2620
+ },
2621
+ },
2622
+ {
2623
+ $lookup: {
2624
+ from: "fs.files",
2625
+ localField: "approval.fileId",
2626
+ foreignField: "_id",
2627
+ as: "approval.fileDetails",
2628
+ },
2629
+ },
2630
+ {
2631
+ $unwind: {
2632
+ path: "$approval.fileDetails",
2633
+ preserveNullAndEmptyArrays: true,
2634
+ },
2635
+ },
2636
+ {
2637
+ $lookup: {
2638
+ from: "accounts",
2639
+ localField: "approval.userId",
2640
+ foreignField: "_id",
2641
+ as: "approval.userDetails",
2642
+ pipeline: [
2643
+ {
2644
+ $project: {
2645
+ _id: 1,
2646
+ username: 1,
2647
+ role: 1,
2648
+ level: 1,
2649
+ },
2650
+ },
2651
+ ],
2652
+ },
2653
+ },
2654
+ {
2655
+ $unwind: {
2656
+ path: "$approval.userDetails",
2657
+ preserveNullAndEmptyArrays: true,
2658
+ },
2659
+ },
2660
+ {
2661
+ $group: {
2662
+ _id: "$_id",
2663
+ document: { $first: "$$ROOT" },
2664
+ approvals: {
2665
+ $push: {
2666
+ $mergeObjects: [
2667
+ "$approval",
2668
+ {
2669
+ file: "$approval.fileDetails",
2670
+ user: "$approval.userDetails",
2671
+ },
2672
+ ],
2673
+ },
2674
+ },
2675
+ },
2676
+ },
2677
+ {
2678
+ $addFields: {
2679
+ "document.approval": {
2680
+ $filter: {
2681
+ input: "$approvals",
2682
+ as: "approval",
2683
+ cond: { $ne: ["$$approval", {}] },
2684
+ },
2685
+ },
2686
+ },
2687
+ },
2688
+ {
2689
+ $replaceRoot: {
2690
+ newRoot: "$document",
2691
+ },
2692
+ },
2693
+ {
2694
+ $project: {
2695
+ "approval.fileDetails": 0,
2696
+ "approval.userDetails": 0,
2697
+ },
2698
+ },
2699
+ ];
2700
+ const lookupStages = additionalFiles.map((obj) => ({
2701
+ $lookup: {
2702
+ from: "fs.files",
2703
+ localField: `additionalFiles.${obj}`,
2704
+ foreignField: "_id",
2705
+ as: obj,
2706
+ },
2707
+ }));
2708
+ pipeline = [
2709
+ ...pipeline,
2710
+ {
2711
+ $lookup: {
2712
+ from: "fs.files",
2713
+ localField: "file",
2714
+ foreignField: "_id",
2715
+ as: "file",
2716
+ },
2717
+ },
2718
+ {
2719
+ $unwind: {
2720
+ path: "$additionalFiles",
2721
+ preserveNullAndEmptyArrays: true,
2722
+ },
2723
+ },
2724
+ {
2725
+ $lookup: {
2726
+ from: "fs.files",
2727
+ localField: "additionalFiles.fileId",
2728
+ foreignField: "_id",
2729
+ as: "lookupResult",
2730
+ },
2731
+ },
2732
+ {
2733
+ $addFields: {
2734
+ lookupResult: {
2735
+ $cond: {
2736
+ if: { $eq: [{ $size: "$lookupResult" }, 0] },
2737
+ then: [{}],
2738
+ else: "$lookupResult",
2739
+ },
2740
+ },
2741
+ },
2742
+ },
2743
+ {
2744
+ $group: {
2745
+ _id: "$_id",
2746
+ originalFields: { $first: "$$ROOT" },
2747
+ additionalFilesDetails: {
2748
+ $push: {
2749
+ $mergeObjects: ["$additionalFiles", { $first: "$lookupResult" }],
2750
+ },
2751
+ },
2752
+ },
2753
+ },
2754
+ {
2755
+ $replaceRoot: {
2756
+ newRoot: {
2757
+ $mergeObjects: [
2758
+ "$originalFields",
2759
+ { additionalFilesDetails: "$additionalFilesDetails" },
2760
+ ],
2761
+ },
2762
+ },
2763
+ },
2764
+ {
2765
+ $lookup: {
2766
+ from: "fs.files",
2767
+ localField: "filexls",
2768
+ foreignField: "_id",
2769
+ as: "filexls",
2770
+ },
2771
+ },
2772
+ ...lookupStages,
2773
+ ...approvalStage,
2774
+ ];
2775
+ if (sortByKey) {
2776
+ const sortStage = {};
2777
+ if (sortByKey === "created_At" || sortByKey === "updated_At") {
2778
+ sortStage[`${sortByKey}`] = Number(sort);
2779
+ }
2780
+ else {
2781
+ sortStage[`originalValues.${sortByKey}`] = Number(sort);
2782
+ }
2783
+ pipeline.push({
2784
+ $sort: sortStage,
2785
+ });
2786
+ }
2787
+ let countPipeline = [
2788
+ ...pipeline,
2789
+ { $group: { _id: null, count: { $sum: 1 } } },
2790
+ ];
2791
+ if (limit && page) {
2792
+ const skip = (parseInt(page) - 1) * parseInt(limit);
2793
+ pipeline.push({ $skip: skip }, { $limit: parseInt(limit) });
2794
+ }
2795
+ const [pendingData, totalCount] = yield Promise.all([
2796
+ document_schema_1.default.aggregate(pipeline),
2797
+ document_schema_1.default.aggregate(countPipeline),
2798
+ ]);
2799
+ // fs.writeFileSync('aggregationResult.json', JSON.stringify(pipeline, null, 2));
2800
+ let totalPages;
2801
+ if (limit) {
2802
+ totalPages = Math.ceil(((_a = totalCount === null || totalCount === void 0 ? void 0 : totalCount[0]) === null || _a === void 0 ? void 0 : _a.count) / parseInt(limit));
2803
+ }
2804
+ return {
2805
+ data: pendingData,
2806
+ totalPages,
2807
+ };
2808
+ }
2809
+ catch (error) {
2810
+ console.log(error === null || error === void 0 ? void 0 : error.message);
2811
+ throw new Error(error.message);
2812
+ }
2813
+ });
2814
+ }
2815
+ exports.getDownloadExcelDocument = getDownloadExcelDocument;
2118
2816
  //# sourceMappingURL=document.repository.js.map