Your IP : 216.73.216.220


Current Path : /proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/test/
Upload File :
Current File : //proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/test/product.test.js

/**
 * Shipment tests, creation, control and monitoring.
 */

// DEPENDENCIES ====================================
const { expect, assert } = require('chai');
const { dotenv } = require('dotenv').config();
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');

// RESOURCES =======================================
const ims = require('../services/ims');
const { productInventoryModel } = require("../models/mongoose/product");
const { productMediaModel } = require("../models/mongoose/product");
const { productModel } = require("../models/mongoose/product");

// TEST OBJECTS ====================================
const myproduct = new productModel();

// TEST CASES ======================================

describe('products', function () {
  this.timeout(10000);

  // Initialization --------------------------------
  before((done) => {
    mongoose.connect(process.env.database_url);
    const db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error'));
    db.once('open', function () {
      console.log('We are connected to test database!');
      done();
    });
  });

  let orderId; // the id of the created order

  it('create', async function () {
    // Start the order processing
    const product = new productModel();
    product.title = "Unit test product";
    product.description = "Unit test product description";
    product.customDescription = "Unit test custom product description";
    product.price = 99.99;
    product.sku = "TEST001";
    product.barcode = "123123123";
    product.weight = 1234;
    // product.size                = 111;
    product.partnerId = "6125199597b1612d8071eafd";
    product.requiresShipping = true;
    product.inventoryLevels = [];
    product.bookedQuantity = 0;
    product.trackInventory = false;
    product.showMsrp = false;
    product.refrigerated = false;
    const deltaInventory = new productInventoryModel();
    deltaInventory.warehouseId = "615369c11200097db30d2004";
    deltaInventory.amount = 3;
    product.inventoryLevels.push(deltaInventory);

    console.log(product);
    // save the product
    const savedProduct = await product.save();
    console.log(savedProduct);
    // create the product in the IMS and retrieve the imsgid
    const imsgid = await ims.createIMSProduct(product);
    savedProduct.imsgid = imsgid;
    savedProduct.isNew = false;
    // update the product
    await savedProduct.save();
    const foundProduct = await productModel.findById(savedProduct._id);
    console.log(foundProduct);
  });

  it('retrieve warehouse products', async function () {
    // Look for all the products in a warehouse
    const warehouseId = "615369c11200097db30d2004";
    const products = await productModel.find({ 'inventoryLevels.warehouseId': warehouseId });
    console.log(products);
  });

  it('loads media', async function () {
    const filePath = "/Users/patam/Documents/Delta/INDACO/workspace/partner-manager-backend/test/testpicture.jpg";
    const fileStats = fs.statSync(filePath);
    const filename = path.basename(filePath);
    const pictureSize = new String(fileStats.size);
    // sanity check
    if (!fileStats.isFile()) throw new Error(`invalid picture path ${filePath}: not a file`);
    // read the file as base64
    const fileData = fs.readFileSync(filePath, { encoding: 'base64' });
    console.log(fileData);
  });

  it('stores product media', async function () {
    const filePath = "/Users/patam/Documents/Delta/INDACO/workspace/partner-manager-backend/test/testpicture.jpg";
    const fileStats = fs.statSync(filePath);
    const filename = path.basename(filePath);
    const pictureSize = new String(fileStats.size);
    // sanity check
    if (!fileStats.isFile()) throw new Error(`invalid picture path ${filePath}: not a file`);
    // read the file as base64
    const fileData = await fs.readFileSync(filePath, { encoding: 'base64' });
    // save raw media data in the db
    const product = new productModel();
    product.title = "Unit test product";
    product.description = "Unit test product description";
    product.customDescription = "Unit test custom product description";
    product.price = 99.99;
    product.sku = "TEST001";
    product.barcode = "123123123";
    product.weight = 1234;
    product.size = {
      height: 1,
      length: 1,
      width: 1,
    };
    product.partnerId = "6125199597b1612d8071eafd";
    product.requiresShipping = true;
    product.inventoryLevels = [];
    product.media = [];
    const productMedia = new productMediaModel();
    productMedia.name = filename;
    productMedia.type = "image/jpeg";
    productMedia.data = "data:image/png;base64," + fileData;
    product.media.push(productMedia);
    //save product
    const savedProduct = await product.save();
    expect(savedProduct).to.exist;
  });

  it('upload media', async function () {
    // Upload a picture
    const productId = "";
    const picture = "";
    const pictureUpload = await ims.uploadPicture(productId, picture);
  });

  it('get media', async function () {
    const productId = "gid://shopify/Product/7026954109104";
    const media = await ims.getIMSProductMedia(productId);
    console.log(media);
  });

  it('delete media', async function () {
    const productId = "gid://shopify/Product/7026954109104";
    const mediaIds = [];
    const deleteIds = await ims.deleteIMSProductMedia(productId, mediaIds);
    console.log(deleteIds);
  });

  it('inventory levels', async function () {
    // Start the order processing
    const product = new productModel();
    product.title = "Unit test product";
    product.description = "Unit test product description";
    product.customDescription = "Unit test custom product description";
    product.price = 99.99;
    product.sku = "TEST001";
    product.barcode = "123123123";
    product.weight = 1234;
    product.size = 1;
    product.partnerId = "6125199597b1612d8071eafd";
    product.inventoryLevels = [];
    try {
      product.quantity = 12312;
      assert(false); // this should not be allowed!
    } catch (err) {
      console.log(product);
    }
    console.log(product);
    console.log(product.quantity);

    const savedProduct = await product.save();

    const deltaInventory = new productInventoryModel();
    deltaInventory.warehouseId = "615369c11200097db30d2004";
    deltaInventory.amount = 12;
    const partnerInventory = new productInventoryModel();
    partnerInventory.warehouseId = "618cde8f6021cf2b0c0f2b92";
    partnerInventory.amount = 8;

    savedProduct.inventoryLevels.push(deltaInventory);
    savedProduct.inventoryLevels.push(partnerInventory);

    savedProduct.isNew = false;
    await savedProduct.save();

    console.log(savedProduct);
    console.log(`product quantity: ${savedProduct.quantity}`);
  });


  it('adjust quantity', async function () {
    const productId = "gid://shopify/Product/6734676787376";
    const quantity = 100
    const adjustquantity = await ims.adjustProductQuantity(productId, quantity);
    console.log(adjustquantity);
  });
});