|
@@ -25,7 +25,7 @@ class CartService extends CrudService {
|
|
|
list = JSON.parse(JSON.stringify(list));
|
|
|
list = list.map(i => {
|
|
|
const obj = {};
|
|
|
- obj.cart_id = _.get(i, '_id');
|
|
|
+ obj.cart = _.get(i, '_id');
|
|
|
obj.shop_name = _.get(i.shop, 'name');
|
|
|
obj.shop = _.get(i.shop, '_id');
|
|
|
obj.goods = this.setCartGoodsData(i.goods, i.goodsSpec);
|
|
@@ -58,6 +58,7 @@ class CartService extends CrudService {
|
|
|
obj.goodsSpec_name = _.get(goodsSpec, 'name');
|
|
|
obj.money = _.get(goodsSpec, 'sell_money');
|
|
|
obj.num = _.get(goodsSpec, 'num');
|
|
|
+ obj.file = _.get(goods, 'file');
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
@@ -77,12 +78,12 @@ class CartService extends CrudService {
|
|
|
const data = await this.model.findOne(body);
|
|
|
if (data) {
|
|
|
const buyNum = parseInt(data.num) + parseInt(num);
|
|
|
- const enough = await this.checkGoodsNum(goodsSpec, buyNum);
|
|
|
+ const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
|
|
|
if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
|
|
|
data.num = buyNum;
|
|
|
await data.save();
|
|
|
} else {
|
|
|
- const enough = await this.checkGoodsNum(goodsSpec, num);
|
|
|
+ const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
|
|
|
if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
|
|
|
await this.model.create({ num, ...body });
|
|
|
}
|
|
@@ -94,11 +95,11 @@ class CartService extends CrudService {
|
|
|
* @param {Number} num 需要的库存数量
|
|
|
* @return {Boolean} true:库存满足购物车条件;false:库存不满足条件
|
|
|
*/
|
|
|
- async checkGoodsNum(goodsSpecId, num) {
|
|
|
+ async checkGoodsNum({ goodsSpecId, num }) {
|
|
|
const goodsSpec = await this.goodsSpecModel.findById(goodsSpecId);
|
|
|
if (!goodsSpec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品的规格数据');
|
|
|
const { num: gsnum = 0 } = goodsSpec;
|
|
|
- return gsnum >= num;
|
|
|
+ return { enough: gsnum >= num, total: gsnum };
|
|
|
}
|
|
|
}
|
|
|
|