erp2goods-service.js 1.15 KB
/**
 * Created by TaoHuang on 2016/11/14.
 */

'use strict';

const api = require('./erp2goods-api');

const _ = require('lodash');
const helpers = global.yoho.helpers;

const findBySkn = (skn) => {
    return api.getProductBySknAsync(skn).then((result) => {
        if (result && result.code && result.code === 200 && result.data.product_url) {
            return {
                url: helpers.getUrlBySkc(result.data.product_id)
            };
        } else {
            return {};
        }
    });
};

const findBySku = (sku) => {
    return api.getProductBySkuAsync(`[${sku}]`).then((result) => {
        if (result && result.code && result.code === 200 && !_.isEmpty(result.data)) {
            let product = _.head(result.data) || {};
            let skn = product.erpProductId || '';

            if (skn) {
                return findBySkn(skn);
            }

            return {};
        } else {
            return {};
        }
    });
};

const find = (q) => {
    if (q.sku) {
        return findBySku(q.sku);
    } else if (q.skn) {
        return findBySkn(q.skn);
    } else {
        return Promise.reject();
    }
};

module.exports = {
    find
};