core(core-flows, medusa): remove request item return (#8146)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-16 11:23:34 -03:00
committed by GitHub
parent 4024935e91
commit 5813216c88
16 changed files with 320 additions and 20 deletions
@@ -228,6 +228,57 @@ medusaIntegrationTestRunner({
total: 50,
fulfilled_total: 50,
return_requested_total: 50,
detail: expect.objectContaining({
return_requested_quantity: 1,
}),
}),
]),
})
)
// Remove item return requesta
const returnItemActionId =
result.data.order_preview.items[0].actions[0].id
result = await api.delete(
`/admin/returns/${returnId}/request-items/${returnItemActionId}`,
adminHeaders
)
expect(result.data.order_preview).toEqual(
expect.objectContaining({
id: order.id,
items: expect.arrayContaining([
expect.objectContaining({
detail: expect.objectContaining({
return_requested_quantity: 0,
}),
}),
]),
})
)
// Add item return request again
result = await api.post(
`/admin/returns/${returnId}/request-items`,
{
items: [
{
id: item.id,
quantity: 1,
},
],
},
adminHeaders
)
expect(result.data.order_preview).toEqual(
expect.objectContaining({
id: order.id,
items: expect.arrayContaining([
expect.objectContaining({
detail: expect.objectContaining({
return_requested_quantity: 1,
}),
}),
]),
})
@@ -300,12 +351,13 @@ medusaIntegrationTestRunner({
})
)
expect(result.data.order_preview.shipping_methods).toHaveLength(2)
// remove shipping method
const action_id = result.data.order_preview.shipping_methods[1].actions[0].id
const shippingActionId =
result.data.order_preview.shipping_methods[1].actions[0].id
result = await api.delete(
`/admin/returns/${returnId}/shipping-method/${action_id}`,
`/admin/returns/${returnId}/shipping-method/${shippingActionId}`,
adminHeaders
)
@@ -205,8 +205,9 @@ medusaIntegrationTestRunner({
metadata: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
tax_lines: [],
adjustments: [
adjustments: expect.arrayContaining([
{
id: expect.any(String),
description: "VIP discount",
@@ -219,6 +220,7 @@ medusaIntegrationTestRunner({
provider_id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
item_id: expect.any(String),
amount: 5e-18,
subtotal: 5e-18,
@@ -232,7 +234,7 @@ medusaIntegrationTestRunner({
precision: 20,
},
},
],
]),
compare_at_unit_price: null,
unit_price: 50,
quantity: 1,
@@ -412,7 +414,7 @@ medusaIntegrationTestRunner({
metadata: null,
created_at: expect.any(String),
updated_at: expect.any(String),
tax_lines: [
tax_lines: expect.arrayContaining([
{
id: expect.any(String),
description: "shipping Tax 1",
@@ -425,6 +427,7 @@ medusaIntegrationTestRunner({
provider_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
shipping_method_id: expect.any(String),
rate: 10,
total: 0.9,
@@ -438,8 +441,8 @@ medusaIntegrationTestRunner({
precision: 20,
},
},
],
adjustments: [
]),
adjustments: expect.arrayContaining([
{
id: expect.any(String),
description: "VIP discount",
@@ -452,6 +455,7 @@ medusaIntegrationTestRunner({
provider_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
shipping_method_id: expect.any(String),
amount: 1,
subtotal: 1,
@@ -465,7 +469,7 @@ medusaIntegrationTestRunner({
precision: 20,
},
},
],
]),
amount: 10,
subtotal: 10,
total: 9.9,
@@ -26,6 +26,7 @@ export * from "./exchange-request-item-return"
export * from "./get-order-detail"
export * from "./get-orders-list"
export * from "./receive-return"
export * from "./remove-request-item-return"
export * from "./remove-return-shipping-method"
export * from "./request-item-return"
export * from "./update-order-change-actions"
@@ -0,0 +1,95 @@
import {
OrderChangeActionDTO,
OrderChangeDTO,
OrderDTO,
OrderWorkflow,
ReturnDTO,
} from "@medusajs/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
import {
WorkflowData,
createStep,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { deleteOrderChangeActionsStep, previewOrderChangeStep } from "../steps"
import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../utils/order-validation"
const validationStep = createStep(
"remove-request-item-return-validation",
async function ({
order,
orderChange,
orderReturn,
input,
}: {
order: OrderDTO
orderReturn: ReturnDTO
orderChange: OrderChangeDTO
input: OrderWorkflow.DeleteRequestItemReturnWorkflowInput
}) {
throwIfIsCancelled(order, "Order")
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
const associatedAction = orderChange.actions?.find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
if (!associatedAction) {
throw new Error(
`No request return found for return ${input.return_id} in order change ${orderChange.id}`
)
} else if (associatedAction.action !== ChangeActionType.RETURN_ITEM) {
throw new Error(
`Action ${associatedAction.id} is not requesting item return`
)
}
}
)
export const removeRequestItemReturnWorkflowId = "remove-request-item-return"
export const removeRequestItemReturnWorkflow = createWorkflow(
removeRequestItemReturnWorkflowId,
function (
input: WorkflowData<OrderWorkflow.DeleteRequestItemReturnWorkflowInput>
): WorkflowData<OrderDTO> {
const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return",
fields: ["id", "status", "order_id"],
variables: { id: input.return_id },
list: false,
throw_if_key_not_found: true,
})
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: ["id", "status", "items.*"],
variables: { id: orderReturn.order_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "order-query" })
const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["id", "status", "version", "actions.*"],
variables: {
filters: {
order_id: orderReturn.order_id,
return_id: orderReturn.id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "order-change-query" })
validationStep({ order, input, orderReturn, orderChange })
deleteOrderChangeActionsStep({ ids: [input.action_id] })
return previewOrderChangeStep(order.id)
}
)
@@ -34,7 +34,7 @@ const validationStep = createStep(
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
const associatedAction = orderChange.actions.find(
const associatedAction = orderChange.actions?.find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
@@ -81,7 +81,7 @@ export const removeReturnShippingMethodWorkflow = createWorkflow(
const dataToRemove = transform(
{ orderChange, input },
({ orderChange, input }) => {
const associatedAction = orderChange.actions.find(
const associatedAction = orderChange.actions?.find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
@@ -4,13 +4,25 @@ export interface RequestItemReturnWorkflowInput {
return_id: string
items: CreateReturnItem[]
}
export interface DeleteRequestItemReturnWorkflowInput {
return_id: string
action_id: string
}
export interface OrderExchangeRequestItemReturnWorkflowInput {
exchange_id: string
items: CreateReturnItem[]
}
export interface DeleteOrderExchangeRequestItemReturnWorkflowInput {
return_id: string
action_id: string
}
export interface OrderClaimRequestItemReturnWorkflowInput {
claim_id: string
items: CreateReturnItem[]
}
export interface DeleteOrderClaimRequestItemReturnWorkflowInput {
return_id: string
action_id: string
}
@@ -0,0 +1,44 @@
import { removeRequestItemReturnWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const { id, action_id } = req.params
const { result: orderPreview } = await removeRequestItemReturnWorkflow(
req.scope
).run({
input: {
return_id: id,
action_id,
},
})
const queryObject = remoteQueryObjectFromString({
entryPoint: "return",
variables: {
id,
filters: {
...req.filterableFields,
},
},
fields: req.remoteQueryConfig.fields,
})
const [orderReturn] = await remoteQuery(queryObject)
res.json({
order_preview: orderPreview,
return: orderReturn,
})
}
@@ -54,6 +54,16 @@ export const adminReturnRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["DELETE"],
matcher: "/admin/returns/:id/request-items/:action_id",
middlewares: [
validateAndTransformQuery(
AdminGetOrdersOrderParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/returns/:id/shipping-method",
@@ -2190,11 +2190,12 @@ moduleIntegrationTestRunner<IOrderModuleService>({
})
const serialized = JSON.parse(JSON.stringify(order))
expect(serialized.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: itemOne.id,
tax_lines: [
tax_lines: expect.arrayContaining([
expect.objectContaining({
id: taxLine!.id,
item_id: itemOne.id,
@@ -2206,7 +2207,7 @@ moduleIntegrationTestRunner<IOrderModuleService>({
rate: 25,
code: "TX-2",
}),
],
]),
}),
])
)
@@ -1,6 +1,6 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240715102100 extends Migration {
export class Migration20240715174100 extends Migration {
async up(): Promise<void> {
const sql = `
DROP INDEX IF EXISTS "IDX_order_shipping_method_shipping_option_id";
@@ -11,8 +11,6 @@ export class Migration20240715102100 extends Migration {
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_shipping_option_id" ON "order_shipping_method" (
shipping_option_id
) WHERE deleted_at IS NULL;
`
this.addSql(sql)
@@ -0,0 +1,64 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240716081800 extends Migration {
async up(): Promise<void> {
const sql = `
DROP INDEX IF EXISTS "IDX_order_line_item_variant_id";
DROP INDEX IF EXISTS "IDX_order_line_item_product_id";
ALTER TABLE "order_line_item"
ADD COLUMN if NOT exists "deleted_at" timestamptz NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_line_item_variant_id" ON "order_line_item" (
variant_id
) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_line_item_product_id" ON "order_line_item" (
product_id
) WHERE deleted_at IS NULL;
DROP INDEX IF EXISTS "IDX_order_shipping_method_tax_line_shipping_method_id";
ALTER TABLE "order_shipping_method_tax_line"
ADD COLUMN if NOT exists "deleted_at" timestamptz NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_tax_line_shipping_method_id" ON "order_shipping_method_tax_line" (
shipping_method_id
) WHERE deleted_at IS NULL;
DROP INDEX IF EXISTS "IDX_order_shipping_method_adjustment_shipping_method_id";
ALTER TABLE "order_shipping_method_adjustment"
ADD COLUMN if NOT exists "deleted_at" timestamptz NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_adjustment_shipping_method_id" ON "order_shipping_method_adjustment" (
shipping_method_id
) WHERE deleted_at IS NULL;
DROP INDEX IF EXISTS "IDX_order_line_item_tax_line_item_id";
ALTER TABLE "order_line_item_tax_line"
ADD COLUMN if NOT exists "deleted_at" timestamptz NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_line_item_tax_line_item_id" ON "order_line_item_tax_line" (
item_id
) WHERE deleted_at IS NULL;
DROP INDEX IF EXISTS "IDX_order_line_item_adjustment_item_id";
ALTER TABLE "order_line_item_adjustment"
ADD COLUMN if NOT exists "deleted_at" timestamptz NULL;
CREATE INDEX IF NOT EXISTS "IDX_order_line_item_adjustment_item_id" ON "order_line_item_adjustment" (
item_id
) WHERE deleted_at IS NULL;
`
this.addSql(sql)
}
}
@@ -49,4 +49,7 @@ export default abstract class AdjustmentLine {
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
}
@@ -1,6 +1,7 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
@@ -10,6 +11,7 @@ import {
Cascade,
Collection,
Entity,
Filter,
OnInit,
OneToMany,
OptionalProps,
@@ -22,17 +24,26 @@ import LineItemTaxLine from "./line-item-tax-line"
type OptionalLineItemProps = DAL.ModelDateColumns
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order_line_item",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const ProductIdIndex = createPsqlIndexStatementHelper({
tableName: "order_line_item",
columns: "product_id",
where: "deleted_at IS NOT NULL",
})
const VariantIdIndex = createPsqlIndexStatementHelper({
tableName: "order_line_item",
columns: "variant_id",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_line_item" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItem {
[OptionalProps]?: OptionalLineItemProps
@@ -145,6 +156,10 @@ export default class LineItem {
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordli")
@@ -71,7 +71,7 @@ export default class ShippingMethod {
() => ShippingMethodTaxLine,
(taxLine) => taxLine.shipping_method,
{
cascade: [Cascade.PERSIST, "soft-remove"] as any,
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
}
)
tax_lines = new Collection<Rel<ShippingMethodTaxLine>>(this)
@@ -80,7 +80,7 @@ export default class ShippingMethod {
() => ShippingMethodAdjustment,
(adjustment) => adjustment.shipping_method,
{
cascade: [Cascade.PERSIST, "soft-remove"] as any,
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
}
)
adjustments = new Collection<Rel<ShippingMethodAdjustment>>(this)
@@ -45,4 +45,7 @@ export default abstract class TaxLine {
defaultRaw: "now()",
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
}
@@ -2124,8 +2124,6 @@ export default class OrderModuleService<
})
}
// TODO - add new shipping methods
const calcOrder = calculated.order
decorateCartTotals(calcOrder as DecorateCartLikeInputDTO)