chore(core-flows): adjust inventory when return is received (#8222)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-23 13:36:46 -03:00
committed by GitHub
parent f38f6d53b4
commit 7a5349c0ae
20 changed files with 579 additions and 286 deletions

View File

@@ -1,4 +1,9 @@
import { ModuleRegistrationName, RuleOperator } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
ModuleRegistrationName,
Modules,
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
adminHeaders,
@@ -14,11 +19,35 @@ medusaIntegrationTestRunner({
let shippingProfile
let fulfillmentSet
let returnReason
let inventoryItem
let location
beforeEach(async () => {
const container = getContainer()
await createAdminUser(dbConnection, adminHeaders, container)
const product = (
await api.post(
"/admin/products",
{
title: "Test product",
variants: [
{
title: "Test variant",
sku: "test-variant",
prices: [
{
currency_code: "usd",
amount: 10,
},
],
},
],
},
adminHeaders
)
).data.product
returnReason = (
await api.post(
"/admin/return-reasons",
@@ -39,6 +68,7 @@ medusaIntegrationTestRunner({
items: [
{
title: "Custom Item 2",
variant_id: product.variants[0].id,
quantity: 2,
unit_price: 25,
},
@@ -123,7 +153,7 @@ medusaIntegrationTestRunner({
)
).data.shipping_profile
let location = (
location = (
await api.post(
`/admin/stock-locations`,
{
@@ -155,6 +185,54 @@ medusaIntegrationTestRunner({
)
).data.fulfillment_set
inventoryItem = (
await api.post(
`/admin/inventory-items`,
{ sku: "inv-1234" },
adminHeaders
)
).data.inventory_item
await api.post(
`/admin/inventory-items/${inventoryItem.id}/location-levels`,
{
location_id: location.id,
stocked_quantity: 2,
},
adminHeaders
)
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
[Modules.FULFILLMENT]: {
fulfillment_set_id: fulfillmentSet.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test",
},
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
},
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.INVENTORY]: {
inventory_item_id: inventoryItem.id,
},
},
])
const shippingOptionPayload = {
name: "Return shipping",
service_zone_id: fulfillmentSet.service_zones[0].id,
@@ -715,6 +793,7 @@ medusaIntegrationTestRunner({
{
order_id: order.id,
description: "Test",
location_id: location.id,
},
adminHeaders
)
@@ -745,6 +824,14 @@ medusaIntegrationTestRunner({
})
it("should receive the return", async () => {
let inventoryLevel = (
await api.get(
`/admin/inventory-items/${inventoryItem.id}/location-levels?location_id[]=${location.id}`,
adminHeaders
)
).data.inventory_levels
expect(inventoryLevel[0].stocked_quantity).toEqual(2)
let result = await api.post(
`/admin/returns/${returnId}/receive`,
{
@@ -838,6 +925,14 @@ medusaIntegrationTestRunner({
],
})
)
inventoryLevel = (
await api.get(
`/admin/inventory-items/${inventoryItem.id}/location-levels?location_id[]=${location.id}`,
adminHeaders
)
).data.inventory_levels
expect(inventoryLevel[0].stocked_quantity).toEqual(3)
})
})
})

View File

@@ -27,6 +27,7 @@ jest.setTimeout(500000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const providerId = "manual_test-provider"
const variantSkuWithInventory = "test-variant"
let inventoryItem
async function prepareDataFixtures({ container }) {
@@ -97,7 +98,7 @@ async function prepareDataFixtures({ container }) {
variants: [
{
title: "Test variant",
sku: "test-variant",
sku: variantSkuWithInventory,
},
{
title: "Test variant no inventory management",
@@ -225,6 +226,7 @@ async function createOrderFixture({ container, product, location }) {
title: "Custom Item 2",
variant_sku: product.variants[0].sku,
variant_title: product.variants[0].title,
variant_id: product.variants[0].id,
quantity: 1,
unit_price: 50,
adjustments: [
@@ -351,6 +353,9 @@ medusaIntegrationTestRunner({
)
const order = await createOrderFixture({ container, product, location })
const itemWithInventory = order.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
// Create a fulfillment
const createOrderFulfillmentData: OrderWorkflow.CreateOrderFulfillmentWorkflowInput =
@@ -359,7 +364,7 @@ medusaIntegrationTestRunner({
created_by: "user_1",
items: [
{
id: order.items![0].id,
id: itemWithInventory.id,
quantity: 1,
},
],
@@ -391,11 +396,17 @@ medusaIntegrationTestRunner({
const [orderFulfill] = await remoteQuery(remoteQueryObject)
let orderFulfillItemWithInventory = orderFulfill.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
expect(orderFulfill.fulfillments).toHaveLength(1)
expect(orderFulfill.items[0].detail.fulfilled_quantity).toEqual(1)
expect(orderFulfillItemWithInventory.detail.fulfilled_quantity).toEqual(
1
)
const reservation = await inventoryModule.listReservationItems({
line_item_id: order.items![0].id,
line_item_id: itemWithInventory.id,
})
expect(reservation).toHaveLength(0)
@@ -436,10 +447,14 @@ medusaIntegrationTestRunner({
remoteQueryObjectFulfill
)
orderFulfillItemWithInventory = orderFulfillAfterCancelled.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
expect(orderFulfillAfterCancelled.fulfillments).toHaveLength(1)
expect(
orderFulfillAfterCancelled.items[0].detail.fulfilled_quantity
).toEqual(0)
expect(orderFulfillItemWithInventory.detail.fulfilled_quantity).toEqual(
0
)
const stockAvailabilityAfterCancelled =
await inventoryModule.retrieveStockedQuantity(inventoryItem.id, [

View File

@@ -1,5 +1,6 @@
import { IInventoryService } from "@medusajs/types"
import {
MathBN,
MedusaError,
ModuleRegistrationName,
promiseAll,
@@ -30,7 +31,7 @@ export const confirmInventoryStep = createStep(
return true
}
const itemQuantity = item.required_quantity * item.quantity
const itemQuantity = MathBN.mult(item.quantity, item.required_quantity)
return await inventoryService.confirmInventory(
item.inventory_item_id,

View File

@@ -51,7 +51,11 @@ export const getLineItemActionsStep = createStep(
itemsToUpdate.push({
selector: { id: existingItem.id },
data: { id: existingItem.id, quantity: quantity },
data: {
id: existingItem.id,
quantity: quantity,
variant_id: item.variant_id!,
},
})
} else {
itemsToCreate.push(item)

View File

@@ -26,9 +26,6 @@ import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartStep } from "./refresh-payment-collection"
// TODO: The AddToCartWorkflow are missing the following steps:
// - Refresh/delete shipping methods (fulfillment module)
export const addToCartWorkflowId = "add-to-cart"
export const addToCartWorkflow = createWorkflow(
addToCartWorkflowId,
@@ -60,14 +57,6 @@ export const addToCartWorkflow = createWorkflow(
validateVariantPricesStep({ variants })
confirmVariantInventoryWorkflow.runAsStep({
input: {
sales_channel_id: input.cart.sales_channel_id as string,
variants,
items: input.items,
},
})
const lineItems = transform({ input, variants }, (data) => {
const items = (data.input.items ?? []).map((item) => {
const variant = data.variants.find((v) => v.id === item.variant_id)!
@@ -91,6 +80,15 @@ export const addToCartWorkflow = createWorkflow(
items: lineItems,
})
confirmVariantInventoryWorkflow.runAsStep({
input: {
sales_channel_id: input.cart.sales_channel_id as string,
variants,
items: input.items,
itemsToUpdate,
},
})
const [createdItems, updatedItems] = parallelize(
createLineItemsStep({
id: input.cart.id,

View File

@@ -34,6 +34,15 @@ export const confirmVariantInventoryWorkflow = createWorkflow(
const salesChannelId = data.input.sales_channel_id
for (const updateItem of data.input.itemsToUpdate ?? []) {
const item = data.input.items.find(
(item) => item.variant_id === updateItem.data.variant_id
)
if (item && updateItem.data.quantity) {
item.quantity = updateItem.data.quantity!
}
}
deepFlatMap(
data.input,
"variants.inventory_items.inventory.location_levels.stock_locations.sales_channels",

View File

@@ -89,7 +89,6 @@ function prepareInventoryUpdate({
location_id: string
adjustment: BigNumberInput
}[] = []
for (const item of fulfillment.items) {
// if this is `null` this means that item is from variant that has `manage_inventory` false
if (item.inventory_item_id) {

View File

@@ -35,6 +35,7 @@ export const beginReturnOrderWorkflow = createWorkflow(
const created = createReturnsStep([
{
order_id: input.order_id,
location_id: input.location_id,
metadata: input.metadata,
},
])

View File

@@ -1,17 +1,25 @@
import {
BigNumberInput,
OrderChangeActionDTO,
OrderChangeDTO,
OrderDTO,
ReturnDTO,
} from "@medusajs/types"
import { ChangeActionType, MathBN, OrderChangeStatus } from "@medusajs/utils"
import {
ChangeActionType,
MathBN,
OrderChangeStatus,
deepFlatMap,
} from "@medusajs/utils"
import {
WorkflowData,
createStep,
createWorkflow,
parallelize,
transform,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../../common"
import { adjustInventoryLevelsStep } from "../../../inventory/steps"
import { previewOrderChangeStep, updateReturnItemsStep } from "../../steps"
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
import {
@@ -40,13 +48,97 @@ const validationStep = createStep(
}
)
// Loop through the items in the return and prepare the inventory adjustment of items associated with each variant
function prepareInventoryUpdate({ orderReturn, returnedQuantityMap }) {
const inventoryAdjustment: {
inventory_item_id: string
location_id: string
adjustment: BigNumberInput
}[] = []
let hasManagedInventory = false
let hasStockLocation = false
const productVariantInventoryItems = new Map<string, any>()
// Create the map of inventory item ids associated with each variant that have inventory management
deepFlatMap(
orderReturn.items,
"item.variant.inventory_items.inventory.location_levels",
({ variant, inventory_items, location_levels }) => {
if (!variant?.manage_inventory) {
return
}
hasManagedInventory = true
if (location_levels?.location_id !== orderReturn.location_id) {
return
}
hasStockLocation = true
if (!inventory_items) {
return
}
const inventoryItemId = inventory_items.inventory_item_id
if (!productVariantInventoryItems.has(inventoryItemId)) {
productVariantInventoryItems.set(inventoryItemId, {
variant_id: inventory_items.variant_id,
inventory_item_id: inventoryItemId,
required_quantity: inventory_items.required_quantity,
})
}
}
)
if (hasManagedInventory && !hasStockLocation) {
throw new Error(
`Cannot receive the Return at location ${orderReturn.location_id}`
)
}
// Adjust the inventory of all inventory items of each variant in the return
for (const [variantId, quantity] of Object.entries(returnedQuantityMap)) {
const inventoryItemsByVariant = Array.from(
productVariantInventoryItems.values()
).filter((i) => i.variant_id === variantId)
for (const inventoryItem of inventoryItemsByVariant) {
inventoryAdjustment.push({
inventory_item_id: inventoryItem.inventory_item_id,
location_id: orderReturn.location_id,
adjustment: MathBN.mult(
quantity as number,
inventoryItem.required_quantity
),
})
}
}
return inventoryAdjustment
}
export const confirmReturnReceiveWorkflowId = "confirm-return-receive"
export const confirmReturnReceiveWorkflow = createWorkflow(
confirmReturnReceiveWorkflowId,
function (input: WorkflowInput): WorkflowData<OrderDTO> {
const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return",
fields: ["id", "status", "order_id", "canceled_at", "items.*"],
fields: [
"id",
"status",
"order_id",
"location_id",
"canceled_at",
"items.*",
"items.item.variant_id",
"items.item.variant.id",
"items.item.variant.manage_inventory",
"items.item.variant.inventory_items.inventory_item_id",
"items.item.variant.inventory_items.required_quantity",
"items.item.variant.inventory_items.inventory.location_levels.location_id",
],
variables: { id: input.return_id },
list: false,
throw_if_key_not_found: true,
@@ -81,45 +173,84 @@ export const confirmReturnReceiveWorkflow = createWorkflow(
list: false,
}).config({ name: "order-change-query" })
const updateReturnItem = transform({ orderChange, orderReturn }, (data) => {
const retItems = data.orderReturn.items!
const received = data.orderChange.actions.filter((act) =>
[
ChangeActionType.RECEIVE_RETURN_ITEM,
ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
].includes(act.action as ChangeActionType)
)
const { updateReturnItem, returnedQuantityMap } = transform(
{ orderChange, orderReturn },
(data) => {
const returnedQuantityMap: Record<string, BigNumberInput> = {}
const itemMap = retItems.reduce((acc, item: any) => {
acc[item.item_id] = item.id
return acc
}, {})
const retItems = data.orderReturn.items ?? []
const received: OrderChangeActionDTO[] = []
const itemUpdates = {}
received.forEach((act) => {
const itemId = act.details!.reference_id as string
if (itemUpdates[itemId]) {
itemUpdates[itemId].received_quantity = MathBN.add(
itemUpdates[itemId].received_quantity,
act.details!.quantity as BigNumberInput
)
return
data.orderChange.actions.forEach((act) => {
if (
[
ChangeActionType.RECEIVE_RETURN_ITEM,
ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
].includes(act.action as ChangeActionType)
) {
received.push(act)
if (act.action === ChangeActionType.RECEIVE_RETURN_ITEM) {
const itemId = act.details!.reference_id as string
const variantId = (retItems as any).find(
(i: any) => i.item_id === itemId
)?.item?.variant_id
if (!variantId) {
return
}
const currentQuantity = returnedQuantityMap[variantId] ?? 0
returnedQuantityMap[variantId] = MathBN.add(
currentQuantity,
act.details!.quantity as number
)
}
}
})
const itemMap = retItems.reduce((acc, item: any) => {
acc[item.item_id] = item.id
return acc
}, {})
const itemUpdates = {}
received.forEach((act) => {
const itemId = act.details!.reference_id as string
if (itemUpdates[itemId]) {
itemUpdates[itemId].received_quantity = MathBN.add(
itemUpdates[itemId].received_quantity,
act.details!.quantity as BigNumberInput
)
return
}
itemUpdates[itemId] = {
id: itemMap[itemId],
received_quantity: act.details!.quantity,
}
})
return {
updateReturnItem: Object.values(itemUpdates) as any,
returnedQuantityMap,
}
}
)
itemUpdates[itemId] = {
id: itemMap[itemId],
received_quantity: act.details!.quantity,
}
})
return Object.values(itemUpdates) as any
})
const inventoryAdjustment = transform(
{ orderReturn, input, returnedQuantityMap },
prepareInventoryUpdate
)
validationStep({ order, orderReturn, orderChange })
updateReturnItemsStep(updateReturnItem)
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
parallelize(
updateReturnItemsStep(updateReturnItem),
confirmOrderChanges({ changes: [orderChange], orderId: order.id }),
adjustInventoryLevelsStep(inventoryAdjustment)
)
return previewOrderChangeStep(order.id)
}

View File

@@ -149,6 +149,12 @@ export interface ConfirmVariantInventoryWorkflowInputDTO {
variant_id?: string
quantity: BigNumberInput
}[]
itemsToUpdate?: {
data: {
variant_id?: string
quantity?: BigNumberInput
}
}[]
}
export interface CartWorkflowDTO {

View File

@@ -421,6 +421,7 @@ export interface RegisterOrderShipmentDTO extends BaseOrderBundledActionsDTO {
}
export interface CreateOrderReturnDTO extends BaseOrderBundledActionsDTO {
location_id?: string
items?: {
id: string
quantity: BigNumberInput
@@ -438,6 +439,7 @@ export interface CreateOrderReturnDTO extends BaseOrderBundledActionsDTO {
export interface UpdateReturnDTO {
id: string
location_id?: string
refund_amount?: BigNumberInput
no_notification?: boolean
claim_id?: string

View File

@@ -1,5 +1,6 @@
export interface BeginOrderReturnWorkflowInput {
order_id: string
location_id?: string
created_by?: string
internal_note?: string
description?: string

View File

@@ -4,6 +4,7 @@ export const defaultAdminReturnFields = [
"exchange_id",
"claim_id",
"display_id",
"location_id",
"order_version",
"status",
"refund_amount",

View File

@@ -1,6 +1,6 @@
import { CreateOrderDTO, IOrderModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { Modules } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000)
@@ -132,7 +132,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
billing_address: expect.objectContaining({
id: expect.stringContaining("ordaddr_"),
}),
items: [
items: expect.arrayContaining([
expect.objectContaining({
id: expect.stringContaining("ordli_"),
quantity: 1,
@@ -174,7 +174,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
version: 1,
}),
}),
],
]),
shipping_methods: [
expect.objectContaining({
id: expect.stringContaining("ordsm_"),

View File

@@ -104,6 +104,9 @@ moduleIntegrationTestRunner({
it("should claim an item and add two new items to the order", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
// Fullfilment
await service.registerFulfillment({

View File

@@ -127,6 +127,9 @@ moduleIntegrationTestRunner<IOrderModuleService>({
it("should change an order by adding actions to it", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
await service.addOrderAction([
{
@@ -219,123 +222,131 @@ moduleIntegrationTestRunner<IOrderModuleService>({
],
relations: ["items", "shipping_methods", "transactions"],
})
const serializedFinalOrder = JSON.parse(JSON.stringify(finalOrder))
const serializedCreatedOrder = JSON.parse(JSON.stringify(createdOrder))
expect(serializedCreatedOrder.items).toEqual([
expect.objectContaining({
title: "Item 1",
unit_price: 8,
quantity: 1,
detail: expect.objectContaining({
version: 1,
expect(serializedCreatedOrder.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "Item 1",
unit_price: 8,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
expect.objectContaining({
title: "Item 2",
compare_at_unit_price: null,
unit_price: 5,
quantity: 2,
}),
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
detail: expect.objectContaining({
version: 1,
expect.objectContaining({
title: "Item 2",
compare_at_unit_price: null,
unit_price: 5,
quantity: 2,
}),
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
])
])
)
expect(serializedFinalOrder).toEqual(
expect.objectContaining({
version: 1,
})
)
expect(serializedFinalOrder.items).toEqual([
expect.objectContaining({
title: "Item 1",
subtitle: "Subtitle 1",
thumbnail: "thumbnail1.jpg",
variant_id: "variant1",
product_id: "product1",
product_title: "Product 1",
product_description: "Description 1",
product_subtitle: "Product Subtitle 1",
product_type: "Type 1",
product_collection: "Collection 1",
product_handle: "handle1",
variant_sku: "SKU1",
variant_barcode: "Barcode1",
variant_title: "Variant 1",
variant_option_values: { size: "Large", color: "Red" },
requires_shipping: true,
is_discountable: true,
is_tax_inclusive: true,
compare_at_unit_price: 10,
unit_price: 8,
quantity: 2,
detail: expect.objectContaining({
version: 1,
expect(serializedFinalOrder.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "Item 1",
subtitle: "Subtitle 1",
thumbnail: "thumbnail1.jpg",
variant_id: "variant1",
product_id: "product1",
product_title: "Product 1",
product_description: "Description 1",
product_subtitle: "Product Subtitle 1",
product_type: "Type 1",
product_collection: "Collection 1",
product_handle: "handle1",
variant_sku: "SKU1",
variant_barcode: "Barcode1",
variant_title: "Variant 1",
variant_option_values: { size: "Large", color: "Red" },
requires_shipping: true,
is_discountable: true,
is_tax_inclusive: true,
compare_at_unit_price: 10,
unit_price: 8,
quantity: 2,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 2,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
expect.objectContaining({
title: "Item 2",
compare_at_unit_price: null,
unit_price: 5,
quantity: 5,
detail: expect.objectContaining({
version: 1,
expect.objectContaining({
title: "Item 2",
compare_at_unit_price: null,
unit_price: 5,
quantity: 5,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 5,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
detail: expect.objectContaining({
version: 1,
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 1,
written_off_quantity: 1,
detail: expect.objectContaining({
version: 1,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 1,
written_off_quantity: 1,
}),
}),
}),
])
])
)
})
it("should create an order change, add actions to it, confirm the changes, revert all the changes and restore the changes again.", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
const orderChange = await service.createOrderChange({
order_id: createdOrder.id,
@@ -439,45 +450,47 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedModifiedOrder.shipping_methods).toHaveLength(1)
expect(serializedModifiedOrder.shipping_methods[0].amount).toEqual(10)
expect(serializedModifiedOrder.items).toEqual([
expect.objectContaining({
quantity: 2,
detail: expect.objectContaining({
version: 2,
expect(serializedModifiedOrder.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
quantity: 2,
detail: expect.objectContaining({
version: 2,
quantity: 2,
}),
}),
}),
expect.objectContaining({
title: "Item 2",
unit_price: 5,
quantity: 5,
detail: expect.objectContaining({
version: 2,
expect.objectContaining({
title: "Item 2",
unit_price: 5,
quantity: 5,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 2,
quantity: 5,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
detail: expect.objectContaining({
version: 2,
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 1,
written_off_quantity: 1,
detail: expect.objectContaining({
version: 2,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 1,
written_off_quantity: 1,
}),
}),
}),
])
])
)
// Revert Last Changes
await service.revertLastVersion(createdOrder.id)
@@ -504,50 +517,55 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedRevertedOrder.shipping_methods).toHaveLength(1)
expect(serializedRevertedOrder.shipping_methods[0].amount).toEqual(10)
expect(serializedRevertedOrder.items).toEqual([
expect.objectContaining({
quantity: 1,
unit_price: 8,
detail: expect.objectContaining({
version: 1,
expect(serializedRevertedOrder.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
quantity: 1,
unit_price: 8,
detail: expect.objectContaining({
version: 1,
quantity: 1,
}),
}),
}),
expect.objectContaining({
title: "Item 2",
unit_price: 5,
quantity: 2,
detail: expect.objectContaining({
version: 1,
expect.objectContaining({
title: "Item 2",
unit_price: 5,
quantity: 2,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 2,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
detail: expect.objectContaining({
version: 1,
expect.objectContaining({
title: "Item 3",
unit_price: 30,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
detail: expect.objectContaining({
version: 1,
quantity: 1,
fulfilled_quantity: 0,
shipped_quantity: 0,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
}),
}),
}),
])
])
)
})
it("should create order change, cancel and reject them.", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
const orderChange = await service.createOrderChange({
order_id: createdOrder.id,

View File

@@ -104,6 +104,9 @@ moduleIntegrationTestRunner({
it("should exchange an item and add two new items to the order", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
// Fullfilment
await service.registerFulfillment({

View File

@@ -104,6 +104,9 @@ moduleIntegrationTestRunner<IOrderModuleService>({
it("should create an order, fulfill, ship and return the items", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
// Fullfilment
await service.registerFulfillment({
@@ -135,7 +138,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -160,7 +163,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
shipped_quantity: 0,
}),
}),
],
]),
})
)
@@ -195,7 +198,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -220,7 +223,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
shipped_quantity: 1,
}),
}),
],
]),
})
)
@@ -290,7 +293,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder.shipping_methods).toHaveLength(3)
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -318,7 +321,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
return_requested_quantity: 1,
}),
}),
],
]),
})
)
@@ -443,7 +446,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -474,13 +477,16 @@ moduleIntegrationTestRunner<IOrderModuleService>({
return_received_quantity: 1,
}),
}),
],
]),
})
)
})
it("should create an order, fulfill, return the items and cancel some item return", async function () {
const createdOrder = await service.createOrders(input)
createdOrder.items = createdOrder.items!.sort((a, b) =>
a.title.localeCompare(b.title)
)
await service.registerFulfillment({
order_id: createdOrder.id,
@@ -553,7 +559,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -578,7 +584,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
return_requested_quantity: 1,
}),
}),
],
]),
})
)
@@ -628,7 +634,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
expect(serializedOrder.shipping_methods).toHaveLength(2)
expect(serializedOrder).toEqual(
expect.objectContaining({
items: [
items: expect.arrayContaining([
expect.objectContaining({
quantity: 1,
detail: expect.objectContaining({
@@ -659,7 +665,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
return_received_quantity: 0,
}),
}),
],
]),
})
)
})

View File

@@ -115,37 +115,37 @@ export class Migration20240219102530 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_display_id" ON "order" (
display_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_region_id" ON "order" (
region_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_customer_id" ON "order" (
customer_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_customer_id" ON "order" (
customer_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_currency_code" ON "order" (
currency_code
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_address_id" ON "order" (
shipping_address_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_billing_address_id" ON "order" (
billing_address_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_deleted_at" ON "order" (
deleted_at
@@ -154,7 +154,7 @@ export class Migration20240219102530 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_is_draft_order" ON "order" (
is_draft_order
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "order_summary" (
@@ -172,7 +172,7 @@ export class Migration20240219102530 extends Migration {
order_id,
version
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "order_change" (
"id" TEXT NOT NULL,
@@ -276,18 +276,18 @@ export class Migration20240219102530 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_item_order_id" ON "order_item" (
order_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_item_order_id_version" ON "order_item" (
order_id,
version
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_item_item_id" ON "order_item" (
item_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "order_shipping" (
"id" TEXT NOT NULL,
@@ -303,18 +303,18 @@ export class Migration20240219102530 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_order_id" ON "order_shipping" (
order_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_order_id_version" ON "order_shipping" (
order_id,
version
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_item_id" ON "order_shipping" (
shipping_method_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "order_line_item" (
"id" TEXT NOT NULL,
@@ -461,17 +461,17 @@ export class Migration20240219102530 extends Migration {
order_id,
version
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_transaction_currency_code" ON "order_transaction" (
currency_code
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_transaction_reference_id" ON "order_transaction" (
reference_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "return_reason"
(
@@ -492,7 +492,7 @@ export class Migration20240219102530 extends Migration {
);
CREATE UNIQUE INDEX IF NOT EXISTS "IDX_return_reason_value" ON "return_reason" USING btree (value ASC NULLS LAST)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
ALTER TABLE if exists "order"

View File

@@ -32,17 +32,17 @@ export class Migration20240604100512 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_transaction_return_id" ON "order_transaction" (
return_id
)
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_transaction_claim_id" ON "order_transaction" (
claim_id
)
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE claim_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_transaction_exchange_id" ON "order_transaction" (
exchange_id
)
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE exchange_id IS NOT NULL AND deleted_at IS NULL;
@@ -58,17 +58,17 @@ export class Migration20240604100512 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_return_id" ON "order_shipping" (
return_id
)
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_claim_id" ON "order_shipping" (
claim_id
)
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE claim_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_exchange_id" ON "order_shipping" (
exchange_id
)
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE exchange_id IS NOT NULL AND deleted_at IS NULL;
@@ -85,17 +85,17 @@ export class Migration20240604100512 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_change_return_id" ON "order_change" (
return_id
)
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_change_claim_id" ON "order_change" (
claim_id
)
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE claim_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_change_exchange_id" ON "order_change" (
exchange_id
)
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE exchange_id IS NOT NULL AND deleted_at IS NULL;
@@ -118,17 +118,17 @@ export class Migration20240604100512 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_change_action_return_id" ON "order_change_action" (
return_id
)
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_change_action_claim_id" ON "order_change_action" (
claim_id
)
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE claim_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_change_action_exchange_id" ON "order_change_action" (
exchange_id
)
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE exchange_id IS NOT NULL AND deleted_at IS NULL;
@@ -162,22 +162,22 @@ export class Migration20240604100512 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_return_order_id" ON "return" (
order_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_claim_id" ON "return" (
claim_id
)
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE claim_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_exchange_id" ON "return" (
exchange_id
)
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE exchange_id IS NOT NULL AND deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_display_id" ON "return" (
display_id
)
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "return_item" (
@@ -198,16 +198,16 @@ export class Migration20240604100512 extends Migration {
);
CREATE INDEX IF NOT EXISTS "IDX_return_item_deleted_at" ON "return_item" ("deleted_at")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_item_return_id" ON "return_item" ("return_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_item_item_id" ON "return_item" ("item_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_return_item_reason_id" ON "return_item" ("reason_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
@@ -230,16 +230,16 @@ export class Migration20240604100512 extends Migration {
);
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_display_id" ON "order_exchange" ("display_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_deleted_at" ON "order_exchange" ("deleted_at")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_order_id" ON "order_exchange" ("order_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_return_id" ON "order_exchange" ("return_id")
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS "order_exchange_item" (
@@ -257,13 +257,13 @@ export class Migration20240604100512 extends Migration {
);
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_deleted_at" ON "order_exchange_item" ("deleted_at")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_exchange_id" ON "order_exchange_item" ("exchange_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_item_id" ON "order_exchange_item" ("item_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
@@ -291,16 +291,16 @@ export class Migration20240604100512 extends Migration {
);
CREATE INDEX IF NOT EXISTS "IDX_order_claim_display_id" ON "order_claim" ("display_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_claim_deleted_at" ON "order_claim" ("deleted_at")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_claim_order_id" ON "order_claim" ("order_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_claim_return_id" ON "order_claim" ("return_id")
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
WHERE return_id IS NOT NULL AND deleted_at IS NULL;
@@ -328,13 +328,13 @@ export class Migration20240604100512 extends Migration {
);
CREATE INDEX IF NOT EXISTS "IDX_order_claim_item_deleted_at" ON "order_claim_item" ("deleted_at")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_claim_item_claim_id" ON "order_claim_item" ("claim_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_claim_item_item_id" ON "order_claim_item" ("item_id")
WHERE deleted_at IS NOT NULL;
WHERE deleted_at IS NULL;