Fixes as a result of 'end-to-end-backend' test

This commit is contained in:
olivermrbl
2020-07-03 17:00:27 +02:00
parent 63a2f126c5
commit 758c72bdf1
18 changed files with 90 additions and 37 deletions
@@ -12,6 +12,8 @@ describe("POST /admin/shipping-options", () => {
name: "Test option",
region_id: "testregion",
provider_id: "test_provider",
data: { id: "test" },
profile_id: "test",
price: {
type: "flat_rate",
amount: 100,
@@ -32,6 +34,7 @@ describe("POST /admin/shipping-options", () => {
})
it("returns 200", () => {
console.log(subject)
expect(subject.status).toEqual(200)
})
@@ -41,6 +44,8 @@ describe("POST /admin/shipping-options", () => {
name: "Test option",
region_id: "testregion",
provider_id: "test_provider",
data: { id: "test" },
profile_id: "test",
price: {
type: "flat_rate",
amount: 100,
@@ -5,11 +5,14 @@ export default async (req, res) => {
name: Validator.string().required(),
region_id: Validator.string().required(),
provider_id: Validator.string().required(),
data: Validator.object(),
price: Validator.object().keys({
type: Validator.string().required(),
amount: Validator.number().optional(),
}),
profile_id: Validator.string().required(),
data: Validator.object().required(),
price: Validator.object()
.keys({
type: Validator.string().required(),
amount: Validator.number().optional(),
})
.required(),
requirements: Validator.array()
.items(
Validator.object({
@@ -33,7 +36,7 @@ export default async (req, res) => {
// Add to default shipping profile
const { _id } = await shippingProfileService.retrieveDefault()
await shippingProfileService.addProduct(_id, data._id)
await shippingProfileService.addShippingOption(_id, data._id)
res.status(200).json({ shipping_option: data })
} catch (err) {
@@ -12,6 +12,7 @@ export default async (req, res) => {
discounts: Validator.array().items({
code: Validator.string(),
}),
customer_id: Validator.string(),
})
const { value, error } = schema.validate(req.body)
@@ -27,6 +28,10 @@ export default async (req, res) => {
}
try {
if (value.customer_id) {
await cartService.updateCustomerId(id, value.customer_id)
}
if (value.region_id) {
await cartService.setRegion(id, value.region_id)
}
@@ -17,7 +17,7 @@ export default async (req, res) => {
try {
const customerService = req.scope.resolve("customerService")
const customer = await customerService.create(value)
const data = await customerService.decorate(customer)
const data = await customerService.decorate(customer, ["_id", "email"])
res.status(201).json({ customer: data })
} catch (err) {
throw err
+34
View File
@@ -370,6 +370,40 @@ class CartService extends BaseService {
return result
})
}
/**
* Sets the customer id of a cart
* @param {string} cartId - the id of the cart to add email to
* @param {string} customerId - the customer to add to cart
* @return {Promise} the result of the update operation
*/
async updateCustomerId(cartId, customerId) {
const cart = await this.retrieve(cartId)
const schema = Validator.string()
.objectId()
.required()
const { value, error } = schema.validate(customerId)
if (error) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The customerId is not valid"
)
}
return this.cartModel_
.updateOne(
{
_id: cart._id,
},
{
$set: { customer_id: value },
}
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(CartService.Events.UPDATED, result)
return result
})
}
/**
* Sets the email of a cart
+1 -1
View File
@@ -26,7 +26,7 @@ class CustomerService 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,
+1 -1
View File
@@ -35,7 +35,7 @@ class DiscountService 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,
@@ -14,7 +14,7 @@ class FulfillmentProviderService {
*/
retrieveProvider(provider_id) {
try {
return this.container_.resolve(`fp_${provider_id}`)
return this.container_[`fp_${provider_id}`]
} catch (err) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
+9 -1
View File
@@ -43,7 +43,7 @@ class OrderService 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,
@@ -139,6 +139,14 @@ class OrderService extends BaseService {
return order
}
/**
* @param {Object} selector - the query object for find
* @return {Promise} the result of the find operation
*/
list(selector) {
return this.orderModel_.find(selector)
}
/**
* Creates an order
* @param {object} order - the order to create
@@ -37,7 +37,7 @@ class ShippingOptionService 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,
@@ -246,10 +246,10 @@ class ShippingOptionService extends BaseService {
}
}
if (price.type === "flat_rate" && (!price.amount || price.amount < 0)) {
if (price.type === "flat_rate" && (!price.amount || price.amount <= 0)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Flat rate prices must have a postive amount field."
"Flat rate prices must have be zero or have a postive amount field."
)
}
+1 -1
View File
@@ -28,7 +28,7 @@ class StoreService 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,