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

View File

@@ -1,7 +1,8 @@
{
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-instanceof"
"@babel/plugin-transform-instanceof",
"@babel/plugin-transform-classes"
],
"presets": ["@babel/preset-env"],
"env": {
@@ -9,4 +10,4 @@
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "medusa-fulfillment-manual",
"version": "0.1.27-alpha.0",
"version": "1.0",
"description": "A manual fulfillment provider for Medusa",
"main": "index.js",
"repository": {
@@ -23,7 +23,7 @@
"jest": "^25.5.2"
},
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
"build": "babel src -d dist",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__"
},
@@ -32,7 +32,6 @@
"@babel/plugin-transform-instanceof": "^7.8.3",
"@babel/runtime": "^7.7.6",
"express": "^4.17.1",
"medusa-core-utils": "^0.3.0",
"medusa-interfaces": "^0.1.27-alpha.0"
"medusa-core-utils": "^0.3.0"
}
}
}

View File

@@ -1,6 +1,6 @@
import { FulfillmentService } from "medusa-interfaces"
class ManualFulfillmentService extends FulfillmentService {
class ManualFulfillmentService extends FulfillmentService {
static identifier = "manual"
constructor() {
@@ -8,9 +8,11 @@ class ManualFulfillmentService extends FulfillmentService {
}
getFulfillmentOptions() {
return [{
id: "manual-fulfillment"
}]
return [
{
id: "manual-fulfillment",
},
]
}
validateFulfillmentData(data, cart) {
@@ -18,11 +20,7 @@ class ManualFulfillmentService extends FulfillmentService {
}
validateOption(data) {
if (data.id === "manual-fulfillment") {
return true
}
return false
return true
}
canCalculate() {

View File

@@ -27,7 +27,7 @@
"medusa-test-utils": "^0.3.0"
},
"scripts": {
"build": "babel src -d dist",
"build": "babel src -d .",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__",
"test": "jest"
@@ -42,4 +42,4 @@
"body-parser": "^1.19.0"
},
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
}
}

View File

@@ -3,7 +3,7 @@ export default async (req, res) => {
let event
try {
const stripeProviderService = req.resolve("pp_stripe")
const stripeProviderService = req.scope.resolve("pp_stripe")
event = stripeProviderService.constructWebhookEvent(req.body, signature)
} catch (err) {
res.status(400).send(`Webhook Error: ${err.message}`)

View File

@@ -103,7 +103,7 @@ class StripeProviderService extends PaymentService {
const paymentIntent = await this.stripe_.paymentIntents.create({
customer: stripeCustomerId,
amount: amount * 100, // Stripe amount is in cents
currency: currency_code
currency: currency_code,
})
return paymentIntent
@@ -134,7 +134,7 @@ class StripeProviderService extends PaymentService {
const { id } = data
const amount = this.totalsService_.getTotal(cart)
return this.stripe_.paymentIntents.update(id, {
amount
amount,
})
} catch (error) {
throw error

View File

@@ -72,4 +72,4 @@
"winston": "^3.2.1"
},
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
}
}

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -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

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

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,

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,

View File

@@ -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,

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

View File

@@ -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."
)
}

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,