updates tests
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
import mongoose from "mongoose"
|
||||
|
||||
String.prototype.equals = function(that) {
|
||||
return this === that
|
||||
}
|
||||
|
||||
class IdMap {
|
||||
ids = {}
|
||||
|
||||
getId(key) {
|
||||
getId(key, backend=false) {
|
||||
if (this.ids[key]) {
|
||||
return this.ids[key]
|
||||
}
|
||||
const id = `${mongoose.Types.ObjectId()}`
|
||||
this.ids[key] = id
|
||||
return id
|
||||
|
||||
const mongooseId = `${mongoose.Types.ObjectId()}`
|
||||
this.ids[key] = mongooseId
|
||||
|
||||
return mongooseId
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ describe("POST /admin/product-variants/:id/options", () => {
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/options`,
|
||||
{
|
||||
payload: {
|
||||
optionId: IdMap.getId("testOption"),
|
||||
optionValue: "test",
|
||||
option_id: IdMap.getId("testOption"),
|
||||
value: "test",
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
|
||||
@@ -10,7 +10,12 @@ describe("POST /admin/product-variants", () => {
|
||||
subject = await request("POST", "/admin/product-variants", {
|
||||
payload: {
|
||||
title: "Test Product Variant",
|
||||
prices: [{}],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "DKK",
|
||||
amount: 1234,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
@@ -32,7 +37,12 @@ describe("POST /admin/product-variants", () => {
|
||||
expect(ProductVariantServiceMock.createDraft).toHaveBeenCalledTimes(1)
|
||||
expect(ProductVariantServiceMock.createDraft).toHaveBeenCalledWith({
|
||||
title: "Test Product Variant",
|
||||
prices: [{}],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "DKK",
|
||||
amount: 1234,
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,7 +12,7 @@ describe("DELETE /admin/product-variants/:id/options", () => {
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/options`,
|
||||
{
|
||||
payload: {
|
||||
optionId: IdMap.getId("testOption"),
|
||||
option_id: IdMap.getId("testOption"),
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
|
||||
describe("POST /admin/product-variants/:id/currency-price", () => {
|
||||
describe("successful sets currency price", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/currency-price`,
|
||||
{
|
||||
payload: {
|
||||
currencyCode: "DKK",
|
||||
amount: 100,
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("calls service setCurrencyPrice", () => {
|
||||
expect(ProductVariantServiceMock.setCurrencyPrice).toHaveBeenCalledTimes(
|
||||
1
|
||||
)
|
||||
expect(ProductVariantServiceMock.setCurrencyPrice).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
"DKK",
|
||||
100
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,40 +0,0 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
|
||||
describe("POST /admin/product-variants/:id/region-price", () => {
|
||||
describe("successfully sets region price", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/region-price`,
|
||||
{
|
||||
payload: {
|
||||
regionId: IdMap.getId("region-fr"),
|
||||
amount: 100,
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("calls service setCurrencyPrice", () => {
|
||||
expect(ProductVariantServiceMock.setRegionPrice).toHaveBeenCalledTimes(1)
|
||||
expect(ProductVariantServiceMock.setRegionPrice).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
IdMap.getId("region-fr"),
|
||||
100
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
|
||||
describe("POST /admin/product-variants/:id/prices", () => {
|
||||
describe("successfully sets region price", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/prices`,
|
||||
{
|
||||
payload: {
|
||||
region_id: IdMap.getId("region-fr"),
|
||||
amount: 100,
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("calls service setCurrencyPrice", () => {
|
||||
expect(ProductVariantServiceMock.setRegionPrice).toHaveBeenCalledTimes(1)
|
||||
expect(ProductVariantServiceMock.setRegionPrice).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
IdMap.getId("region-fr"),
|
||||
100
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("successfully sets currency price", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/product-variants/${IdMap.getId("testVariant")}/prices`,
|
||||
{
|
||||
payload: {
|
||||
currency_code: "EUR",
|
||||
amount: 100,
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("calls service setCurrencyPrice", () => {
|
||||
expect(ProductVariantServiceMock.setCurrencyPrice).toHaveBeenCalledTimes(
|
||||
1
|
||||
)
|
||||
expect(ProductVariantServiceMock.setCurrencyPrice).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
"EUR",
|
||||
100
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -13,7 +13,12 @@ describe("POST /admin/product-variants/:id", () => {
|
||||
{
|
||||
payload: {
|
||||
title: "Test Product Variant Updated",
|
||||
prices: [{}],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "DKK",
|
||||
amount: 1234,
|
||||
},
|
||||
],
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
@@ -34,7 +39,12 @@ describe("POST /admin/product-variants/:id", () => {
|
||||
IdMap.getId("testVariant"),
|
||||
{
|
||||
title: "Test Product Variant Updated",
|
||||
prices: [{}],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "DKK",
|
||||
amount: 1234,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object()
|
||||
.keys({
|
||||
region_id: Validator.string(),
|
||||
currency_code: Validator.string(),
|
||||
amount: Validator.number().required(),
|
||||
})
|
||||
.xor("region_id", "currency_code")
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
try {
|
||||
const productVariantService = req.scope.resolve("productVariantService")
|
||||
|
||||
if (value.region_id) {
|
||||
const productVariant = await productVariantService.setRegionPrice(
|
||||
id,
|
||||
value.region_id,
|
||||
value.amount
|
||||
)
|
||||
|
||||
res.status(200).json(productVariant)
|
||||
} else {
|
||||
const productVariant = await productVariantService.setCurrencyPrice(
|
||||
id,
|
||||
value.currency_code,
|
||||
value.amount
|
||||
)
|
||||
|
||||
res.status(200).json(productVariant)
|
||||
}
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
optionId: Validator.objectId().required(),
|
||||
option_id: Validator.objectId().required(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -16,7 +16,7 @@ export default async (req, res) => {
|
||||
const productVariantService = req.scope.resolve("productVariantService")
|
||||
const productVariant = await productVariantService.deleteOptionValue(
|
||||
id,
|
||||
value.optionId
|
||||
value.option_id
|
||||
)
|
||||
|
||||
res.status(200).json(productVariant)
|
||||
|
||||
@@ -17,15 +17,7 @@ export default app => {
|
||||
middlewares.wrap(require("./publish-product-variant").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/currency-price",
|
||||
middlewares.wrap(require("./set-currency-price").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/region-price",
|
||||
middlewares.wrap(require("./set-region-price").default)
|
||||
)
|
||||
route.post("/:id/prices", middlewares.wrap(require("./add-price").default))
|
||||
|
||||
route.post(
|
||||
"/:id/options",
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
currencyCode: Validator.string().required(),
|
||||
amount: Validator.number().required(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
try {
|
||||
const productVariantService = req.scope.resolve("productVariantService")
|
||||
const productVariant = await productVariantService.setCurrencyPrice(
|
||||
id,
|
||||
value.currencyCode,
|
||||
value.amount
|
||||
)
|
||||
|
||||
res.status(200).json(productVariant)
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
regionId: Validator.objectId().required(),
|
||||
region_id: Validator.objectId().required(),
|
||||
amount: Validator.number().required(),
|
||||
})
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ describe("POST /admin/products/:id/options", () => {
|
||||
`/admin/products/${IdMap.getId("productWithOptions")}/options`,
|
||||
{
|
||||
payload: {
|
||||
optionTitle: "Test option",
|
||||
option_title: "Test option",
|
||||
},
|
||||
adminSession: {
|
||||
jwt: {
|
||||
|
||||
@@ -27,7 +27,6 @@ export default async (req, res) => {
|
||||
])
|
||||
res.json(data)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("POST /admin/regions/:region_id/countries", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
const id = IdMap.getId("region")
|
||||
const id = IdMap.getId("testRegion")
|
||||
subject = await request("POST", `/admin/regions/${id}/countries`, {
|
||||
payload: {
|
||||
country_code: "se",
|
||||
@@ -27,7 +27,7 @@ describe("POST /admin/regions/:region_id/countries", () => {
|
||||
it("calls service addCountry", () => {
|
||||
expect(RegionServiceMock.addCountry).toHaveBeenCalledTimes(1)
|
||||
expect(RegionServiceMock.addCountry).toHaveBeenCalledWith(
|
||||
IdMap.getId("region"),
|
||||
IdMap.getId("testRegion"),
|
||||
"se"
|
||||
)
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("POST /admin/regions/:region_id/fulfillment-providers", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
const id = IdMap.getId("region")
|
||||
const id = IdMap.getId("testRegion")
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/regions/${id}/fulfillment-providers`,
|
||||
@@ -31,7 +31,7 @@ describe("POST /admin/regions/:region_id/fulfillment-providers", () => {
|
||||
it("calls service addCountry", () => {
|
||||
expect(RegionServiceMock.addFulfillmentProvider).toHaveBeenCalledTimes(1)
|
||||
expect(RegionServiceMock.addFulfillmentProvider).toHaveBeenCalledWith(
|
||||
IdMap.getId("region"),
|
||||
IdMap.getId("testRegion"),
|
||||
"default_provider"
|
||||
)
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("POST /admin/regions/:region_id/payment-providers", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
const id = IdMap.getId("region")
|
||||
const id = IdMap.getId("testRegion")
|
||||
subject = await request(
|
||||
"POST",
|
||||
`/admin/regions/${id}/payment-providers`,
|
||||
@@ -31,7 +31,7 @@ describe("POST /admin/regions/:region_id/payment-providers", () => {
|
||||
it("calls service addCountry", () => {
|
||||
expect(RegionServiceMock.addPaymentProvider).toHaveBeenCalledTimes(1)
|
||||
expect(RegionServiceMock.addPaymentProvider).toHaveBeenCalledWith(
|
||||
IdMap.getId("region"),
|
||||
IdMap.getId("testRegion"),
|
||||
"default_provider"
|
||||
)
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("GET /admin/regions/:region_id", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
const id = IdMap.getId("region")
|
||||
const id = IdMap.getId("testRegion")
|
||||
subject = await request("GET", `/admin/regions/${id}`, {
|
||||
adminSession: {
|
||||
jwt: {
|
||||
@@ -24,7 +24,7 @@ describe("GET /admin/regions/:region_id", () => {
|
||||
it("calls service addCountry", () => {
|
||||
expect(RegionServiceMock.retrieve).toHaveBeenCalledTimes(1)
|
||||
expect(RegionServiceMock.retrieve).toHaveBeenCalledWith(
|
||||
IdMap.getId("region")
|
||||
IdMap.getId("testRegion")
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -32,8 +32,8 @@ describe("POST /store/carts/:id", () => {
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledTimes(1)
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
3,
|
||||
IdMap.getId("testRegion")
|
||||
IdMap.getId("testRegion"),
|
||||
3
|
||||
)
|
||||
})
|
||||
|
||||
@@ -71,8 +71,8 @@ describe("POST /store/carts/:id", () => {
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledTimes(1)
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("fail"),
|
||||
3,
|
||||
IdMap.getId("testRegion")
|
||||
IdMap.getId("testRegion"),
|
||||
3
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ describe("GET /store/carts", () => {
|
||||
expect(CartServiceMock.retrieve).toHaveBeenCalledWith("none")
|
||||
})
|
||||
|
||||
it("returns products", () => {
|
||||
it("returns 404 status", () => {
|
||||
expect(subject.status).toEqual(404)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -20,8 +20,8 @@ export default async (req, res) => {
|
||||
|
||||
const lineItem = await lineItemService.generate(
|
||||
value.variant_id,
|
||||
value.quantity,
|
||||
cart.region_id
|
||||
cart.region_id,
|
||||
value.quantity
|
||||
)
|
||||
await cartService.addLineItem(cart._id, lineItem)
|
||||
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
let cart = await cartService.retrieve(id)
|
||||
try {
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
let cart = await cartService.retrieve(id)
|
||||
cart = await cartService.decorate(cart)
|
||||
|
||||
if (!cart) {
|
||||
res.sendStatus(404)
|
||||
return
|
||||
res.json(cart)
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
cart = await cartService.decorate(cart)
|
||||
|
||||
res.json(cart)
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export const orders = {
|
||||
quantity: 10,
|
||||
},
|
||||
],
|
||||
region: IdMap.getId("region-france"),
|
||||
region_id: IdMap.getId("region-france"),
|
||||
customer_id: IdMap.getId("test-customer"),
|
||||
payment_method: {
|
||||
provider_id: "default_provider",
|
||||
@@ -102,7 +102,7 @@ export const orders = {
|
||||
quantity: 10,
|
||||
},
|
||||
],
|
||||
region: IdMap.getId("region-france"),
|
||||
region_id: IdMap.getId("region-france"),
|
||||
customer_id: IdMap.getId("test-customer"),
|
||||
payment_method: {
|
||||
provider_id: "default_provider",
|
||||
@@ -182,7 +182,7 @@ export const orders = {
|
||||
quantity: 10,
|
||||
},
|
||||
],
|
||||
region: IdMap.getId("region-france"),
|
||||
region_id: IdMap.getId("region-france"),
|
||||
customer_id: IdMap.getId("test-customer"),
|
||||
payment_method: {
|
||||
provider_id: "default_provider",
|
||||
|
||||
@@ -8,7 +8,7 @@ import LineItemSchema from "./schemas/line-item"
|
||||
import PaymentMethodSchema from "./schemas/payment-method"
|
||||
import ShippingMethodSchema from "./schemas/shipping-method"
|
||||
import AddressSchema from "./schemas/address"
|
||||
import DiscountModel from "./discount"
|
||||
import DiscountSchema from "./schemas/discount"
|
||||
|
||||
class CartModel extends BaseModel {
|
||||
static modelName = "Cart"
|
||||
@@ -19,7 +19,7 @@ class CartModel extends BaseModel {
|
||||
shipping_address: { type: AddressSchema },
|
||||
items: { type: [LineItemSchema], default: [] },
|
||||
region_id: { type: String, required: true },
|
||||
discounts: { type: [DiscountModel.schema], default: [] },
|
||||
discounts: { type: [DiscountSchema], default: [] },
|
||||
customer_id: { type: String, default: "" },
|
||||
payment_sessions: { type: [PaymentMethodSchema], default: [] },
|
||||
shipping_options: { type: [ShippingMethodSchema], default: [] },
|
||||
|
||||
@@ -21,7 +21,7 @@ class OrderModel extends BaseModel {
|
||||
billing_address: { type: AddressSchema, required: true },
|
||||
shipping_address: { type: AddressSchema, required: true },
|
||||
items: { type: [LineItemSchema], required: true },
|
||||
region: { type: String, required: true },
|
||||
region_id: { type: String, required: true },
|
||||
discounts: { type: [DiscountModel.schema], default: [] },
|
||||
customer_id: { type: String, required: true },
|
||||
payment_method: { type: PaymentMethodSchema, required: true },
|
||||
|
||||
14
packages/medusa/src/models/schemas/discount.js
Normal file
14
packages/medusa/src/models/schemas/discount.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import mongoose from "mongoose"
|
||||
|
||||
import DiscountRule from "./discount-rule"
|
||||
|
||||
export default new mongoose.Schema({
|
||||
code: { type: String },
|
||||
discount_rule: { type: DiscountRule },
|
||||
usage_count: { type: Number },
|
||||
disabled: { type: Boolean },
|
||||
starts_at: { type: Date },
|
||||
ends_at: { type: Date },
|
||||
regions: { type: [String], default: [] },
|
||||
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
|
||||
const adminUser = {
|
||||
_id: IdMap.getId("admin_user"),
|
||||
_id: IdMap.getId("admin_user", true),
|
||||
password: "1235",
|
||||
name: "hi",
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ export const CartServiceMock = {
|
||||
if (cartId === IdMap.getId("cartWithPaySessions")) {
|
||||
return Promise.resolve(carts.cartWithPaySessions)
|
||||
}
|
||||
return Promise.resolve(undefined)
|
||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "cart not found")
|
||||
}),
|
||||
addLineItem: jest.fn().mockImplementation((cartId, lineItem) => {
|
||||
return Promise.resolve()
|
||||
|
||||
@@ -41,7 +41,7 @@ export const orders = {
|
||||
quantity: 10,
|
||||
},
|
||||
],
|
||||
region: IdMap.getId("testRegion"),
|
||||
region_id: IdMap.getId("testRegion"),
|
||||
customer_id: IdMap.getId("testCustomer"),
|
||||
payment_method: {
|
||||
provider_id: "default_provider",
|
||||
@@ -94,7 +94,7 @@ export const orders = {
|
||||
quantity: 10,
|
||||
},
|
||||
],
|
||||
region: IdMap.getId("region-france"),
|
||||
region_id: IdMap.getId("region-france"),
|
||||
customer_id: IdMap.getId("test-customer"),
|
||||
payment_method: {
|
||||
provider_id: "default_provider",
|
||||
|
||||
@@ -103,6 +103,7 @@ const emptyVariant = {
|
||||
|
||||
const eur10us12 = {
|
||||
_id: IdMap.getId("eur-10-us-12"),
|
||||
title: "EUR10US-12",
|
||||
}
|
||||
|
||||
export const variants = {
|
||||
|
||||
@@ -101,6 +101,7 @@ export const ProductServiceMock = {
|
||||
{
|
||||
_id: "1234",
|
||||
title: "test",
|
||||
thumbnail: "test.1234",
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
@@ -21,18 +21,21 @@ export const regions = {
|
||||
},
|
||||
regionUs: {
|
||||
_id: IdMap.getId("region-us"),
|
||||
tax_rate: 0.25,
|
||||
name: "USA",
|
||||
countries: ["US"],
|
||||
currency_code: "usd",
|
||||
},
|
||||
regionGermany: {
|
||||
_id: IdMap.getId("region-de"),
|
||||
tax_rate: 0.25,
|
||||
name: "Germany",
|
||||
countries: ["DE"],
|
||||
currency_code: "eur",
|
||||
},
|
||||
regionSweden: {
|
||||
_id: IdMap.getId("region-se"),
|
||||
tax_rate: 0.25,
|
||||
name: "Sweden",
|
||||
countries: ["SE"],
|
||||
currency_code: "sek",
|
||||
@@ -56,7 +59,7 @@ export const RegionServiceMock = {
|
||||
if (regionId === IdMap.getId("region-se")) {
|
||||
return Promise.resolve(regions.regionSweden)
|
||||
}
|
||||
return Promise.resolve(undefined)
|
||||
throw Error(regionId + "not found")
|
||||
}),
|
||||
delete: jest.fn().mockImplementation(data => Promise.resolve()),
|
||||
create: jest.fn().mockImplementation(data => Promise.resolve()),
|
||||
|
||||
@@ -24,24 +24,22 @@ describe("LineItemService", () => {
|
||||
|
||||
it("generates line item and successfully defaults quantity of content to 1", () => {
|
||||
expect(result).toEqual({
|
||||
title: "test",
|
||||
description: "EUR10US-12",
|
||||
thumbnail: "test.1234",
|
||||
content: {
|
||||
unit_price: 10,
|
||||
variant: {
|
||||
_id: IdMap.getId("eur-10-us-12"),
|
||||
title: "EUR10US-12",
|
||||
},
|
||||
product: {
|
||||
_id: "1234",
|
||||
title: "test",
|
||||
thumbnail: "test.1234",
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
product: {
|
||||
_id: "1234",
|
||||
title: "test",
|
||||
},
|
||||
variant: {
|
||||
_id: IdMap.getId("eur-10-us-12"),
|
||||
},
|
||||
quantity: 2,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -23,6 +23,7 @@ class CartService extends BaseService {
|
||||
shippingOptionService,
|
||||
shippingProfileService,
|
||||
discountService,
|
||||
totalsService,
|
||||
}) {
|
||||
super()
|
||||
|
||||
@@ -55,6 +56,9 @@ class CartService extends BaseService {
|
||||
|
||||
/** @private @const {DiscountService} */
|
||||
this.discountService_ = discountService
|
||||
|
||||
/** @private @const {DiscountService} */
|
||||
this.totalsService_ = totalsService
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +68,7 @@ class CartService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
@@ -221,7 +225,9 @@ class CartService extends BaseService {
|
||||
* @return {Cart} return the decorated cart.
|
||||
*/
|
||||
async decorate(cart, fields, expandFields = []) {
|
||||
return cart
|
||||
const c = cart.toObject()
|
||||
c.total = await this.totalsService_.getTotal(cart)
|
||||
return c
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -101,9 +101,10 @@ class LineItemService extends BaseService {
|
||||
)
|
||||
|
||||
return {
|
||||
variant,
|
||||
product,
|
||||
title: product.title,
|
||||
description: variant.title,
|
||||
quantity,
|
||||
thumbnail: product.thumbnail,
|
||||
content: {
|
||||
unit_price,
|
||||
variant,
|
||||
|
||||
@@ -28,7 +28,7 @@ class ProductVariantService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
|
||||
@@ -29,7 +29,7 @@ class ProductService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
@@ -193,7 +193,7 @@ class ProductService extends BaseService {
|
||||
})
|
||||
|
||||
let combinationExists = false
|
||||
if (product.variants) {
|
||||
if (product.variants && product.variants.length) {
|
||||
// Check if option value of the variant to add already exists. Go through
|
||||
// each existing variant. Check if this variants option values are
|
||||
// identical to the option values of the variant being added.
|
||||
|
||||
@@ -166,7 +166,7 @@ class RegionService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
@@ -195,6 +195,16 @@ class RegionService extends BaseService {
|
||||
return region
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all regions based on a query
|
||||
* @param {string} regionId - the id of the region to retrieve
|
||||
* @return {Region} the region
|
||||
*/
|
||||
async list(query) {
|
||||
const regions = await this.regionModel_.find(query)
|
||||
return regions
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a region.
|
||||
* @param {string} regionId - the region to delete
|
||||
|
||||
@@ -74,7 +74,7 @@ class TotalsService extends BaseService {
|
||||
async getTaxTotal(object) {
|
||||
const subtotal = this.getSubtotal(object)
|
||||
const shippingTotal = this.getShippingTotal(object)
|
||||
const region = await this.regionService_.retrieve(object.region)
|
||||
const region = await this.regionService_.retrieve(object.region_id)
|
||||
const { tax_rate } = region
|
||||
return (subtotal + shippingTotal) * tax_rate
|
||||
}
|
||||
@@ -101,7 +101,7 @@ class TotalsService extends BaseService {
|
||||
|
||||
const subtotal = this.getSubtotal({ items: lineItems })
|
||||
|
||||
const region = await this.regionService_.retrieve(order.region)
|
||||
const region = await this.regionService_.retrieve(order.region_id)
|
||||
|
||||
// if nothing is discounted, return the subtotal of line items
|
||||
if (!discount) {
|
||||
|
||||
@@ -26,7 +26,7 @@ class UserService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
|
||||
@@ -4522,14 +4522,6 @@ media-typer@0.3.0:
|
||||
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
||||
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
|
||||
|
||||
medusa-core-utils@^0.3.0:
|
||||
version "0.1.39"
|
||||
resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-0.1.39.tgz#d57816c9bd43f9a92883650c1e66add1665291df"
|
||||
integrity sha512-R8+U1ile7if+nR6Cjh5exunx0ETV0OfkWUUBUpz1KmHSDv0V0CcvQqU9lcZesPFDEbu3Y2iEjsCqidVA4nG2nQ==
|
||||
dependencies:
|
||||
"@hapi/joi" "^16.1.8"
|
||||
joi-objectid "^3.0.1"
|
||||
|
||||
medusa-interfaces@^0.1.27:
|
||||
version "0.1.27"
|
||||
resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-0.1.27.tgz#e77f9a9f82a7118eac8b35c1498ef8a5cec78898"
|
||||
@@ -4537,13 +4529,6 @@ medusa-interfaces@^0.1.27:
|
||||
dependencies:
|
||||
mongoose "^5.8.0"
|
||||
|
||||
medusa-test-utils@^0.3.0:
|
||||
version "0.1.39"
|
||||
resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-0.1.39.tgz#b7c166006a2fa4f02e52ab3bfafc19a3ae787f3e"
|
||||
integrity sha512-M/Br8/HYvl7x2oLnme4NxdQwoyV0XUyOWiCyvPp7q1HUTB684lhJf1MikZVrcSjsh2L1rpyi3GRbKdf4cpJWvw==
|
||||
dependencies:
|
||||
mongoose "^5.8.0"
|
||||
|
||||
memory-pager@^1.0.2:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5"
|
||||
|
||||
Reference in New Issue
Block a user