@optimiser/common 1.0.264 → 1.0.265
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.
- package/dist/lib/connection.js +46 -18
- package/dist/lib/utility.js +42 -24
- package/package.json +1 -1
package/dist/lib/connection.js
CHANGED
|
@@ -316,25 +316,54 @@ var Connection = /** @class */ (function () {
|
|
|
316
316
|
*/
|
|
317
317
|
Connection.prototype.FindAndReturnDBConnection = function (DBName, DBAddress) {
|
|
318
318
|
var _this = this;
|
|
319
|
+
/*Code Updated By Suraj on 07-Apr-2022, Try catch and some If conditions added to prevent from breaking the code.*/
|
|
319
320
|
return new Promise(function (resolve, reject) {
|
|
320
|
-
|
|
321
|
-
_this.connections[Buffer.from(DBAddress).toString('base64')]
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
321
|
+
try {
|
|
322
|
+
if (DBAddress && _this.connections[Buffer.from(DBAddress).toString('base64')]) {
|
|
323
|
+
_this.connections[Buffer.from(DBAddress).toString('base64')].db(DBName);
|
|
324
|
+
}
|
|
325
|
+
var masterDb = null;
|
|
326
|
+
if (_this.connections[Buffer.from(_this.masterConfig.MASTER_CONNECTION_STRING).toString('base64')]) {
|
|
327
|
+
masterDb = _this.connections[Buffer.from(_this.masterConfig.MASTER_CONNECTION_STRING).toString('base64')].db(_this.masterConfig.MASTER_DB_NAME);
|
|
328
|
+
}
|
|
329
|
+
//checking master db connection
|
|
330
|
+
if (!masterDb) {
|
|
331
|
+
console.log("Master db connection not available");
|
|
332
|
+
reject(new Error("Master db connection not available"));
|
|
333
|
+
return;
|
|
333
334
|
}
|
|
334
|
-
|
|
335
|
-
|
|
335
|
+
if (DBName == _this.masterConfig.MASTER_DB_NAME) {
|
|
336
|
+
return resolve(masterDb);
|
|
336
337
|
}
|
|
337
|
-
|
|
338
|
+
masterDb.collection("Company").findOne({ DBName: DBName }).then(function (companyData) {
|
|
339
|
+
if (companyData) {
|
|
340
|
+
_this.ConnectToDb({
|
|
341
|
+
DBAddress: companyData.DBAddress,
|
|
342
|
+
DBName: companyData.DBName
|
|
343
|
+
}).then(function () {
|
|
344
|
+
//Checking company connection first, if exists then resolve
|
|
345
|
+
if (_this.connections[Buffer.from(companyData.DBAddress).toString('base64')]) {
|
|
346
|
+
resolve(_this.connections[Buffer.from(companyData.DBAddress).toString('base64')].db(companyData.DBName));
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
console.log("Company db connection not established");
|
|
350
|
+
reject(new Error("Company db connection not established"));
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
reject();
|
|
356
|
+
}
|
|
357
|
+
}, function (error) {
|
|
358
|
+
console.log("Error occurred :", error);
|
|
359
|
+
reject(error);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
catch (ex) {
|
|
363
|
+
console.log("Error occurred :", ex);
|
|
364
|
+
ex.message += "- db connection error";
|
|
365
|
+
reject(ex);
|
|
366
|
+
}
|
|
338
367
|
});
|
|
339
368
|
};
|
|
340
369
|
/**
|
|
@@ -344,7 +373,6 @@ var Connection = /** @class */ (function () {
|
|
|
344
373
|
*/
|
|
345
374
|
Connection.prototype.ConnectToCompanyDB = function (DBName, DBAddress) {
|
|
346
375
|
return __awaiter(this, void 0, void 0, function () {
|
|
347
|
-
var masterdb;
|
|
348
376
|
return __generator(this, function (_a) {
|
|
349
377
|
switch (_a.label) {
|
|
350
378
|
case 0:
|
|
@@ -355,7 +383,7 @@ var Connection = /** @class */ (function () {
|
|
|
355
383
|
DBName: this.masterConfig.MASTER_DB_NAME
|
|
356
384
|
})];
|
|
357
385
|
case 2:
|
|
358
|
-
|
|
386
|
+
_a.sent();
|
|
359
387
|
return [2 /*return*/, this.FindAndReturnDBConnection(DBName, DBAddress)];
|
|
360
388
|
}
|
|
361
389
|
});
|
package/dist/lib/utility.js
CHANGED
|
@@ -1420,12 +1420,29 @@ exports.SyncUserInOtherCollection = SyncUserInOtherCollection;
|
|
|
1420
1420
|
//Build _LookupDataField before save
|
|
1421
1421
|
function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
1422
1422
|
return new Promise(function (resolve, reject) {
|
|
1423
|
+
/**
|
|
1424
|
+
* Added By Suraj on 06-Apr-2022,
|
|
1425
|
+
* helper function for all below cases to print error log on console.
|
|
1426
|
+
* @param fieldValue value of a field
|
|
1427
|
+
* @param err error occurred during processing
|
|
1428
|
+
*/
|
|
1429
|
+
var errorHandler = function (fieldValue, err) {
|
|
1430
|
+
var fieldValueType = (Array.isArray(fieldValue) ? 'array' : '');
|
|
1431
|
+
if (!fieldValueType) {
|
|
1432
|
+
fieldValueType = typeof (fieldValue);
|
|
1433
|
+
}
|
|
1434
|
+
var errorInfo = "##### COMMON MOUDLE ERROR INFO ####";
|
|
1435
|
+
errorInfo += "\n Function Name : BuildLookupDataField";
|
|
1436
|
+
errorInfo += "\n Error Info: Error while building Lookup Data for Company - " + (db.databaseName) + ", ObjectName - " + (fieldSchema.LookupObject || '') + ", FieldName - " + (fieldSchema.Name || '') + " for value - " + fieldValue + " and data type of values is " + (fieldValueType || '') + " ";
|
|
1437
|
+
console.log("Error Information : ", errorInfo);
|
|
1438
|
+
console.log("Error Full Object : ", err);
|
|
1439
|
+
};
|
|
1423
1440
|
if (fieldSchema.UIDataType && constants_1.default.LookupTypeFields.includes(fieldSchema.UIDataType)) {
|
|
1424
1441
|
var fldName = fieldSchema.Name;
|
|
1425
|
-
var
|
|
1442
|
+
var fldValue_1 = updateObj[fldName];
|
|
1426
1443
|
var alias_1 = fldName + constants_1.default.LookupAlias;
|
|
1427
1444
|
var fldLookupData_1 = null;
|
|
1428
|
-
if (
|
|
1445
|
+
if (fldValue_1) {
|
|
1429
1446
|
var aggregateArry = [];
|
|
1430
1447
|
var project = { '_id': 1, IsActive: 1 };
|
|
1431
1448
|
if (fieldSchema.ExtraLookupObjects) {
|
|
@@ -1456,13 +1473,13 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1456
1473
|
}
|
|
1457
1474
|
switch (fieldSchema.UIDataType) {
|
|
1458
1475
|
case "lookup":
|
|
1459
|
-
aggregateArry.push({ $match: { '_id':
|
|
1476
|
+
aggregateArry.push({ $match: { '_id': fldValue_1 } });
|
|
1460
1477
|
aggregateArry.push({ $project: project });
|
|
1461
1478
|
db.collection(fieldSchema.LookupObject).aggregate(aggregateArry).toArray(function (err, data) {
|
|
1462
1479
|
if (err) {
|
|
1463
|
-
|
|
1480
|
+
errorHandler(fldValue_1, err);
|
|
1464
1481
|
}
|
|
1465
|
-
if (data && data.length > 0) {
|
|
1482
|
+
else if (data && data.length > 0) {
|
|
1466
1483
|
fldLookupData_1 = data[0];
|
|
1467
1484
|
updateObj[alias_1] = fldLookupData_1;
|
|
1468
1485
|
}
|
|
@@ -1470,7 +1487,7 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1470
1487
|
});
|
|
1471
1488
|
break;
|
|
1472
1489
|
case "multilookup":
|
|
1473
|
-
aggregateArry.push({ $match: { '_id': { $in:
|
|
1490
|
+
aggregateArry.push({ $match: { '_id': { $in: fldValue_1 } } });
|
|
1474
1491
|
aggregateArry.push({ $project: project });
|
|
1475
1492
|
if (fieldSchema.UnionWith && fieldSchema.UnionWith.length > 0) {
|
|
1476
1493
|
for (var i = 0; i < fieldSchema.UnionWith.length; i++) {
|
|
@@ -1478,7 +1495,7 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1478
1495
|
if (unionDetail) {
|
|
1479
1496
|
var unionPipeline = [];
|
|
1480
1497
|
unionPipeline.push({
|
|
1481
|
-
$match: { '_id': { $in:
|
|
1498
|
+
$match: { '_id': { $in: fldValue_1 } }
|
|
1482
1499
|
});
|
|
1483
1500
|
unionPipeline.push({ $project: project });
|
|
1484
1501
|
var unionObj = { coll: unionDetail.LookupObject, pipeline: unionPipeline };
|
|
@@ -1488,9 +1505,10 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1488
1505
|
}
|
|
1489
1506
|
db.collection(fieldSchema.LookupObject).aggregate(aggregateArry).toArray(function (err, data) {
|
|
1490
1507
|
if (err) {
|
|
1491
|
-
throw new Error(err.toString());
|
|
1508
|
+
// throw new Error(err.toString());
|
|
1509
|
+
errorHandler(fldValue_1, err);
|
|
1492
1510
|
}
|
|
1493
|
-
if (data && data.length > 0) {
|
|
1511
|
+
else if (data && data.length > 0) {
|
|
1494
1512
|
fldLookupData_1 = data;
|
|
1495
1513
|
updateObj[alias_1] = fldLookupData_1;
|
|
1496
1514
|
}
|
|
@@ -1499,13 +1517,13 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1499
1517
|
break;
|
|
1500
1518
|
case "dropdown":
|
|
1501
1519
|
if (fieldSchema.LookupObject) {
|
|
1502
|
-
aggregateArry.push({ $match: { _id:
|
|
1520
|
+
aggregateArry.push({ $match: { _id: fldValue_1 } });
|
|
1503
1521
|
aggregateArry.push({ $project: project });
|
|
1504
1522
|
db.collection(fieldSchema.LookupObject).aggregate(aggregateArry).toArray(function (err, data) {
|
|
1505
1523
|
if (err) {
|
|
1506
|
-
|
|
1524
|
+
errorHandler(fldValue_1, err);
|
|
1507
1525
|
}
|
|
1508
|
-
if (data && data.length > 0) {
|
|
1526
|
+
else if (data && data.length > 0) {
|
|
1509
1527
|
fldLookupData_1 = data[0];
|
|
1510
1528
|
updateObj[alias_1] = fldLookupData_1;
|
|
1511
1529
|
}
|
|
@@ -1521,12 +1539,12 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1521
1539
|
}
|
|
1522
1540
|
aggregateArry.push({ $unwind: { path: '$Data' } });
|
|
1523
1541
|
aggregateArry.push({ $project: { _id: 0, Key: '$Data.Key', Value: '$Data.Value', IsActive: '$Data.IsActive' } });
|
|
1524
|
-
aggregateArry.push({ $match: { 'Key':
|
|
1542
|
+
aggregateArry.push({ $match: { 'Key': fldValue_1 } });
|
|
1525
1543
|
db.collection('ListSchema').aggregate(aggregateArry).toArray(function (err, data) {
|
|
1526
1544
|
if (err) {
|
|
1527
|
-
|
|
1545
|
+
errorHandler(fldValue_1, err);
|
|
1528
1546
|
}
|
|
1529
|
-
if (data && data.length > 0) {
|
|
1547
|
+
else if (data && data.length > 0) {
|
|
1530
1548
|
fldLookupData_1 = data[0];
|
|
1531
1549
|
updateObj[alias_1] = fldLookupData_1;
|
|
1532
1550
|
}
|
|
@@ -1536,13 +1554,13 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1536
1554
|
break;
|
|
1537
1555
|
case "multiselect":
|
|
1538
1556
|
if (fieldSchema.LookupObject) {
|
|
1539
|
-
aggregateArry.push({ $match: { '_id': { $in:
|
|
1557
|
+
aggregateArry.push({ $match: { '_id': { $in: fldValue_1 } } });
|
|
1540
1558
|
aggregateArry.push({ $project: project });
|
|
1541
1559
|
db.collection(fieldSchema.LookupObject).aggregate(aggregateArry).toArray(function (err, data) {
|
|
1542
1560
|
if (err) {
|
|
1543
|
-
|
|
1561
|
+
errorHandler(fldValue_1, err);
|
|
1544
1562
|
}
|
|
1545
|
-
if (data && data.length > 0) {
|
|
1563
|
+
else if (data && data.length > 0) {
|
|
1546
1564
|
fldLookupData_1 = data;
|
|
1547
1565
|
updateObj[alias_1] = fldLookupData_1;
|
|
1548
1566
|
}
|
|
@@ -1553,12 +1571,12 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1553
1571
|
aggregateArry.push({ $match: { 'Name': fieldSchema.ListSchemaName } });
|
|
1554
1572
|
aggregateArry.push({ $unwind: { path: '$Data' } });
|
|
1555
1573
|
aggregateArry.push({ $project: { _id: 0, Key: '$Data.Key', Value: '$Data.Value', IsActive: '$Data.IsActive' } });
|
|
1556
|
-
aggregateArry.push({ $match: { 'Key': { $in:
|
|
1574
|
+
aggregateArry.push({ $match: { 'Key': { $in: fldValue_1 } } });
|
|
1557
1575
|
db.collection('ListSchema').aggregate(aggregateArry).toArray(function (err, data) {
|
|
1558
1576
|
if (err) {
|
|
1559
|
-
|
|
1577
|
+
errorHandler(fldValue_1, err);
|
|
1560
1578
|
}
|
|
1561
|
-
if (data && data.length > 0) {
|
|
1579
|
+
else if (data && data.length > 0) {
|
|
1562
1580
|
fldLookupData_1 = data;
|
|
1563
1581
|
updateObj[alias_1] = fldLookupData_1;
|
|
1564
1582
|
}
|
|
@@ -1569,14 +1587,14 @@ function BuildLookupDataField(fieldSchema, updateObj, db) {
|
|
|
1569
1587
|
case "file":
|
|
1570
1588
|
case "image":
|
|
1571
1589
|
if (!fieldSchema.IsMultiple) {
|
|
1572
|
-
aggregateArry.push({ $match: { '_id':
|
|
1590
|
+
aggregateArry.push({ $match: { '_id': fldValue_1 } });
|
|
1573
1591
|
project['FileName'] = 1;
|
|
1574
1592
|
aggregateArry.push({ $project: project });
|
|
1575
1593
|
db.collection('Drive').aggregate(aggregateArry).toArray(function (err, data) {
|
|
1576
1594
|
if (err) {
|
|
1577
|
-
|
|
1595
|
+
errorHandler(fldValue_1, err);
|
|
1578
1596
|
}
|
|
1579
|
-
if (data && data.length > 0) {
|
|
1597
|
+
else if (data && data.length > 0) {
|
|
1580
1598
|
fldLookupData_1 = data[0];
|
|
1581
1599
|
updateObj[alias_1] = fldLookupData_1;
|
|
1582
1600
|
}
|