ticket-service.js 1.65 KB
/**
 * Created by TaoHuang on 2017/6/22.
 */

const api = require('./ticket-api');
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const _handleUseYhoCoin = require('./order-ensure-handle').handleUseYohoCoin;

function _handleGoodsList(list) {
    return list.map((i) => {
        i.linkToGoods = helpers.getUrlBySkc(i.product_skn);
        i.productPrice = i.sales_price;
        return i;
    });
}

function _handleAmount(info) {
    return _.get(info, 'data.shopping_cart_data.last_order_amount', 0);
}

const addTicket = co(function * (uid, sku, count, yohoCoin) {
    let ticketInfo = yield api.add(uid, sku, count, yohoCoin);
    let result = {};

    if (ticketInfo.code !== 200) {
        return {
            last_order_amount: 0,
            error: ticketInfo.message
        };
    }

    result.goodsList = _handleGoodsList(_.get(ticketInfo, 'data.goods_list', []));
    result.last_order_amount = _handleAmount(ticketInfo);
    Object.assign(result, _handleUseYhoCoin(_.get(ticketInfo, 'data.shopping_cart_data', {})));

    return result;
});

const submitTicket = co(function * (uid, sku, count, mobile, yohoCoin) {
    let result = yield api.submit(uid, sku, count, mobile, yohoCoin);

    if (result.code !== 200) {
        return {
            code: 410,
            message: '提交失败'
        };
    }

    return {
        code: 200,
        message: '提交成功',
        data: {
            refer: helpers.urlFormat('/shopping/newpay', {
                ordercode: result.data.order_code
            })
        }
    };
});

module.exports = {
    addTicket,
    submitTicket
};