tringuyen25

shipment-request-script

Nov 20th, 2020
533
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const moment = require("moment");
  2. const Shipments = mongoose.model("Shipments");
  3. const SHIPMENT_STATUS = require("./config/consumers/snp.js").SHIPMENT_STATUS;
  4.  
  5. (async () => {
  6.     try {
  7.         const shipments = await Shipments.find(
  8.             {
  9.                 shipmentStatus: SHIPMENT_STATUS.NOT_ASSIGNED,
  10.                 isDeleted: false,
  11.                 createdAt: { $gte: moment().subtract(30, "days") },
  12.             },
  13.             {
  14.                 orderReleaseCodes: true,
  15.                 legNumber: true,
  16.                 shipmentLegPositions: true,
  17.                 organizationId: true,
  18.                 shipmentCode: true,
  19.             },
  20.         );
  21.         logger.debug("Found", shipments.length, "shipments");
  22.         let counter = 1;
  23.         for (let shipment of shipments) {
  24.             logger.debug("Processing shipment", shipment.shipmentCode);
  25.             logger.debug("Shipment number", counter);
  26.             if (Array.isArray(shipment.orderReleaseCodes) && shipment.orderReleaseCodes.length !== 0 && shipment.organizationId) {
  27.                 for (let orderReleaseCode of shipment.orderReleaseCodes) {
  28.                     const otherLeg = await Shipments.findOne(
  29.                         {
  30.                             orderReleaseCodes: orderReleaseCode,
  31.                             isDeleted: false,
  32.                             organizationId: shipment.organizationId,
  33.                             _id: { $ne: shipment._id },
  34.                         },
  35.                         { _id: true },
  36.                     );
  37.                     if (!otherLeg) {
  38.                         logger.debug("Single leg shipment", shipment.shipmentCode);
  39.                         await Shipments.findOneAndUpdate({
  40.                             _id: shipment._id,
  41.                             isDeleted: false,
  42.                         }, { $set: { isSingleLeg: true } });
  43.                     }
  44.                     if (typeof shipment.legNumber === "number" || (Array.isArray(shipment.shipmentLegPositions) && shipment.shipmentLegPositions.length !== 0)) {
  45.                         let orConditions = [];
  46.                         if (typeof shipment.legNumber === "number") {
  47.                             orConditions.push({ legNumber: shipment.legNumber - 1 });
  48.                         }
  49.                         if (Array.isArray(shipment.shipmentLegPositions) && shipment.shipmentLegPositions.length !== 0) {
  50.                             orConditions.push({ shipmentLegPositions: { $in: shipment.shipmentLegPositions.map(legPosition => legPosition - 1) } });
  51.                         }
  52.                         const lastLegShipment = orConditions.length !== 0 ? await Shipments.findOne(
  53.                             {
  54.                                 orderReleaseCodes: orderReleaseCode,
  55.                                 isDeleted: false,
  56.                                 organizationId: shipment.organizationId,
  57.                                 _id: { $ne: shipment._id },
  58.                                 shipmentStatus: SHIPMENT_STATUS.SHIPPING_COMPLETED,
  59.                                 $or: orConditions,
  60.                             },
  61.                             { _id: true },
  62.                         ) : null;
  63.                         if (lastLegShipment) {
  64.                             await Shipments.findOneAndUpdate({
  65.                                 _id: shipment._id,
  66.                                 isDeleted: false,
  67.                             }, { $set: { previousLegFinished: true } });
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.             logger.debug("Finished processing", shipment.shipmentCode);
  73.             counter += 1;
  74.         }
  75.         logger.debug("DONE");
  76.     } catch (err) {
  77.         logger.error(err);
  78.     }
  79. })();
RAW Paste Data