Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: draft order api #6797

Merged
merged 50 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
4a88be5
wip
carlos-r-l-rodrigues Mar 13, 2024
94208a9
wip
carlos-r-l-rodrigues Mar 14, 2024
7392c50
wip
carlos-r-l-rodrigues Mar 15, 2024
9186283
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 15, 2024
b6ab87d
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 18, 2024
01355ef
wip
carlos-r-l-rodrigues Mar 19, 2024
492d00f
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 19, 2024
b67d5d6
wip
carlos-r-l-rodrigues Mar 20, 2024
60e4970
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 20, 2024
11fd760
wip
carlos-r-l-rodrigues Mar 20, 2024
a41b6ea
wip
carlos-r-l-rodrigues Mar 20, 2024
e47322c
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 21, 2024
4802899
wip
carlos-r-l-rodrigues Mar 21, 2024
795dc68
migration and links
carlos-r-l-rodrigues Mar 22, 2024
4c5422c
migration
carlos-r-l-rodrigues Mar 22, 2024
cbd314a
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 22, 2024
2c00b16
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 22, 2024
f2c7af2
rename flows
carlos-r-l-rodrigues Mar 22, 2024
6b01e43
check already registered workflows
carlos-r-l-rodrigues Mar 22, 2024
9fe7699
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 22, 2024
24c8d66
rename old flows
carlos-r-l-rodrigues Mar 22, 2024
ff4aa6f
tests
carlos-r-l-rodrigues Mar 22, 2024
2f19cd1
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 25, 2024
3b75a3a
endpoints
carlos-r-l-rodrigues Mar 25, 2024
b48bb45
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 25, 2024
b26d478
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 25, 2024
c514ecc
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 25, 2024
b5006c1
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 25, 2024
9ab3f03
rm remove methods
carlos-r-l-rodrigues Mar 26, 2024
83ef695
types
carlos-r-l-rodrigues Mar 26, 2024
cd6fb61
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 27, 2024
0b7f889
tests and calculated price
carlos-r-l-rodrigues Mar 27, 2024
2384b70
output type
carlos-r-l-rodrigues Mar 27, 2024
c147de8
serializer
carlos-r-l-rodrigues Mar 27, 2024
231f2a8
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 27, 2024
1dd381e
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Mar 27, 2024
c4f31fe
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Mar 29, 2024
b3371cc
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Apr 4, 2024
9e82ebd
is_draft_order flag
carlos-r-l-rodrigues Apr 4, 2024
cfa10ee
default fields
carlos-r-l-rodrigues Apr 4, 2024
2536ac1
defaults
carlos-r-l-rodrigues Apr 4, 2024
d61436d
OperatorMap
carlos-r-l-rodrigues Apr 4, 2024
60eeb25
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Apr 4, 2024
e761b63
type
carlos-r-l-rodrigues Apr 5, 2024
907f845
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Apr 5, 2024
6417c36
merge
carlos-r-l-rodrigues Apr 5, 2024
1df2885
Merge branch 'develop' of https://github.com/medusajs/medusa into fea…
carlos-r-l-rodrigues Apr 5, 2024
f21c570
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Apr 5, 2024
2245fae
joiner config
carlos-r-l-rodrigues Apr 5, 2024
8dea6d7
Merge branch 'develop' into feat/draft-order-api
carlos-r-l-rodrigues Apr 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 253 additions & 0 deletions integration-tests/modules/__tests__/order/draft-order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
IFulfillmentModuleService,
IInventoryServiceNext,
IPaymentModuleService,
IPricingModuleService,
IProductModuleService,
IRegionModuleService,
ISalesChannelModuleService,
IStockLocationServiceNext,
ITaxModuleService,
} from "@medusajs/types"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
createAdminUser,
} from "../../../helpers/create-admin-user"
import { setupTaxStructure } from "../fixtures"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }

medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
let appContainer
let cartModuleService: ICartModuleService
let regionModuleService: IRegionModuleService
let scModuleService: ISalesChannelModuleService
let customerModule: ICustomerModuleService
let productModule: IProductModuleService
let pricingModule: IPricingModuleService
let paymentModule: IPaymentModuleService
let inventoryModule: IInventoryServiceNext
let stockLocationModule: IStockLocationServiceNext
let fulfillmentModule: IFulfillmentModuleService
let locationModule: IStockLocationServiceNext
let taxModule: ITaxModuleService
let remoteLink, remoteQuery

let defaultRegion

beforeAll(async () => {
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModuleService = appContainer.resolve(ModuleRegistrationName.REGION)
scModuleService = appContainer.resolve(
ModuleRegistrationName.SALES_CHANNEL
)
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
productModule = appContainer.resolve(ModuleRegistrationName.PRODUCT)
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING)
paymentModule = appContainer.resolve(ModuleRegistrationName.PAYMENT)
inventoryModule = appContainer.resolve(ModuleRegistrationName.INVENTORY)
stockLocationModule = appContainer.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
locationModule = appContainer.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
taxModule = appContainer.resolve(ModuleRegistrationName.TAX)
remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
remoteQuery = appContainer.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
})

beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)

// Here, so we don't have to create a region for each test
defaultRegion = await regionModuleService.create({
name: "Default Region",
currency_code: "dkk",
})
})

describe("Draft Orders - Admin", () => {
it("should create a draft order", async () => {
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})

const salesChannel = await scModuleService.create({
name: "Webshop",
})

const location = await stockLocationModule.create({
name: "Warehouse",
})

const [product, product_2] = await productModule.create([
{
title: "Test product",
variants: [
{
title: "Test variant",
},
],
},
{
title: "Another product",
variants: [
{
title: "Variant variable",
manage_inventory: false,
},
],
},
])

const inventoryItem = await inventoryModule.create({
sku: "inv-1234",
})

await inventoryModule.createInventoryLevels([
{
inventory_item_id: inventoryItem.id,
location_id: location.id,
stocked_quantity: 2,
reserved_quantity: 0,
},
])

const [priceSet, priceSet_2] = await pricingModule.create([
{
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
},
{
prices: [
{
amount: 1000,
currency_code: "usd",
},
],
},
])

await remoteLink.create([
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.PRICING]: {
price_set_id: priceSet.id,
},
},
{
[Modules.PRODUCT]: {
variant_id: product_2.variants[0].id,
},
[Modules.PRICING]: {
price_set_id: priceSet_2.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
},
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.INVENTORY]: {
inventory_item_id: inventoryItem.id,
},
},
])

await setupTaxStructure(taxModule)

const payload = {
email: "[email protected]",
region_id: region.id,
shipping_address: {
first_name: "Test",
last_name: "Test",
address_1: "Test",
city: "Test",
country_code: "US",
postal_code: "12345",
phone: "12345",
},
billing_address: {
first_name: "Test",
last_name: "Test",
address_1: "Test",
city: "Test",
country_code: "US",
postal_code: "12345",
},
discounts: [{ code: "testytest" }],
items: [
/*
{
variant_id: product.variants[0].id,
quantity: 2,
metadata: {},
},
*/
{
variant_id: product_2.variants[0].id,
unit_price: 200,
quantity: 1,
metadata: {
note: "reduced price",
},
},
{
title: "Custom Item",
sku: "sku123",
barcode: "barcode123",
unit_price: 3000,
quantity: 1,
metadata: {},
},
],
currency_code: "USD",
shipping_methods: [
{
name: "test-method",
option_id: "test-option",
amount: 100,
},
],
}

const response = await api.post(
"/admin/draft-orders",
payload,
adminHeaders
)

expect(response.status).toEqual(200)
carlos-r-l-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
})
})
},
})
1 change: 1 addition & 0 deletions integration-tests/modules/medusa-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
[Modules.TAX]: true,
[Modules.CURRENCY]: true,
[Modules.PAYMENT]: true,
[Modules.ORDER]: true,
[Modules.FULFILLMENT]: {
/** @type {import('@medusajs/fulfillment').FulfillmentModuleOptions} */
options: {
Expand Down
2 changes: 2 additions & 0 deletions packages/core-flows/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./steps/remove-remote-links"
export * from "./steps/use-remote-query"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CreateShippingMethodDTO, ICartModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "../../../../../modules-sdk/dist"

interface StepInput {
shipping_methods: CreateShippingMethodDTO[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CreateLineItemForCartDTO, ICartModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "../../../../../modules-sdk/dist"

interface StepInput {
items: CreateLineItemForCartDTO[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IInventoryService } from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { MedusaError } from "medusa-core-utils"
import { ModuleRegistrationName } from "../../../../../modules-sdk/dist"

interface StepInput {
items: {
Expand All @@ -22,10 +22,10 @@ export const confirmInventoryStep = createStep(
)

// TODO: Should be bulk
const promises = data.items.map((item) => {
const promises = data.items.map(async (item) => {
const itemQuantity = item.required_quantity * item.quantity

return inventoryService.confirmInventory(
return await inventoryService.confirmInventory(
carlos-r-l-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
item.inventory_item_id,
item.location_ids,
itemQuantity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CartLineItemDTO,
CartShippingMethodDTO,
Expand All @@ -11,7 +12,6 @@ import {
} from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "../../../../../modules-sdk/dist"

interface StepInput {
cart: CartWorkflowDTO
Expand Down Expand Up @@ -42,7 +42,7 @@ function normalizeTaxModuleContext(
return null
}

let customer = cart.customer
const customer = cart.customer
? {
id: cart.customer.id,
email: cart.customer.email,
Expand Down Expand Up @@ -104,6 +104,7 @@ export const getItemTaxLinesStep = createStep(
shipping_methods: shippingMethods,
force_tax_calculation: forceTaxCalculation = false,
} = data

const taxService = container.resolve<ITaxModuleService>(
ModuleRegistrationName.TAX
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface StepInput {
export const updateTaxLinesStepId = "update-tax-lines-step"
export const updateTaxLinesStep = createStep(
updateTaxLinesStepId,
async (input: StepInput, { container, idempotencyKey }) => {
async (input: StepInput, { container }) => {
const { transaction } = await updateTaxLinesWorkflow(container).run({
input,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigNumberInput } from "@medusajs/types"
import { MedusaError } from "medusa-core-utils"

interface ConfirmInventoryPreparationInput {
Expand All @@ -6,7 +7,7 @@ interface ConfirmInventoryPreparationInput {
inventory_item_id: string
required_quantity: number
}[]
items: { variant_id?: string; quantity: number }[]
items: { variant_id?: string; quantity: BigNumberInput }[]
variants: { id: string; manage_inventory?: boolean }[]
location_ids: string[]
}
Expand Down Expand Up @@ -36,6 +37,12 @@ export const prepareConfirmInventoryInput = ({
const itemsToConfirm: ConfirmInventoryItem[] = []

items.forEach((item) => {
const variant = variantsMap.get(item.variant_id!)

if (!variant?.manage_inventory) {
return
}

const variantInventoryItem = product_variant_inventory_items.find(
(i) => i.variant_id === item.variant_id
)
Expand All @@ -47,16 +54,12 @@ export const prepareConfirmInventoryInput = ({
)
}

const variant = variantsMap.get(item.variant_id!)

if (variant?.manage_inventory) {
itemsToConfirm.push({
inventory_item_id: variantInventoryItem.inventory_item_id,
required_quantity: variantInventoryItem.required_quantity,
quantity: item.quantity,
location_ids: location_ids,
})
}
itemsToConfirm.push({
inventory_item_id: variantInventoryItem.inventory_item_id,
required_quantity: variantInventoryItem.required_quantity,
quantity: item.quantity as number, // TODO: update type to BigNumberInput
location_ids: location_ids,
})
})

return itemsToConfirm
Expand Down
Loading
Loading