Adds cart creation

This commit is contained in:
Sebastian Rindom
2020-07-08 18:26:56 +02:00
parent 7247095078
commit edd64b5f62
5 changed files with 25 additions and 6 deletions

View File

@@ -160,15 +160,19 @@ class ContentfulService extends BaseService {
try {
productEntry = await environment.getEntry(product._id)
} catch (error) {
console.log(error)
return this.createProductInContentful(product)
}
const variantEntries = await this.getVariantEntries_(product.variants)
const variantEntries = await this.getVariantEntries_(product._id)
const variantLinks = this.getVariantLinks_(variantEntries)
productEntry.fields = _.assignIn(productEntry.fields, {
title: {
"en-US": product.title,
},
options: {
"en-US": product.options,
},
variants: {
"en-US": variantLinks,
},
@@ -219,6 +223,9 @@ class ContentfulService extends BaseService {
sku: {
"en-US": variant.sku,
},
options: {
"en-US": variant.options,
},
prices: {
"en-US": variant.prices,
},

View File

@@ -58,7 +58,7 @@ export default async (req, res) => {
const product = await productService.update(oldProduct._id, value)
const data = await productService.decorate(
newProduct,
product,
[
"title",
"description",

View File

@@ -2,7 +2,7 @@ import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
region_id: Validator.string().required(),
region_id: Validator.string(),
items: Validator.array()
.items({
variant_id: Validator.string().required(),
@@ -19,7 +19,16 @@ export default async (req, res) => {
try {
const lineItemService = req.scope.resolve("lineItemService")
const cartService = req.scope.resolve("cartService")
let cart = await cartService.create({ region_id: value.region_id })
// Add a default region if no region has been specified
let regionId = value.region_id
if (!value.region_id) {
const regionService = req.scope.resolve("regionService")
const regions = await regionService.list()
regionId = regions[0]._id
}
let cart = await cartService.create({ region_id: regionId })
if (value.items) {
await Promise.all(
@@ -35,7 +44,7 @@ export default async (req, res) => {
}
cart = await cartService.retrieve(cart._id)
cart = await cartService.decorate(cart)
cart = await cartService.decorate(cart, [], ["region"])
res.status(200).json({ cart })
} catch (err) {
throw err

View File

@@ -3,7 +3,7 @@ export default async (req, res) => {
try {
const cartService = req.scope.resolve("cartService")
let cart = await cartService.retrieve(id)
cart = await cartService.decorate(cart)
cart = await cartService.decorate(cart, [], ["region"])
res.json({ cart })
} catch (err) {
throw err

View File

@@ -227,6 +227,9 @@ class CartService extends BaseService {
async decorate(cart, fields, expandFields = []) {
const c = cart.toObject()
c.total = await this.totalsService_.getTotal(cart)
if (expandFields.includes("region")) {
c.region = await this.regionService_.retrieve(cart.region_id)
}
return c
}