fix: make packages/medusa/src/api/routes/store/carts pass eslint (#652)
This commit is contained in:
@@ -26,7 +26,6 @@
|
|||||||
/packages/medusa/src/api/routes/admin/collections
|
/packages/medusa/src/api/routes/admin/collections
|
||||||
/packages/medusa/src/api/routes/admin/notes
|
/packages/medusa/src/api/routes/admin/notes
|
||||||
/packages/medusa/src/api/routes/admin/store
|
/packages/medusa/src/api/routes/admin/store
|
||||||
/packages/medusa/src/api/routes/store/carts
|
|
||||||
/packages/medusa/src/api/routes/store/return-reasons
|
/packages/medusa/src/api/routes/store/return-reasons
|
||||||
/packages/medusa/src/api/routes/store/returns
|
/packages/medusa/src/api/routes/store/returns
|
||||||
/packages/medusa/src/api/routes/store/swaps
|
/packages/medusa/src/api/routes/store/swaps
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import _ from "lodash"
|
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
import { defaultFields, defaultRelations } from "./"
|
import { defaultFields, defaultRelations } from "./"
|
||||||
|
|
||||||
@@ -36,31 +35,27 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const manager = req.scope.resolve("manager")
|
||||||
const manager = req.scope.resolve("manager")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
const txCartService = cartService.withTransaction(m)
|
const txCartService = cartService.withTransaction(m)
|
||||||
|
|
||||||
await txCartService.addShippingMethod(id, value.option_id, value.data)
|
await txCartService.addShippingMethod(id, value.option_id, value.data)
|
||||||
|
|
||||||
const updated = await txCartService.retrieve(id, {
|
const updated = await txCartService.retrieve(id, {
|
||||||
relations: ["payment_sessions"],
|
relations: ["payment_sessions"],
|
||||||
})
|
|
||||||
|
|
||||||
if (updated.payment_sessions?.length) {
|
|
||||||
await txCartService.setPaymentSessions(id)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const updatedCart = await cartService.retrieve(id, {
|
if (updated.payment_sessions?.length) {
|
||||||
select: defaultFields,
|
await txCartService.setPaymentSessions(id)
|
||||||
relations: defaultRelations,
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart: updatedCart })
|
const updatedCart = await cartService.retrieve(id, {
|
||||||
} catch (err) {
|
select: defaultFields,
|
||||||
throw err
|
relations: defaultRelations,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
res.status(200).json({ cart: updatedCart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,226 +57,222 @@ export default async (req, res) => {
|
|||||||
res.setHeader("Access-Control-Expose-Headers", "Idempotency-Key")
|
res.setHeader("Access-Control-Expose-Headers", "Idempotency-Key")
|
||||||
res.setHeader("Idempotency-Key", idempotencyKey.idempotency_key)
|
res.setHeader("Idempotency-Key", idempotencyKey.idempotency_key)
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
const orderService = req.scope.resolve("orderService")
|
||||||
const orderService = req.scope.resolve("orderService")
|
const swapService = req.scope.resolve("swapService")
|
||||||
const swapService = req.scope.resolve("swapService")
|
|
||||||
|
|
||||||
let inProgress = true
|
let inProgress = true
|
||||||
let err = false
|
let err = false
|
||||||
|
|
||||||
while (inProgress) {
|
while (inProgress) {
|
||||||
switch (idempotencyKey.recovery_point) {
|
switch (idempotencyKey.recovery_point) {
|
||||||
case "started": {
|
case "started": {
|
||||||
const { key, error } = await idempotencyKeyService.workStage(
|
const { key, error } = await idempotencyKeyService.workStage(
|
||||||
idempotencyKey.idempotency_key,
|
idempotencyKey.idempotency_key,
|
||||||
async (manager) => {
|
async (manager) => {
|
||||||
let cart = await cartService.withTransaction(manager).retrieve(id)
|
let cart = await cartService.withTransaction(manager).retrieve(id)
|
||||||
|
|
||||||
if (cart.completed_at) {
|
if (cart.completed_at) {
|
||||||
|
return {
|
||||||
|
response_code: 409,
|
||||||
|
response_body: {
|
||||||
|
code: MedusaError.Codes.CART_INCOMPATIBLE_STATE,
|
||||||
|
message: "Cart has already been completed",
|
||||||
|
type: MedusaError.Types.NOT_ALLOWED,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cart = await cartService
|
||||||
|
.withTransaction(manager)
|
||||||
|
.authorizePayment(id, {
|
||||||
|
...req.request_context,
|
||||||
|
idempotency_key: idempotencyKey.idempotency_key,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (cart.payment_session) {
|
||||||
|
if (
|
||||||
|
cart.payment_session.status === "requires_more" ||
|
||||||
|
cart.payment_session.status === "pending"
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
response_code: 409,
|
response_code: 200,
|
||||||
response_body: {
|
response_body: {
|
||||||
code: MedusaError.Codes.CART_INCOMPATIBLE_STATE,
|
data: cart,
|
||||||
message: "Cart has already been completed",
|
payment_status: cart.payment_session.status,
|
||||||
type: MedusaError.Types.NOT_ALLOWED,
|
type: "cart",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cart = await cartService
|
return {
|
||||||
.withTransaction(manager)
|
recovery_point: "payment_authorized",
|
||||||
.authorizePayment(id, {
|
}
|
||||||
...req.request_context,
|
}
|
||||||
idempotency_key: idempotencyKey.idempotency_key,
|
)
|
||||||
})
|
|
||||||
|
if (error) {
|
||||||
|
inProgress = false
|
||||||
|
err = error
|
||||||
|
} else {
|
||||||
|
idempotencyKey = key
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
case "payment_authorized": {
|
||||||
|
const { key, error } = await idempotencyKeyService.workStage(
|
||||||
|
idempotencyKey.idempotency_key,
|
||||||
|
async (manager) => {
|
||||||
|
const cart = await cartService
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(id, {
|
||||||
|
select: ["total"],
|
||||||
|
relations: ["payment", "payment_sessions"],
|
||||||
|
})
|
||||||
|
|
||||||
|
let order
|
||||||
|
|
||||||
|
// If cart is part of swap, we register swap as complete
|
||||||
|
switch (cart.type) {
|
||||||
|
case "swap": {
|
||||||
|
try {
|
||||||
|
const swapId = cart.metadata?.swap_id
|
||||||
|
let swap = await swapService
|
||||||
|
.withTransaction(manager)
|
||||||
|
.registerCartCompletion(swapId)
|
||||||
|
|
||||||
|
swap = await swapService
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(swap.id, { relations: ["shipping_address"] })
|
||||||
|
|
||||||
if (cart.payment_session) {
|
|
||||||
if (
|
|
||||||
cart.payment_session.status === "requires_more" ||
|
|
||||||
cart.payment_session.status === "pending"
|
|
||||||
) {
|
|
||||||
return {
|
return {
|
||||||
response_code: 200,
|
response_code: 200,
|
||||||
response_body: {
|
response_body: { data: swap, type: "swap" },
|
||||||
data: cart,
|
}
|
||||||
payment_status: cart.payment_session.status,
|
} catch (error) {
|
||||||
type: "cart",
|
if (
|
||||||
},
|
error &&
|
||||||
|
error.code === MedusaError.Codes.INSUFFICIENT_INVENTORY
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
response_code: 409,
|
||||||
|
response_body: {
|
||||||
|
message: error.message,
|
||||||
|
type: error.type,
|
||||||
|
code: error.code,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// case "payment_link":
|
||||||
|
default: {
|
||||||
|
if (!cart.payment && cart.total > 0) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.INVALID_DATA,
|
||||||
|
`Cart payment not authorized`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
try {
|
||||||
recovery_point: "payment_authorized",
|
order = await orderService
|
||||||
}
|
.withTransaction(manager)
|
||||||
}
|
.createFromCart(cart.id)
|
||||||
)
|
} catch (error) {
|
||||||
|
if (
|
||||||
if (error) {
|
error &&
|
||||||
inProgress = false
|
error.message === "Order from cart already exists"
|
||||||
err = error
|
) {
|
||||||
} else {
|
order = await orderService
|
||||||
idempotencyKey = key
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
case "payment_authorized": {
|
|
||||||
const { key, error } = await idempotencyKeyService.workStage(
|
|
||||||
idempotencyKey.idempotency_key,
|
|
||||||
async (manager) => {
|
|
||||||
const cart = await cartService
|
|
||||||
.withTransaction(manager)
|
|
||||||
.retrieve(id, {
|
|
||||||
select: ["total"],
|
|
||||||
relations: ["payment", "payment_sessions"],
|
|
||||||
})
|
|
||||||
|
|
||||||
let order
|
|
||||||
|
|
||||||
// If cart is part of swap, we register swap as complete
|
|
||||||
switch (cart.type) {
|
|
||||||
case "swap": {
|
|
||||||
try {
|
|
||||||
const swapId = cart.metadata?.swap_id
|
|
||||||
let swap = await swapService
|
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.registerCartCompletion(swapId)
|
.retrieveByCartId(id, {
|
||||||
|
select: [
|
||||||
swap = await swapService
|
"subtotal",
|
||||||
.withTransaction(manager)
|
"tax_total",
|
||||||
.retrieve(swap.id, { relations: ["shipping_address"] })
|
"shipping_total",
|
||||||
|
"discount_total",
|
||||||
|
"total",
|
||||||
|
],
|
||||||
|
relations: ["shipping_address", "items", "payments"],
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
response_code: 200,
|
response_code: 200,
|
||||||
response_body: { data: swap, type: "swap" },
|
response_body: { data: order, type: "order" },
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} else if (
|
||||||
if (
|
error &&
|
||||||
error &&
|
error.code === MedusaError.Codes.INSUFFICIENT_INVENTORY
|
||||||
error.code === MedusaError.Codes.INSUFFICIENT_INVENTORY
|
) {
|
||||||
) {
|
return {
|
||||||
return {
|
response_code: 409,
|
||||||
response_code: 409,
|
response_body: {
|
||||||
response_body: {
|
message: error.message,
|
||||||
message: error.message,
|
type: error.type,
|
||||||
type: error.type,
|
code: error.code,
|
||||||
code: error.code,
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// case "payment_link":
|
|
||||||
default: {
|
|
||||||
if (!cart.payment && cart.total > 0) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.INVALID_DATA,
|
|
||||||
`Cart payment not authorized`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
order = await orderService
|
|
||||||
.withTransaction(manager)
|
|
||||||
.createFromCart(cart.id)
|
|
||||||
} catch (error) {
|
|
||||||
if (
|
|
||||||
error &&
|
|
||||||
error.message === "Order from cart already exists"
|
|
||||||
) {
|
|
||||||
order = await orderService
|
|
||||||
.withTransaction(manager)
|
|
||||||
.retrieveByCartId(id, {
|
|
||||||
select: [
|
|
||||||
"subtotal",
|
|
||||||
"tax_total",
|
|
||||||
"shipping_total",
|
|
||||||
"discount_total",
|
|
||||||
"total",
|
|
||||||
],
|
|
||||||
relations: ["shipping_address", "items", "payments"],
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
response_code: 200,
|
|
||||||
response_body: { data: order, type: "order" },
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
error &&
|
|
||||||
error.code === MedusaError.Codes.INSUFFICIENT_INVENTORY
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
response_code: 409,
|
|
||||||
response_body: {
|
|
||||||
message: error.message,
|
|
||||||
type: error.type,
|
|
||||||
code: error.code,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
order = await orderService
|
|
||||||
.withTransaction(manager)
|
|
||||||
.retrieve(order.id, {
|
|
||||||
select: [
|
|
||||||
"subtotal",
|
|
||||||
"tax_total",
|
|
||||||
"shipping_total",
|
|
||||||
"discount_total",
|
|
||||||
"total",
|
|
||||||
],
|
|
||||||
relations: ["shipping_address", "items", "payments"],
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
response_code: 200,
|
|
||||||
response_body: { data: order, type: "order" },
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
if (error) {
|
order = await orderService
|
||||||
inProgress = false
|
.withTransaction(manager)
|
||||||
err = error
|
.retrieve(order.id, {
|
||||||
} else {
|
select: [
|
||||||
idempotencyKey = key
|
"subtotal",
|
||||||
|
"tax_total",
|
||||||
|
"shipping_total",
|
||||||
|
"discount_total",
|
||||||
|
"total",
|
||||||
|
],
|
||||||
|
relations: ["shipping_address", "items", "payments"],
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
response_code: 200,
|
||||||
|
response_body: { data: order, type: "order" },
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break
|
)
|
||||||
}
|
|
||||||
|
|
||||||
case "finished": {
|
if (error) {
|
||||||
inProgress = false
|
inProgress = false
|
||||||
break
|
err = error
|
||||||
|
} else {
|
||||||
|
idempotencyKey = key
|
||||||
}
|
}
|
||||||
|
break
|
||||||
default:
|
|
||||||
idempotencyKey = await idempotencyKeyService.update(
|
|
||||||
idempotencyKey.idempotency_key,
|
|
||||||
{
|
|
||||||
recovery_point: "finished",
|
|
||||||
response_code: 500,
|
|
||||||
response_body: { message: "Unknown recovery point" },
|
|
||||||
}
|
|
||||||
)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (err) {
|
case "finished": {
|
||||||
throw err
|
inProgress = false
|
||||||
}
|
break
|
||||||
|
}
|
||||||
|
|
||||||
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
default:
|
||||||
} catch (error) {
|
idempotencyKey = await idempotencyKeyService.update(
|
||||||
throw error
|
idempotencyKey.idempotency_key,
|
||||||
|
{
|
||||||
|
recovery_point: "finished",
|
||||||
|
response_code: 500,
|
||||||
|
response_body: { message: "Unknown recovery point" },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,74 +70,70 @@ export default async (req, res) => {
|
|||||||
user_agent: req.get("user-agent"),
|
user_agent: req.get("user-agent"),
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
const entityManager = req.scope.resolve("manager")
|
const entityManager = req.scope.resolve("manager")
|
||||||
|
|
||||||
await entityManager.transaction(async (manager) => {
|
await entityManager.transaction(async (manager) => {
|
||||||
// Add a default region if no region has been specified
|
// Add a default region if no region has been specified
|
||||||
let regionId = value.region_id
|
let regionId = value.region_id
|
||||||
if (!value.region_id) {
|
if (!value.region_id) {
|
||||||
const regionService = req.scope.resolve("regionService")
|
const regionService = req.scope.resolve("regionService")
|
||||||
const regions = await regionService.withTransaction(manager).list({})
|
const regions = await regionService.withTransaction(manager).list({})
|
||||||
|
|
||||||
if (!regions?.length) {
|
if (!regions?.length) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.INVALID_DATA,
|
MedusaError.Types.INVALID_DATA,
|
||||||
`A region is required to create a cart`
|
`A region is required to create a cart`
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
regionId = regions[0].id
|
|
||||||
}
|
|
||||||
|
|
||||||
const toCreate = {
|
|
||||||
region_id: regionId,
|
|
||||||
context: {
|
|
||||||
...reqContext,
|
|
||||||
...value.context,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.user && req.user.customer_id) {
|
|
||||||
const customerService = req.scope.resolve("customerService")
|
|
||||||
const customer = await customerService
|
|
||||||
.withTransaction(manager)
|
|
||||||
.retrieve(req.user.customer_id)
|
|
||||||
toCreate.customer_id = customer.id
|
|
||||||
toCreate.email = customer.email
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.country_code) {
|
|
||||||
toCreate.shipping_address = {
|
|
||||||
country_code: value.country_code.toLowerCase(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let cart = await cartService.withTransaction(manager).create(toCreate)
|
|
||||||
if (value.items) {
|
|
||||||
await Promise.all(
|
|
||||||
value.items.map(async (i) => {
|
|
||||||
await lineItemService.withTransaction(manager).create({
|
|
||||||
cart_id: cart.id,
|
|
||||||
variant_id: i.variant_id,
|
|
||||||
quantity: i.quantity,
|
|
||||||
region_id: value.region_id,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
cart = await cartService.withTransaction(manager).retrieve(cart.id, {
|
regionId = regions[0].id
|
||||||
select: defaultFields,
|
}
|
||||||
relations: defaultRelations,
|
|
||||||
})
|
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
const toCreate = {
|
||||||
|
region_id: regionId,
|
||||||
|
context: {
|
||||||
|
...reqContext,
|
||||||
|
...value.context,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.user && req.user.customer_id) {
|
||||||
|
const customerService = req.scope.resolve("customerService")
|
||||||
|
const customer = await customerService
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(req.user.customer_id)
|
||||||
|
toCreate.customer_id = customer.id
|
||||||
|
toCreate.email = customer.email
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.country_code) {
|
||||||
|
toCreate.shipping_address = {
|
||||||
|
country_code: value.country_code.toLowerCase(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cart = await cartService.withTransaction(manager).create(toCreate)
|
||||||
|
if (value.items) {
|
||||||
|
await Promise.all(
|
||||||
|
value.items.map(async (i) => {
|
||||||
|
await lineItemService.withTransaction(manager).create({
|
||||||
|
cart_id: cart.id,
|
||||||
|
variant_id: i.variant_id,
|
||||||
|
quantity: i.quantity,
|
||||||
|
region_id: value.region_id,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
cart = await cartService.withTransaction(manager).retrieve(cart.id, {
|
||||||
|
select: defaultFields,
|
||||||
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
|
||||||
throw err
|
res.status(200).json({ cart })
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,38 +38,34 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const manager = req.scope.resolve("manager")
|
||||||
const manager = req.scope.resolve("manager")
|
const lineItemService = req.scope.resolve("lineItemService")
|
||||||
const lineItemService = req.scope.resolve("lineItemService")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
const txCartService = cartService.withTransaction(m)
|
const txCartService = cartService.withTransaction(m)
|
||||||
const cart = await txCartService.retrieve(id)
|
const cart = await txCartService.retrieve(id)
|
||||||
|
|
||||||
const line = await lineItemService
|
const line = await lineItemService
|
||||||
.withTransaction(m)
|
.withTransaction(m)
|
||||||
.generate(value.variant_id, cart.region_id, value.quantity, {
|
.generate(value.variant_id, cart.region_id, value.quantity, {
|
||||||
metadata: value.metadata,
|
metadata: value.metadata,
|
||||||
})
|
|
||||||
await txCartService.addLineItem(id, line)
|
|
||||||
|
|
||||||
const updated = await txCartService.retrieve(id, {
|
|
||||||
relations: ["payment_sessions"],
|
|
||||||
})
|
})
|
||||||
|
await txCartService.addLineItem(id, line)
|
||||||
|
|
||||||
if (updated.payment_sessions?.length) {
|
const updated = await txCartService.retrieve(id, {
|
||||||
await txCartService.setPaymentSessions(id)
|
relations: ["payment_sessions"],
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
if (updated.payment_sessions?.length) {
|
||||||
select: defaultFields,
|
await txCartService.setPaymentSessions(id)
|
||||||
relations: defaultRelations,
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
const cart = await cartService.retrieve(id, {
|
||||||
} catch (err) {
|
select: defaultFields,
|
||||||
throw err
|
relations: defaultRelations,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
res.status(200).json({ cart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,18 +22,14 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await cartService.setPaymentSessions(id)
|
await cartService.setPaymentSessions(id)
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
const cart = await cartService.retrieve(id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,31 +23,27 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id, code } = req.params
|
const { id, code } = req.params
|
||||||
|
|
||||||
try {
|
const manager = req.scope.resolve("manager")
|
||||||
const manager = req.scope.resolve("manager")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
// Remove the discount
|
// Remove the discount
|
||||||
await cartService.withTransaction(m).removeDiscount(id, code)
|
await cartService.withTransaction(m).removeDiscount(id, code)
|
||||||
|
|
||||||
// If the cart has payment sessions update these
|
// If the cart has payment sessions update these
|
||||||
const updated = await cartService.withTransaction(m).retrieve(id, {
|
const updated = await cartService.withTransaction(m).retrieve(id, {
|
||||||
relations: ["payment_sessions"],
|
relations: ["payment_sessions"],
|
||||||
})
|
|
||||||
|
|
||||||
if (updated.payment_sessions?.length) {
|
|
||||||
await cartService.withTransaction(m).setPaymentSessions(id)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
if (updated.payment_sessions?.length) {
|
||||||
select: defaultFields,
|
await cartService.withTransaction(m).setPaymentSessions(id)
|
||||||
relations: defaultRelations,
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
const cart = await cartService.retrieve(id, {
|
||||||
} catch (err) {
|
select: defaultFields,
|
||||||
throw err
|
relations: defaultRelations,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
res.status(200).json({ cart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,31 +23,27 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id, line_id } = req.params
|
const { id, line_id } = req.params
|
||||||
|
|
||||||
try {
|
const manager = req.scope.resolve("manager")
|
||||||
const manager = req.scope.resolve("manager")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
// Remove the line item
|
// Remove the line item
|
||||||
await cartService.withTransaction(m).removeLineItem(id, line_id)
|
await cartService.withTransaction(m).removeLineItem(id, line_id)
|
||||||
|
|
||||||
// If the cart has payment sessions update these
|
// If the cart has payment sessions update these
|
||||||
const updated = await cartService.withTransaction(m).retrieve(id, {
|
const updated = await cartService.withTransaction(m).retrieve(id, {
|
||||||
relations: ["payment_sessions"],
|
relations: ["payment_sessions"],
|
||||||
})
|
|
||||||
|
|
||||||
if (updated.payment_sessions?.length) {
|
|
||||||
await cartService.withTransaction(m).setPaymentSessions(id)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
if (updated.payment_sessions?.length) {
|
||||||
select: defaultFields,
|
await cartService.withTransaction(m).setPaymentSessions(id)
|
||||||
relations: defaultRelations,
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
const cart = await cartService.retrieve(id, {
|
||||||
} catch (err) {
|
select: defaultFields,
|
||||||
throw err
|
relations: defaultRelations,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
res.status(200).json({ cart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,17 +23,13 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id, provider_id } = req.params
|
const { id, provider_id } = req.params
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await cartService.deletePaymentSession(id, provider_id)
|
await cartService.deletePaymentSession(id, provider_id)
|
||||||
const cart = await cartService.retrieve(id, {
|
const cart = await cartService.retrieve(id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,33 +22,29 @@ import { defaultFields, defaultRelations } from "./"
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
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, {
|
||||||
relations: ["customer"],
|
relations: ["customer"],
|
||||||
})
|
})
|
||||||
|
|
||||||
// If there is a logged in user add the user to the cart
|
// If there is a logged in user add the user to the cart
|
||||||
if (req.user && req.user.customer_id) {
|
if (req.user && req.user.customer_id) {
|
||||||
if (
|
if (
|
||||||
!cart.customer_id ||
|
!cart.customer_id ||
|
||||||
!cart.email ||
|
!cart.email ||
|
||||||
cart.customer_id !== req.user.customer_id
|
cart.customer_id !== req.user.customer_id
|
||||||
) {
|
) {
|
||||||
await cartService.update(id, {
|
await cartService.update(id, {
|
||||||
customer_id: req.user.customer_id,
|
customer_id: req.user.customer_id,
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cart = await cartService.retrieve(id, {
|
|
||||||
select: defaultFields,
|
|
||||||
relations: defaultRelations,
|
|
||||||
})
|
|
||||||
|
|
||||||
res.json({ cart })
|
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cart = await cartService.retrieve(id, {
|
||||||
|
select: defaultFields,
|
||||||
|
relations: defaultRelations,
|
||||||
|
})
|
||||||
|
|
||||||
|
res.json({ cart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,30 +21,26 @@
|
|||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id, provider_id } = req.params
|
const { id, provider_id } = req.params
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await cartService.refreshPaymentSession(id, provider_id)
|
await cartService.refreshPaymentSession(id, provider_id)
|
||||||
const cart = await cartService.retrieve(id, {
|
const cart = await cartService.retrieve(id, {
|
||||||
select: [
|
select: [
|
||||||
"subtotal",
|
"subtotal",
|
||||||
"tax_total",
|
"tax_total",
|
||||||
"shipping_total",
|
"shipping_total",
|
||||||
"discount_total",
|
"discount_total",
|
||||||
"total",
|
"total",
|
||||||
],
|
],
|
||||||
relations: [
|
relations: [
|
||||||
"region",
|
"region",
|
||||||
"region.countries",
|
"region.countries",
|
||||||
"region.payment_providers",
|
"region.payment_providers",
|
||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"payment_sessions",
|
"payment_sessions",
|
||||||
"shipping_methods.shipping_option",
|
"shipping_methods.shipping_option",
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,17 +33,13 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
let cart = await cartService.setPaymentSession(id, value.provider_id)
|
let cart = await cartService.setPaymentSession(id, value.provider_id)
|
||||||
cart = await cartService.retrieve(id, {
|
cart = await cartService.retrieve(id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import _ from "lodash"
|
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
import { defaultFields, defaultRelations } from "./"
|
import { defaultFields, defaultRelations } from "./"
|
||||||
|
|||||||
@@ -34,53 +34,49 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const manager = req.scope.resolve("manager")
|
||||||
const manager = req.scope.resolve("manager")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
// If the quantity is 0 that is effectively deletion
|
// If the quantity is 0 that is effectively deletion
|
||||||
if (value.quantity === 0) {
|
if (value.quantity === 0) {
|
||||||
await cartService.withTransaction(m).removeLineItem(id, line_id)
|
await cartService.withTransaction(m).removeLineItem(id, line_id)
|
||||||
} else {
|
} else {
|
||||||
const cart = await cartService.retrieve(id, { relations: ["items"] })
|
const cart = await cartService.retrieve(id, { relations: ["items"] })
|
||||||
|
|
||||||
const existing = cart.items.find((i) => i.id === line_id)
|
const existing = cart.items.find((i) => i.id === line_id)
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.INVALID_DATA,
|
MedusaError.Types.INVALID_DATA,
|
||||||
"Could not find the line item"
|
"Could not find the line item"
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const lineItemUpdate = {
|
|
||||||
variant_id: existing.variant.id,
|
|
||||||
region_id: cart.region_id,
|
|
||||||
quantity: value.quantity,
|
|
||||||
metadata: existing.metadata || {},
|
|
||||||
}
|
|
||||||
|
|
||||||
await cartService
|
|
||||||
.withTransaction(m)
|
|
||||||
.updateLineItem(id, line_id, lineItemUpdate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the cart has payment sessions update these
|
const lineItemUpdate = {
|
||||||
const updated = await cartService.withTransaction(m).retrieve(id, {
|
variant_id: existing.variant.id,
|
||||||
relations: ["payment_sessions"],
|
region_id: cart.region_id,
|
||||||
})
|
quantity: value.quantity,
|
||||||
|
metadata: existing.metadata || {},
|
||||||
if (updated.payment_sessions?.length) {
|
|
||||||
await cartService.withTransaction(m).setPaymentSessions(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await cartService
|
||||||
|
.withTransaction(m)
|
||||||
|
.updateLineItem(id, line_id, lineItemUpdate)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cart has payment sessions update these
|
||||||
|
const updated = await cartService.withTransaction(m).retrieve(id, {
|
||||||
|
relations: ["payment_sessions"],
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
if (updated.payment_sessions?.length) {
|
||||||
select: defaultFields,
|
await cartService.withTransaction(m).setPaymentSessions(id)
|
||||||
relations: defaultRelations,
|
}
|
||||||
})
|
})
|
||||||
res.status(200).json({ cart })
|
|
||||||
} catch (err) {
|
const cart = await cartService.retrieve(id, {
|
||||||
throw err
|
select: defaultFields,
|
||||||
}
|
relations: defaultRelations,
|
||||||
|
})
|
||||||
|
res.status(200).json({ cart })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,13 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
let cart = await cartService.setPaymentMethod(id, value)
|
let cart = await cartService.setPaymentMethod(id, value)
|
||||||
cart = await cartService.retrieve(id, {
|
cart = await cartService.retrieve(id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,19 +35,15 @@ export default async (req, res) => {
|
|||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const cartService = req.scope.resolve("cartService")
|
||||||
const cartService = req.scope.resolve("cartService")
|
|
||||||
|
|
||||||
await cartService.setPaymentSession(id, provider_id)
|
await cartService.setPaymentSession(id, provider_id)
|
||||||
await cartService.updatePaymentSession(id, value.data)
|
await cartService.updatePaymentSession(id, value.data)
|
||||||
|
|
||||||
const cart = await cartService.retrieve(id, {
|
const cart = await cartService.retrieve(id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ cart })
|
res.status(200).json({ cart })
|
||||||
} catch (err) {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user