fix: better store/customer support

This commit is contained in:
Sebastian Rindom
2021-06-29 16:31:30 +02:00
parent 19dcaf8fb3
commit 6342e68d06
13 changed files with 235 additions and 34 deletions
+2 -1
View File
@@ -5,10 +5,11 @@
export const MedusaErrorTypes = {
/** Errors stemming from the database */
DB_ERROR: "database_error",
DUPLICATE_ERROR: "duplicate_error",
INVALID_ARGUMENT: "invalid_argument",
INVALID_DATA: "invalid_data",
NOT_FOUND: "not_found",
NOT_ALLOWED: "not_allowed"
NOT_ALLOWED: "not_allowed",
}
/**
@@ -9,6 +9,9 @@ export default () => {
let statusCode = 500
switch (err.name) {
case MedusaError.Types.DUPLICATE_ERROR:
statusCode = 409
break
case MedusaError.Types.NOT_ALLOWED:
case MedusaError.Types.INVALID_DATA:
statusCode = 400
@@ -1,4 +1,5 @@
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers/{id}/addresses
@@ -45,7 +46,8 @@ export default async (req, res) => {
let customer = await customerService.addAddress(id, value.address)
customer = await customerService.retrieve(id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
@@ -1,6 +1,7 @@
import jwt from "jsonwebtoken"
import { Validator, MedusaError } from "medusa-core-utils"
import config from "../../../../config"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers
@@ -40,6 +41,7 @@ export default async (req, res) => {
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.create(value)
@@ -50,7 +52,8 @@ export default async (req, res) => {
})
customer = await customerService.retrieve(customer.id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
@@ -1,3 +1,5 @@
import { defaultRelations, defaultFields } from "./"
/**
* @oas [delete] /customers/{id}/addresses/{address_id}
* operationId: DeleteCustomersCustomerAddressesAddress
@@ -25,7 +27,8 @@ export default async (req, res) => {
try {
await customerService.removeAddress(id, address_id)
customer = await customerService.retrieve(id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
@@ -1,3 +1,5 @@
import { defaultRelations, defaultFields } from "./"
/**
* @oas [get] /customers/{id}
* operationId: GetCustomersCustomer
@@ -22,7 +24,8 @@ export default async (req, res) => {
try {
const customerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
} catch (err) {
@@ -57,3 +57,39 @@ export default (app, container) => {
return app
}
export const defaultRelations = ["shipping_addresses"]
export const defaultFields = [
"id",
"email",
"first_name",
"last_name",
"billing_address_id",
"phone",
"has_account",
"created_at",
"updated_at",
"deleted_at",
"metadata",
]
export const allowedRelations = [
"shipping_addresses",
"billing_address",
"orders",
]
export const allowedFields = [
"id",
"email",
"first_name",
"last_name",
"billing_address_id",
"phone",
"has_account",
"created_at",
"updated_at",
"deleted_at",
"metadata",
]
@@ -1,4 +1,5 @@
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers/{id}/addresses/{address_id}
@@ -50,7 +51,8 @@ export default async (req, res) => {
)
customer = await customerService.retrieve(id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.json({ customer })
@@ -1,4 +1,5 @@
import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./"
/**
* @oas [post] /customers/{id}
@@ -24,6 +25,9 @@ import { Validator, MedusaError } from "medusa-core-utils"
* phone:
* description: "The Customer's phone number."
* type: string
* metadata:
* description: "Metadata about the customer."
* type: object
* tags:
* - Customer
* responses:
@@ -44,6 +48,7 @@ export default async (req, res) => {
last_name: Validator.string().optional(),
password: Validator.string().optional(),
phone: Validator.string().optional(),
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
@@ -56,7 +61,8 @@ export default async (req, res) => {
let customer = await customerService.update(id, value)
customer = await customerService.retrieve(customer.id, {
relations: ["shipping_addresses"],
relations: defaultRelations,
select: defaultFields,
})
res.status(200).json({ customer })
+7
View File
@@ -318,6 +318,13 @@ class CustomerService extends BaseService {
const existing = await this.retrieveByEmail(email).catch(err => undefined)
if (existing && existing.has_account) {
throw new MedusaError(
MedusaError.Types.DUPLICATE_ERROR,
"A customer with the given email already has an account. Log in instead"
)
}
if (existing && password && !existing.has_account) {
const hashedPassword = await this.hashPassword_(password)
customer.password_hash = hashedPassword