fix(medusa): migrate cart service to typescript (#884)
* fix: migrate cart service to typescript * fix: jsdoc inventory service * fix: revert route unit test change * fix: typo * fix: revert integration test packages * fix: cleanup * fix: tests * fix: integration tests * fix: create props type guards * fix: move total field to common types
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Router } from "express"
|
||||
import { DraftOrder, Order } from "../../../.."
|
||||
import { DraftOrder, Order, Cart } from "../../../.."
|
||||
import middlewares from "../../../middlewares"
|
||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||
|
||||
@@ -62,7 +62,7 @@ export const defaultAdminDraftOrdersCartRelations = [
|
||||
"discounts.rule",
|
||||
]
|
||||
|
||||
export const defaultAdminDraftOrdersCartFields = [
|
||||
export const defaultAdminDraftOrdersCartFields: (keyof Cart)[] = [
|
||||
"subtotal",
|
||||
"tax_total",
|
||||
"shipping_total",
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
defaultAdminDraftOrdersFields,
|
||||
} from "."
|
||||
import { DraftOrder } from "../../../.."
|
||||
import { LineItemUpdate } from "../../../../types/cart"
|
||||
import { CartService, DraftOrderService } from "../../../../services"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
/**
|
||||
@@ -112,15 +113,6 @@ export default async (req, res) => {
|
||||
})
|
||||
}
|
||||
|
||||
class LineItemUpdate {
|
||||
title?: string
|
||||
unit_price?: number
|
||||
quantity?: number
|
||||
metadata?: object = {}
|
||||
region_id?: string
|
||||
variant_id?: string
|
||||
}
|
||||
|
||||
export class AdminPostDraftOrdersDraftOrderLineItemsItemReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
SwapService,
|
||||
} from "../../../../services"
|
||||
|
||||
import { Order } from "../../../../models/order"
|
||||
|
||||
/**
|
||||
* @oas [post] /carts/{id}/complete
|
||||
* summary: "Complete a Cart"
|
||||
@@ -144,7 +146,7 @@ export default async (req, res) => {
|
||||
relations: ["payment", "payment_sessions"],
|
||||
})
|
||||
|
||||
let order
|
||||
let order: Order
|
||||
|
||||
// If cart is part of swap, we register swap as complete
|
||||
switch (cart.type) {
|
||||
@@ -183,6 +185,15 @@ export default async (req, res) => {
|
||||
}
|
||||
// case "payment_link":
|
||||
default: {
|
||||
if (typeof cart.total === "undefined") {
|
||||
return {
|
||||
response_code: 500,
|
||||
response_body: {
|
||||
message: "Unexpected state",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (!cart.payment && cart.total > 0) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
|
||||
@@ -10,9 +10,11 @@ import {
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import reqIp from "request-ip"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService, LineItemService } from "../../../../services"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
|
||||
/**
|
||||
* @oas [post] /carts
|
||||
@@ -74,8 +76,11 @@ export default async (req, res) => {
|
||||
|
||||
await entityManager.transaction(async (manager) => {
|
||||
// Add a default region if no region has been specified
|
||||
let regionId = validated.region_id
|
||||
if (!validated.region_id) {
|
||||
let regionId: string
|
||||
|
||||
if (typeof validated.region_id !== "undefined") {
|
||||
regionId = validated.region_id
|
||||
} else {
|
||||
const regionService = req.scope.resolve("regionService")
|
||||
const regions = await regionService.withTransaction(manager).list({})
|
||||
|
||||
@@ -90,11 +95,11 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
const toCreate: {
|
||||
region_id: string | undefined
|
||||
region_id: string
|
||||
context: object
|
||||
customer_id?: string
|
||||
email?: string
|
||||
shipping_address?: object
|
||||
shipping_address?: Partial<AddressPayload>
|
||||
} = {
|
||||
region_id: regionId,
|
||||
context: {
|
||||
@@ -117,11 +122,11 @@ export default async (req, res) => {
|
||||
country_code: validated.country_code.toLowerCase(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let cart = await cartService.withTransaction(manager).create(toCreate)
|
||||
if (validated.items) {
|
||||
await Promise.all(
|
||||
validated.items.map(async i => {
|
||||
validated.items.map(async (i) => {
|
||||
const lineItem = await lineItemService
|
||||
.withTransaction(manager)
|
||||
.generate(i.variant_id, regionId, i.quantity)
|
||||
|
||||
@@ -97,7 +97,7 @@ export default (app, container) => {
|
||||
return app
|
||||
}
|
||||
|
||||
export const defaultStoreCartFields = [
|
||||
export const defaultStoreCartFields: (keyof Cart)[] = [
|
||||
"subtotal",
|
||||
"tax_total",
|
||||
"shipping_total",
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { defaultStoreCartFields, defaultStoreCartRelations } from "."
|
||||
import { CartService } from "../../../../services"
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { CartUpdateProps } from "../../../../types/cart"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
|
||||
@@ -87,7 +88,25 @@ export default async (req, res) => {
|
||||
const cartService: CartService = req.scope.resolve("cartService")
|
||||
|
||||
// Update the cart
|
||||
await cartService.update(id, validated)
|
||||
const { shipping_address, billing_address, ...rest } = validated
|
||||
|
||||
const toUpdate: CartUpdateProps = {
|
||||
...rest,
|
||||
}
|
||||
|
||||
if (typeof shipping_address === "string") {
|
||||
toUpdate.shipping_address_id = shipping_address
|
||||
} else {
|
||||
toUpdate.shipping_address = shipping_address
|
||||
}
|
||||
|
||||
if (typeof billing_address === "string") {
|
||||
toUpdate.billing_address_id = billing_address
|
||||
} else {
|
||||
toUpdate.billing_address = billing_address
|
||||
}
|
||||
|
||||
await cartService.update(id, toUpdate)
|
||||
|
||||
// If the cart has payment sessions update these
|
||||
const updated = await cartService.retrieve(id, {
|
||||
|
||||
Reference in New Issue
Block a user