Adds cart creation
This commit is contained in:
@@ -160,15 +160,19 @@ class ContentfulService extends BaseService {
|
|||||||
try {
|
try {
|
||||||
productEntry = await environment.getEntry(product._id)
|
productEntry = await environment.getEntry(product._id)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
return this.createProductInContentful(product)
|
return this.createProductInContentful(product)
|
||||||
}
|
}
|
||||||
|
|
||||||
const variantEntries = await this.getVariantEntries_(product.variants)
|
const variantEntries = await this.getVariantEntries_(product._id)
|
||||||
const variantLinks = this.getVariantLinks_(variantEntries)
|
const variantLinks = this.getVariantLinks_(variantEntries)
|
||||||
productEntry.fields = _.assignIn(productEntry.fields, {
|
productEntry.fields = _.assignIn(productEntry.fields, {
|
||||||
title: {
|
title: {
|
||||||
"en-US": product.title,
|
"en-US": product.title,
|
||||||
},
|
},
|
||||||
|
options: {
|
||||||
|
"en-US": product.options,
|
||||||
|
},
|
||||||
variants: {
|
variants: {
|
||||||
"en-US": variantLinks,
|
"en-US": variantLinks,
|
||||||
},
|
},
|
||||||
@@ -219,6 +223,9 @@ class ContentfulService extends BaseService {
|
|||||||
sku: {
|
sku: {
|
||||||
"en-US": variant.sku,
|
"en-US": variant.sku,
|
||||||
},
|
},
|
||||||
|
options: {
|
||||||
|
"en-US": variant.options,
|
||||||
|
},
|
||||||
prices: {
|
prices: {
|
||||||
"en-US": variant.prices,
|
"en-US": variant.prices,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const product = await productService.update(oldProduct._id, value)
|
const product = await productService.update(oldProduct._id, value)
|
||||||
const data = await productService.decorate(
|
const data = await productService.decorate(
|
||||||
newProduct,
|
product,
|
||||||
[
|
[
|
||||||
"title",
|
"title",
|
||||||
"description",
|
"description",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
|||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const schema = Validator.object().keys({
|
const schema = Validator.object().keys({
|
||||||
region_id: Validator.string().required(),
|
region_id: Validator.string(),
|
||||||
items: Validator.array()
|
items: Validator.array()
|
||||||
.items({
|
.items({
|
||||||
variant_id: Validator.string().required(),
|
variant_id: Validator.string().required(),
|
||||||
@@ -19,7 +19,16 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const lineItemService = req.scope.resolve("lineItemService")
|
const lineItemService = req.scope.resolve("lineItemService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
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) {
|
if (value.items) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
@@ -35,7 +44,7 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cart = await cartService.retrieve(cart._id)
|
cart = await cartService.retrieve(cart._id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const cartService = req.scope.resolve("cartService")
|
const cartService = req.scope.resolve("cartService")
|
||||||
let cart = await cartService.retrieve(id)
|
let cart = await cartService.retrieve(id)
|
||||||
cart = await cartService.decorate(cart)
|
cart = await cartService.decorate(cart, [], ["region"])
|
||||||
res.json({ cart })
|
res.json({ cart })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -227,6 +227,9 @@ class CartService extends BaseService {
|
|||||||
async decorate(cart, fields, expandFields = []) {
|
async decorate(cart, fields, expandFields = []) {
|
||||||
const c = cart.toObject()
|
const c = cart.toObject()
|
||||||
c.total = await this.totalsService_.getTotal(cart)
|
c.total = await this.totalsService_.getTotal(cart)
|
||||||
|
if (expandFields.includes("region")) {
|
||||||
|
c.region = await this.regionService_.retrieve(cart.region_id)
|
||||||
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user