chore: Move factories and helpers to a better place (#4551)

* chore: Move factories and helpers to a better place

* align factory product variant

* fix factory cart

* add simple store fac

* fix tests

* fix tests

* fix

* fix cart seeder
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-07-20 13:16:04 +02:00
1 parent 26e2f81c24
commit 4d326fbbdf
197 files changed
+1073 -4332

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
const Scrypt = require("scrypt-kdf")
const { User } = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 })
const password_hash = buf.toString("base64")
await manager.insert(User, {
id: "admin_user",
email: "admin@medusa.js",
api_token: "test_token",
role: "admin",
password_hash,
...data,
})
}
@@ -0,0 +1,192 @@
const {
Region,
Product,
ProductVariant,
PriceList,
CustomerGroup,
Customer,
Image,
ShippingProfile,
ProductCollection,
ProductOption,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
new Date()
)
const tenDaysAgo = ((today) => new Date(today.setDate(today.getDate() - 10)))(
new Date()
)
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfile.default },
})
const collection = manager.create(ProductCollection, {
id: "test-collection",
handle: "test-collection",
title: "Test collection",
})
await manager.save(collection)
await manager.insert(Region, {
id: "reg-europe",
name: "Test Region Europe",
currency_code: "eur",
tax_rate: 0,
})
await manager.insert(Region, {
id: "reg-us",
name: "Test Region US",
currency_code: "usd",
tax_rate: 0,
})
const customer = await manager.create(Customer, {
id: "test-customer",
email: "john@doe.com",
first_name: "John",
last_name: "Doe",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: true,
})
const customerGroup = await manager.create(CustomerGroup, {
id: "test-group",
name: "test-group",
})
await manager.save(customerGroup)
customer.groups = [customerGroup]
await manager.save(customer)
const priceListActive = await manager.create(PriceList, {
id: "pl",
name: "VIP sale",
description: "All year sale for VIP customers.",
type: "sale",
status: "active",
})
await manager.save(priceListActive)
const priceListExpired = await manager.create(PriceList, {
id: "pl_expired",
name: "VIP summer sale",
description: "Summer sale for VIP customers.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: yesterday,
})
await manager.save(priceListExpired)
const priceListWithCustomers = await manager.create(PriceList, {
id: "pl_with_customers",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
priceListWithCustomers.customer_groups = [customerGroup]
await manager.save(priceListWithCustomers)
const productMultiReg = manager.create(Product, {
id: "test-product",
handle: "test-product-reg",
title: "Multi Reg Test product",
profile_id: defaultProfile.id,
description: "test-product-description",
status: "published",
collection_id: "test-collection",
})
const image = manager.create(Image, {
id: "test-image",
url: "test-image.png",
})
productMultiReg.images = [image]
await manager.save(productMultiReg)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product",
})
const variantMultiReg = await manager.create(ProductVariant, {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
variant_rank: 0,
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
barcode: "test-barcode",
product_id: "test-product",
prices: [
{
id: "test-price-multi-usd",
currency_code: "usd",
type: "default",
amount: 100,
},
{
id: "test-price-discount-multi-usd",
currency_code: "usd",
amount: 80,
price_list_id: "pl",
},
{
id: "test-price-discount-expired-multi-usd",
currency_code: "usd",
amount: 70,
price_list_id: "pl_expired",
},
{
id: "test-price-multi-eur",
currency_code: "eur",
amount: 100,
},
{
id: "test-price-discount-multi-eur",
currency_code: "eur",
amount: 80,
price_list_id: "pl",
},
{
id: "test-price-discount-multi-eur-with-customer",
currency_code: "eur",
amount: 40,
price_list_id: "pl_with_customers",
},
{
id: "test-price-discount-expired-multi-eur",
currency_code: "eur",
amount: 70,
price_list_id: "pl_expired",
},
],
options: [
{
id: "test-variant-option-reg",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variantMultiReg)
}
@@ -0,0 +1,26 @@
const { Region } = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
await manager.insert(Region, {
id: "region-product-import-0",
name: "ImportLand",
currency_code: "eur",
tax_rate: 0,
})
await manager.insert(Region, {
id: "region-product-import-1",
name: "denmark",
currency_code: "dkk",
tax_rate: 0,
})
await manager.insert(Region, {
id: "region-product-import-2",
name: "Denmark",
currency_code: "dkk",
tax_rate: 0,
})
}
-26
View File
@@ -1,26 +0,0 @@
const path = require("path")
const express = require("express")
const getPort = require("get-port")
module.exports = {
bootstrapApp: async ({ cwd } = {}) => {
const app = express()
const loaders = require("@medusajs/medusa/dist/loaders").default
const { container, dbConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
})
const PORT = await getPort()
return {
container,
db: dbConnection,
app,
port: PORT,
}
},
}
+100
View File
@@ -0,0 +1,100 @@
const { useApi } = require("../environment-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.
* @return {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]
}
+953
View File
@@ -0,0 +1,953 @@
const {
Customer,
Region,
Cart,
DiscountRule,
Discount,
ShippingProfile,
ShippingOption,
ShippingMethod,
Address,
Product,
ProductVariant,
MoneyAmount,
LineItem,
Payment,
PaymentSession,
CustomerGroup,
PriceList,
ShippingProfileType,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const salesChannelId = data?.sales_channel_id
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
new Date()
)
const tomorrow = ((today) => new Date(today.setDate(today.getDate() + 1)))(
new Date()
)
const tenDaysAgo = ((today) => new Date(today.setDate(today.getDate() - 10)))(
new Date()
)
const tenDaysFromToday = ((today) =>
new Date(today.setDate(today.getDate() + 10)))(new Date())
const manager = dataSource.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
const gcProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.GIFT_CARD },
})
await manager.insert(Address, {
id: "test-general-address",
first_name: "superman",
country_code: "us",
})
const r = manager.create(Region, {
id: "test-region",
name: "Test Region",
payment_providers: [{ id: "test-pay" }],
currency_code: "usd",
tax_rate: 0,
})
await manager.save(r)
const europeRegion = manager.create(Region, {
id: "eur-region",
name: "Europe Region",
payment_providers: [{ id: "test-pay" }],
currency_code: "eur",
tax_rate: 0,
})
await manager.save(europeRegion)
// Region with multiple countries
const regionWithMultipleCoutries = manager.create(Region, {
id: "test-region-multiple",
name: "Test Region",
currency_code: "eur",
tax_rate: 0,
})
await manager.save(regionWithMultipleCoutries)
await manager.query(
`UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'no'`
)
await manager.query(
`UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'dk'`
)
const customer5 = await manager.create(Customer, {
id: "test-customer-5",
email: "test5@email.com",
first_name: "John",
last_name: "Deere",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: true,
})
await manager.save(customer5)
const c_group_5 = await manager.create(CustomerGroup, {
id: "test-group-5",
name: "test-group-5",
})
await manager.save(c_group_5)
customer5.groups = [c_group_5]
await manager.save(customer5)
const priceList_customer = await manager.create(PriceList, {
id: "pl_customer",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
priceList_customer.customer_groups = [c_group_5]
await manager.save(priceList_customer)
const freeRule = manager.create(DiscountRule, {
id: "free-shipping-rule",
description: "Free shipping rule",
type: "free_shipping",
value: 100,
allocation: "total",
})
const freeDisc = manager.create(Discount, {
id: "free-shipping",
code: "FREE_SHIPPING",
is_dynamic: false,
is_disabled: false,
})
freeDisc.regions = [r]
freeDisc.rule = freeRule
await manager.save(freeDisc)
const tenPercentRule = manager.create(DiscountRule, {
id: "tenpercent-rule",
description: "Ten percent rule",
type: "percentage",
value: 10,
allocation: "total",
})
const tenPercent = manager.create(Discount, {
id: "10Percent",
code: "10PERCENT",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
tenPercent.regions = [r, europeRegion]
tenPercent.rule = tenPercentRule
await manager.save(tenPercent)
const totalFixed100Rule = manager.create(DiscountRule, {
id: "total-fixed-100-rule",
description: "Fixed 100 total",
type: "fixed",
value: 100,
allocation: "total",
})
const totalFixed100 = manager.create(Discount, {
id: "total-fixed-100",
code: "FIXED100",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
totalFixed100.regions = [r]
totalFixed100.rule = totalFixed100Rule
await manager.save(totalFixed100)
const itemFixed200Rule = manager.create(DiscountRule, {
id: "item-fixed-200-rule",
description: "Item 200 fixed",
type: "fixed",
value: 200,
allocation: "item",
})
const itemFixed200 = manager.create(Discount, {
id: "item-fixed-200",
code: "FIXED200",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
itemFixed200.regions = [r]
itemFixed200.rule = itemFixed200Rule
await manager.save(itemFixed200)
const itemPerc15Rule = manager.create(DiscountRule, {
id: "item-percentage-15-rule",
description: "Item 15 percentage",
type: "percentage",
value: 15,
allocation: "item",
})
const itemPerc15 = manager.create(Discount, {
id: "item-percentage-15",
code: "15PERCENT",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
itemPerc15.regions = [r]
itemPerc15.rule = itemPerc15Rule
await manager.save(itemPerc15)
const dUsageLimit = await manager.create(Discount, {
id: "test-discount-usage-limit",
code: "SPENT",
is_dynamic: false,
is_disabled: false,
usage_limit: 10,
usage_count: 10,
})
const drUsage = await manager.create(DiscountRule, {
id: "test-discount-rule-usage-limit",
description: "Created",
type: "fixed",
value: 10000,
allocation: "total",
})
dUsageLimit.rule = drUsage
dUsageLimit.regions = [r]
await manager.save(dUsageLimit)
const d = await manager.create(Discount, {
id: "test-discount",
code: "CREATED",
is_dynamic: false,
is_disabled: false,
})
const dr = await manager.create(DiscountRule, {
id: "test-discount-rule",
description: "Created",
type: "fixed",
value: 10000,
allocation: "total",
})
d.rule = dr
d.regions = [r]
await manager.save(d)
const usedDiscount = manager.create(Discount, {
id: "used-discount",
code: "USED",
is_dynamic: false,
is_disabled: false,
usage_limit: 1,
usage_count: 1,
})
await manager.save(usedDiscount)
const expiredRule = manager.create(DiscountRule, {
id: "expiredRule",
description: "expired rule",
type: "fixed",
value: 100,
allocation: "total",
})
const expiredDisc = manager.create(Discount, {
id: "expiredDisc",
code: "EXP_DISC",
is_dynamic: false,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: yesterday,
})
expiredDisc.regions = [r]
expiredDisc.rule = expiredRule
await manager.save(expiredDisc)
const prematureRule = manager.create(DiscountRule, {
id: "prematureRule",
description: "premature rule",
type: "fixed",
value: 100,
allocation: "total",
})
const prematureDiscount = manager.create(Discount, {
id: "prematureDiscount",
code: "PREM_DISC",
is_dynamic: false,
is_disabled: false,
starts_at: tomorrow,
ends_at: tenDaysFromToday,
})
prematureDiscount.regions = [r]
prematureDiscount.rule = prematureRule
await manager.save(prematureDiscount)
const invalidDynamicRule = manager.create(DiscountRule, {
id: "invalidDynamicRule",
description: "invalidDynamic rule",
type: "fixed",
value: 100,
allocation: "total",
})
const invalidDynamicDiscount = manager.create(Discount, {
id: "invalidDynamicDiscount",
code: "INV_DYN_DISC",
is_dynamic: true,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: yesterday,
valid_duration: "P1D", // one day
})
invalidDynamicDiscount.regions = [r]
invalidDynamicDiscount.rule = invalidDynamicRule
await manager.save(invalidDynamicDiscount)
const DynamicRule = manager.create(DiscountRule, {
id: "DynamicRule",
description: "Dynamic rule",
type: "fixed",
value: 10000,
allocation: "total",
})
const DynamicDiscount = manager.create(Discount, {
id: "DynamicDiscount",
code: "DYN_DISC",
is_dynamic: true,
is_disabled: false,
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
valid_duration: "P1M", // one month
})
DynamicDiscount.regions = [r]
DynamicDiscount.rule = DynamicRule
await manager.save(DynamicDiscount)
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.insert(Customer, {
id: "test-customer",
email: "test@email.com",
})
const c2 = await manager.create(Customer, {
id: "test-customer-2",
email: "test-2@email.com",
})
const cg = await manager.create(CustomerGroup, {
id: "cgroup",
name: "customer group",
})
await manager.save(cg)
c2.groups = [cg]
await manager.save(c2)
await manager.insert(Customer, {
id: "some-customer",
email: "some-customer@email.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "gc-option",
name: "Digital copy",
provider_id: "test-ful",
region_id: "test-region",
profile_id: gcProfile.id,
price_type: "flat_rate",
amount: 0,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-option-2",
name: "test-option-2",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 500,
data: {},
})
const priceList = await manager.create(PriceList, {
id: "pl",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
await manager.save(priceList)
const priceList1 = await manager.create(PriceList, {
id: "pl_current",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
await manager.save(priceList1)
await manager.insert(Product, {
id: "giftcard-product",
title: "Giftcard",
is_giftcard: true,
discountable: false,
profile_id: gcProfile.id,
options: [{ id: "denom", title: "Denomination" }],
})
await manager.insert(ProductVariant, {
id: "giftcard-denom",
title: "1000",
product_id: "giftcard-product",
inventory_quantity: 1,
options: [
{
option_id: "denom",
value: "1000",
},
],
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.insert(ProductVariant, {
id: "test-variant-quantity",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1000,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-sale",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1000,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-sale-customer",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1000,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "test variant 2",
product_id: "test-product",
inventory_quantity: 0,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
type: "default",
amount: 1000,
})
await manager.save(ma)
const maEur = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "eur",
type: "default",
amount: 2000,
})
await manager.save(maEur)
const ma_sale = manager.create(MoneyAmount, {
variant_id: "test-variant-sale",
currency_code: "usd",
amount: 1000,
})
await manager.save(ma_sale)
const ma_sale_1 = manager.create(MoneyAmount, {
variant_id: "test-variant-sale",
currency_code: "usd",
amount: 800,
price_list_id: "pl_current",
})
await manager.save(ma_sale_1)
const ma_sale_customer = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-customer",
currency_code: "usd",
amount: 1000,
})
await manager.save(ma_sale_customer)
const ma_sale_customer_1 = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-customer",
currency_code: "usd",
amount: 700,
price_list_id: "pl_customer",
})
await manager.save(ma_sale_customer_1)
const ma_sale_customer_2 = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-customer",
currency_code: "usd",
amount: 800,
price_list_id: "pl_current",
})
await manager.save(ma_sale_customer_2)
const ma_quantity = manager.create(MoneyAmount, {
variant_id: "test-variant-quantity",
currency_code: "usd",
type: "default",
amount: 1000,
})
await manager.save(ma_quantity)
const ma_quantity_1 = manager.create(MoneyAmount, {
variant_id: "test-variant-quantity",
currency_code: "usd",
type: "sale",
min_quantity: 10,
max_quantity: 100,
amount: 800,
})
await manager.save(ma_quantity_1)
const ma_quantity_2 = manager.create(MoneyAmount, {
variant_id: "test-variant-quantity",
currency_code: "usd",
type: "sale",
min_quantity: 100,
amount: 700,
})
await manager.save(ma_quantity_2)
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
type: "default",
amount: 8000,
})
await manager.save(ma2)
const ma3 = manager.create(MoneyAmount, {
variant_id: "giftcard-denom",
currency_code: "usd",
type: "default",
amount: 1000,
})
await manager.save(ma3)
const cart = manager.create(Cart, {
id: "test-cart",
customer_id: "some-customer",
email: "some-customer@email.com",
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cart)
const cartWithTotalFixedDiscount = manager.create(Cart, {
id: "test-cart-w-total-fixed-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [totalFixed100],
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithTotalFixedDiscount)
const cartWithItemFixedDiscount = manager.create(Cart, {
id: "test-cart-w-item-fixed-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [itemFixed200],
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithItemFixedDiscount)
const cartWithTotalPercDiscount = manager.create(Cart, {
id: "test-cart-w-total-percentage-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [tenPercent],
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithTotalPercDiscount)
const cartWithItemPercDiscount = manager.create(Cart, {
id: "test-cart-w-item-percentage-discount",
customer_id: "some-customer",
email: "some-customer@email.com",
discounts: [itemPerc15],
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cartWithItemPercDiscount)
const cart2 = manager.create(Cart, {
id: "test-cart-2",
customer_id: "some-customer",
email: "some-customer@email.com",
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
})
const swapCart = manager.create(Cart, {
id: "swap-cart",
type: "swap",
customer_id: "some-customer",
email: "some-customer@email.com",
sales_channel_id: salesChannelId,
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
metadata: {
swap_id: "test-swap",
},
})
const pay = manager.create(Payment, {
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
cart2.payment = pay
await manager.save(cart2)
const swapPay = manager.create(Payment, {
id: "test-swap-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
await manager.save(swapPay)
cart2.payment = pay
swapCart.payment = swapPay
await manager.save(cart2)
await manager.save(swapCart)
await manager.insert(PaymentSession, {
id: "test-session",
cart_id: "test-cart-2",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
await manager.insert(PaymentSession, {
id: "test-swap-session",
cart_id: "swap-cart",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
await manager.insert(ShippingMethod, {
id: "test-method",
shipping_option_id: "test-option",
cart_id: "test-cart",
price: 1000,
data: {},
})
const li = manager.create(LineItem, {
id: "test-item",
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-2",
})
await manager.save(li)
const cart3 = manager.create(Cart, {
id: "test-cart-3",
customer_id: "some-customer",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
})
await manager.save(cart3)
const ps = manager.create(PaymentSession, {
id: "test-cart-session",
cart_id: "test-cart-3",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
await manager.save(ps)
cart3.payment_sessions = [ps]
cart3.payment_session = ps
await manager.save(cart3)
await manager.insert(ShippingMethod, {
id: "test-method-2",
shipping_option_id: "test-option",
cart_id: "test-cart-3",
price: 0,
data: {},
})
const li2 = manager.create(LineItem, {
id: "test-item-2",
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-3",
})
await manager.save(li2)
const cart4 = manager.create(Cart, {
id: "test-cart-4",
email: "some-customer@email.com",
shipping_address: {
id: "test-shipping-address",
first_name: "lebron",
country_code: "us",
},
region_id: "test-region",
currency_code: "usd",
completed_at: null,
items: [],
})
await manager.save(cart4)
await manager.insert(ProductVariant, {
id: "test-variant-sale-cg",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1000,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
const ma_cg = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-cg",
currency_code: "usd",
amount: 1000,
})
await manager.save(ma_cg)
const ma_sale_cg = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-cg",
currency_code: "usd",
price_list_id: "pl",
amount: 500,
})
await manager.save(ma_sale_cg)
const ma_sale_cg_new_region = manager.create(MoneyAmount, {
variant_id: "test-variant-sale-cg",
region_id: "test-region-multiple",
currency_code: "eur",
amount: 700,
})
await manager.save(ma_sale_cg_new_region)
const li3 = await manager.create(LineItem, {
id: "test-item3",
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant-sale-cg",
cart_id: "test-cart-3",
metadata: { "some-existing": "prop" },
})
await manager.save(li3)
}
+118
View File
@@ -0,0 +1,118 @@
const {
ClaimOrder,
Order,
LineItem,
Fulfillment,
Return,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const 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: {},
})
}
@@ -0,0 +1,20 @@
const AUTH_COOKIE = {}
export async function getClientAuthenticationCookie(
api,
email = null,
password = null
) {
const user = {
email: email ?? "test@medusajs.com",
password: password ?? "test",
}
if (AUTH_COOKIE[user.email]) {
return AUTH_COOKIE[user.email]
}
const authResponse = await api.post("/store/auth", user)
AUTH_COOKIE[user.email] = authResponse.headers["set-cookie"][0].split(";")
return AUTH_COOKIE[user.email]
}
@@ -0,0 +1,113 @@
const { Customer, Address, CustomerGroup } = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const testAddr = await manager.create(Address, {
id: "test-address",
first_name: "Lebron",
last_name: "James",
})
await manager.save(testAddr)
const customer = await manager.create(Customer, {
id: "test-customer-1",
email: "test1@email.com",
})
customer.billing_address = testAddr
customer.shipping_addresses = [testAddr]
await manager.save(customer)
await manager.insert(Customer, {
id: "test-customer-2",
email: "test2@email.com",
})
await manager.insert(Customer, {
id: "test-customer-3",
email: "test3@email.com",
})
await manager.insert(Customer, {
id: "test-customer-has_account",
email: "test4@email.com",
has_account: true,
})
const customer5 = await manager.create(Customer, {
id: "test-customer-5",
email: "test5@email.com",
})
await manager.save(customer5)
const customer6 = await manager.create(Customer, {
id: "test-customer-6",
email: "test6@email.com",
})
await manager.save(customer6)
const customer7 = await manager.create(Customer, {
id: "test-customer-7",
email: "test7@email.com",
})
await manager.save(customer7)
const deletionCustomer = await manager.create(Customer, {
id: "test-customer-delete-cg",
email: "test-deletetion-cg@email.com",
})
await manager.save(deletionCustomer)
await manager.insert(CustomerGroup, {
id: "customer-group-1",
name: "vip-customers",
})
await manager.insert(CustomerGroup, {
id: "customer-group-2",
name: "test-group-2",
metadata: { data1: "value1" },
})
await manager.insert(CustomerGroup, {
id: "customer-group-3",
name: "test-group-3",
})
await manager.insert(CustomerGroup, {
id: "test-group-4",
name: "test-group-4",
})
const c_group_5 = await manager.create(CustomerGroup, {
id: "test-group-5",
name: "test-group-5",
})
await manager.save(c_group_5)
const c_group_6 = await manager.create(CustomerGroup, {
id: "test-group-6",
name: "test-group-6",
})
await manager.save(c_group_6)
customer5.groups = [c_group_5]
await manager.save(customer5)
customer6.groups = [c_group_5]
await manager.save(customer6)
customer7.groups = [c_group_5, c_group_6]
await manager.save(customer7)
const c_group_delete = await manager.create(CustomerGroup, {
id: "test-group-delete",
name: "test-group-delete",
})
await manager.save(c_group_delete)
deletionCustomer.groups = [c_group_delete]
await manager.save(deletionCustomer)
}
@@ -0,0 +1,35 @@
const {
ShippingProfile,
Region,
Discount,
DiscountRule,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(Region, {
id: "test-region-2",
name: "Test Region 2",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
}
@@ -0,0 +1,315 @@
const {
ShippingProfile,
Customer,
MoneyAmount,
ShippingOption,
ShippingOptionRequirement,
Product,
ProductVariant,
Region,
Address,
Cart,
PaymentSession,
DraftOrder,
Discount,
DiscountRule,
Payment,
ShippingProfileType,
} = require("@medusajs/medusa")
const { simpleSalesChannelFactory } = require("../factories")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
const salesChannel = await simpleSalesChannelFactory(dataSource, {
id: "sales-channel",
is_default: true,
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.query(
`insert into product_sales_channel values ('test-product', '${salesChannel.id}');`
)
await manager.insert(Address, {
id: "oli-shipping",
first_name: "oli",
last_name: "test",
country_code: "us",
})
await manager.insert(Product, {
id: "test-product-2",
title: "test product 2",
profile_id: defaultProfile.id,
options: [{ id: "test-option-color", title: "Color" }],
})
await manager.query(
`insert into product_sales_channel values ('test-product-2', '${salesChannel.id}');`
)
await manager.insert(ProductVariant, {
id: "test-variant-without-prices",
title: "test variant without prices",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "no prices",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "test variant-2",
product_id: "test-product-2",
inventory_quantity: 4,
options: [
{
option_id: "test-option-color",
value: "Color",
},
],
})
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma)
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
amount: 10000,
})
await manager.save(ma2)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(Region, {
id: "test-region-2",
name: "Test Region 2",
currency_code: "eur",
tax_rate: 0,
payment_providers: [
{
id: "test-pay",
is_installed: true,
},
],
})
await manager.insert(DiscountRule, {
id: "discount_rule_id",
description: "test description",
value: 10,
allocation: "total",
type: "percentage",
})
await manager.insert(DiscountRule, {
id: "free-shipping-rule",
description: "Free shipping rule",
type: "free_shipping",
value: 100,
allocation: "total",
})
const testDiscount = manager.create(Discount, {
id: "test-discount",
code: "TEST",
is_dynamic: false,
is_disabled: false,
rule_id: "discount_rule_id",
})
const freeShippingDiscount = manager.create(Discount, {
id: "free-shipping-discount",
code: "FREE-SHIPPING",
is_dynamic: false,
is_disabled: false,
rule_id: "free-shipping-rule",
})
testDiscount.regions = [
{
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
},
]
freeShippingDiscount.regions = [
{
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
},
]
await manager.save(testDiscount)
await manager.save(freeShippingDiscount)
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.query(
`UPDATE "country" SET region_id='test-region-2' WHERE iso_2 = 'de'`
)
await manager.insert(Customer, {
id: "oli-test",
email: "oli@test.dk",
})
await manager.insert(Customer, {
id: "lebron-james",
email: "lebron@james.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-option-req",
name: "test-option-req",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req",
shipping_option_id: "test-option-req",
type: "min_subtotal",
amount: 10,
})
const c = manager.create(Cart, {
id: "test-cart",
customer_id: "oli-test",
email: "oli@test.dk",
shipping_address_id: "oli-shipping",
region_id: "test-region",
currency_code: "usd",
payment_sessions: [],
items: [
{
id: "test-item",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
type: "draft_order",
metadata: { draft_order_id: "test-draft-order" },
})
const pay = manager.create(Payment, {
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
})
await manager.save(pay)
c.payment = pay
await manager.save(c)
await manager.insert(PaymentSession, {
id: "test-session",
cart_id: "test-cart",
provider_id: "test-pay",
is_selected: true,
data: {},
status: "authorized",
})
const draftOrder = manager.create(DraftOrder, {
id: "test-draft-order",
status: "open",
display_id: 4,
cart_id: "test-cart",
customer_id: "oli-test",
items: [
{
id: "test-item",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
email: "oli@test.dk",
region_id: "test-region",
discounts: [],
...data,
})
await manager.save(draftOrder)
}
+416
View File
@@ -0,0 +1,416 @@
const {
Customer,
Discount,
DiscountRule,
LineItem,
MoneyAmount,
Order,
Payment,
Product,
ProductVariant,
Region,
ShippingMethod,
ShippingOption,
ShippingProfile,
Swap,
ShippingProfileType,
} = require("@medusajs/medusa")
const { simpleSalesChannelFactory } = require("../factories")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
const salesChannel = await simpleSalesChannelFactory(dataSource, {
id: "test-channel",
is_default: true,
})
await manager.insert(Product, {
id: "test-product",
title: "test product",
profile_id: defaultProfile.id,
options: [{ id: "test-option", title: "Size" }],
})
await manager.query(
`insert into product_sales_channel values ('test-product', '${salesChannel.id}');`
)
await manager.insert(ProductVariant, {
id: "test-variant",
title: "test variant",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Size",
},
],
})
await manager.insert(ProductVariant, {
id: "test-variant-2",
title: "Swap product",
product_id: "test-product",
inventory_quantity: 1,
options: [
{
option_id: "test-option",
value: "Large",
},
],
})
const ma2 = manager.create(MoneyAmount, {
variant_id: "test-variant-2",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma2)
const ma = manager.create(MoneyAmount, {
variant_id: "test-variant",
currency_code: "usd",
amount: 8000,
})
await manager.save(ma)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
)
await manager.insert(Customer, {
id: "test-customer",
email: "test@email.com",
})
await manager.insert(ShippingOption, {
id: "test-option",
name: "test-option",
provider_id: "test-ful",
region_id: "test-region",
profile_id: defaultProfile.id,
price_type: "flat_rate",
amount: 1000,
data: {},
})
await manager.insert(ShippingOption, {
id: "test-return-option",
name: "Test ret",
profile_id: defaultProfile.id,
region_id: "test-region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 1000,
is_return: true,
})
const order = manager.create(Order, {
id: "test-order",
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: [
{
id: "test-discount",
code: "TEST134",
is_dynamic: false,
rule: {
id: "test-rule",
description: "Test Discount",
type: "percentage",
value: 10,
allocation: "total",
},
is_disabled: false,
regions: [
{
id: "test-region",
},
],
},
],
payments: [
{
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
captured_at: new Date(),
data: {},
},
],
items: [],
...data,
})
await manager.save(order)
const li = manager.create(LineItem, {
id: "test-item",
fulfilled_quantity: 1,
returned_quantity: 0,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: "test-order",
adjustments: [
{
amount: 800,
discount_id: "test-discount",
description: "discount",
item_id: "test-item",
},
],
})
await manager.save(li)
await manager.insert(ShippingMethod, {
id: "test-method",
order_id: "test-order",
shipping_option_id: "test-option",
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(),
})
const drule = manager.create(DiscountRule, {
id: "test-rule",
description: "Test Discount",
type: "percentage",
value: 10,
allocation: "total",
})
const discount = manager.create(Discount, {
id: "test-discount-o",
code: "TEST1234",
is_dynamic: false,
rule: drule,
is_disabled: false,
regions: [
{
id: "test-region",
},
],
})
await manager.save(discount)
const payment = manager.create(Payment, {
id: "test-payment-d",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
captured_at: new Date(),
data: {},
})
const discountedOrder = manager.create(Order, {
id: "discount-order",
customer_id: "test-customer",
email: "test-discount@email.com",
payment_status: "captured",
fulfillment_status: "fulfilled",
discounts: [discount],
billing_address: {
id: "test-discount-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,
payments: [payment],
items: [],
...data,
})
await manager.save(discountedOrder)
const dli = manager.create(LineItem, {
id: "test-item-1",
fulfilled_quantity: 1,
returned_quantity: 0,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: "discount-order",
})
await manager.save(dli)
}
@@ -0,0 +1,83 @@
const { Region, PriceList, MoneyAmount } = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const priceListNoCustomerGroups = await manager.create(PriceList, {
id: "pl_no_customer_groups",
name: "VIP winter sale",
description: "Winter sale for VIP customers. 25% off selected items.",
type: "sale",
status: "active",
starts_at: "2022-07-01T00:00:00.000Z",
ends_at: "2022-07-31T00:00:00.000Z",
})
await manager.save(priceListNoCustomerGroups)
await manager.insert(Region, {
id: "region-pl",
name: "Test Region",
currency_code: "eur",
tax_rate: 0,
})
const moneyAmount1 = await manager.create(MoneyAmount, {
id: "ma_test_1",
amount: 100,
currency_code: "usd",
min_quantity: 1,
max_quantity: 100,
variant_id: "test-variant",
price_list_id: "pl_no_customer_groups",
})
await manager.save(moneyAmount1)
const moneyAmount2 = await manager.create(MoneyAmount, {
id: "ma_test_2",
amount: 80,
currency_code: "usd",
min_quantity: 101,
max_quantity: 500,
variant_id: "test-variant",
price_list_id: "pl_no_customer_groups",
})
await manager.save(moneyAmount2)
const moneyAmount3 = await manager.create(MoneyAmount, {
id: "ma_test_3",
amount: 50,
currency_code: "usd",
min_quantity: 501,
max_quantity: 1000,
variant_id: "test-variant",
price_list_id: "pl_no_customer_groups",
})
await manager.save(moneyAmount3)
const moneyAmount4 = await manager.create(MoneyAmount, {
id: "ma_test_4",
amount: 70,
currency_code: "usd",
variant_id: "test-variant",
})
await manager.save(moneyAmount4)
const priceListWithMA = await manager.create(PriceList, {
id: "pl_with_some_ma",
name: "Weeken sale",
description: "Desc. of the list",
type: "sale",
status: "active",
starts_at: "2022-07-01T00:00:00.000Z",
ends_at: "2022-07-31T00:00:00.000Z",
})
priceListWithMA.prices = [moneyAmount4]
await manager.save(priceListWithMA)
}
@@ -0,0 +1,884 @@
const {
Customer,
CustomerGroup,
ShippingProfile,
Product,
ProductVariant,
ProductCollection,
ProductOption,
Region,
Cart,
PriceList,
ShippingProfileType,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
new Date()
)
const tomorrow = ((today) => new Date(today.setDate(today.getDate() + 1)))(
new Date()
)
const tenDaysAgo = ((today) => new Date(today.setDate(today.getDate() - 10)))(
new Date()
)
const tenDaysFromToday = ((today) =>
new Date(today.setDate(today.getDate() + 10)))(new Date())
const manager = dataSource.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
await manager.insert(Region, {
id: "test-region-1",
name: "Test Region no prices",
currency_code: "usd",
tax_rate: 0,
})
await manager.insert(Region, {
id: "test-region-2",
name: "Test Region no prices",
currency_code: "dkk",
tax_rate: 0,
})
const coll = manager.create(ProductCollection, {
id: "test-collection",
handle: "test-collection",
title: "Test collection",
})
await manager.save(coll)
const customer5 = await manager.create(Customer, {
id: "test-customer-5",
email: "test5@email.com",
first_name: "John",
last_name: "Deere",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: true,
})
await manager.save(customer5)
const customer6 = await manager.create(Customer, {
id: "test-customer-6",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
email: "test6@email.com",
has_account: true,
})
await manager.save(customer6)
const customer7 = await manager.create(Customer, {
id: "test-customer-7",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
email: "test7@email.com",
has_account: true,
})
await manager.save(customer7)
const c_group_5 = await manager.create(CustomerGroup, {
id: "test-group-5",
name: "test-group-5",
})
await manager.save(c_group_5)
const c_group_6 = await manager.create(CustomerGroup, {
id: "test-group-6",
name: "test-group-6",
})
await manager.save(c_group_6)
customer5.groups = [c_group_5]
await manager.save(customer5)
customer6.groups = [c_group_6]
await manager.save(customer6)
customer7.groups = [c_group_5, c_group_6]
await manager.save(customer7)
const priceList = await manager.create(PriceList, {
id: "pl",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
await manager.save(priceList)
const priceList1 = await manager.create(PriceList, {
id: "pl_1",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
priceList1.customer_groups = [c_group_5]
await manager.save(priceList1)
const priceList2 = await manager.create(PriceList, {
id: "pl_2",
name: "VVIP winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
})
priceList2.customer_groups = [c_group_6]
await manager.save(priceList2)
const priceList3 = await manager.create(PriceList, {
id: "pl_expired",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: yesterday,
})
await manager.save(priceList3)
const priceList4 = await manager.create(PriceList, {
id: "pl_upcoming",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tomorrow,
ends_at: tenDaysFromToday,
})
await manager.save(priceList4)
const priceList5 = await manager.create(PriceList, {
id: "pl_current",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
await manager.save(priceList5)
const priceList6 = await manager.create(PriceList, {
id: "pl_current_1",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: tomorrow,
})
await manager.save(priceList6)
const priceList7 = await manager.create(PriceList, {
id: "pl_upcoming-customer",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tomorrow,
ends_at: tenDaysFromToday,
})
priceList7.customer_groups = [c_group_5]
await manager.save(priceList7)
const priceList8 = await manager.create(PriceList, {
id: "pl_current-customer",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: tenDaysFromToday,
})
priceList8.customer_groups = [c_group_5]
await manager.save(priceList8)
const priceList9 = await manager.create(PriceList, {
id: "pl_expired-customer",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: yesterday,
})
priceList9.customer_groups = [c_group_5]
await manager.save(priceList9)
const p1 = await manager.create(Product, {
id: "test-product",
handle: "test-product",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p1)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product",
})
const variant4 = await manager.create(ProductVariant, {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku",
ean: "test-ean",
status: "published",
upc: "test-upc",
product_id: "test-product",
prices: [
{
id: "test-price",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl_1",
},
{
id: "test-price1",
region_id: "test-region",
currency_code: "usd",
amount: 120,
},
{
id: "test-price2",
region_id: "test-region",
currency_code: "usd",
amount: 130,
price_list_id: "pl_2",
},
{
id: "test-price3",
region_id: "test-region",
currency_code: "usd",
amount: 110,
price_list_id: "pl",
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant4)
const p2 = await manager.create(Product, {
id: "test-product-quantity",
handle: "test-product-quantity",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p2)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product-quantity",
})
const variant_quantity = await manager.create(ProductVariant, {
id: "test-variant-quantity",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-quantity",
ean: "test-ean-quantity",
status: "published",
upc: "test-upc-quantity",
product_id: "test-product-quantity",
prices: [
{
id: "test-price-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl",
min_quantity: 10,
max_quantity: 100,
},
{
id: "test-price1-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 120,
price_list_id: "pl",
min_quantity: 101,
max_quantity: 1000,
},
{
id: "test-price1-quantity-group",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_1",
},
{
id: "test-price2-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 130,
price_list_id: "pl",
max_quantity: 9,
},
{
id: "test-price3-quantity-expired",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_expired",
},
{
id: "test-price3-quantity-future",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_upcoming",
},
{
id: "test-price3-quantity-now",
region_id: "test-region",
currency_code: "usd",
amount: 140,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_current",
},
{
id: "test-price3-quantity-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant_quantity)
const p3 = await manager.create(Product, {
id: "test-product-sale",
handle: "test-product-sale",
title: "Test product sale",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p3)
await manager.save(ProductOption, {
id: "test-option-sale",
title: "test-option sale",
product_id: "test-product-sale",
})
const variant_sale = await manager.create(ProductVariant, {
id: "test-variant-sale",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-sale",
ean: "test-ean-sale",
status: "published",
upc: "test-upc-sale",
product_id: "test-product-sale",
prices: [
{
id: "test-price-sale",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl_expired",
},
{
id: "test-price1-sale",
region_id: "test-region",
currency_code: "usd",
amount: 120,
price_list_id: "pl_current",
},
{
id: "test-price2-sale",
region_id: "test-region",
currency_code: "usd",
amount: 130,
price_list_id: "pl_upcoming",
},
{
id: "test-price2-sale-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant_sale)
const p4 = await manager.create(Product, {
id: "test-product-sale-overlap",
handle: "test-product-sale-overlap",
title: "Test product sale",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p4)
await manager.save(ProductOption, {
id: "test-option-sale-overlap",
title: "test-option sale",
product_id: "test-product-sale-overlap",
})
const variant_sale_overlap = await manager.create(ProductVariant, {
id: "test-variant-sale-overlap",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-sale-overlap",
ean: "test-ean-sale-overlap",
status: "published",
upc: "test-upc-sale-overlap",
product_id: "test-product-sale-overlap",
prices: [
{
id: "test-price-sale-overlap-1",
region_id: "test-region",
currency_code: "usd",
amount: 140,
price_list_id: "pl_current_1",
},
{
id: "test-price1-sale-overlap",
region_id: "test-region",
currency_code: "usd",
amount: 120,
price_list_id: "pl_current",
},
{
id: "test-price2-sale-overlap-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
},
],
options: [
{
id: "test-variant-option-overlap",
value: "Default variant",
option_id: "test-option-sale-overlap",
},
],
})
await manager.save(variant_sale_overlap)
const multiRegionProduct = await manager.create(Product, {
id: "test-product-multi-region",
handle: "test-product-multi-region",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-multi-region-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(multiRegionProduct)
await manager.save(ProductOption, {
id: "test-option-multi-region",
title: "test-option",
product_id: "test-product-multi-region",
})
const variant_multi_region = await manager.create(ProductVariant, {
id: "test-variant-multi-region",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-multi-region",
ean: "test-ean-multi-region",
status: "published",
upc: "test-upc-multi-region",
product_id: "test-product-multi-region",
prices: [
{
id: "test-price-region-1",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl",
},
{
id: "test-price1-region-1",
region_id: "test-region",
currency_code: "usd",
amount: 120,
},
{
id: "test-price1-region-2",
region_id: "test-region-2",
currency_code: "dkk",
amount: 130,
},
{
id: "test-price3-region-2",
region_id: "test-region-2",
currency_code: "dkk",
price_list_id: "pl",
amount: 110,
},
],
options: [
{
id: "test-variant-option-multi-region",
value: "Default variant",
option_id: "test-option-multi-region",
},
],
})
await manager.save(variant_multi_region)
const p5 = await manager.create(Product, {
id: "test-product-quantity-customer",
handle: "test-product-quantity-customer",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p5)
await manager.save(ProductOption, {
id: "test-option-quantity-customer",
title: "test-option",
product_id: "test-product-quantity-customer",
})
const variant_quantity_customer = await manager.create(ProductVariant, {
id: "test-variant-quantity-customer",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-quantity-customer",
ean: "test-ean-quantity-customer",
status: "published",
upc: "test-upc-quantity-customer",
product_id: "test-product-quantity-customer",
prices: [
{
id: "test-price-quantity-customer",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl",
min_quantity: 10,
max_quantity: 100,
},
{
id: "test-price1-quantity-customer",
region_id: "test-region",
currency_code: "usd",
amount: 120,
price_list_id: "pl",
min_quantity: 101,
max_quantity: 1000,
},
{
id: "test-price2-quantity-customer",
region_id: "test-region",
currency_code: "usd",
amount: 130,
price_list_id: "pl",
max_quantity: 9,
},
{
id: "test-price2-quantity-customer-group",
region_id: "test-region",
currency_code: "usd",
amount: 100,
max_quantity: 9,
price_list_id: "pl_1",
},
{
id: "test-price3-quantity-customer-expired",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_expired",
},
{
id: "test-price3-quantity-customer-future",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_upcoming",
},
{
id: "test-price3-quantity-customer-now",
region_id: "test-region",
currency_code: "usd",
amount: 140,
min_quantity: 101,
max_quantity: 1000,
price_list_id: "pl_current",
},
{
id: "test-price3-quantity-customer-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option-quantity-customer",
},
],
})
await manager.save(variant_quantity_customer)
const p6 = await manager.create(Product, {
id: "test-product-sale-customer",
handle: "test-product-sale-customer",
title: "Test product sale",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p6)
await manager.save(ProductOption, {
id: "test-option-sale",
title: "test-option sale",
product_id: "test-product-sale-customer",
})
const variant_sale_customer = await manager.create(ProductVariant, {
id: "test-variant-sale-customer",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-sale-customer",
ean: "test-ean-sale-customer",
status: "published",
upc: "test-upc-sale-customer",
product_id: "test-product-sale-customer",
prices: [
{
id: "test-price-sale-customer",
region_id: "test-region",
currency_code: "usd",
amount: 120,
price_list_id: "pl_expired-customer",
},
{
id: "test-price1-sale-customer",
region_id: "test-region",
currency_code: "usd",
amount: 100,
price_list_id: "pl_current-customer",
},
{
id: "test-price2-sale-customer",
region_id: "test-region",
currency_code: "usd",
amount: 130,
price_list_id: "pl_upcoming-customer",
},
{
id: "test-price2-sale-customer-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
type: "default",
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant_sale_customer)
const p7 = await manager.create(Product, {
id: "test-product-sale-customer-quantity",
handle: "test-product-sale-customer-quantity",
title: "Test product sale",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
tags: [],
})
await manager.save(p7)
await manager.save(ProductOption, {
id: "test-option-sale",
title: "test-option sale",
product_id: "test-product-sale-customer-quantity",
})
const variant_sale_customer_quantity = await manager.create(ProductVariant, {
id: "test-variant-sale-customer-quantity",
inventory_quantity: 10,
title: "Test variant",
sku: "test-sku-sale-customer-quantity",
ean: "test-ean-sale-customer-quantity",
status: "published",
upc: "test-upc-sale-customer-quantity",
product_id: "test-product-sale-customer-quantity",
prices: [
{
id: "test-price-sale-customer-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 120,
min_quantity: 100,
max_quantity: 1000,
price_list_id: "pl_expired-customer",
},
{
id: "test-price1-sale-customer-quantity-groups",
region_id: "test-region",
currency_code: "usd",
amount: 100,
max_quantity: 99,
price_list_id: "pl_current-customer",
},
{
id: "test-price2-sale-customer-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 130,
min_quantity: 500,
max_quantity: 900,
price_list_id: "pl_upcoming-customer",
},
{
id: "test-price2-sale-customer-quantity-default",
region_id: "test-region",
currency_code: "usd",
amount: 150,
},
{
id: "test-price1-sale-customer-quantity",
region_id: "test-region",
currency_code: "usd",
amount: 110,
max_quantity: 99,
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant_sale_customer_quantity)
const cart = await manager.create(Cart, {
id: "test-cart",
region_id: "test-region",
currency_code: "usd",
items: [],
})
await manager.save(cart)
const cart_region2 = await manager.create(Cart, {
id: "test-cart-1",
region_id: "test-region-2",
currency_code: "dkk",
items: [],
})
await manager.save(cart_region2)
const cart_region1 = await manager.create(Cart, {
id: "test-cart-2",
region_id: "test-region-1",
currency_code: "usd",
items: [],
})
await manager.save(cart_region1)
}
+334
View File
@@ -0,0 +1,334 @@
const {
ProductCollection,
ProductTag,
ProductType,
ProductOption,
Region,
Product,
ShippingProfile,
ProductVariant,
Image,
ShippingProfileType,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const defaultProfile = await manager.findOne(ShippingProfile, {
where: {
type: ShippingProfileType.DEFAULT,
},
})
const coll = await manager.create(ProductCollection, {
id: "test-collection",
handle: "test-collection",
title: "Test collection",
})
await manager.save(coll)
const coll1 = await manager.create(ProductCollection, {
id: "test-collection1",
handle: "test-collection1",
title: "Test collection 1",
})
await manager.save(coll1)
const coll2 = await manager.create(ProductCollection, {
id: "test-collection2",
handle: "test-collection2",
title: "Test collection 2",
})
await manager.save(coll2)
const tag = await manager.create(ProductTag, {
id: "tag1",
value: "123",
})
await manager.save(tag)
const tag3 = await manager.create(ProductTag, {
id: "tag3",
value: "1235",
})
await manager.save(tag3)
const tag4 = await manager.create(ProductTag, {
id: "tag4",
value: "1234",
})
await manager.save(tag4)
const type = await manager.create(ProductType, {
id: "test-type",
value: "test-type",
})
await manager.save(type)
const type2 = await manager.create(ProductType, {
id: "test-type-new",
value: "test-type-new",
})
await manager.save(type2)
const image = manager.create(Image, {
id: "test-image",
url: "test-image.png",
})
await manager.save(image)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
const p = await manager.create(Product, {
id: "test-product",
handle: "test-product",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
})
p.images = [image]
await manager.save(p)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product",
})
const variant1 = await manager.create(ProductVariant, {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
variant_rank: 0,
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
barcode: "test-barcode",
product_id: "test-product",
prices: [{ id: "test-price", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant1)
const sale = await manager.create(ProductVariant, {
id: "test-variant-sale",
inventory_quantity: 10,
title: "Test variant",
variant_rank: 3,
sku: "test-sku-sale",
ean: "test-ean-sale",
upc: "test-upc-sale",
barcode: "test-barcode-sale",
product_id: "test-product",
prices: [
{
id: "test-price-sale",
currency_code: "usd",
amount: 1000,
},
],
options: [
{
id: "test-variant-option-sale",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(sale)
const variant2 = await manager.create(ProductVariant, {
id: "test-variant_1",
inventory_quantity: 10,
title: "Test variant rank (1)",
variant_rank: 2,
sku: "test-sku1",
ean: "test-ean1",
upc: "test-upc1",
barcode: "test-barcode 1",
product_id: "test-product",
prices: [{ id: "test-price1", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-1",
value: "Default variant 1",
option_id: "test-option",
},
],
})
await manager.save(variant2)
const variant3 = await manager.create(ProductVariant, {
id: "test-variant_2",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku2",
ean: "test-ean2",
upc: "test-upc2",
product_id: "test-product",
prices: [{ id: "test-price2", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-2",
value: "Default variant 2",
option_id: "test-option",
},
],
})
await manager.save(variant3)
const p1 = await manager.create(Product, {
id: "test-product1",
handle: "test-product1",
title: "Test product1",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
})
await manager.save(p1)
const variant4 = await manager.create(ProductVariant, {
id: "test-variant_3",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku3",
ean: "test-ean3",
upc: "test-upc3",
product_id: "test-product1",
prices: [
{
id: "test-price3",
region_id: "test-region",
currency_code: "usd",
amount: 100,
type: "default",
},
],
options: [
{
id: "test-variant-option-3",
value: "Default variant 3",
option_id: "test-option",
},
],
})
await manager.save(variant4)
const variant5 = await manager.create(ProductVariant, {
id: "test-variant_4",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 0,
sku: "test-sku4",
ean: "test-ean4",
upc: "test-upc4",
product_id: "test-product1",
prices: [
{ id: "test-price4", currency_code: "usd", amount: 100, type: "default" },
],
options: [
{
id: "test-variant-option-4",
value: "Default variant 4",
option_id: "test-option",
},
],
})
await manager.save(variant5)
const product1 = await manager.create(Product, {
id: "test-product_filtering_1",
handle: "test-product_filtering_1",
title: "Test product filtering 1",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "proposed",
tags: [{ id: "tag3", value: "123" }],
})
await manager.save(product1)
const product2 = await manager.create(Product, {
id: "test-product_filtering_2",
handle: "test-product_filtering_2",
title: "Test product filtering 2",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection2",
status: "published",
tags: [{ id: "tag3", value: "123" }],
})
await manager.save(product2)
const product3 = await manager.create(Product, {
id: "test-product_filtering_3",
handle: "test-product_filtering_3",
title: "Test product filtering 3",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "draft",
tags: [{ id: "tag4", value: "1234" }],
})
await manager.save(product3)
const product4 = await manager.create(Product, {
id: "test-product_filtering_4",
handle: "test-product_filtering_4",
title: "Test product filtering 4",
profile_id: defaultProfile.id,
description: "test-product-description",
status: "proposed",
deleted_at: new Date().toISOString(),
})
await manager.save(product4)
}
-48
View File
@@ -1,48 +0,0 @@
const path = require("path")
const { spawn } = require("child_process")
const { setPort } = require("./use-api")
module.exports = ({ cwd, redisUrl, uploadDir, verbose, env }) => {
const serverPath = path.join(__dirname, "test-server.js")
// in order to prevent conflicts in redis, use a different db for each worker
// same fix as for databases (works with up to 15)
// redis dbs are 0-indexed and jest worker ids are indexed from 1
const workerId = parseInt(process.env.JEST_WORKER_ID || "1")
const redisUrlWithDatabase = `${redisUrl}/${workerId - 1}`
verbose = verbose ?? false
return new Promise((resolve, reject) => {
const medusaProcess = spawn("node", [path.resolve(serverPath)], {
cwd,
env: {
...process.env,
NODE_ENV: "development",
JWT_SECRET: "test",
COOKIE_SECRET: "test",
REDIS_URL: redisUrl ? redisUrlWithDatabase : undefined, // If provided, will use a real instance, otherwise a fake instance
UPLOAD_DIR: uploadDir, // If provided, will be used for the fake local file service
...env,
},
stdio: verbose
? ["inherit", "inherit", "inherit", "ipc"]
: ["ignore", "ignore", "ignore", "ipc"],
})
medusaProcess.on("error", (err) => {
console.log(err)
process.exit()
})
medusaProcess.on("uncaughtException", (err) => {
console.log(err)
medusaProcess.kill()
})
medusaProcess.on("message", (port) => {
setPort(port)
resolve(medusaProcess)
})
})
}
@@ -0,0 +1,84 @@
const {
Region,
ShippingProfile,
ShippingOption,
ShippingOptionRequirement,
ShippingProfileType,
} = require("@medusajs/medusa")
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
await manager.insert(Region, {
id: "region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
await manager.insert(ShippingOption, {
id: "test-out",
name: "Test out",
profile_id: defaultProfile.id,
region_id: "region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req-admin-only",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
admin_only: true,
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
is_return: false,
})
await manager.insert(ShippingOption, {
id: "test-option-req-return",
name: "With req",
profile_id: defaultProfile.id,
region_id: "region",
is_return: true,
provider_id: "test-ful",
data: {},
price_type: "flat_rate",
amount: 2000,
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req",
shipping_option_id: "test-option-req",
type: "min_subtotal",
amount: 5,
})
await manager.insert(ShippingOptionRequirement, {
id: "option-req-2",
shipping_option_id: "test-option-req",
type: "max_subtotal",
amount: 10,
})
}
@@ -1,38 +0,0 @@
const setupServer = require("./setup-server")
const { initDb } = require("./use-db")
const startServerWithEnvironment = async ({
cwd,
redisUrl,
uploadDir,
verbose,
env,
}) => {
if (env) {
Object.entries(env).forEach(([key, value]) => {
process.env[key] = value
})
}
const dbConnection = await initDb({
cwd,
})
if (env) {
Object.entries(env).forEach(([key]) => {
delete process.env[key]
})
}
const medusaProcess = await setupServer({
cwd,
verbose,
redisUrl,
uploadDir,
env,
})
return [medusaProcess, dbConnection]
}
export default startServerWithEnvironment
@@ -0,0 +1,379 @@
const {
ProductCollection,
ProductTag,
ProductType,
ProductOption,
Region,
Product,
ShippingProfile,
ProductVariant,
Image,
Cart,
PriceList,
ShippingProfileType,
} = require("@medusajs/medusa")
module.exports = async (dataSource, defaultSalesChannel) => {
const manager = dataSource.manager
const yesterday = ((today) => new Date(today.setDate(today.getDate() - 1)))(
new Date()
)
const tomorrow = ((today) => new Date(today.setDate(today.getDate() + 1)))(
new Date()
)
const tenDaysAgo = ((today) => new Date(today.setDate(today.getDate() - 10)))(
new Date()
)
const priceList = await manager.create(PriceList, {
id: "pl",
name: "VIP winter sale",
description: "Winter sale for VIP customers.",
type: "sale",
status: "active",
})
await manager.save(priceList)
const priceList1 = await manager.create(PriceList, {
id: "pl_expired",
name: "Past winter sale",
description: "Winter sale for key accounts.",
type: "sale",
status: "active",
starts_at: tenDaysAgo,
ends_at: yesterday,
})
await manager.save(priceList1)
const defaultProfile = await manager.findOne(ShippingProfile, {
where: { type: ShippingProfileType.DEFAULT },
})
const coll = manager.create(ProductCollection, {
id: "test-collection",
handle: "test-collection",
title: "Test collection",
})
await manager.save(coll)
const coll1 = manager.create(ProductCollection, {
id: "test-collection1",
handle: "test-collection1",
title: "Test collection 1",
})
await manager.save(coll1)
const coll2 = manager.create(ProductCollection, {
id: "test-collection2",
handle: "test-collection2",
title: "Test collection 2",
})
await manager.save(coll2)
const tag = manager.create(ProductTag, {
id: "tag1",
value: "123",
})
await manager.save(tag)
const tag3 = manager.create(ProductTag, {
id: "tag3",
value: "123",
})
await manager.save(tag3)
const tag4 = manager.create(ProductTag, {
id: "tag4",
value: "123",
})
await manager.save(tag4)
const type = manager.create(ProductType, {
id: "test-type",
value: "test-type",
})
await manager.save(type)
const image = manager.create(Image, {
id: "test-image",
url: "test-image.png",
})
await manager.save(image)
await manager.insert(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
await manager.insert(Cart, {
id: "test-cart",
region_id: "test-region",
currency_code: "usd",
items: [],
})
const p = manager.create(Product, {
id: "test-product",
handle: "test-product",
title: "Test product",
profile_id: defaultProfile.id,
description: "test-product-description",
status: "published",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
p.images = [image]
await manager.save(p)
await manager.save(ProductOption, {
id: "test-option",
title: "test-option",
product_id: "test-product",
})
const variant1 = await manager.create(ProductVariant, {
id: "test-variant",
inventory_quantity: 10,
title: "Test variant",
variant_rank: 0,
sku: "test-sku",
ean: "test-ean",
upc: "test-upc",
barcode: "test-barcode",
product_id: "test-product",
prices: [
{ id: "test-price", currency_code: "usd", type: "default", amount: 100 },
{
id: "test-price-discount",
currency_code: "usd",
amount: 80,
price_list_id: "pl",
},
{
id: "test-price-discount-expired",
currency_code: "usd",
amount: 70,
price_list_id: "pl_expired",
},
],
options: [
{
id: "test-variant-option",
value: "Default variant",
option_id: "test-option",
},
],
})
await manager.save(variant1)
const variant2 = await manager.create(ProductVariant, {
id: "test-variant_1",
inventory_quantity: 10,
title: "Test variant rank (1)",
variant_rank: 2,
sku: "test-sku1",
ean: "test-ean1",
upc: "test-upc1",
barcode: "test-barcode 1",
product_id: "test-product",
prices: [
{ id: "test-price1", currency_code: "usd", type: "default", amount: 100 },
{
id: "test-price1-discount",
currency_code: "usd",
amount: 80,
price_list_id: "pl",
},
{
id: "test-price1-discount-expired",
currency_code: "usd",
amount: 70,
price_list_id: "pl_expired",
},
],
options: [
{
id: "test-variant-option-1",
value: "Default variant 1",
option_id: "test-option",
},
],
})
await manager.save(variant2)
const variant3 = await manager.create(ProductVariant, {
id: "test-variant_2",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku2",
ean: "test-ean2",
upc: "test-upc2",
product_id: "test-product",
prices: [
{ id: "test-price2", currency_code: "usd", type: "default", amount: 100 },
{
id: "test-price2-discount",
currency_code: "usd",
amount: 80,
price_list_id: "pl",
},
{
id: "test-price2-discount-expired",
currency_code: "usd",
amount: 70,
price_list_id: "pl_expired",
},
],
options: [
{
id: "test-variant-option-2",
value: "Default variant 2",
option_id: "test-option",
},
],
})
await manager.save(variant3)
const p1 = manager.create(Product, {
id: "test-product1",
handle: "test-product1",
title: "Test product1",
status: "published",
profile_id: defaultProfile.id,
description: "test-product-description1",
collection_id: "test-collection",
type: { id: "test-type", value: "test-type" },
tags: [
{ id: "tag1", value: "123" },
{ tag: "tag2", value: "456" },
],
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
await manager.save(p1)
const variant4 = await manager.create(ProductVariant, {
id: "test-variant_3",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 1,
sku: "test-sku3",
ean: "test-ean3",
upc: "test-upc3",
product_id: "test-product1",
prices: [{ id: "test-price3", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-3",
value: "Default variant 3",
option_id: "test-option",
},
],
})
await manager.save(variant4)
const variant5 = await manager.create(ProductVariant, {
id: "test-variant_4",
inventory_quantity: 10,
title: "Test variant rank (2)",
variant_rank: 0,
sku: "test-sku4",
ean: "test-ean4",
upc: "test-upc4",
product_id: "test-product1",
prices: [{ id: "test-price4", currency_code: "usd", amount: 100 }],
options: [
{
id: "test-variant-option-4",
value: "Default variant 4",
option_id: "test-option",
},
],
})
await manager.save(variant5)
const product1 = manager.create(Product, {
id: "test-product_filtering_1",
handle: "test-product_filtering_1",
title: "Test product filtering 1",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "published",
tags: [{ id: "tag3", value: "123" }],
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
await manager.save(product1)
const product2 = manager.create(Product, {
id: "test-product_filtering_2",
handle: "test-product_filtering_2",
title: "Test product filtering 2",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection2",
status: "published",
tags: [{ id: "tag4", value: "1234" }],
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
await manager.save(product2)
const product3 = manager.create(Product, {
id: "test-product_filtering_3",
handle: "test-product_filtering_3",
title: "Test product filtering 3",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
collection_id: "test-collection1",
status: "draft",
tags: [{ id: "tag4", value: "1234" }],
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
await manager.save(product3)
const gift_card = manager.create(Product, {
id: "giftcard",
handle: "giftcard",
is_giftcard: true,
title: "giftcard",
profile_id: defaultProfile.id,
description: "test-product-description",
type: { id: "test-type", value: "test-type" },
status: "published",
sales_channels: defaultSalesChannel ? [defaultSalesChannel] : [],
})
await manager.save(gift_card)
}
+458
View File
@@ -0,0 +1,458 @@
const {
Discount,
DiscountRule,
LineItem,
ShippingMethod,
Order,
Swap,
Cart,
Return,
} = require("@medusajs/medusa")
const {
CustomShippingOption,
} = require("@medusajs/medusa/dist/models/custom-shipping-option")
const { Region } = require("@medusajs/medusa/dist/models/region")
let regionId
let region
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
regionId = "test-region"
region = await manager.findOne(Region, { where: { id: regionId } })
let orderWithSwap = manager.create(Order, {
id: "order-with-swap",
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: regionId,
currency_code: "usd",
tax_rate: 0,
discounts: [],
payments: [
{
id: "test-payment",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
],
items: [],
...data,
})
orderWithSwap = await manager.save(orderWithSwap)
const cart = manager.create(Cart, {
id: "test-cart-w-swap",
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: regionId,
type: "swap",
metadata: {
swap_id: "test-swap",
parent_order_id: orderWithSwap.id,
},
})
await manager.save(cart)
const swap = manager.create(Swap, {
id: "test-swap",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
cart_id: "test-cart-w-swap",
payment: {
id: "test-payment-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swapped",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 9000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart-w-swap",
},
],
})
await manager.save(swap)
const cartWithCustomSo = manager.create(Cart, {
id: "test-cart-rma",
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: regionId,
type: "swap",
metadata: {
swap_id: "test-swap",
parent_order_id: orderWithSwap.id,
},
})
await manager.save(cartWithCustomSo)
const liRma = manager.create(LineItem, {
id: "test-item-rma",
title: "Line Item RMA",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
cart_id: "test-cart-rma",
})
await manager.save(liRma)
await manager.insert(CustomShippingOption, {
id: "cso-test",
cart_id: cartWithCustomSo.id,
price: 0,
shipping_option_id: "test-option",
})
const swapWithRMAMethod = manager.create(Swap, {
id: "test-swap-rma",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
cart_id: cartWithCustomSo.id,
payment: {
id: "test-payment-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swapped",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 9000,
quantity: 1,
variant_id: "test-variant-2",
cart_id: "test-cart",
},
],
})
await manager.save(swapWithRMAMethod)
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: regionId,
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,
title: "Return Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
order_id: orderWithSwap.id,
cart_id: cart.id,
})
await manager.save(li)
const li2 = manager.create(LineItem, {
id: "test-item-many",
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: orderWithSwap.id,
})
await manager.save(li2)
const swapReturn = await manager.create(Return, {
swap_id: swap.id,
order_id: orderWithSwap.id,
item_id: li.id,
refund_amount: li.quantity * li.unit_price,
})
await manager.save(swapReturn)
const return_item1 = manager.create(LineItem, {
...li,
unit_price: -1 * li.unit_price,
})
await manager.save(return_item1)
await manager.insert(ShippingMethod, {
id: "another-test-method",
shipping_option_id: "test-option",
cart_id: "test-cart-w-swap",
price: 1000,
data: {},
})
const swapOnSwap = manager.create(Swap, {
id: "swap-on-swap",
order_id: "order-with-swap",
payment_status: "captured",
fulfillment_status: "fulfilled",
return_order: {
id: "return-on-swap",
refund_amount: 9000,
items: [
{
return_id: "return-on-swap",
item_id: "test-item-swapped",
quantity: 1,
},
],
},
payment: {
id: "test-payment-swap-on-swap",
amount: 10000,
currency_code: "usd",
amount_refunded: 0,
provider_id: "test-pay",
data: {},
},
additional_items: [
{
id: "test-item-swap-on-swap",
fulfilled_quantity: 1,
title: "Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
variant_id: "test-variant",
},
],
})
await manager.save(swapOnSwap)
await manager.insert(ShippingMethod, {
id: "test-method-swap-order",
shipping_option_id: "test-option",
order_id: "order-with-swap",
price: 1000,
data: {},
})
await createSwap({ id: "disc-swap" }, manager)
}
const createSwap = async (options, manager) => {
const swapId = options.id
const dRule = manager.create(DiscountRule, {
id: `${swapId}-cart-discount-rule`,
description: "Ten percent rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.save(dRule)
const discount = manager.create(Discount, {
code: `${swapId}`,
is_dynamic: false,
is_disabled: false,
rule: dRule,
})
const discountDb = await manager.save(discount)
const cart = manager.create(Cart, {
id: `${swapId}-cart`,
customer_id: "test-customer",
email: "test-customer@email.com",
shipping_address_id: "test-shipping-address",
billing_address_id: "test-billing-address",
region_id: regionId,
type: "swap",
discounts: [discount],
metadata: {
swap_id: swapId,
parent_order_id: "order-with-swap",
},
})
await manager.save(cart)
const swapTemplate = async () => {
return {
order_id: `order-with-swap`,
fulfillment_status: "fulfilled",
payment_status: "not_paid",
cart_id: cart.id,
}
}
const swapWithReturn = manager.create(Swap, {
id: swapId,
return_order: {
id: `${swapId}-return-id`,
status: "requested",
refund_amount: 0,
},
...(await swapTemplate(swapId)),
})
await manager.save(swapWithReturn)
const li = manager.create(LineItem, {
id: `${swapId}-return-item-1`,
fulfilled_quantity: 1,
title: "Return Line Item",
description: "Line Item Desc",
thumbnail: "https://test.js/1234",
unit_price: 8000,
quantity: 1,
tax_lines: [
{
rate: region.tax_rate,
code: region.name,
name: region.name,
},
],
adjustments: [
{
amount: -800,
description: "discount",
discount_id: discountDb.id,
item_id: `${swapId}-return-item-1`,
},
],
variant_id: "test-variant",
order_id: "order-with-swap",
cart_id: cart.id,
})
await manager.save(li)
const li2 = manager.create(LineItem, {
id: `${swapId}-test-item-many`,
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: "order-with-swap",
})
await manager.save(li2)
const return_item1 = manager.create(LineItem, {
...li,
is_return: true,
unit_price: -1 * li.unit_price,
})
await manager.save(return_item1)
await manager.insert(ShippingMethod, {
id: `${swapId}-test-method`,
shipping_option_id: "test-option",
cart_id: cart.id,
price: 1000,
data: {},
})
}
-11
View File
@@ -1,11 +0,0 @@
const { bootstrapApp } = require("./bootstrap-app")
const setup = async () => {
const { app, port } = await bootstrapApp()
app.listen(port, (err) => {
process.send(port)
})
}
setup()
-21
View File
@@ -1,21 +0,0 @@
const axios = require("axios").default
const ServerTestUtil = {
port_: null,
client_: null,
setPort: function (port) {
this.client_ = axios.create({ baseURL: `http://localhost:${port}` })
},
}
const instance = ServerTestUtil
module.exports = {
setPort: function (port) {
instance.setPort(port)
},
useApi: function () {
return instance.client_
},
}
-136
View File
@@ -1,136 +0,0 @@
const path = require("path")
const { getConfigFile } = require("medusa-core-utils")
const { dropDatabase } = require("pg-god")
const { DataSource } = require("typeorm")
const dbFactory = require("./use-template-db")
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
const pgGodCredentials = {
user: DB_USERNAME,
password: DB_PASSWORD,
host: DB_HOST,
}
const keepTables = [
"store",
"staged_job",
"shipping_profile",
"fulfillment_provider",
"payment_provider",
"country",
"currency",
]
const DbTestUtil = {
db_: null,
setDb: function (dataSource) {
this.db_ = dataSource
},
clear: async function () {
this.db_.synchronize(true)
},
teardown: async function ({ forceDelete } = {}) {
forceDelete = forceDelete || []
const entities = this.db_.entityMetadatas
const manager = this.db_.manager
await manager.query(`SET session_replication_role = 'replica';`)
for (const entity of entities) {
if (
keepTables.includes(entity.tableName) &&
!forceDelete.includes(entity.tableName)
) {
continue
}
await manager.query(`DELETE
FROM "${entity.tableName}";`)
}
await manager.query(`SET session_replication_role = 'origin';`)
},
shutdown: async function () {
await this.db_.destroy()
return await dropDatabase({ DB_NAME }, pgGodCredentials)
},
}
const instance = DbTestUtil
module.exports = {
initDb: async function ({ cwd, database_extra }) {
const { configModule } = getConfigFile(cwd, `medusa-config`)
const { featureFlags } = configModule
const featureFlagsLoader =
require("@medusajs/medusa/dist/loaders/feature-flags").default
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
const modelsLoader = require("@medusajs/medusa/dist/loaders/models").default
const entities = modelsLoader({}, { register: false })
await dbFactory.createFromTemplate(DB_NAME)
// get migrations with enabled featureflags
const migrationDir = path.resolve(
path.join(
__dirname,
`../../`,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`migrations`,
`*.js`
)
)
const {
getEnabledMigrations,
getModuleSharedResources,
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
const { migrations: moduleMigrations, models: moduleModels } =
getModuleSharedResources(configModule, featureFlagsRouter)
const enabledMigrations = getEnabledMigrations([migrationDir], (flag) =>
featureFlagsRouter.isFeatureEnabled(flag)
)
const enabledEntities = entities.filter(
(e) => typeof e.isFeatureEnabled === "undefined" || e.isFeatureEnabled()
)
const dbDataSource = new DataSource({
type: "postgres",
url: DB_URL,
entities: enabledEntities.concat(moduleModels),
migrations: enabledMigrations.concat(moduleMigrations),
extra: database_extra ?? {},
name: "integration-tests",
})
await dbDataSource.initialize()
await dbDataSource.runMigrations()
instance.setDb(dbDataSource)
return dbDataSource
},
useDb: function () {
return instance
},
}
-41
View File
@@ -1,41 +0,0 @@
const ServerTestUtil = {
server_: null,
app_: null,
setApp: function (app) {
this.app_ = app
},
start: async function () {
this.server_ = await new Promise((resolve, reject) => {
const s = this.app_.listen(PORT, (err) => {
if (err) {
reject(err)
}
})
resolve(s)
})
},
kill: function () {
return new Promise((resolve, _) => {
if (this.server_) {
this.server_.close(() => resolve())
}
resolve()
})
},
}
const instance = ServerTestUtil
module.exports = {
setApp: function (app) {
instance.setApp(app)
return instance
},
useServer: function () {
return instance
},
}
@@ -1,120 +0,0 @@
const path = require("path")
require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
const { getConfigFile } = require("medusa-core-utils")
const { createDatabase, dropDatabase } = require("pg-god")
const { DataSource } = require("typeorm")
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}`
let masterDataSource
const pgGodCredentials = {
user: DB_USERNAME,
password: DB_PASSWORD,
host: DB_HOST,
}
class DatabaseFactory {
constructor() {
this.dataSource_ = null
this.masterDataSourceName = "master"
this.templateDbName = "medusa-integration-template"
}
async createTemplateDb_({ cwd }) {
const { configModule } = getConfigFile(cwd, `medusa-config`)
const dataSource = await this.getMasterDataSource()
const migrationDir = path.resolve(
path.join(
__dirname,
`../../`,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`migrations`,
`*.js`
)
)
const {
getEnabledMigrations,
getModuleSharedResources,
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
// filter migrations to only include those that don't have feature flags
const enabledMigrations = getEnabledMigrations(
[migrationDir],
(flag) => false
)
const { migrations: moduleMigrations } =
getModuleSharedResources(configModule)
await dropDatabase(
{
databaseName: this.templateDbName,
errorIfNonExist: false,
},
pgGodCredentials
)
await createDatabase(
{ databaseName: this.templateDbName },
pgGodCredentials
)
const templateDbDataSource = new DataSource({
type: "postgres",
name: "templateDataSource",
url: `${DB_URL}/${this.templateDbName}`,
migrations: enabledMigrations.concat(moduleMigrations),
})
await templateDbDataSource.initialize()
await templateDbDataSource.runMigrations()
await templateDbDataSource.destroy()
return dataSource
}
async getMasterDataSource() {
masterDataSource = masterDataSource || (await this.createMasterDataSource())
return masterDataSource
}
async createMasterDataSource() {
const dataSource = new DataSource({
type: "postgres",
name: this.masterDataSourceName,
url: `${DB_URL}`,
})
await dataSource.initialize()
return dataSource
}
async createFromTemplate(dbName) {
const dataSource = await this.getMasterDataSource()
await dataSource.query(`DROP DATABASE IF EXISTS "${dbName}";`)
await dataSource.query(
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
)
}
async destroy() {
const dataSource = await this.getMasterDataSource()
await dataSource.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
await dataSource.destroy()
}
}
module.exports = new DatabaseFactory()
+53
View File
@@ -0,0 +1,53 @@
const { User, Invite } = require("@medusajs/medusa")
import jwt from "jsonwebtoken"
const generateToken = (data) => {
return jwt.sign(data, "test", {
expiresIn: "7d",
})
}
const expires_at = new Date()
expires_at.setDate(expires_at.getDate() + 8)
module.exports = async (dataSource, data = {}) => {
const manager = dataSource.manager
const memberUser = await manager.create(User, {
id: "member-user",
role: "member",
email: "member@test.com",
first_name: "member",
last_name: "user",
})
await manager.save(memberUser)
const memberInvite = await manager.create(Invite, {
id: "memberInvite",
user_email: "invite-member@test.com",
role: "member",
token: generateToken({
invite_id: "memberInvite",
role: "member",
user_email: "invite-member@test.com",
}),
accepted: false,
expires_at: expires_at,
})
await manager.save(memberInvite)
const adminInvite = await manager.create(Invite, {
id: "adminInvite",
user_email: "invite-admin@test.com",
role: "admin",
accepted: false,
token: generateToken({
invite_id: "adminInvite",
role: "admin",
user_email: "invite-admin@test.com",
}),
expires_at: expires_at,
})
await manager.save(adminInvite)
}