diff --git a/integration-tests/api/__tests__/admin/customer.js b/integration-tests/api/__tests__/admin/customer.js index 1458311eff..510ad13192 100644 --- a/integration-tests/api/__tests__/admin/customer.js +++ b/integration-tests/api/__tests__/admin/customer.js @@ -57,7 +57,7 @@ describe("/admin/customers", () => { }) expect(response.status).toEqual(200) - expect(response.data.count).toEqual(3) + expect(response.data.count).toEqual(4) expect(response.data.customers).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -69,6 +69,9 @@ describe("/admin/customers", () => { expect.objectContaining({ 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", + }) + ) + }) + }) }) diff --git a/integration-tests/api/helpers/customer-seeder.js b/integration-tests/api/helpers/customer-seeder.js index 78b3e22ec5..acef948bbd 100644 --- a/integration-tests/api/helpers/customer-seeder.js +++ b/integration-tests/api/helpers/customer-seeder.js @@ -1,27 +1,33 @@ -const { Customer, Address } = require("@medusajs/medusa"); +const { Customer, Address } = require("@medusajs/medusa") module.exports = async (connection, data = {}) => { - const manager = connection.manager; + const manager = connection.manager await manager.insert(Customer, { id: "test-customer-1", email: "test1@email.com", - }); + }) await manager.insert(Customer, { id: "test-customer-2", email: "test2@email.com", - }); + }) await manager.insert(Customer, { id: "test-customer-3", email: "test3@email.com", - }); + }) + + await manager.insert(Customer, { + id: "test-customer-has_account", + email: "test4@email.com", + has_account: true, + }) await manager.insert(Address, { id: "test-address", first_name: "Lebron", last_name: "James", customer_id: "test-customer-1", - }); -}; + }) +} diff --git a/packages/medusa/src/api/routes/admin/customers/update-customer.js b/packages/medusa/src/api/routes/admin/customers/update-customer.js index c85475e31e..bc3dd3ff67 100644 --- a/packages/medusa/src/api/routes/admin/customers/update-customer.js +++ b/packages/medusa/src/api/routes/admin/customers/update-customer.js @@ -12,6 +12,9 @@ import { Validator, MedusaError } from "medusa-core-utils" * application/json: * schema: * properties: + * email: + * type: string + * description: The Customer's email. Only providable if user not registered. * first_name: * type: string * description: The Customer's first name. @@ -37,6 +40,7 @@ export default async (req, res) => { const { id } = req.params const schema = Validator.object().keys({ + email: Validator.string().optional(), first_name: Validator.string().optional(), last_name: Validator.string().optional(), password: Validator.string().optional(), @@ -50,9 +54,19 @@ export default async (req, res) => { try { 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) - const customer = await customerService.retrieve(id, { + customer = await customerService.retrieve(id, { relations: ["orders"], }) res.status(200).json({ customer }) diff --git a/packages/medusa/src/api/routes/store/customers/__tests__/update-customer.js b/packages/medusa/src/api/routes/store/customers/__tests__/update-customer.js index a1a9c7925e..fb84059304 100644 --- a/packages/medusa/src/api/routes/store/customers/__tests__/update-customer.js +++ b/packages/medusa/src/api/routes/store/customers/__tests__/update-customer.js @@ -11,6 +11,7 @@ describe("POST /store/customers/:id", () => { payload: { first_name: "LeBron", last_name: "James", + email: "test@email.com", }, clientSession: { jwt: { @@ -31,6 +32,7 @@ describe("POST /store/customers/:id", () => { { first_name: "LeBron", last_name: "James", + email: "test@email.com", } ) }) diff --git a/packages/medusa/src/api/routes/store/customers/update-customer.js b/packages/medusa/src/api/routes/store/customers/update-customer.js index a3635f9ec5..8f862b3551 100644 --- a/packages/medusa/src/api/routes/store/customers/update-customer.js +++ b/packages/medusa/src/api/routes/store/customers/update-customer.js @@ -1,3 +1,4 @@ +import { optional } from "joi" import { Validator, MedusaError } from "medusa-core-utils" import { defaultRelations, defaultFields } from "./" @@ -27,6 +28,9 @@ import { defaultRelations, defaultFields } from "./" * phone: * description: "The Customer's phone number." * type: string + * email: + * description: "The email of the customer." + * type: string * metadata: * description: "Metadata about the customer." * type: object @@ -51,6 +55,7 @@ export default async (req, res) => { last_name: Validator.string().optional(), password: Validator.string().optional(), phone: Validator.string().optional(), + email: Validator.string().optional(), metadata: Validator.object().optional(), }) diff --git a/packages/medusa/src/services/__tests__/customer.js b/packages/medusa/src/services/__tests__/customer.js index e980cda5a3..0c3ed6dd50 100644 --- a/packages/medusa/src/services/__tests__/customer.js +++ b/packages/medusa/src/services/__tests__/customer.js @@ -1,5 +1,4 @@ import { IdMap, MockManager, MockRepository } from "medusa-test-utils" -import { add } from "winston" import CustomerService from "../customer" const eventBusService = { diff --git a/packages/medusa/yarn.lock b/packages/medusa/yarn.lock index df967251e9..e863d0abe7 100644 --- a/packages/medusa/yarn.lock +++ b/packages/medusa/yarn.lock @@ -1407,7 +1407,7 @@ "@types/yargs" "^15.0.0" chalk "^3.0.0" -"@medusajs/medusa-cli@^1.1.17": +"@medusajs/medusa-cli@^1.1.18": version "1.1.18" resolved "https://registry.yarnpkg.com/@medusajs/medusa-cli/-/medusa-cli-1.1.18.tgz#a2b34575a81a7df239d6d06cf0d0b192e2b8c8db" integrity sha512-JEvQVjebaGuOF5BsqjZYnewmU4TPbrnhODKVyadPKPb/cxPcCMODg21d5QyoaVlcXood08LgTFe8CfdWoyubVw== @@ -2046,7 +2046,7 @@ babel-preset-jest@^25.5.0: babel-plugin-jest-hoist "^25.5.0" 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" resolved "https://registry.yarnpkg.com/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.15.tgz#6917cadd8abe9a1f64c71b5c43ab507df193effc" integrity sha512-toA8mFdvLeKbbRJ7KvQvpL6VJnzkKURZv7Yd97cXMMNpdjrhp+SZppcNOL2tk6ywgBAs4NC2LCVjtZInMMBS6Q== @@ -5462,7 +5462,7 @@ medusa-core-utils@^0.1.27: "@hapi/joi" "^16.1.8" 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" resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.22.tgz#84ce0af0a7c672191d758ea462056e30a39d08b1" 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-objectid "^3.0.1" -medusa-interfaces@^1.1.22: +medusa-interfaces@^1.1.23: version "1.1.23" resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-1.1.23.tgz#b552a8c1d0eaddeff30472ab238652b9e1a56e73" integrity sha512-dHCOnsyYQvjrtRd3p0ZqQZ4M/zmo4M/BAgVfRrYSyGrMdQ86TK9Z1DQDCHEzM1216AxEfXz2JYUD7ilTfG2iHQ== @@ -5492,7 +5492,7 @@ medusa-telemetry@^0.0.5: remove-trailing-slash "^0.1.1" uuid "^8.3.2" -medusa-test-utils@^1.1.24: +medusa-test-utils@^1.1.25: version "1.1.25" 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==