diff --git a/packages/medusa-fulfillment-manual/.babelrc b/packages/medusa-fulfillment-manual/.babelrc index 5c12149795..1d150fdfd9 100644 --- a/packages/medusa-fulfillment-manual/.babelrc +++ b/packages/medusa-fulfillment-manual/.babelrc @@ -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"] } } -} +} \ No newline at end of file diff --git a/packages/medusa-fulfillment-manual/package.json b/packages/medusa-fulfillment-manual/package.json index 2eee6c4ac4..a6fdd9c7a6 100644 --- a/packages/medusa-fulfillment-manual/package.json +++ b/packages/medusa-fulfillment-manual/package.json @@ -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" } -} +} \ No newline at end of file diff --git a/packages/medusa-fulfillment-manual/src/services/manual-fulfillment.js b/packages/medusa-fulfillment-manual/src/services/manual-fulfillment.js index 95df825e89..bfb27c7bab 100644 --- a/packages/medusa-fulfillment-manual/src/services/manual-fulfillment.js +++ b/packages/medusa-fulfillment-manual/src/services/manual-fulfillment.js @@ -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() { diff --git a/packages/medusa-payment-stripe/package.json b/packages/medusa-payment-stripe/package.json index 63e05ba197..2eaccc8d27 100644 --- a/packages/medusa-payment-stripe/package.json +++ b/packages/medusa-payment-stripe/package.json @@ -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" -} +} \ No newline at end of file diff --git a/packages/medusa-payment-stripe/src/api/routes/hooks/stripe.js b/packages/medusa-payment-stripe/src/api/routes/hooks/stripe.js index df6f45cc1c..9584b26a78 100644 --- a/packages/medusa-payment-stripe/src/api/routes/hooks/stripe.js +++ b/packages/medusa-payment-stripe/src/api/routes/hooks/stripe.js @@ -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}`) diff --git a/packages/medusa-payment-stripe/src/services/stripe-provider.js b/packages/medusa-payment-stripe/src/services/stripe-provider.js index 0e52d4b2ce..87001ddccc 100644 --- a/packages/medusa-payment-stripe/src/services/stripe-provider.js +++ b/packages/medusa-payment-stripe/src/services/stripe-provider.js @@ -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 diff --git a/packages/medusa/package.json b/packages/medusa/package.json index c5abb6feb7..041582e8d8 100644 --- a/packages/medusa/package.json +++ b/packages/medusa/package.json @@ -72,4 +72,4 @@ "winston": "^3.2.1" }, "gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc" -} +} \ No newline at end of file diff --git a/packages/medusa/src/api/routes/admin/shipping-options/__tests__/create-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/__tests__/create-shipping-option.js index ed7aa5bab5..d83611a723 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/__tests__/create-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/__tests__/create-shipping-option.js @@ -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, diff --git a/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js index 1e3e3f0d3e..fa5fb1123a 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js @@ -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) { diff --git a/packages/medusa/src/api/routes/store/carts/update-cart.js b/packages/medusa/src/api/routes/store/carts/update-cart.js index 61ee9b37b2..64cc1158c5 100644 --- a/packages/medusa/src/api/routes/store/carts/update-cart.js +++ b/packages/medusa/src/api/routes/store/carts/update-cart.js @@ -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) } diff --git a/packages/medusa/src/api/routes/store/customers/create-customer.js b/packages/medusa/src/api/routes/store/customers/create-customer.js index d8027b4833..1d4f0378f6 100644 --- a/packages/medusa/src/api/routes/store/customers/create-customer.js +++ b/packages/medusa/src/api/routes/store/customers/create-customer.js @@ -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 diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index cc3f9f25fb..c89f013edc 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -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 diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 37fae0dfde..573caf52f5 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -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, diff --git a/packages/medusa/src/services/discount.js b/packages/medusa/src/services/discount.js index 8698a34d29..3b00c31498 100644 --- a/packages/medusa/src/services/discount.js +++ b/packages/medusa/src/services/discount.js @@ -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, diff --git a/packages/medusa/src/services/fulfillment-provider.js b/packages/medusa/src/services/fulfillment-provider.js index 3b28263c26..d5a1784ccd 100644 --- a/packages/medusa/src/services/fulfillment-provider.js +++ b/packages/medusa/src/services/fulfillment-provider.js @@ -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, diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index deb9a722e6..baa83283fe 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -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 diff --git a/packages/medusa/src/services/shipping-option.js b/packages/medusa/src/services/shipping-option.js index f26ecf47ac..443f40c7be 100644 --- a/packages/medusa/src/services/shipping-option.js +++ b/packages/medusa/src/services/shipping-option.js @@ -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." ) } diff --git a/packages/medusa/src/services/store.js b/packages/medusa/src/services/store.js index e78fa7d817..5bf7860860 100644 --- a/packages/medusa/src/services/store.js +++ b/packages/medusa/src/services/store.js @@ -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,