@resolveio/server-lib 22.3.94 → 22.3.96

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "22.3.94",
3
+ "version": "22.3.96",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "package": "./build_package.sh",
@@ -598,8 +598,13 @@ function buildResolveIORunnerLocalQaScript() {
598
598
  'socket.once("error", () => process.exit(1));',
599
599
  'RESOLVEIO_QA_MONGO_TCP',
600
600
  '}',
601
+ 'mongo_primary_ready() {',
602
+ ' mongo_tcp_ready >/dev/null 2>&1 || return 1',
603
+ ' if ! command -v mongosh >/dev/null 2>&1; then return 0; fi',
604
+ ' timeout 6s mongosh "mongodb://127.0.0.1:$MONGO_PORT/admin?directConnection=true" --quiet --eval "const h=db.adminCommand({hello:1}); if (h.isWritablePrimary || h.ismaster || !h.setName) { quit(0); } quit(2);" >/dev/null 2>&1',
605
+ '}',
601
606
  'start_local_mongo_for_prestart_seed() {',
602
- ' if mongo_tcp_ready >/dev/null 2>&1; then return 0; fi',
607
+ ' if mongo_primary_ready >/dev/null 2>&1; then return 0; fi',
603
608
  ' if command -v mongod >/dev/null 2>&1; then',
604
609
  ' mkdir -p "$PROJECT_ROOT/server/mongo/data/db" "$PROJECT_ROOT/server/mongo/log"',
605
610
  ' mongod --fork --dbpath "$PROJECT_ROOT/server/mongo/data/db" --logpath "$PROJECT_ROOT/server/mongo/log/mongo.log" --port "$MONGO_PORT" --replSet rs0 --bind_ip 127.0.0.1 >/dev/null 2>&1 || true',
@@ -613,8 +618,15 @@ function buildResolveIORunnerLocalQaScript() {
613
618
  ' if command -v mongosh >/dev/null 2>&1; then',
614
619
  ' timeout 8s mongosh "mongodb://127.0.0.1:$MONGO_PORT/admin?directConnection=true" --quiet --eval "try { rs.initiate({_id: \'rs0\', members: [{ _id: 0, host: \'127.0.0.1:$MONGO_PORT\' }]}) } catch (e) {}" >/dev/null 2>&1 || true',
615
620
  ' fi',
616
- ' sleep 3',
617
- ' mongo_tcp_ready >/dev/null 2>&1',
621
+ ' local primary_end=$((SECONDS + 75))',
622
+ ' while [ "$SECONDS" -lt "$primary_end" ]; do',
623
+ ' if mongo_primary_ready >/dev/null 2>&1; then return 0; fi',
624
+ ' if command -v mongosh >/dev/null 2>&1; then',
625
+ ' timeout 6s mongosh "mongodb://127.0.0.1:$MONGO_PORT/admin?directConnection=true" --quiet --eval "try { rs.initiate({_id: \'rs0\', members: [{ _id: 0, host: \'127.0.0.1:$MONGO_PORT\' }]}) } catch (e) {}" >/dev/null 2>&1 || true',
626
+ ' fi',
627
+ ' sleep 2',
628
+ ' done',
629
+ ' mongo_primary_ready >/dev/null 2>&1',
618
630
  '}',
619
631
  'materialize_live_mongo_runtime() {',
620
632
  ' local runtime_file="$ARTIFACT_DIR/.mongo-runtime.json"',
@@ -1048,6 +1060,9 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1048
1060
  ' if (existing && extractBolIdentifiers().length && existing.selected && existing.selected.truck_treating_bol_context && !Object.prototype.hasOwnProperty.call(existing.selected.truck_treating_bol_context, "primary_bol_status")) {',
1049
1061
  ' return;',
1050
1062
  ' }',
1063
+ ' if (existing && shouldAutoDiscoverAssetContext() && !(existing.selected && existing.selected.qa_asset_context)) {',
1064
+ ' return;',
1065
+ ' }',
1051
1066
  ' if (["pass", "needs-data"].includes(status)) {',
1052
1067
  ' normalizeCoverageMatrixFromLiveSeed(existing);',
1053
1068
  ' const preserved = { ...existing, reused_existing: true, reuse_reason: reason, checked_at: new Date().toISOString() };',
@@ -1194,6 +1209,24 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1194
1209
  ' return /\\b(deliver(?:y|ed|ies)?|bol|bill\\s+of\\s+lading|truck\\s*treat(?:ing)?|flush(?:es)?|selected\\s+treatments?|chemical|red)\\b/i.test(text);',
1195
1210
  '}',
1196
1211
  '',
1212
+ 'function shouldAutoDiscoverAssetContext() {',
1213
+ ' const text = readSeedHintText();',
1214
+ ' return /\\b(asset|assets|unit|units|equipment|current\\s+location|location\\s+name|location\\s+names|default\\s+yard|yard)\\b/i.test(text);',
1215
+ '}',
1216
+ '',
1217
+ 'function extractAssetIdentifiers() {',
1218
+ ' const text = readSeedHintText();',
1219
+ ' const identifiers = new Set();',
1220
+ ' const explicit = /\\b(?:asset|unit|equipment)\\s*(?:#|no\\.?|number|num)?\\s*[:#-]?\\s*([A-Z0-9][A-Z0-9._-]{1,})\\b/gi;',
1221
+ ' let match;',
1222
+ ' while ((match = explicit.exec(text)) !== null) {',
1223
+ ' const value = String(match[1] || "").trim();',
1224
+ ' if (isUsefulSeedIdentifier(value)) identifiers.add(value);',
1225
+ ' }',
1226
+ ' if (shouldAutoDiscoverAssetContext()) extractSeedIdentifiers().forEach((value) => identifiers.add(value));',
1227
+ ' return Array.from(identifiers).slice(0, 20);',
1228
+ '}',
1229
+ '',
1197
1230
  'function extractRouteNameHints() {',
1198
1231
  ' const text = readSeedHintText();',
1199
1232
  ' const routes = new Set();',
@@ -1241,6 +1274,25 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1241
1274
  ' return ors.length ? { $or: ors } : null;',
1242
1275
  '}',
1243
1276
  '',
1277
+ 'function assetLookupQuery(identifiers) {',
1278
+ ' const ors = [];',
1279
+ ' const fields = ["_id", "asset_number", "asset_number_string", "asset_unit_number", "unit_number", "number", "serial_number", "name"];',
1280
+ ' for (const identifier of identifiers || []) {',
1281
+ ' const value = String(identifier || "").trim();',
1282
+ ' if (!value) continue;',
1283
+ ' const escaped = value.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&");',
1284
+ ' const exactText = new RegExp(`^${escaped}$`, "i");',
1285
+ ' const containsText = new RegExp(escaped, "i");',
1286
+ ' for (const field of fields) ors.push({ [field]: exactText });',
1287
+ ' for (const field of ["asset_number_string", "asset_unit_number", "unit_number", "number", "serial_number", "name"]) ors.push({ [field]: containsText });',
1288
+ ' if (/^\\d+$/.test(value)) {',
1289
+ ' const numericValue = Number(value);',
1290
+ ' for (const field of ["asset_number", "unit_number", "number"]) ors.push({ [field]: numericValue });',
1291
+ ' }',
1292
+ ' }',
1293
+ ' return ors.length ? { $or: ors } : null;',
1294
+ '}',
1295
+ '',
1244
1296
  'async function copyByIds(sourceDb, targetDb, collectionName, ids, summary, limit = 100) {',
1245
1297
  ' const cleanIds = unique(ids).slice(0, limit);',
1246
1298
  ' if (!cleanIds.length) return [];',
@@ -1417,6 +1469,45 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1417
1469
  ' return [...bols, ...productionRoutes, ...productionDeliveries, ...truckTreatingDeliveries, ...copiedTreatmentPlans];',
1418
1470
  '}',
1419
1471
  '',
1472
+ 'async function copyTicketAssetContext(sourceDb, targetDb, summary) {',
1473
+ ' if (!shouldAutoDiscoverAssetContext()) return [];',
1474
+ ' const assetIdentifiers = extractAssetIdentifiers();',
1475
+ ' summary.selected.asset_identifiers = assetIdentifiers;',
1476
+ ' const query = assetLookupQuery(assetIdentifiers);',
1477
+ ' if (!query) return [];',
1478
+ ' const assets = await copyQuery(sourceDb, targetDb, "assets", query, summary, 20, { updatedAt: -1, asset_number: 1 });',
1479
+ ' if (!assets.length) {',
1480
+ ' summary.notes.push(`No live assets matched ticket identifiers: ${assetIdentifiers.join(", ")}`);',
1481
+ ' return [];',
1482
+ ' }',
1483
+ ' const yardIds = unique([',
1484
+ ' ...idValues(assets, ["id_yard", "id_default_yard"]),',
1485
+ ' ...collectNestedStringValues(assets, ["id", "id_yard"])',
1486
+ ' ]);',
1487
+ ' const locationIds = unique([',
1488
+ ' ...idValues(assets, ["id_location", "id_current_location", "id_default_location"]),',
1489
+ ' ...collectNestedStringValues(assets, ["id_location"])',
1490
+ ' ]);',
1491
+ ' const userIds = unique([',
1492
+ ' ...idValues(assets, ["id_user", "id_driver", "id_created_by", "id_updated_by"]),',
1493
+ ' ...collectNestedStringValues(assets, ["id_user", "id_driver", "id_created_by", "id_updated_by"])',
1494
+ ' ]);',
1495
+ ' await copyByIds(sourceDb, targetDb, "yards", yardIds, summary, 100);',
1496
+ ' await copyByIds(sourceDb, targetDb, "production-locations", locationIds, summary, 100);',
1497
+ ' await copyByIds(sourceDb, targetDb, "users", userIds, summary, 100);',
1498
+ ' const assetIds = assets.map((doc) => doc._id).filter(Boolean);',
1499
+ ' const assetNumbers = assets.map((doc) => String(doc.asset_number || doc.asset_number_string || doc.asset_unit_number || doc.unit_number || "")).filter(Boolean);',
1500
+ ' summary.selected.qa_asset_context = {',
1501
+ ' asset_ids: assetIds,',
1502
+ ' asset_numbers: assetNumbers,',
1503
+ ' yard_ids: yardIds,',
1504
+ ' production_location_ids: locationIds,',
1505
+ ' browser_routes: assetIds[0] ? { list: "/asset/list", detail: `/asset/detail/${assetIds[0]}`, edit: `/asset/edit/${assetIds[0]}` } : { list: "/asset/list" }',
1506
+ ' };',
1507
+ ' summary.notes.push(`Seeded live asset context for ${assetIdentifiers.join(", ")}: ${assets.length} asset(s), ${yardIds.length} yard id(s), ${locationIds.length} production location id(s).`);',
1508
+ ' return assets;',
1509
+ '}',
1510
+ '',
1420
1511
  'async function ensureBillingSurchargePricingFixtures(targetDb, summary, serviceItems, customerIds) {',
1421
1512
  ' const now = new Date();',
1422
1513
  ' const activeServiceItems = (serviceItems || []).filter((doc) => {',
@@ -1581,6 +1672,7 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1581
1672
  ' const identifiers = extractSeedIdentifiers();',
1582
1673
  ' summary.selected.seed_identifiers = identifiers;',
1583
1674
  ' const truckTreatingBolContextDocs = await copyTruckTreatingBolContext(sourceDb, targetDb, summary);',
1675
+ ' const ticketAssetContextDocs = await copyTicketAssetContext(sourceDb, targetDb, summary);',
1584
1676
  ' if (!includeBillingFixtures) {',
1585
1677
  ' summary.notes.push("Using live-context seed profile: billing dashboard fixtures are disabled because ticket context does not mention billing, invoices, taxes, inventory, pricing, surcharges, checkout, or pick tickets.");',
1586
1678
  ' await cleanupSyntheticBillingDashboardQaFixtures(targetDb, summary);',
@@ -1619,7 +1711,7 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1619
1711
  ' summary.notes.push("No live billable delivery or truck-treatment records matched the Billing Dashboard awaiting-invoice filters.");',
1620
1712
  ' }',
1621
1713
  '',
1622
- ' const sourceDocs = [...truckTreatingBolContextDocs, ...productionDeliveries, ...truckTreatingDeliveries];',
1714
+ ' const sourceDocs = [...truckTreatingBolContextDocs, ...ticketAssetContextDocs, ...productionDeliveries, ...truckTreatingDeliveries];',
1623
1715
  ' const customerIds = idValues(sourceDocs, ["id_customer"]);',
1624
1716
  ' const locationIds = idValues(sourceDocs, ["id_location"]);',
1625
1717
  ' const wellGroupIds = idValues(sourceDocs, ["id_well_group"]);',
@@ -1675,7 +1767,7 @@ function buildResolveIORunnerQaLiveDataSeederScript() {
1675
1767
  ' ] }, summary, 120, { date: -1, updatedAt: -1 });',
1676
1768
  ' }',
1677
1769
  '',
1678
- ' summary.ready = truckTreatingBolContextDocs.length > 0 || productionDeliveries.length > 0 || truckTreatingDeliveries.length > 0;',
1770
+ ' summary.ready = truckTreatingBolContextDocs.length > 0 || ticketAssetContextDocs.length > 0 || productionDeliveries.length > 0 || truckTreatingDeliveries.length > 0;',
1679
1771
  ' return summary;',
1680
1772
  '}',
1681
1773
  '',