{"size": 3074, "ext": "js", "max_stars_repo_path": "plugins/gatsby-custom-artwork/gatsby-node.js", "max_stars_repo_name": "UK090483/meetFrida", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "plugins/gatsby-custom-artwork/gatsby-node.js", "max_issues_repo_name": "UK090483/meetFrida", "max_issues_repo_issues_event_min_datetime": "2020-07-21T12:48:04.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-05T16:30:45.000Z", "max_forks_repo_path": "plugins/gatsby-custom-artwork/gatsby-node.js", "max_forks_repo_name": "UK090483/meetFrida", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 22.6029411765, "max_line_length": 78, "alphanum_fraction": 0.6167859466}
const { sanity } = require("../../lib/sanityClient")

const { SantityToShopify } = require("./lib/functions")
const { getAllShopifyProducts } = require("./lib/shopify")
const { getPriceWithTax } = require("../../lib/getPriceWithTax")

const loadArtworksSanity = async () => {
  console.time("SANITY_____Load")
  const query = `
  *[_type == 'artwork' && !(_id in path('drafts.**'))]{
    "uuid":_id,
    "slug" : slug.current,
    "artworkName":name,
    'title': name,
    availability,
    description,
    width,
    height,
    depth,
    price,
    "stil":stil->name,
    "medium":medium->name,
    "artistName": artist->anzeigeName,
    "artistWebLink": artist->webLink,
    "artistDescription": artist->description,
    "instagramLink": artist->instagramLink,
    "artistId": artist->_id,
    image,
    "rating":coalesce(rating, 0),
    "banner":coalesce(banner, 'unknown'),
    shopify_handle,
    shopify_product_id,
    "alternativeSlug": alternaiveSlug.current,
    _updatedAt
    }`

  const params = {}

  const res = await sanity.fetch(query, params)

  console.timeEnd("SANITY_____Load")
  console.log("loaded " + res.length + " artworks ")

  return res
}
async function createArtworNodes(actions, createNodeId, createContentDigest) {
  const sanityArtworksbefore = await loadArtworksSanity()

  await SantityToShopify("artwork", sanityArtworksbefore)

  const sanityArtworks = await loadArtworksSanity()

  const { createNode } = actions
  sanityArtworks.forEach(item => {
    const {
      slug,
      uuid,
      artworkName,
      availability,
      description,
      width,
      height,
      depth,
      price,
      rating,
      banner,
      stil,
      medium,
      artistName,
      artistWebLink,
      artistDescription,
      instagramLink,
      artistId,
      shopify_handle,
      alternativeSlug,
      image: {
        asset: { _ref: imageAssetId },
      },
    } = item

    const data = {
      alternativeSlug,
      ransortNumber: getWithRating(rating),
      uuid,
      availability,
      description,
      artistName,
      artistWebLink,
      artistDescription,
      instagramLink,
      medium,
      stil,
      artworkName,
      artistId,
      slug,
      width,
      height,
      depth,
      rating,
      banner,
      shopify_handle,
      price: getPriceWithTax(price),
      image: {
        imageAssetId,
      },
    }

    const nodeContent = JSON.stringify(data)
    const nodeMeta = {
      id: createNodeId(`frida-artwork-${uuid}`),
      parent: null,
      children: [],
      internal: {
        type: `FridaArtwork`,
        mediaType: `text/html`,
        content: nodeContent,
        contentDigest: createContentDigest(data),
      },
    }
    const node = Object.assign({}, data, nodeMeta)
    createNode(node)
  })
}

function getWithRating(rating) {
  return rating === 0 ? Math.random() : (rating + Math.random() - 1) * 0.1
}

exports.sourceNodes = async ({
  actions,
  createNodeId,
  createContentDigest,
}) => {
  await createArtworNodes(actions, createNodeId, createContentDigest)
}
