feat(medusa): Payment Collection endpoints (#2525)
This commit is contained in:
1 parent
27184f3878
commit
755ba90c05
58 files changed
+1190
-484
No files matched your search
@@ -29,7 +29,7 @@ describe("/admin/collections", () => {
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
medusaProcess = await setupServer({ cwd, verbose: true })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -22,7 +22,7 @@ const {
|
||||
} = require("../../factories")
|
||||
const { OrderEditItemChangeType, OrderEdit } = require("@medusajs/medusa")
|
||||
|
||||
jest.setTimeout(50000)
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const adminHeaders = {
|
||||
headers: {
|
||||
@@ -869,23 +869,82 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
const product1 = await simpleProductFactory(dbConnection)
|
||||
const product2 = await simpleProductFactory(dbConnection)
|
||||
|
||||
const { id, order_id } = await simpleOrderEditFactory(dbConnection, {
|
||||
const order = await simpleOrderFactory(dbConnection, {
|
||||
id: IdMap.getId("order-test-2"),
|
||||
email: "test@testson.com",
|
||||
tax_rate: null,
|
||||
fulfillment_status: "fulfilled",
|
||||
payment_status: "captured",
|
||||
region: {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 0,
|
||||
},
|
||||
line_items: [
|
||||
{
|
||||
id: "lineItemId1",
|
||||
variant_id: product2.variants[0].id,
|
||||
quantity: 1,
|
||||
fulfilled_quantity: 1,
|
||||
shipped_quantity: 1,
|
||||
unit_price: 1000,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 10,
|
||||
code: "code1",
|
||||
name: "code1",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
shipping_methods: [
|
||||
{
|
||||
shipping_option: {
|
||||
name: "random",
|
||||
region_id: "test-region",
|
||||
},
|
||||
price: 10,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 0,
|
||||
code: "code1",
|
||||
name: "code1",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const { id } = await simpleOrderEditFactory(dbConnection, {
|
||||
created_by: "admin_user",
|
||||
order_id: order.id,
|
||||
})
|
||||
|
||||
const noChangesEdit = await simpleOrderEditFactory(dbConnection, {
|
||||
created_by: "admin_user",
|
||||
})
|
||||
|
||||
await simpleLineItemFactory(dbConnection, {
|
||||
order_id: order_id,
|
||||
const lineItemAdded = await simpleLineItemFactory(dbConnection, {
|
||||
order_id: null,
|
||||
order_edit_id: id,
|
||||
variant_id: product1.variants[0].id,
|
||||
unit_price: 2000,
|
||||
quantity: 1,
|
||||
tax_lines: [
|
||||
{
|
||||
rate: 0,
|
||||
code: "code1",
|
||||
name: "code1",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
await simpleOrderItemChangeFactory(dbConnection, {
|
||||
order_edit_id: id,
|
||||
type: "item_add",
|
||||
type: OrderEditItemChangeType.ITEM_ADD,
|
||||
line_item_id: lineItemAdded.id,
|
||||
})
|
||||
|
||||
orderEditId = id
|
||||
@@ -900,6 +959,28 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
it("requests order edit", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const result = await api.post(
|
||||
`/admin/order-edits/${orderEditId}/request`,
|
||||
{
|
||||
payment_collection_description: "Payment collection description",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(result.status).toEqual(200)
|
||||
expect(result.data.order_edit).toEqual(
|
||||
expect.objectContaining({
|
||||
id: orderEditId,
|
||||
requested_at: expect.any(String),
|
||||
requested_by: "admin_user",
|
||||
status: "requested",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates payment collection if difference_due > 0", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const result = await api.post(
|
||||
`/admin/order-edits/${orderEditId}/request`,
|
||||
{},
|
||||
@@ -913,6 +994,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
requested_at: expect.any(String),
|
||||
requested_by: "admin_user",
|
||||
status: "requested",
|
||||
payment_collection_id: expect.any(String),
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -934,6 +1016,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("requests order edit", async () => {
|
||||
const api = useApi()
|
||||
|
||||
@@ -2635,7 +2718,6 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
const lineItemId2Discount = IdMap.getId("line-item-2-discount")
|
||||
|
||||
beforeEach(async () => {
|
||||
const api = useApi()
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
product = await simpleProductFactory(dbConnection, {
|
||||
@@ -2646,7 +2728,7 @@ describe("[MEDUSA_FF_ORDER_EDITING] /admin/order-edits", () => {
|
||||
id: prodId2,
|
||||
})
|
||||
|
||||
const reagion = await simpleRegionFactory(dbConnection, {
|
||||
const region = await simpleRegionFactory(dbConnection, {
|
||||
id: "test-region",
|
||||
name: "Test region",
|
||||
tax_rate: 12.5,
|
||||
|
||||
@@ -14,7 +14,7 @@ describe("/store/collections", () => {
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
medusaProcess = await setupServer({ cwd, verbose: true })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -18,3 +18,4 @@ export * from "./simple-price-list-factory"
|
||||
export * from "./simple-batch-job-factory"
|
||||
export * from "./simple-sales-channel-factory"
|
||||
export * from "./simple-custom-shipping-option-factory"
|
||||
export * from "./simple-payment-collection-factory"
|
||||
@@ -31,6 +31,7 @@ export type LineItemFactoryData = {
|
||||
tax_lines?: TaxLineFactoryData[]
|
||||
adjustments: LineItemAdjustmentFactoryData[]
|
||||
includes_tax?: boolean
|
||||
order_edit_id?: string
|
||||
}
|
||||
|
||||
export const simpleLineItemFactory = async (
|
||||
@@ -72,6 +73,7 @@ export const simpleLineItemFactory = async (
|
||||
returned_quantity: data.returned_quantity || null,
|
||||
adjustments: data.adjustments,
|
||||
includes_tax: data.includes_tax,
|
||||
order_edit_id: data.order_edit_id,
|
||||
})
|
||||
|
||||
const line = await manager.save(toSave)
|
||||
|
||||
@@ -8,6 +8,7 @@ export type OrderEditFactoryData = {
|
||||
order_id?: string
|
||||
internal_note?: string
|
||||
declined_reason?: string
|
||||
payment_collection_id?: string
|
||||
confirmed_at?: Date | string
|
||||
confirmed_by?: string
|
||||
created_at?: Date | string
|
||||
@@ -46,6 +47,7 @@ export const simpleOrderEditFactory = async (
|
||||
created_by: data.created_by,
|
||||
confirmed_at: data.confirmed_at,
|
||||
confirmed_by: data.confirmed_by,
|
||||
payment_collection_id: data.payment_collection_id,
|
||||
})
|
||||
|
||||
return await manager.save<OrderEdit>(orderEdit)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Connection } from "typeorm"
|
||||
|
||||
import { simpleRegionFactory } from "./simple-region-factory"
|
||||
import { simplePaymentFactory } from "./simple-payment-factory"
|
||||
import { Payment, PaymentCollection } from "@medusajs/medusa"
|
||||
|
||||
export const simplePaymentCollectionFactory = async (
|
||||
connection: Connection,
|
||||
data: Partial<PaymentCollection> = {}
|
||||
): Promise<PaymentCollection> => {
|
||||
const manager = connection.manager
|
||||
|
||||
const defaultData = {
|
||||
currency_code: data.currency_code ?? "usd",
|
||||
}
|
||||
|
||||
if (!data.region && !data.region_id) {
|
||||
data.region = await simpleRegionFactory(connection, {
|
||||
id: data.region_id || "test-region",
|
||||
currency_code: defaultData.currency_code,
|
||||
})
|
||||
data.region_id = data.region.id
|
||||
}
|
||||
|
||||
if (data.payments?.length) {
|
||||
const payments: Payment[] = []
|
||||
for (const payment of data.payments) {
|
||||
payment.currency_code = payment.currency_code ?? defaultData.currency_code
|
||||
|
||||
const savedPayment = await simplePaymentFactory(
|
||||
connection,
|
||||
payment as any
|
||||
)
|
||||
payments.push(savedPayment)
|
||||
}
|
||||
|
||||
data.payments = payments
|
||||
}
|
||||
|
||||
const obj = {
|
||||
...defaultData,
|
||||
...data,
|
||||
}
|
||||
|
||||
const payCol = manager.create<PaymentCollection>(PaymentCollection, obj)
|
||||
|
||||
return await manager.save<PaymentCollection>(payCol)
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "jest --silent=false --runInBand --bail",
|
||||
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit",
|
||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -12,7 +12,7 @@ class TestPayService extends AbstractPaymentService {
|
||||
}
|
||||
|
||||
async retrieveSavedMethods(customer) {
|
||||
return Promise.resolve([])
|
||||
return []
|
||||
}
|
||||
|
||||
async createPayment(cart) {
|
||||
|
||||
@@ -12,17 +12,16 @@ function replacePath(requirePath, package, concatPackage = true) {
|
||||
const idx = requirePath.indexOf(package)
|
||||
const packPath = requirePath.substring(idx + package.length)
|
||||
|
||||
let newPath = path.resolve(
|
||||
let newPath =
|
||||
medusaCore +
|
||||
"/" +
|
||||
(concatPackage ? package + "/" : "") +
|
||||
packPath.replace("/dist", "/src").replace(".js", "")
|
||||
)
|
||||
"/" +
|
||||
(concatPackage ? package + "/" : "") +
|
||||
packPath.replace("/dist", "/src").replace(".js", "")
|
||||
|
||||
if (!newPath.includes("/src")) {
|
||||
newPath += "/src"
|
||||
}
|
||||
return newPath
|
||||
return path.resolve(newPath)
|
||||
}
|
||||
|
||||
Module.prototype.require = function (...args) {
|
||||
|
||||
@@ -12,7 +12,7 @@ class TestPayService extends AbstractPaymentService {
|
||||
}
|
||||
|
||||
async retrieveSavedMethods(customer) {
|
||||
return Promise.resolve([])
|
||||
return []
|
||||
}
|
||||
|
||||
async createPayment(cart) {
|
||||
|
||||
@@ -12,7 +12,7 @@ class TestPayService extends AbstractPaymentService {
|
||||
}
|
||||
|
||||
async retrieveSavedMethods(customer) {
|
||||
return Promise.resolve([])
|
||||
return []
|
||||
}
|
||||
|
||||
async createPayment(cart) {
|
||||
|
||||
Reference in new issue
Block a user