Your IP : 216.73.216.220


Current Path : /home/deltalab/PMS/ims-connector/graphql/schema/
Upload File :
Current File : //home/deltalab/PMS/ims-connector/graphql/schema/media.schema.js

const productAdapter = require('../../logic/shopify/product');

const { mediaIdTC, MediaId } = require('../types/media.type');
const { mediaInputType } = require('../types/media.type');
const { mediaTC } = require('../types/media.type');

// This mutation will upload the given media and attach it to the product specified
// It will return the unique id for the media for future references or nothing if something goes wrong
mediaTC.addResolver(
  {
    kind: 'mutation',
    name: 'productCreateMedia',
    type: mediaIdTC,
    args: {
      productId: 'ID!',
      media: mediaInputType,
    },
    resolve: async ({ args }) => {
      const { productId } = args;
      const { media } = args;
      const response = new MediaId();
      response.imsgid = await productAdapter.uploadProductMedia(productId, media.name, media.data, media.type);
      return response;
    },
  },
);

// This mutation will delete the given media and remove it from the specified product
// It will return the unique id for the media or nothing if it couldnt make it
mediaTC.addResolver(
  {
    kind: 'mutation',
    name: 'productDeleteMedia',
    type: [mediaIdTC],
    args: {
      productId: 'ID!',
      mediaIds: '[ID!]!',
    },
    resolve: async ({ args }) => {
      const { productId } = args;
      const { mediaIds } = args;
      const response = [];
      const deletedimsgids = await productAdapter.deleteProductMedia(productId, mediaIds);
      // Load the imsgids in suitable mediaids in the response
      for (const deletedImsgid in deletedimsgids) {
        const deletedId = new MediaId();
        deletedId.imsgid = deletedImsgid;
        response.push(deletedId);
      }
      return response;
    },
  },
);

/**
 * Retrieve just the product media based on the product id
 */
mediaTC.addResolver(
  {
    kind: 'query',
    name: 'productMedia',
    type: [mediaIdTC],
    args: {
      productId: 'ID!',
    },
    resolve: async ({ args }) => {
      const { productId } = args;
      const productMedia = [];
      const mediaArray = await productAdapter.getProductMedia(productId);
      for (const media of mediaArray) {
        productMedia.push(new MediaId(media));
      }
      return productMedia;
    },
  },
);

const mediaMutation = {
  productCreateMedia: mediaTC.getResolver('productCreateMedia'),
  productDeleteMedia: mediaTC.getResolver('productDeleteMedia'),
};

const mediaQuery = {
  productMedia: mediaTC.getResolver('productMedia'),
};

module.exports = {
  mediaMutation,
  mediaQuery,
};