Your IP : 216.73.217.13


Current Path : /home/deltalab/PMS/sms-connector/test/
Upload File :
Current File : //home/deltalab/PMS/sms-connector/test/shippypro-adapter.test.js

/**
 * ShippyPro adapter API tests
 */

// DEPENDENCIES ====================================
const { expect }  = require ('chai');
const { dotenv }  = require ('dotenv').config();

// RESOURCES =======================================
const { adapter: shippyProAdapter }        = require ('../adapters/shippypro-adapter');
const { addressModel }            = require('../models/mongoose/address');
const pickup                      = require('../models/mongoose/pickup');
const { bookPickupModel }         = require('../models/mongoose/pickup');
const { carrierModel }            = require('../models/mongoose/carrier');
const { carrierBaseModel }        = require('../models/mongoose/carrier');
const { parcelModel }             = require('../models/mongoose/parcel');
const shipment                    = require ('../models/mongoose/shipment');
const { shipmentManifestModel }   = require ('../models/mongoose/shipment');
const { shipmentModel }           = require ('../models/mongoose/shipment');
const { shipmentValueInfoModel }  = require ('../models/mongoose/shipment');
const { shipmentBaseInfoModel }   = require ('../models/mongoose/shipment');
const { shipmentInfoModel }       = require ('../models/mongoose/shipment');

// TEST OBJECTS ====================================
const shipmentBaseInfo = new shipmentBaseInfoModel();
const fromAddress     = new addressModel();
fromAddress.name      = "Delta Informatica Spa";
fromAddress.company   = "Delta Informatica Spa";
fromAddress.street1   = "Via Kufstein, 5";
fromAddress.street2   = "";
fromAddress.city      = "Trento";
fromAddress.province  = "TN";
fromAddress.zip       = "38121";
fromAddress.state     = "Italy";
fromAddress.country   = "IT";
fromAddress.email     = "deltalab@deltainformatica.eu";
fromAddress.phone     = "+39";
const toAddress       = new addressModel();
toAddress.name        = "Matteo Pedrotti";
toAddress.company     = "";
toAddress.street1     = "Via Fermi, 16";
toAddress.street2     = "";
toAddress.city        = "Trento";
toAddress.province    = "TN";
toAddress.zip         = "38123";
toAddress.state       = "Italy";
toAddress.country     = "IT";
toAddress.email     = "matteo.pedrotti@deltainformatica.eu";
toAddress.phone       = "+39";
const parcel          = new parcelModel();
parcel.length         = 1;
parcel.width          = 1;
parcel.height         = 1;
parcel.weight         = 1;
const shipmentValueInfo             = shipment.createShipmentValueInfo(15);
shipmentBaseInfo.from               = fromAddress;
shipmentBaseInfo.to                 = toAddress;
shipmentBaseInfo.parcels            = [ parcel ];
shipmentBaseInfo.shipmentValueInfo  = shipmentValueInfo;
shipmentBaseInfo.shipmentInfoModel  = new shipmentValueInfoModel();

// TEST CASES ======================================
describe ('shippypro-adapter', function () {
  this.timeout(10000); // wait 10 seconds

  // Initialization --------------------------------
  before(() => {
  });

  it ('get rates', async function () {
    // execute query
    const rates = await shippyProAdapter.getRatesAsync(shipmentBaseInfo);
    // check
    console.log(rates);
    // {
    //   carrier: {
    //     smsid: 2861,
    //     name: 'Generic',
    //     service: 'Standard'
    //   },
    //   rate: 0,
    //   rateId: '16388962957568',
    //   deliveryDays: '1-10',
    //   currency: 'EUR'
    // },
    expect(rates.length).to.be.greaterThan(0);
  });

  it ('get carriers', async function () {
    const carriers = await shippyProAdapter.getCarriersAsync();
    console.log(carriers);
    // [
    //   { smsid: '7571', name: 'DHLExpress', service: 'EXPRESS DOMESTIC' },
    //   { smsid: '1156', name: 'POSTEITALIANE', service: 'Crono' },
    //   { smsid: '1157', name: 'POSTEITALIANE', service: 'Crono Express' },
    //   { smsid: '2861', name: 'Generic', service: 'Standard' },
    //   { smsid: '20', name: 'Stef', service: 'Standard' }
    // ]
    expect(carriers.length).to.be.greaterThan(0);
    const carrierBase = new carrierBaseModel(carriers[0]);
    console.log(carrierBase);
    const carrier = new carrierModel(carrierBase);
    console.log(carrier);
  });

  it ('create shipment', async function () {
    // WARNING: this test will create an actual order in shippypro
    const orderId       = "TEST01";
    const carrierId     = 2861;           // using fake carrier INDACO Generic!
    const rateId        = 16388953518276; // using fake rate ?
    const shipmentInfo  = shipment.createShipmentInfo(orderId, carrierId, rateId, shipmentBaseInfo);
    // query
    const myShipment    = await shippyProAdapter.createShipmentAsync(shipmentInfo);
    // check
    console.log(myShipment);
    // {
    //   shipmentId: '68781965',
    //   labelUrl: 'https://cdn.shippypro.com/labels/label-u28840n68781965-16388957638.pdf'
    // }
    expect(myShipment.shipmentId).to.exist;
    expect(myShipment.labelUrl).to.exist;
  });

  it ('get shipment status', async function () {
    const shipmentId = 67998420;
    const shipmentStatus = await shippyProAdapter.getShipmentStatusAsync(shipmentId);
    expect(shipmentStatus).to.exist;

    const shipment = new shipmentModel(shipmentStatus);

    console.log(shipmentStatus);
    console.log(shipment);
  });

  it ('manage manifests', async function () {
    const shipmentId = 67998420;
    // create a manifest for a given shipment
    const manifestCreationData = await shippyProAdapter.createManifestAsync(shipmentId);
    console.log(manifestCreationData);
    // {
    //   manifestUrl: 'https://cdn.shippypro.com/manifests/manifest-u28840n43-16389581978.pdf',
    //   manifestNumber: '43'
    // }
    expect(manifestCreationData).to.exist;
    const manifestCreation = new shipmentManifestModel(manifestCreationData);
    // no fetch the actual manifest data
    const manifestData = await shippyProAdapter.getManifestAsync(manifestCreation.manifestNumber);
    console.log(manifestData);
    expect(manifestData).to.exist;

    const manifest = new shipmentManifestModel(manifestData);

    // map the manifest in the shipment
    const shipment = new shipmentModel();
    shipment.manifest = manifest;
    console.log(shipment);
  });

  it ('book pickup', async function () {
    const carrierId     = 2861;           // using fake carrier INDACO Generic!

    // preparing a pickup request
    const pickupRequest   = new bookPickupModel();
    pickupRequest.from    = fromAddress;
    pickupRequest.to      = toAddress;
    pickupRequest.parcels = [ parcel ];
    pickupRequest.carrierId = carrierId;
    pickup.setTomorrowPickup(pickupRequest);
    pickupRequest.orderIds = "76960798";

    console.log(pickupRequest);

    const response = await shippyProAdapter.bookPickupAsync(pickupRequest);

    console.log(response);
    // {
    //   error: 'Internal server error',
    //   message: 'Params.from_address.email: The property email is required'
    // }
    // { 
    //   result: 'OK', 
    //   message: 'Ritiro prenotato',
    //   confirmationId: '',
    // }
    expect(response.result).to.equals('OK');
  });
});