feat: update and cancel swaps, claims, and returns (#310)
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
const { useApi } = require("../../helpers/use-api");
|
||||
|
||||
const header = {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
};
|
||||
|
||||
const resolveCall = async (path, payload, header) => {
|
||||
const api = useApi();
|
||||
let res;
|
||||
try {
|
||||
const resp = await api.post(path, payload, header);
|
||||
res = resp.status;
|
||||
} catch (expectedException) {
|
||||
try {
|
||||
res = expectedException.response.status;
|
||||
} catch (_) {
|
||||
console.error(expectedException);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
const determineFail = (actual, expected, path) => {
|
||||
if (expected !== actual) {
|
||||
console.log(`failed at path : ${path}`);
|
||||
}
|
||||
expect(actual).toEqual(expected);
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows you to wrap a Call function so that you may reuse some input values.
|
||||
* @param {Function} fun - the function to call with partial information
|
||||
* @param {Object} input - the constant input which we want to supply now
|
||||
* @returns
|
||||
*/
|
||||
module.exports.partial = function (fun, input = {}) {
|
||||
return async (remaining) => await fun({ ...remaining, ...input });
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows you to assert a specific code result from a POST call.
|
||||
* @param {Object} input - the information needed to make the call
|
||||
* (path & payload) and the expected code (code)
|
||||
*/
|
||||
module.exports.expectPostCallToReturn = async function (
|
||||
input = {
|
||||
code,
|
||||
path,
|
||||
payload: {},
|
||||
}
|
||||
) {
|
||||
const res = await resolveCall(input.path, input.payload, header);
|
||||
determineFail(res, input.code, input.path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows you to assert a specific code result from multiple POST
|
||||
* calls.
|
||||
* @param {Object} input - the collection of objects to execute
|
||||
* calls from (col), a function which yields the path (pathf),
|
||||
* and another one which provides the payload (payloadf), as
|
||||
* well as the code (code) which we want to assert.
|
||||
*/
|
||||
module.exports.expectAllPostCallsToReturn = async function (
|
||||
input = {
|
||||
code,
|
||||
col,
|
||||
pathf,
|
||||
payloadf,
|
||||
}
|
||||
) {
|
||||
for (const i of input.col) {
|
||||
const res = await resolveCall(
|
||||
input.pathf(i),
|
||||
input.payloadf ? input.payloadf(i) : {},
|
||||
header
|
||||
);
|
||||
determineFail(res, input.code, input.pathf(i));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows you to retrieve a specific object the response
|
||||
* from get call,
|
||||
* and simultaneously assert that the call was successful.
|
||||
* @param {Object} param0 - contains the path which to
|
||||
* call (path), and the object within the response.data (get)
|
||||
* we want to retrieve.
|
||||
* @returns {Object} found within response.data corresponding
|
||||
* to the get parameter provided.
|
||||
*/
|
||||
module.exports.callGet = async function ({ path, get }) {
|
||||
const api = useApi();
|
||||
const res = await api.get(path, header);
|
||||
|
||||
determineFail(res.status, 200, path);
|
||||
return res?.data[get];
|
||||
};
|
||||
@@ -0,0 +1,118 @@
|
||||
const {
|
||||
ClaimOrder,
|
||||
Order,
|
||||
LineItem,
|
||||
Fulfillment,
|
||||
Return,
|
||||
} = require("@medusajs/medusa");
|
||||
|
||||
module.exports = async (connection, data = {}) => {
|
||||
const manager = connection.manager;
|
||||
|
||||
let orderWithClaim = manager.create(Order, {
|
||||
id: "order-with-claim",
|
||||
customer_id: "test-customer",
|
||||
email: "test@email.com",
|
||||
payment_status: "captured",
|
||||
fulfillment_status: "fulfilled",
|
||||
billing_address: {
|
||||
id: "test-billing-address",
|
||||
first_name: "lebron",
|
||||
},
|
||||
shipping_address: {
|
||||
id: "test-shipping-address",
|
||||
first_name: "lebron",
|
||||
country_code: "us",
|
||||
},
|
||||
region_id: "test-region",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
discounts: [],
|
||||
payments: [
|
||||
{
|
||||
id: "test-payment-for-claim-order",
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
items: [],
|
||||
...data,
|
||||
});
|
||||
|
||||
await manager.save(orderWithClaim);
|
||||
|
||||
const li = manager.create(LineItem, {
|
||||
id: "test-item-co-2",
|
||||
fulfilled_quantity: 1,
|
||||
title: "Line Item",
|
||||
description: "Line Item Desc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 8000,
|
||||
quantity: 1,
|
||||
variant_id: "test-variant",
|
||||
order_id: orderWithClaim.id,
|
||||
});
|
||||
|
||||
await manager.save(li);
|
||||
|
||||
const li2 = manager.create(LineItem, {
|
||||
id: "test-item-co-3",
|
||||
fulfilled_quantity: 4,
|
||||
title: "Line Item",
|
||||
description: "Line Item Desc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 8000,
|
||||
quantity: 4,
|
||||
variant_id: "test-variant",
|
||||
order_id: orderWithClaim.id,
|
||||
});
|
||||
|
||||
await manager.save(li2);
|
||||
|
||||
const claimWithFulfillment = manager.create(ClaimOrder, {
|
||||
id: "claim-w-f",
|
||||
type: "replace",
|
||||
payment_status: "na",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
order_id: "order-with-claim",
|
||||
...data,
|
||||
});
|
||||
|
||||
const ful1 = manager.create(Fulfillment, {
|
||||
id: "fulfillment-co-1",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
});
|
||||
|
||||
const ful2 = manager.create(Fulfillment, {
|
||||
id: "fulfillment-co-2",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
});
|
||||
|
||||
claimWithFulfillment.fulfillments = [ful1, ful2];
|
||||
|
||||
await manager.save(claimWithFulfillment);
|
||||
|
||||
const claimWithReturn = manager.create(ClaimOrder, {
|
||||
id: "claim-w-r",
|
||||
type: "replace",
|
||||
payment_status: "na",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
order_id: "order-with-claim",
|
||||
...data,
|
||||
});
|
||||
|
||||
await manager.save(claimWithReturn);
|
||||
|
||||
await manager.insert(Return, {
|
||||
id: "return-id-2",
|
||||
claim_order_id: "claim-w-r",
|
||||
status: "requested",
|
||||
refund_amount: 0,
|
||||
data: {},
|
||||
});
|
||||
};
|
||||
@@ -9,6 +9,7 @@ const {
|
||||
Product,
|
||||
ProductVariant,
|
||||
Region,
|
||||
Payment,
|
||||
Order,
|
||||
Swap,
|
||||
} = require("@medusajs/medusa");
|
||||
@@ -183,4 +184,140 @@ module.exports = async (connection, data = {}) => {
|
||||
price: 1000,
|
||||
data: {},
|
||||
});
|
||||
|
||||
const orderTemplate = () => {
|
||||
return {
|
||||
customer_id: "test-customer",
|
||||
email: "test@gmail.com",
|
||||
payment_status: "not_paid",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
region_id: "test-region",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
const paymentTemplate = () => {
|
||||
return {
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
};
|
||||
};
|
||||
|
||||
const orderWithClaim = manager.create(Order, {
|
||||
id: "test-order-w-c",
|
||||
claims: [
|
||||
{
|
||||
type: "replace",
|
||||
id: "claim-1",
|
||||
payment_status: "na",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
payment_provider: "test-pay",
|
||||
},
|
||||
],
|
||||
...orderTemplate(),
|
||||
});
|
||||
|
||||
await manager.save(orderWithClaim);
|
||||
|
||||
await manager.insert(Payment, {
|
||||
order_id: "test-order-w-c",
|
||||
id: "o-pay1",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
|
||||
const orderWithSwap = manager.create(Order, {
|
||||
id: "test-order-w-s",
|
||||
...orderTemplate(),
|
||||
});
|
||||
|
||||
await manager.save(orderWithSwap);
|
||||
|
||||
await manager.insert(Payment, {
|
||||
order_id: "test-order-w-s",
|
||||
id: "o-pay2",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
|
||||
const swap1 = manager.create(Swap, {
|
||||
id: "swap-1",
|
||||
order_id: "test-order-w-s",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
payment_status: "not_paid",
|
||||
});
|
||||
|
||||
const pay1 = manager.create(Payment, {
|
||||
id: "pay1",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
|
||||
swap1.payment = pay1;
|
||||
|
||||
const swap2 = manager.create(Swap, {
|
||||
id: "swap-2",
|
||||
order_id: "test-order-w-s",
|
||||
fulfillment_status: "not_fulfilled",
|
||||
payment_status: "not_paid",
|
||||
});
|
||||
|
||||
const pay2 = manager.create(Payment, {
|
||||
id: "pay2",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
|
||||
swap2.payment = pay2;
|
||||
|
||||
await manager.save(swap1);
|
||||
await manager.save(swap2);
|
||||
|
||||
const orderWithFulfillment = manager.create(Order, {
|
||||
id: "test-order-w-f",
|
||||
fulfillments: [
|
||||
{
|
||||
id: "fulfillment-on-order-1",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
},
|
||||
{
|
||||
id: "fulfillment-on-order-2",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
},
|
||||
],
|
||||
...orderTemplate(),
|
||||
});
|
||||
|
||||
await manager.save(orderWithFulfillment);
|
||||
|
||||
await manager.insert(Payment, {
|
||||
order_id: "test-order-w-f",
|
||||
id: "o-pay3",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
|
||||
const orderWithReturn = manager.create(Order, {
|
||||
id: "test-order-w-r",
|
||||
returns: [
|
||||
{
|
||||
id: "return-on-order-1",
|
||||
refund_amount: 0,
|
||||
},
|
||||
{
|
||||
id: "return-on-order-2",
|
||||
refund_amount: 0,
|
||||
},
|
||||
],
|
||||
...orderTemplate(),
|
||||
});
|
||||
|
||||
await manager.save(orderWithReturn);
|
||||
|
||||
await manager.insert(Payment, {
|
||||
order_id: "test-order-w-r",
|
||||
id: "o-pay4",
|
||||
...paymentTemplate(),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@ const {
|
||||
Product,
|
||||
ProductVariant,
|
||||
Region,
|
||||
Payment,
|
||||
Order,
|
||||
Swap,
|
||||
Cart,
|
||||
@@ -43,7 +44,7 @@ module.exports = async (connection, data = {}) => {
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test",
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
@@ -80,7 +81,7 @@ module.exports = async (connection, data = {}) => {
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test",
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
additional_items: [
|
||||
@@ -100,6 +101,70 @@ module.exports = async (connection, data = {}) => {
|
||||
|
||||
await manager.save(swap);
|
||||
|
||||
const cartTemplate = async (cartId) => {
|
||||
const cart = manager.create(Cart, {
|
||||
id: cartId,
|
||||
customer_id: "test-customer",
|
||||
email: "test-customer@email.com",
|
||||
shipping_address_id: "test-shipping-address",
|
||||
billing_address_id: "test-billing-address",
|
||||
region_id: "test-region",
|
||||
type: "swap",
|
||||
metadata: {},
|
||||
...data,
|
||||
});
|
||||
|
||||
await manager.save(cart);
|
||||
};
|
||||
|
||||
const swapTemplate = async (cartId) => {
|
||||
await cartTemplate(cartId);
|
||||
return {
|
||||
order_id: orderWithSwap.id,
|
||||
fulfillment_status: "fulfilled",
|
||||
payment_status: "not_paid",
|
||||
cart_id: cartId,
|
||||
payment: {
|
||||
amount: 5000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
const swapWithFulfillments = manager.create(Swap, {
|
||||
id: "swap-w-f",
|
||||
fulfillments: [
|
||||
{
|
||||
id: "fulfillment-1",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
},
|
||||
{
|
||||
id: "fulfillment-2",
|
||||
data: {},
|
||||
provider_id: "test-ful",
|
||||
},
|
||||
],
|
||||
...(await swapTemplate("sc-w-f")),
|
||||
});
|
||||
|
||||
await manager.save(swapWithFulfillments);
|
||||
|
||||
const swapWithReturn = manager.create(Swap, {
|
||||
id: "swap-w-r",
|
||||
return_order: {
|
||||
id: "return-id",
|
||||
status: "requested",
|
||||
refund_amount: 0,
|
||||
},
|
||||
...(await swapTemplate("sc-w-r")),
|
||||
});
|
||||
|
||||
await manager.save(swapWithReturn);
|
||||
const li = manager.create(LineItem, {
|
||||
id: "return-item-1",
|
||||
fulfilled_quantity: 1,
|
||||
@@ -175,7 +240,7 @@ module.exports = async (connection, data = {}) => {
|
||||
amount: 10000,
|
||||
currency_code: "usd",
|
||||
amount_refunded: 0,
|
||||
provider_id: "test",
|
||||
provider_id: "test-pay",
|
||||
data: {},
|
||||
},
|
||||
additional_items: [
|
||||
|
||||
Reference in New Issue
Block a user