chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,160 @@
import { OrderChangeEvent } from "../../../../types"
import { ChangeActionType, calculateOrderChange } from "../../../../utils"
describe("Order Exchange - Actions", function () {
const originalOrder = {
items: [
{
id: "1",
quantity: 1,
unit_price: 10,
detail: {
quantity: 1,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "2",
quantity: 2,
unit_price: 100,
detail: {
quantity: 2,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "3",
quantity: 3,
unit_price: 20,
detail: {
quantity: 3,
shipped_quantity: 3,
fulfilled_quantity: 3,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
],
shipping_methods: [
{
id: "ship_123",
price: 0,
},
],
total: 270,
}
it("should perform an item exchage", function () {
const actions = [
{
action: ChangeActionType.RETURN_ITEM,
reference_id: "return_123",
details: {
reference_id: "3",
quantity: 1,
},
},
{
action: ChangeActionType.ITEM_ADD,
reference_id: "item_555",
details: {
unit_price: 50,
quantity: 1,
},
},
{
action: ChangeActionType.SHIPPING_ADD,
reference_id: "shipping_345",
amount: 5,
},
{
action: ChangeActionType.SHIPPING_ADD,
reference_id: "return_shipping_345",
amount: 7.5,
},
] as OrderChangeEvent[]
const changes = calculateOrderChange({
order: originalOrder,
actions: actions,
})
const sumToJSON = JSON.parse(JSON.stringify(changes.summary))
expect(sumToJSON).toEqual({
transactionTotal: 0,
originalOrderTotal: 270,
currentOrderTotal: 312.5,
temporaryDifference: 62.5,
futureDifference: 0,
futureTemporaryDifference: 0,
pendingDifference: 312.5,
differenceSum: 42.5,
})
const toJson = JSON.parse(JSON.stringify(changes.order.items))
expect(toJson).toEqual([
{
id: "1",
quantity: 1,
unit_price: 10,
detail: {
quantity: 1,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "2",
quantity: 2,
unit_price: 100,
detail: {
quantity: 2,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "3",
quantity: 3,
unit_price: 20,
detail: {
quantity: 3,
shipped_quantity: 3,
fulfilled_quantity: 3,
return_requested_quantity: "1",
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "item_555",
unit_price: 50,
quantity: 1,
},
])
})
})

View File

@@ -0,0 +1,305 @@
import { OrderChangeEvent } from "../../../../types"
import { ChangeActionType, calculateOrderChange } from "../../../../utils"
describe("Order Return - Actions", function () {
const originalOrder = {
items: [
{
id: "1",
quantity: 1,
unit_price: 10,
detail: {
quantity: 1,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "2",
quantity: 2,
unit_price: 100,
detail: {
quantity: 2,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "3",
quantity: 3,
unit_price: 20,
detail: {
quantity: 3,
shipped_quantity: 3,
fulfilled_quantity: 3,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
],
shipping_methods: [
{
id: "ship_123",
price: 0,
},
],
total: 270,
}
it("should validate return requests", function () {
const actions = [
{
action: ChangeActionType.RETURN_ITEM,
reference_id: "return_123",
details: {
reference_id: "1",
quantity: 1,
},
},
] as OrderChangeEvent[]
expect(() => {
actions[0].details!.quantity = 2
calculateOrderChange({
order: originalOrder,
actions,
})
}).toThrow(
"Cannot request to return more items than what was shipped for item 1."
)
expect(() => {
actions[0].details!.reference_id = undefined
calculateOrderChange({
order: originalOrder,
actions,
})
}).toThrow("Details reference ID is required.")
expect(() => {
actions[0].details!.reference_id = "333"
calculateOrderChange({
order: originalOrder,
actions,
})
}).toThrow(`Reference ID "333" not found.`)
})
it("should validate return received", function () {
const [] = []
const actions = [
{
action: ChangeActionType.RETURN_ITEM,
reference_id: "return_123",
details: {
reference_id: "2",
quantity: 1,
},
},
{
action: ChangeActionType.RETURN_ITEM,
reference_id: "return_123",
details: {
reference_id: "3",
quantity: 2,
},
},
] as OrderChangeEvent[]
const changes = calculateOrderChange({
order: originalOrder,
actions,
})
const toJson = JSON.parse(JSON.stringify(changes.order.items))
expect(toJson).toEqual([
{
id: "1",
quantity: 1,
unit_price: 10,
detail: {
quantity: 1,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "2",
quantity: 2,
unit_price: 100,
detail: {
quantity: 2,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: "1",
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "3",
quantity: 3,
unit_price: 20,
detail: {
quantity: 3,
shipped_quantity: 3,
fulfilled_quantity: 3,
return_requested_quantity: "2",
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
])
const modifiedOrder = {
...originalOrder,
items: [...changes.order.items],
}
expect(() => {
calculateOrderChange({
order: modifiedOrder,
actions: [
{
action: ChangeActionType.RETURN_ITEM,
details: {
reference_id: "3",
quantity: 2,
},
},
],
})
}).toThrow(
"Cannot request to return more items than what was shipped for item 3."
)
expect(() => {
calculateOrderChange({
order: modifiedOrder,
actions: [
{
action: ChangeActionType.RECEIVE_RETURN_ITEM,
details: {
reference_id: "3",
quantity: 3,
},
},
],
})
}).toThrow(
"Cannot receive more items than what was requested to be returned for item 3."
)
expect(() => {
calculateOrderChange({
order: modifiedOrder,
actions: [
{
action: ChangeActionType.RECEIVE_RETURN_ITEM,
details: {
reference_id: "3",
quantity: 1,
},
},
{
action: ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
details: {
reference_id: "3",
quantity: 2,
},
},
],
})
}).toThrow(
"Cannot receive more items than what was requested to be returned for item 3."
)
const receivedChanges = calculateOrderChange({
order: modifiedOrder,
actions: [
{
action: ChangeActionType.RECEIVE_RETURN_ITEM,
details: {
reference_id: "3",
quantity: 1,
},
},
{
action: ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
details: {
reference_id: "3",
quantity: 1,
},
},
],
})
const toJsonReceived = JSON.parse(
JSON.stringify(receivedChanges.order.items)
)
expect(toJsonReceived).toEqual([
{
id: "1",
quantity: 1,
unit_price: 10,
detail: {
quantity: 1,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: 0,
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "2",
quantity: 2,
unit_price: 100,
detail: {
quantity: 2,
shipped_quantity: 1,
fulfilled_quantity: 1,
return_requested_quantity: "1",
return_received_quantity: 0,
return_dismissed_quantity: 0,
written_off_quantity: 0,
},
},
{
id: "3",
quantity: 3,
unit_price: 20,
detail: {
quantity: 3,
shipped_quantity: 3,
fulfilled_quantity: 3,
return_requested_quantity: "0",
return_received_quantity: "1",
return_dismissed_quantity: "1",
written_off_quantity: 0,
},
},
])
})
})

View File

@@ -0,0 +1,3 @@
export { default as OrderChangeService } from "./order-change-service"
export { default as OrderModuleService } from "./order-module-service"
export { default as OrderService } from "./order-service"

View File

@@ -0,0 +1,139 @@
import {
Context,
DAL,
FindConfig,
OrderTypes,
RepositoryService,
} from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
OrderChangeStatus,
deduplicate,
} from "@medusajs/utils"
import { OrderChange } from "@models"
type InjectedDependencies = {
orderChangeRepository: DAL.RepositoryService
}
export default class OrderChangeService<
TEntity extends OrderChange = OrderChange
> extends ModulesSdkUtils.internalModuleServiceFactory<InjectedDependencies>(
OrderChange
)<TEntity> {
protected readonly orderChangeRepository_: RepositoryService<TEntity>
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
this.orderChangeRepository_ = container.orderChangeRepository
}
@InjectManager("orderChangeRepository_")
async listCurrentOrderChange<TEntityMethod = OrderTypes.OrderDTO>(
orderId: string | string[],
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const allChanges = await super.list(
{ order_id: orderId },
config ?? {
select: ["order_id", "status", "version"],
order: {
order_id: "ASC",
version: "DESC",
},
}
)
if (!allChanges.length) {
return []
}
const lastChanges: string[] = []
const seen = new Set()
for (let i = 0; i < allChanges.length; i++) {
if (seen.has(allChanges[i].order_id)) {
continue
}
seen.add(allChanges[i].order_id)
if (this.isActive(allChanges[i])) {
lastChanges.push(allChanges[i].id)
}
}
let orderChange!: TEntity
if (allChanges?.length > 0) {
if (this.isActive(allChanges[0])) {
orderChange = allChanges[0]
}
}
if (!orderChange) {
return []
}
const relations = deduplicate([...(config.relations ?? []), "actions"])
config.relations = relations
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
{
id: lastChanges,
order: {
items: {
version: orderChange.version,
},
},
},
config
)
return await this.orderChangeRepository_.find(queryConfig, sharedContext)
}
isActive(orderChange: OrderChange): boolean {
return (
orderChange.status === OrderChangeStatus.PENDING ||
orderChange.status === OrderChangeStatus.REQUESTED
)
}
async create(
data: Partial<TEntity>[],
sharedContext?: Context
): Promise<TEntity[]>
async create(
data: Partial<TEntity>,
sharedContext?: Context
): Promise<TEntity>
@InjectTransactionManager("orderChangeRepository_")
async create(
data: Partial<TEntity>[] | Partial<TEntity>,
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[] | TEntity> {
const dataArr = Array.isArray(data) ? data : [data]
const activeOrderEdit = await this.listCurrentOrderChange(
dataArr.map((d) => d.order_id!),
{},
sharedContext
)
if (activeOrderEdit.length > 0) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`An active order change already exists for the order(s) ${activeOrderEdit
.map((a) => a.order_id)
.join(",")}`
)
}
return await super.create(dataArr, sharedContext)
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,58 @@
import {
Context,
DAL,
FindConfig,
OrderTypes,
RepositoryService,
} from "@medusajs/types"
import {
InjectManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
} from "@medusajs/utils"
import { Order } from "@models"
type InjectedDependencies = {
orderRepository: DAL.RepositoryService
}
export default class OrderService<
TEntity extends Order = Order
> extends ModulesSdkUtils.internalModuleServiceFactory<InjectedDependencies>(
Order
)<TEntity> {
protected readonly orderRepository_: RepositoryService<TEntity>
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
this.orderRepository_ = container.orderRepository
}
@InjectManager("orderRepository_")
async retrieveOrderVersion<TEntityMethod = OrderTypes.OrderDTO>(
id: string,
version: number,
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
{ id, items: { version } },
{ ...config, take: 1 }
)
const [result] = await this.orderRepository_.find(
queryConfig,
sharedContext
)
if (!result) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Order with id: "${id}" and version: "${version}" not found`
)
}
return result
}
}