feat: customer-information (#413)

* added the ability to update email as long as user has_account=false

* revamped and added fix for MC-132

Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
Sebastian Mateos Nicolajsen
2021-09-23 10:22:18 +02:00
committed by GitHub
co-authored by olivermrbl
parent 897ccf475a
commit a70e3ed0ae
7 changed files with 91 additions and 15 deletions
@@ -57,7 +57,7 @@ describe("/admin/customers", () => {
}) })
expect(response.status).toEqual(200) expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3) expect(response.data.count).toEqual(4)
expect(response.data.customers).toEqual( expect(response.data.customers).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
@@ -69,6 +69,9 @@ describe("/admin/customers", () => {
expect.objectContaining({ expect.objectContaining({
id: "test-customer-3", id: "test-customer-3",
}), }),
expect.objectContaining({
id: "test-customer-has_account",
}),
]) ])
) )
}) })
@@ -129,4 +132,51 @@ describe("/admin/customers", () => {
) )
}) })
}) })
describe("POST /admin/customers/:id", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
await customerSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("Correctly updates customer", async () => {
const api = useApi()
const response = await api
.post(
"/admin/customers/test-customer-3",
{
first_name: "newf",
last_name: "newl",
email: "new@email.com",
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
first_name: "newf",
last_name: "newl",
email: "new@email.com",
})
)
})
})
}) })
@@ -1,27 +1,33 @@
const { Customer, Address } = require("@medusajs/medusa"); const { Customer, Address } = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => { module.exports = async (connection, data = {}) => {
const manager = connection.manager; const manager = connection.manager
await manager.insert(Customer, { await manager.insert(Customer, {
id: "test-customer-1", id: "test-customer-1",
email: "test1@email.com", email: "test1@email.com",
}); })
await manager.insert(Customer, { await manager.insert(Customer, {
id: "test-customer-2", id: "test-customer-2",
email: "test2@email.com", email: "test2@email.com",
}); })
await manager.insert(Customer, { await manager.insert(Customer, {
id: "test-customer-3", id: "test-customer-3",
email: "test3@email.com", email: "test3@email.com",
}); })
await manager.insert(Customer, {
id: "test-customer-has_account",
email: "test4@email.com",
has_account: true,
})
await manager.insert(Address, { await manager.insert(Address, {
id: "test-address", id: "test-address",
first_name: "Lebron", first_name: "Lebron",
last_name: "James", last_name: "James",
customer_id: "test-customer-1", customer_id: "test-customer-1",
}); })
}; }
@@ -12,6 +12,9 @@ import { Validator, MedusaError } from "medusa-core-utils"
* application/json: * application/json:
* schema: * schema:
* properties: * properties:
* email:
* type: string
* description: The Customer's email. Only providable if user not registered.
* first_name: * first_name:
* type: string * type: string
* description: The Customer's first name. * description: The Customer's first name.
@@ -37,6 +40,7 @@ export default async (req, res) => {
const { id } = req.params const { id } = req.params
const schema = Validator.object().keys({ const schema = Validator.object().keys({
email: Validator.string().optional(),
first_name: Validator.string().optional(), first_name: Validator.string().optional(),
last_name: Validator.string().optional(), last_name: Validator.string().optional(),
password: Validator.string().optional(), password: Validator.string().optional(),
@@ -50,9 +54,19 @@ export default async (req, res) => {
try { try {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id)
if (value.email && customer.has_account) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Email cannot be changed when the user has registered their account"
)
}
await customerService.update(id, value) await customerService.update(id, value)
const customer = await customerService.retrieve(id, { customer = await customerService.retrieve(id, {
relations: ["orders"], relations: ["orders"],
}) })
res.status(200).json({ customer }) res.status(200).json({ customer })
@@ -11,6 +11,7 @@ describe("POST /store/customers/:id", () => {
payload: { payload: {
first_name: "LeBron", first_name: "LeBron",
last_name: "James", last_name: "James",
email: "test@email.com",
}, },
clientSession: { clientSession: {
jwt: { jwt: {
@@ -31,6 +32,7 @@ describe("POST /store/customers/:id", () => {
{ {
first_name: "LeBron", first_name: "LeBron",
last_name: "James", last_name: "James",
email: "test@email.com",
} }
) )
}) })
@@ -1,3 +1,4 @@
import { optional } from "joi"
import { Validator, MedusaError } from "medusa-core-utils" import { Validator, MedusaError } from "medusa-core-utils"
import { defaultRelations, defaultFields } from "./" import { defaultRelations, defaultFields } from "./"
@@ -27,6 +28,9 @@ import { defaultRelations, defaultFields } from "./"
* phone: * phone:
* description: "The Customer's phone number." * description: "The Customer's phone number."
* type: string * type: string
* email:
* description: "The email of the customer."
* type: string
* metadata: * metadata:
* description: "Metadata about the customer." * description: "Metadata about the customer."
* type: object * type: object
@@ -51,6 +55,7 @@ export default async (req, res) => {
last_name: Validator.string().optional(), last_name: Validator.string().optional(),
password: Validator.string().optional(), password: Validator.string().optional(),
phone: Validator.string().optional(), phone: Validator.string().optional(),
email: Validator.string().optional(),
metadata: Validator.object().optional(), metadata: Validator.object().optional(),
}) })
@@ -1,5 +1,4 @@
import { IdMap, MockManager, MockRepository } from "medusa-test-utils" import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
import { add } from "winston"
import CustomerService from "../customer" import CustomerService from "../customer"
const eventBusService = { const eventBusService = {
+5 -5
View File
@@ -1407,7 +1407,7 @@
"@types/yargs" "^15.0.0" "@types/yargs" "^15.0.0"
chalk "^3.0.0" chalk "^3.0.0"
"@medusajs/medusa-cli@^1.1.17": "@medusajs/medusa-cli@^1.1.18":
version "1.1.18" version "1.1.18"
resolved "https://registry.yarnpkg.com/@medusajs/medusa-cli/-/medusa-cli-1.1.18.tgz#a2b34575a81a7df239d6d06cf0d0b192e2b8c8db" resolved "https://registry.yarnpkg.com/@medusajs/medusa-cli/-/medusa-cli-1.1.18.tgz#a2b34575a81a7df239d6d06cf0d0b192e2b8c8db"
integrity sha512-JEvQVjebaGuOF5BsqjZYnewmU4TPbrnhODKVyadPKPb/cxPcCMODg21d5QyoaVlcXood08LgTFe8CfdWoyubVw== integrity sha512-JEvQVjebaGuOF5BsqjZYnewmU4TPbrnhODKVyadPKPb/cxPcCMODg21d5QyoaVlcXood08LgTFe8CfdWoyubVw==
@@ -2046,7 +2046,7 @@ babel-preset-jest@^25.5.0:
babel-plugin-jest-hoist "^25.5.0" babel-plugin-jest-hoist "^25.5.0"
babel-preset-current-node-syntax "^0.1.2" babel-preset-current-node-syntax "^0.1.2"
babel-preset-medusa-package@^1.1.14: babel-preset-medusa-package@^1.1.15:
version "1.1.15" version "1.1.15"
resolved "https://registry.yarnpkg.com/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.15.tgz#6917cadd8abe9a1f64c71b5c43ab507df193effc" resolved "https://registry.yarnpkg.com/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.15.tgz#6917cadd8abe9a1f64c71b5c43ab507df193effc"
integrity sha512-toA8mFdvLeKbbRJ7KvQvpL6VJnzkKURZv7Yd97cXMMNpdjrhp+SZppcNOL2tk6ywgBAs4NC2LCVjtZInMMBS6Q== integrity sha512-toA8mFdvLeKbbRJ7KvQvpL6VJnzkKURZv7Yd97cXMMNpdjrhp+SZppcNOL2tk6ywgBAs4NC2LCVjtZInMMBS6Q==
@@ -5462,7 +5462,7 @@ medusa-core-utils@^0.1.27:
"@hapi/joi" "^16.1.8" "@hapi/joi" "^16.1.8"
joi-objectid "^3.0.1" joi-objectid "^3.0.1"
medusa-core-utils@^1.1.21, medusa-core-utils@^1.1.22: medusa-core-utils@^1.1.22:
version "1.1.22" version "1.1.22"
resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.22.tgz#84ce0af0a7c672191d758ea462056e30a39d08b1" resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.22.tgz#84ce0af0a7c672191d758ea462056e30a39d08b1"
integrity sha512-kMuRkWOuNG4Bw6epg/AYu95UJuE+rjHTeTWRLbEPrYGjWREV82tLWVDI21/QcccmaHmMU98Rkw2z9JwyFZIiyw== integrity sha512-kMuRkWOuNG4Bw6epg/AYu95UJuE+rjHTeTWRLbEPrYGjWREV82tLWVDI21/QcccmaHmMU98Rkw2z9JwyFZIiyw==
@@ -5470,7 +5470,7 @@ medusa-core-utils@^1.1.21, medusa-core-utils@^1.1.22:
joi "^17.3.0" joi "^17.3.0"
joi-objectid "^3.0.1" joi-objectid "^3.0.1"
medusa-interfaces@^1.1.22: medusa-interfaces@^1.1.23:
version "1.1.23" version "1.1.23"
resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-1.1.23.tgz#b552a8c1d0eaddeff30472ab238652b9e1a56e73" resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-1.1.23.tgz#b552a8c1d0eaddeff30472ab238652b9e1a56e73"
integrity sha512-dHCOnsyYQvjrtRd3p0ZqQZ4M/zmo4M/BAgVfRrYSyGrMdQ86TK9Z1DQDCHEzM1216AxEfXz2JYUD7ilTfG2iHQ== integrity sha512-dHCOnsyYQvjrtRd3p0ZqQZ4M/zmo4M/BAgVfRrYSyGrMdQ86TK9Z1DQDCHEzM1216AxEfXz2JYUD7ilTfG2iHQ==
@@ -5492,7 +5492,7 @@ medusa-telemetry@^0.0.5:
remove-trailing-slash "^0.1.1" remove-trailing-slash "^0.1.1"
uuid "^8.3.2" uuid "^8.3.2"
medusa-test-utils@^1.1.24: medusa-test-utils@^1.1.25:
version "1.1.25" version "1.1.25"
resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-1.1.25.tgz#7c4aa8a70ec8a95875304258ffbe7493a1e5a7fc" resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-1.1.25.tgz#7c4aa8a70ec8a95875304258ffbe7493a1e5a7fc"
integrity sha512-4xy20KsZBR1XcuzckGRq9A+GJwh+CFHzVw3dajaO4iiNpL/a9K3Yj2N4f/8BgRcQyw5PnkKGJ0pzv+OR8+5GVw== integrity sha512-4xy20KsZBR1XcuzckGRq9A+GJwh+CFHzVw3dajaO4iiNpL/a9K3Yj2N4f/8BgRcQyw5PnkKGJ0pzv+OR8+5GVw==