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
+3 -2
View File
@@ -1,7 +1,8 @@
{ {
"plugins": [ "plugins": [
"@babel/plugin-proposal-class-properties", "@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-instanceof" "@babel/plugin-transform-instanceof",
"@babel/plugin-transform-classes"
], ],
"presets": ["@babel/preset-env"], "presets": ["@babel/preset-env"],
"env": { "env": {
@@ -9,4 +10,4 @@
"plugins": ["@babel/plugin-transform-runtime"] "plugins": ["@babel/plugin-transform-runtime"]
} }
} }
} }
@@ -1,6 +1,6 @@
{ {
"name": "medusa-fulfillment-manual", "name": "medusa-fulfillment-manual",
"version": "0.1.27-alpha.0", "version": "1.0",
"description": "A manual fulfillment provider for Medusa", "description": "A manual fulfillment provider for Medusa",
"main": "index.js", "main": "index.js",
"repository": { "repository": {
@@ -23,7 +23,7 @@
"jest": "^25.5.2" "jest": "^25.5.2"
}, },
"scripts": { "scripts": {
"build": "babel src --out-dir . --ignore **/__tests__", "build": "babel src -d dist",
"prepare": "cross-env NODE_ENV=production npm run build", "prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__" "watch": "babel -w src --out-dir . --ignore **/__tests__"
}, },
@@ -32,7 +32,6 @@
"@babel/plugin-transform-instanceof": "^7.8.3", "@babel/plugin-transform-instanceof": "^7.8.3",
"@babel/runtime": "^7.7.6", "@babel/runtime": "^7.7.6",
"express": "^4.17.1", "express": "^4.17.1",
"medusa-core-utils": "^0.3.0", "medusa-core-utils": "^0.3.0"
"medusa-interfaces": "^0.1.27-alpha.0"
} }
} }
@@ -1,6 +1,6 @@
import { FulfillmentService } from "medusa-interfaces" import { FulfillmentService } from "medusa-interfaces"
class ManualFulfillmentService extends FulfillmentService { class ManualFulfillmentService extends FulfillmentService {
static identifier = "manual" static identifier = "manual"
constructor() { constructor() {
@@ -8,9 +8,11 @@ class ManualFulfillmentService extends FulfillmentService {
} }
getFulfillmentOptions() { getFulfillmentOptions() {
return [{ return [
id: "manual-fulfillment" {
}] id: "manual-fulfillment",
},
]
} }
validateFulfillmentData(data, cart) { validateFulfillmentData(data, cart) {
@@ -18,11 +20,7 @@ class ManualFulfillmentService extends FulfillmentService {
} }
validateOption(data) { validateOption(data) {
if (data.id === "manual-fulfillment") { return true
return true
}
return false
} }
canCalculate() { canCalculate() {
+2 -2
View File
@@ -27,7 +27,7 @@
"medusa-test-utils": "^0.3.0" "medusa-test-utils": "^0.3.0"
}, },
"scripts": { "scripts": {
"build": "babel src -d dist", "build": "babel src -d .",
"prepare": "cross-env NODE_ENV=production npm run build", "prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__", "watch": "babel -w src --out-dir . --ignore **/__tests__",
"test": "jest" "test": "jest"
@@ -42,4 +42,4 @@
"body-parser": "^1.19.0" "body-parser": "^1.19.0"
}, },
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc" "gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
} }
@@ -3,7 +3,7 @@ export default async (req, res) => {
let event let event
try { try {
const stripeProviderService = req.resolve("pp_stripe") const stripeProviderService = req.scope.resolve("pp_stripe")
event = stripeProviderService.constructWebhookEvent(req.body, signature) event = stripeProviderService.constructWebhookEvent(req.body, signature)
} catch (err) { } catch (err) {
res.status(400).send(`Webhook Error: ${err.message}`) res.status(400).send(`Webhook Error: ${err.message}`)
@@ -103,7 +103,7 @@ class StripeProviderService extends PaymentService {
const paymentIntent = await this.stripe_.paymentIntents.create({ const paymentIntent = await this.stripe_.paymentIntents.create({
customer: stripeCustomerId, customer: stripeCustomerId,
amount: amount * 100, // Stripe amount is in cents amount: amount * 100, // Stripe amount is in cents
currency: currency_code currency: currency_code,
}) })
return paymentIntent return paymentIntent
@@ -134,7 +134,7 @@ class StripeProviderService extends PaymentService {
const { id } = data const { id } = data
const amount = this.totalsService_.getTotal(cart) const amount = this.totalsService_.getTotal(cart)
return this.stripe_.paymentIntents.update(id, { return this.stripe_.paymentIntents.update(id, {
amount amount,
}) })
} catch (error) { } catch (error) {
throw error throw error
+1 -1
View File
@@ -72,4 +72,4 @@
"winston": "^3.2.1" "winston": "^3.2.1"
}, },
"gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc" "gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc"
} }
@@ -12,6 +12,8 @@ describe("POST /admin/shipping-options", () => {
name: "Test option", name: "Test option",
region_id: "testregion", region_id: "testregion",
provider_id: "test_provider", provider_id: "test_provider",
data: { id: "test" },
profile_id: "test",
price: { price: {
type: "flat_rate", type: "flat_rate",
amount: 100, amount: 100,
@@ -32,6 +34,7 @@ describe("POST /admin/shipping-options", () => {
}) })
it("returns 200", () => { it("returns 200", () => {
console.log(subject)
expect(subject.status).toEqual(200) expect(subject.status).toEqual(200)
}) })
@@ -41,6 +44,8 @@ describe("POST /admin/shipping-options", () => {
name: "Test option", name: "Test option",
region_id: "testregion", region_id: "testregion",
provider_id: "test_provider", provider_id: "test_provider",
data: { id: "test" },
profile_id: "test",
price: { price: {
type: "flat_rate", type: "flat_rate",
amount: 100, amount: 100,
@@ -5,11 +5,14 @@ export default async (req, res) => {
name: Validator.string().required(), name: Validator.string().required(),
region_id: Validator.string().required(), region_id: Validator.string().required(),
provider_id: Validator.string().required(), provider_id: Validator.string().required(),
data: Validator.object(), profile_id: Validator.string().required(),
price: Validator.object().keys({ data: Validator.object().required(),
type: Validator.string().required(), price: Validator.object()
amount: Validator.number().optional(), .keys({
}), type: Validator.string().required(),
amount: Validator.number().optional(),
})
.required(),
requirements: Validator.array() requirements: Validator.array()
.items( .items(
Validator.object({ Validator.object({
@@ -33,7 +36,7 @@ export default async (req, res) => {
// Add to default shipping profile // Add to default shipping profile
const { _id } = await shippingProfileService.retrieveDefault() const { _id } = await shippingProfileService.retrieveDefault()
await shippingProfileService.addProduct(_id, data._id) await shippingProfileService.addShippingOption(_id, data._id)
res.status(200).json({ shipping_option: data }) res.status(200).json({ shipping_option: data })
} catch (err) { } catch (err) {
@@ -12,6 +12,7 @@ export default async (req, res) => {
discounts: Validator.array().items({ discounts: Validator.array().items({
code: Validator.string(), code: Validator.string(),
}), }),
customer_id: Validator.string(),
}) })
const { value, error } = schema.validate(req.body) const { value, error } = schema.validate(req.body)
@@ -27,6 +28,10 @@ export default async (req, res) => {
} }
try { try {
if (value.customer_id) {
await cartService.updateCustomerId(id, value.customer_id)
}
if (value.region_id) { if (value.region_id) {
await cartService.setRegion(id, value.region_id) await cartService.setRegion(id, value.region_id)
} }
@@ -17,7 +17,7 @@ export default async (req, res) => {
try { try {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
const customer = await customerService.create(value) 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 }) res.status(201).json({ customer: data })
} catch (err) { } catch (err) {
throw err throw err
+34
View File
@@ -370,6 +370,40 @@ class CartService extends BaseService {
return result 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 * Sets the email of a cart
+1 -1
View File
@@ -26,7 +26,7 @@ class CustomerService extends BaseService {
*/ */
validateId_(rawId) { validateId_(rawId) {
const schema = Validator.objectId() const schema = Validator.objectId()
const { value, error } = schema.validate(rawId) const { value, error } = schema.validate(rawId.toString())
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, MedusaError.Types.INVALID_ARGUMENT,
+1 -1
View File
@@ -35,7 +35,7 @@ class DiscountService extends BaseService {
*/ */
validateId_(rawId) { validateId_(rawId) {
const schema = Validator.objectId() const schema = Validator.objectId()
const { value, error } = schema.validate(rawId) const { value, error } = schema.validate(rawId.toString())
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, MedusaError.Types.INVALID_ARGUMENT,
@@ -14,7 +14,7 @@ class FulfillmentProviderService {
*/ */
retrieveProvider(provider_id) { retrieveProvider(provider_id) {
try { try {
return this.container_.resolve(`fp_${provider_id}`) return this.container_[`fp_${provider_id}`]
} catch (err) { } catch (err) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_FOUND, MedusaError.Types.NOT_FOUND,
+9 -1
View File
@@ -43,7 +43,7 @@ class OrderService extends BaseService {
*/ */
validateId_(rawId) { validateId_(rawId) {
const schema = Validator.objectId() const schema = Validator.objectId()
const { value, error } = schema.validate(rawId) const { value, error } = schema.validate(rawId.toString())
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, MedusaError.Types.INVALID_ARGUMENT,
@@ -139,6 +139,14 @@ class OrderService extends BaseService {
return order 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 * Creates an order
* @param {object} order - the order to create * @param {object} order - the order to create
@@ -37,7 +37,7 @@ class ShippingOptionService extends BaseService {
*/ */
validateId_(rawId) { validateId_(rawId) {
const schema = Validator.objectId() const schema = Validator.objectId()
const { value, error } = schema.validate(rawId) const { value, error } = schema.validate(rawId.toString())
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, 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( throw new MedusaError(
MedusaError.Types.INVALID_DATA, 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) { validateId_(rawId) {
const schema = Validator.objectId() const schema = Validator.objectId()
const { value, error } = schema.validate(rawId) const { value, error } = schema.validate(rawId.toString())
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, MedusaError.Types.INVALID_ARGUMENT,