(feat): Adds phone number to Customer
This commit is contained in:
@@ -8,6 +8,7 @@ export default async (req, res) => {
|
||||
first_name: Validator.string().required(),
|
||||
last_name: Validator.string().required(),
|
||||
password: Validator.string().required(),
|
||||
phone: Validator.string().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
|
||||
@@ -5,7 +5,13 @@ export default async (req, res) => {
|
||||
let customer = await customerService.retrieve(id)
|
||||
customer = await customerService.decorate(
|
||||
customer,
|
||||
["email", "payment_methods", "has_account", "shipping_addresses"],
|
||||
[
|
||||
"email",
|
||||
"payment_methods",
|
||||
"has_account",
|
||||
"shipping_addresses",
|
||||
"phone",
|
||||
],
|
||||
["orders"]
|
||||
)
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@ export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
first_name: Validator.string(),
|
||||
last_name: Validator.string(),
|
||||
password: Validator.string(),
|
||||
first_name: Validator.string().optional(),
|
||||
last_name: Validator.string().optional(),
|
||||
password: Validator.string().optional(),
|
||||
phone: Validator.string().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
|
||||
@@ -10,6 +10,7 @@ export default async (req, res) => {
|
||||
first_name: Validator.string().required(),
|
||||
last_name: Validator.string().required(),
|
||||
password: Validator.string().required(),
|
||||
phone: Validator.string().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -36,6 +37,7 @@ export default async (req, res) => {
|
||||
"shipping_addresses",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"phone",
|
||||
])
|
||||
res.status(201).json({ customer: data })
|
||||
} catch (err) {
|
||||
|
||||
@@ -5,7 +5,7 @@ export default async (req, res) => {
|
||||
let customer = await customerService.retrieve(id)
|
||||
customer = customerService.decorate(
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["email", "first_name", "last_name", "shipping_addresses", "phone"],
|
||||
["orders"]
|
||||
)
|
||||
res.json({ customer })
|
||||
|
||||
@@ -4,9 +4,10 @@ export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const schema = Validator.object().keys({
|
||||
first_name: Validator.string(),
|
||||
last_name: Validator.string(),
|
||||
password: Validator.string(),
|
||||
first_name: Validator.string().optional(),
|
||||
last_name: Validator.string().optional(),
|
||||
password: Validator.string().optional(),
|
||||
phone: Validator.string().optional(),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
@@ -19,7 +20,7 @@ export default async (req, res) => {
|
||||
const customer = await customerService.update(id, value)
|
||||
const data = await customerService.decorate(
|
||||
customer,
|
||||
["email", "first_name", "last_name", "shipping_addresses"],
|
||||
["email", "first_name", "last_name", "shipping_addresses", "phone"],
|
||||
["orders"]
|
||||
)
|
||||
res.status(200).json({ customer: data })
|
||||
|
||||
@@ -17,6 +17,15 @@ export const customers = {
|
||||
billingAddress: {},
|
||||
password_hash: "123456789",
|
||||
},
|
||||
customerWithPhone: {
|
||||
_id: IdMap.getId("customerWithPhone"),
|
||||
email: "oliver@medusa.com",
|
||||
first_name: "Oliver",
|
||||
last_name: "Juhl",
|
||||
billingAddress: {},
|
||||
password_hash: "123456789",
|
||||
phone: "12345678",
|
||||
},
|
||||
}
|
||||
|
||||
export const CustomerModelMock = {
|
||||
@@ -29,6 +38,9 @@ export const CustomerModelMock = {
|
||||
if (query.email === "oliver@medusa.com") {
|
||||
return Promise.resolve(customers.testCustomer)
|
||||
}
|
||||
if (query.phone === "12345678") {
|
||||
return Promise.resolve(customers.customerWithPhone)
|
||||
}
|
||||
if (query._id === IdMap.getId("testCustomer")) {
|
||||
return Promise.resolve(customers.testCustomer)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class CustomerModel extends BaseModel {
|
||||
payment_methods: { type: [mongoose.Schema.Types.Mixed], default: [] },
|
||||
shipping_addresses: { type: [AddressSchema], default: [] },
|
||||
password_hash: { type: String },
|
||||
phone: { type: String, default: "" },
|
||||
has_account: { type: Boolean, default: false },
|
||||
orders: { type: [String], default: [] },
|
||||
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
|
||||
|
||||
@@ -48,6 +48,28 @@ describe("CustomerService", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveByPhone", () => {
|
||||
let result
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
const customerService = new CustomerService({
|
||||
customerModel: CustomerModelMock,
|
||||
})
|
||||
result = await customerService.retrieveByPhone("12345678")
|
||||
})
|
||||
|
||||
it("calls customer model functions", () => {
|
||||
expect(CustomerModelMock.findOne).toHaveBeenCalledTimes(1)
|
||||
expect(CustomerModelMock.findOne).toHaveBeenCalledWith({
|
||||
phone: "12345678",
|
||||
})
|
||||
})
|
||||
|
||||
it("returns the customer", () => {
|
||||
expect(result).toEqual(customers.customerWithPhone)
|
||||
})
|
||||
})
|
||||
|
||||
describe("setMetadata", () => {
|
||||
const customerService = new CustomerService({
|
||||
customerModel: CustomerModelMock,
|
||||
|
||||
@@ -168,6 +168,26 @@ class CustomerService extends BaseService {
|
||||
return customer
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a customer by phone.
|
||||
* @param {string} phone - the phone of the customer to get.
|
||||
* @return {Promise<Customer>} the customer document.
|
||||
*/
|
||||
async retrieveByPhone(phone) {
|
||||
const customer = await this.customerModel_.findOne({ phone }).catch(err => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
|
||||
if (!customer) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Customer with phone ${phone} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return customer
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a password
|
||||
* @param {string} password - the value to hash
|
||||
|
||||
Reference in New Issue
Block a user