Setup future usage
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import passport from "passport"
|
||||
|
||||
export default () => {
|
||||
// Always go to next
|
||||
return (req, res, next) => {
|
||||
passport.authenticate(
|
||||
["jwt", "bearer"],
|
||||
{ session: false },
|
||||
(err, user, info) => {
|
||||
if (err) {
|
||||
return next(err)
|
||||
}
|
||||
req.user = user
|
||||
return next()
|
||||
}
|
||||
)(req, res, next)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import { default as authenticateCustomer } from "./authenticate-customer"
|
||||
import { default as authenticate } from "./authenticate"
|
||||
import { default as wrap } from "./await-middleware"
|
||||
|
||||
export default {
|
||||
authenticate,
|
||||
wrap
|
||||
authenticateCustomer,
|
||||
wrap,
|
||||
}
|
||||
|
||||
@@ -28,7 +28,17 @@ export default async (req, res) => {
|
||||
regionId = regions[0]._id
|
||||
}
|
||||
|
||||
let cart = await cartService.create({ region_id: regionId })
|
||||
let customerId = ""
|
||||
if (req.user && req.user.customer_id) {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.retrieve(req.user.customer_id)
|
||||
customerId = customer._id
|
||||
}
|
||||
|
||||
let cart = await cartService.create({
|
||||
region_id: regionId,
|
||||
customer_id: customerId,
|
||||
})
|
||||
|
||||
if (value.items) {
|
||||
await Promise.all(
|
||||
|
||||
@@ -3,6 +3,18 @@ export default async (req, res) => {
|
||||
try {
|
||||
const cartService = req.scope.resolve("cartService")
|
||||
let cart = await cartService.retrieve(id)
|
||||
|
||||
// If there is a logged in user add the user to the cart
|
||||
if (req.user && req.user.customer_id) {
|
||||
if (!cart.customer_id || cart.customer_id !== req.user.customer_id) {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.retrieve(req.user.customer_id)
|
||||
|
||||
cart = await cartService.updateCustomerId(id, customer._id)
|
||||
cart = await cartService.updateEmail(id, customer.email)
|
||||
}
|
||||
}
|
||||
|
||||
cart = await cartService.decorate(cart, [], ["region"])
|
||||
res.json({ cart })
|
||||
} catch (err) {
|
||||
|
||||
@@ -12,7 +12,6 @@ export default async (req, res) => {
|
||||
discounts: Validator.array().items({
|
||||
code: Validator.string(),
|
||||
}),
|
||||
customer_id: Validator.string(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -28,10 +27,6 @@ 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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id, address_id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
address: Validator.address(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
try {
|
||||
const customer = await customerService.addAddress(id, value.address)
|
||||
const data = await customerService.decorate(
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["orders"]
|
||||
)
|
||||
res.json({ customer: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
export default async (req, res) => {
|
||||
const { id, address_id } = req.params
|
||||
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
try {
|
||||
const customer = await customerService.removeAddress(id, address_id)
|
||||
const data = await customerService.decorate(
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["orders"]
|
||||
)
|
||||
res.json({ customer: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ export default async (req, res) => {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
let customer = await customerService.retrieve(id)
|
||||
customer = customerService.decorate(
|
||||
customer._id,
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["orders"]
|
||||
)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
try {
|
||||
const storeService = req.scope.resolve("storeService")
|
||||
const paymentProviderService = req.scope.resolve("paymentProviderService")
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
|
||||
let customer = await customerService.retrieve(id)
|
||||
|
||||
const store = await storeService.retrieve()
|
||||
|
||||
const methods = await Promise.all(
|
||||
store.payment_providers.map(async next => {
|
||||
const provider = paymentProviderService.retrieveProvider(next)
|
||||
|
||||
const pMethods = await provider.retrieveSavedMethods(customer)
|
||||
return pMethods.map(m => ({
|
||||
provider_id: next,
|
||||
data: m,
|
||||
}))
|
||||
})
|
||||
)
|
||||
|
||||
res.json({ payment_methods: methods.flat() })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -29,5 +29,26 @@ export default app => {
|
||||
"/:id/password",
|
||||
middlewares.wrap(require("./update-password").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/addresses",
|
||||
middlewares.wrap(require("./create-address").default)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id/addresses/:address_id",
|
||||
middlewares.wrap(require("./update-address").default)
|
||||
)
|
||||
|
||||
route.delete(
|
||||
"/:id/addresses/:address_id",
|
||||
middlewares.wrap(require("./delete-address").default)
|
||||
)
|
||||
|
||||
route.get(
|
||||
"/:id/payment-methods",
|
||||
middlewares.wrap(require("./get-payment-methods").default)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { id, address_id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
address: Validator.address(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
try {
|
||||
const customer = await customerService.updateAddress(
|
||||
id,
|
||||
address_id,
|
||||
value.address
|
||||
)
|
||||
const data = await customerService.decorate(
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["orders"]
|
||||
)
|
||||
res.json({ customer: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Router } from "express"
|
||||
import cors from "cors"
|
||||
|
||||
import middlewares from "../../middlewares"
|
||||
|
||||
import authRoutes from "./auth"
|
||||
import productRoutes from "./products"
|
||||
import cartRoutes from "./carts"
|
||||
@@ -22,6 +24,8 @@ export default (app, container, config) => {
|
||||
})
|
||||
)
|
||||
|
||||
route.use(middlewares.authenticateCustomer())
|
||||
|
||||
authRoutes(route)
|
||||
customerRoutes(route)
|
||||
productRoutes(route)
|
||||
|
||||
Reference in New Issue
Block a user