fix: /api/routes/store/customers pass linting (#646)

This commit is contained in:
timothy22000
2021-10-25 07:55:37 +01:00
committed by GitHub
parent 5cb1b5687e
commit 7cc94dc479
10 changed files with 99 additions and 139 deletions

View File

@@ -49,7 +49,6 @@
/packages/medusa/src/api/routes/store/auth
/packages/medusa/src/api/routes/store/carts
/packages/medusa/src/api/routes/store/customers
/packages/medusa/src/api/routes/store/regions
/packages/medusa/src/api/routes/store/return-reasons

View File

@@ -41,17 +41,13 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
const customerService = req.scope.resolve("customerService")
let customer = await customerService.addAddress(id, value.address)
customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
let customer = await customerService.addAddress(id, value.address)
customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
} catch (err) {
throw err
}
res.status(200).json({ customer })
}

View File

@@ -40,22 +40,18 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.create(value)
const customerService = req.scope.resolve("customerService")
let customer = await customerService.create(value)
// Add JWT to cookie
req.session.jwt = jwt.sign({ customer_id: customer.id }, config.jwtSecret, {
expiresIn: "30d",
})
// Add JWT to cookie
req.session.jwt = jwt.sign({ customer_id: customer.id }, config.jwtSecret, {
expiresIn: "30d",
})
customer = await customerService.retrieve(customer.id, {
relations: defaultRelations,
select: defaultFields,
})
customer = await customerService.retrieve(customer.id, {
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
} catch (err) {
throw err
}
res.status(200).json({ customer })
}

View File

@@ -25,15 +25,11 @@ export default async (req, res) => {
const { address_id } = req.params
const customerService = req.scope.resolve("customerService")
try {
await customerService.removeAddress(id, address_id)
customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
await customerService.removeAddress(id, address_id)
const customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
} catch (err) {
throw err
}
res.json({ customer })
}

View File

@@ -19,14 +19,10 @@ import { defaultRelations, defaultFields } from "./"
*/
export default async (req, res) => {
const id = req.user.customer_id
try {
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
} catch (err) {
throw err
}
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
}

View File

@@ -27,29 +27,25 @@
*/
export default async (req, res) => {
const id = req.user.customer_id
try {
const storeService = req.scope.resolve("storeService")
const paymentProviderService = req.scope.resolve("paymentProviderService")
const customerService = req.scope.resolve("customerService")
const storeService = req.scope.resolve("storeService")
const paymentProviderService = req.scope.resolve("paymentProviderService")
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id)
const customer = await customerService.retrieve(id)
const store = await storeService.retrieve(["payment_providers"])
const store = await storeService.retrieve(["payment_providers"])
const methods = await Promise.all(
store.payment_providers.map(async (next) => {
const provider = paymentProviderService.retrieveProvider(next)
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,
}))
})
)
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
}
res.json({ payment_methods: methods.flat() })
}

View File

@@ -1,4 +1,3 @@
import _ from "lodash"
import {
defaultRelations,
defaultFields,
@@ -29,43 +28,37 @@ import {
*/
export default async (req, res) => {
const id = req.user.customer_id
try {
const orderService = req.scope.resolve("orderService")
const selector = {
customer_id: id,
}
const orderService = req.scope.resolve("orderService")
const limit = parseInt(req.query.limit) || 10
const offset = parseInt(req.query.offset) || 0
let includeFields = []
if ("fields" in req.query) {
includeFields = req.query.fields.split(",")
includeFields = includeFields.filter((f) => allowedFields.includes(f))
}
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
expandFields = expandFields.filter((f) => allowedRelations.includes(f))
}
const listConfig = {
select: includeFields.length ? includeFields : defaultFields,
relations: expandFields.length ? expandFields : defaultRelations,
skip: offset,
take: limit,
order: { created_at: "DESC" },
}
const [orders, count] = await orderService.listAndCount(
selector,
listConfig
)
res.json({ orders, count, offset, limit })
} catch (error) {
throw error
const selector = {
customer_id: id,
}
const limit = parseInt(req.query.limit) || 10
const offset = parseInt(req.query.offset) || 0
let includeFields = []
if ("fields" in req.query) {
includeFields = req.query.fields.split(",")
includeFields = includeFields.filter((f) => allowedFields.includes(f))
}
let expandFields = []
if ("expand" in req.query) {
expandFields = req.query.expand.split(",")
expandFields = expandFields.filter((f) => allowedRelations.includes(f))
}
const listConfig = {
select: includeFields.length ? includeFields : defaultFields,
relations: expandFields.length ? expandFields : defaultRelations,
skip: offset,
take: limit,
order: { created_at: "DESC" },
}
const [orders, count] = await orderService.listAndCount(selector, listConfig)
res.json({ orders, count, offset, limit })
}

View File

@@ -21,15 +21,11 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieveByEmail(value.email)
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieveByEmail(value.email)
// Will generate a token and send it to the customer via an email privder
await customerService.generateResetPasswordToken(customer.id)
// Will generate a token and send it to the customer via an email privder
await customerService.generateResetPasswordToken(customer.id)
res.sendStatus(204)
} catch (error) {
throw error
}
res.sendStatus(204)
}

View File

@@ -43,20 +43,17 @@ export default async (req, res) => {
}
const customerService = req.scope.resolve("customerService")
try {
let customer = await customerService.updateAddress(
id,
address_id,
value.address
)
customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
let customer = await customerService.updateAddress(
id,
address_id,
value.address
)
res.json({ customer })
} catch (err) {
throw err
}
customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
}

View File

@@ -1,4 +1,3 @@
import { optional } from "joi"
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
@@ -64,17 +63,13 @@ export default async (req, res) => {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
await customerService.update(id, value)
const customerService = req.scope.resolve("customerService")
await customerService.update(id, value)
const customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
const customer = await customerService.retrieve(id, {
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
} catch (err) {
throw err
}
res.status(200).json({ customer })
}