<commit_before>import Vue from 'vue';
import { ActionTree } from 'vuex';
import i18n from '@vue-storefront/i18n';
// requires cart module
import * as types from '@vue-storefront/core/modules/cart/store/mutation-types';
import RootState from '@vue-storefront/core/types/RootState';
import StockState from '../../types/StockState';
import rootStore from '@vue-storefront/core/store';
import { TaskQueue } from '@vue-storefront/core/lib/sync';
import { Logger } from '@vue-storefront/core/lib/logger';
import config from 'config';

const actions: ActionTree<StockState, RootState> = {
  /**
   * Reset current configuration and selected variatnts
   */
  async check (context, { product, qty = 1 }) {
    if (rootStore.state.config.stock.synchronize) {
      const task: any = await TaskQueue.execute({
        url:
          rootStore.state.config.stock.endpoint +
          '/check?sku=' +
          encodeURIComponent(product.sku),
        payload: {
          method: 'GET',
          headers: { 'Content-Type': 'application/json' },
          mode: 'cors'
        },
        product_sku: product.sku,
        callback_event: 'store:stock/stockAfterCheck'
      });
      let result = task.result;
      const cartProduct = await rootStore.dispatch('cart/getItem', product.sku);
      let totalQty = cartProduct ? cartProduct.qty + product.qty : product.qty;
      const status = result.is_in_stock
        ? totalQty > result.qty
          ? 'not_enough_stock'
          : 'ok'
        : 'out_of_stock';
      return Object.assign({}, task, {
        qty: task.result.qty,
        status,
        onlineCheckTaskId: task.task_id
      });
    } else {
      return {
        qty: product.stock ? product.stock.qty : 0,
        status: product.stock
          ? product.stock.is_in_stock
            ? 'ok'
            : 'out_of_stock'
          : 'volatile'
      }; // if not online, cannot check the source of true here
    }
  },
  /**
   * Reset current configuration and selected variatnts
   */
  list (context, { skus }) {
    return new Promise((resolve, reject) => {
      if (config.stock.synchronize) {
        TaskQueue.execute({
          url:
            config.stock.endpoint +
            '/list?skus=' +
            encodeURIComponent(skus.join(',')),
          payload: {
            method: 'GET',
            headers: { 'Content-Type': 'application/json' },
            mode: 'cors'
          },
          skus: skus
        })
          .then((task: any) => {
            if (task.resultCode === 200) {
              for (const si of task.result) {
                context.state.cache[si.product_id] = {
                  is_in_stock: si.is_in_stock,
                  qty: si.qty,
                  product_id: si.product_id
                }; // TODO: should be moved to mutation
              }
            }
            resolve(task); // if online we can return ok because it will be verified anyway
          })
          .catch(err => {
            Logger.error(err, 'stock')();
            resolve(null);
          });
      } else {<commit_msg>Added changes to actions filem proposed at CR<commit_after>import Vue from 'vue';
import { ActionTree } from 'vuex';
import i18n from '@vue-storefront/i18n';
// requires cart module
import * as types from '@vue-storefront/core/modules/cart/store/mutation-types';
import RootState from '@vue-storefront/core/types/RootState';
import StockState from '../../types/StockState';
import rootStore from '@vue-storefront/core/store';
import { TaskQueue } from '@vue-storefront/core/lib/sync';
import { Logger } from '@vue-storefront/core/lib/logger';
import config from 'config';

const actions: ActionTree<StockState, RootState> = {
  /**
   * Reset current configuration and selected variatnts
   */
  async check ({ dispatch }, { product, qty = 1 }) {
    if (config.stock.synchronize) {
      const task: any = await TaskQueue.execute({
        url: `${config.stock.endpoint}/check?sku=${encodeURIComponent(
          product.sku
        )}`,
        payload: {
          method: 'GET',
          headers: { 'Content-Type': 'application/json' },
          mode: 'cors'
        },
        product_sku: product.sku,
        callback_event: 'store:stock/stockAfterCheck'
      });
      let result = task.result;
      const cartProduct = await dispatch('cart/getItem', product.sku);
      const totalQty = cartProduct
        ? cartProduct.qty + product.qty
        : product.qty;
      const status = result.is_in_stock
        ? totalQty > result.qty
          ? 'not_enough_stock'
          : 'ok'
        : 'out_of_stock';
      return Object.assign({}, task, {
        qty: task.result.qty,
        status,
        onlineCheckTaskId: task.task_id
      });
    } else {
      const status = product.stock
        ? product.stock.is_in_stock
          ? 'ok'
          : 'out_of_stock'
        : 'volatile';
      return {
        qty: product.stock ? product.stock.qty : 0,
        status
      }; // if not online, cannot check the source of true here
    }
  },
  /**
   * Reset current configuration and selected variatnts
   */
  list (context, { skus }) {
    return new Promise((resolve, reject) => {
      if (config.stock.synchronize) {
        TaskQueue.execute({
          url: `${config.stock.endpoint}/list?skus=${encodeURIComponent(
            skus.join(',')
          )}`,
          payload: {
            method: 'GET',
            headers: { 'Content-Type': 'application/json' },
            mode: 'cors'
          },
          skus: skus
        })
          .then((task: any) => {
            if (task.resultCode === 200) {
              for (const si of task.result) {
                context.state.cache[si.product_id] = {
                  is_in_stock: si.is_in_stock,
                  qty: si.qty,
                  product_id: si.product_id
                }; // TODO: should be moved to mutation
              }
            }
            resolve(task); // if online we can return ok because it will be verified anyway
          })
          .catch(err => {
            Logger.error(err, 'stock')();
            resolve(null);
          });
      } else {
{'fraction_non_alphanumeric': 0.12642183945401364, 'fraction_numerical': 0.0029249268768280793, 'mean_word_length': 1.908790170132325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5145864', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1791, 'n_words': 421}