Your IP : 216.73.216.43


Current Path : /proc/thread-self/root/home/deltalab/PMS/ims-connector/logic/shopify/
Upload File :
Current File : //proc/thread-self/root/home/deltalab/PMS/ims-connector/logic/shopify/listing.js

const axios = require('axios');

const { listingBaseModel } = require('../../models/mongoose/listing');

// HEADERS =====================================
const SHOPIFY_HEADER = {
  'Content-Type': 'application/json',
  Accept: 'application/json',
  'X-Shopify-Access-Token': process.env.SHOPIFY_PASSWORD,
};

// FUNCTIONS ==================================

/**
 * Retrieved the specified listing
 * @param {*} id imsgid of the listing to update
 * @returns the corresponding listing or null if it could not be found
 */
async function getListing(id) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      query ($id : ID!) {
        collection (id: $id) {
          id
          handle
          title
          description
          descriptionHtml
          updatedAt
        }
      }
    `,
      variables: {
        id,
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { collection } = result.data.data;
  if (!collection) return null;
  const listing = new listingBaseModel();
  populateListing(listing, collection);
  return listing;
}

/**
 * Update the specified listing with the given data
 * @param {string} id imsgid of the listing to update
 * @param {string} name the listing name
 * @param {string} description html description
 * @returns the id of the updated listing or null if error
 */
async function updateListing(id, name, description) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation ($input : CollectionInput!) {
        collectionUpdate(input: $input) {
          collection {
           id
          }
        }
      }
    `,
      variables: {
        input: {
          id,
          title: name,
          descriptionHtml: description,
        },
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { collection } = result.data.data.collectionUpdate;
  if (!collection) return null;
  return collection.id;
}

/**
 * Creates a new listing with the given parameters
 * @param {string} title
 * @param {string} description HTML formatted description
 * @returns the listing created or null if it was not created
 */
async function createListing(title, description) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation collectionCreate($input: CollectionInput!) {
        collectionCreate(input: $input) {
          collection {
            id
          }
        }
      }
    `,
      variables: {
        input: {
          title,
          descriptionHtml: description,
        },
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { collection } = result.data.data.collectionCreate;
  if (!collection) return null;

  // publish the collection to the app channel
  // after the creation
  await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation publishablePublishToCurrentChannel($id: ID!) {
        publishablePublishToCurrentChannel(id: $id) {
          userErrors {
            field,
            message
          }
        }
      }
    `,
      variables: {
        id: collection.id,
      },
    },
    headers: SHOPIFY_HEADER,
  });

  return collection.id;
}

/**
 * Removes the listing with the given id
 * @param {string} id the imsgid of the listing
 * @returns the id of the listing or null if error
 */
async function deleteListing(id) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation collectionDelete($input: CollectionDeleteInput!) {
        collectionDelete(input: $input) {
          deletedCollectionId
        }
      }
    `,
      variables: {
        input: {
          id,
        },
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { collectionDelete } = result.data.data;
  // Sanity check
  if (!collectionDelete) return null;
  return collectionDelete.deletedCollectionId;
}

/**
 * Add the given list of products to the specified listing
 * @param {*} id imsgid of the listing to add the products to
 * @param {*} productIds imsgid or the products to add to the listing
 * @returns the id of the affected listing or null
 */
async function addProductsToListing(id, productIds) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation collectionAddProducts($id:ID!, $ids:[ID!]!) {
        collectionAddProducts(id:$id, productIds:$ids) {
          collection {
            id
          }
        }
      }
    `,
      variables: {
        id,
        ids: productIds,
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { collection } = result.data.data;
  if (!collection) return null;
  return collection.id;
}

/**
 * Removes the given list of products from the specified listing
 * @param {*} id imsgid of the listing from which products will be removed
 * @param {*} productIds imsgid or the products to remove from the listing
 * @returns the id of the affected listing or null
 */
async function removeProductsFromListing(id, productIds) {
  const result = await axios({
    url: process.env.SHOPIFY_STORE_URL,
    method: 'post',
    data: {
      query: `
      mutation collectionRemoveProducts($id:ID!, $ids:[ID!]!) {
        collectionRemoveProducts(id:$id, productIds:$ids) {
          job {
            done
          }
        }
      }
    `,
      variables: {
        id,
        ids: productIds,
      },
    },
    headers: SHOPIFY_HEADER,
  });
  const { job } = result.data.data;
  if (!job) return null;
  return job.done;
}

// EXPORTS ===============================================
module.exports = {
  addProductsToListing,
  removeProductsFromListing,
  createListing,
  deleteListing,
  getListing,
  updateListing,
};

// INTERNAL FUNCTIONS ===================================

/**
 * Maps the given collection on the specified listing
 * @param {listingBaseModel} listing the listing to be populated
 * @param {*} collection the collection to be used to populate the listing
 */
function populateListing(listing, collection) {
  listing.imsgid = collection.id;
  listing.name = collection.title;
  listing.handle = collection.handle;
  listing.description = collection.descriptionHtml;
  listing.updatedAt = collection.updatedAt;
  return listing;
}