fix: better store/customer support
This commit is contained in:
1 parent
19dcaf8fb3
commit
6342e68d06
13 files changed
+235
-34
No files matched your search
@@ -0,0 +1,135 @@
|
|||||||
|
const { dropDatabase } = require("pg-god");
|
||||||
|
const path = require("path");
|
||||||
|
const { Customer } = require("@medusajs/medusa");
|
||||||
|
|
||||||
|
const setupServer = require("../../../helpers/setup-server");
|
||||||
|
const { useApi } = require("../../../helpers/use-api");
|
||||||
|
const { initDb } = require("../../../helpers/use-db");
|
||||||
|
|
||||||
|
const customerSeeder = require("../../helpers/customer-seeder");
|
||||||
|
|
||||||
|
jest.setTimeout(30000);
|
||||||
|
|
||||||
|
describe("/store/customers", () => {
|
||||||
|
let medusaProcess;
|
||||||
|
let dbConnection;
|
||||||
|
|
||||||
|
const doAfterEach = async (manager) => {
|
||||||
|
await manager.query(`DELETE FROM "address"`);
|
||||||
|
await manager.query(`DELETE FROM "customer"`);
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", ".."));
|
||||||
|
dbConnection = await initDb({ cwd });
|
||||||
|
medusaProcess = await setupServer({ cwd });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
dbConnection.close();
|
||||||
|
await dropDatabase({ databaseName: "medusa-integration" });
|
||||||
|
|
||||||
|
medusaProcess.kill();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /store/customers", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
await manager.insert(Customer, {
|
||||||
|
id: "test_customer",
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Deere",
|
||||||
|
email: "john@deere.com",
|
||||||
|
has_account: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
await doAfterEach(manager);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a customer", async () => {
|
||||||
|
const api = useApi();
|
||||||
|
|
||||||
|
const response = await api.post("/store/customers", {
|
||||||
|
first_name: "James",
|
||||||
|
last_name: "Bond",
|
||||||
|
email: "james@bond.com",
|
||||||
|
password: "test",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.data.customer).not.toHaveProperty("password_hash");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("responds 409 on duplicate", async () => {
|
||||||
|
const api = useApi();
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post("/store/customers", {
|
||||||
|
first_name: "James",
|
||||||
|
last_name: "Bond",
|
||||||
|
email: "john@deere.com",
|
||||||
|
password: "test",
|
||||||
|
})
|
||||||
|
.catch((err) => err.response);
|
||||||
|
|
||||||
|
expect(response.status).toEqual(409);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /store/customers/:id", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
await manager.insert(Customer, {
|
||||||
|
id: "test_customer",
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Deere",
|
||||||
|
email: "john@deere.com",
|
||||||
|
password_hash:
|
||||||
|
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
|
||||||
|
has_account: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
await doAfterEach(manager);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates a customer", async () => {
|
||||||
|
const api = useApi();
|
||||||
|
|
||||||
|
const authResponse = await api.post("/store/auth", {
|
||||||
|
email: "john@deere.com",
|
||||||
|
password: "test",
|
||||||
|
});
|
||||||
|
|
||||||
|
const customerId = authResponse.data.customer.id;
|
||||||
|
const [authCookie] = authResponse.headers["set-cookie"][0].split(";");
|
||||||
|
console.log(authCookie);
|
||||||
|
|
||||||
|
const response = await api.post(
|
||||||
|
`/store/customers/${customerId}`,
|
||||||
|
{
|
||||||
|
password: "test",
|
||||||
|
metadata: { key: "value" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Cookie: authCookie,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.data.customer).not.toHaveProperty("password_hash");
|
||||||
|
expect(response.data.customer).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
metadata: { key: "value" },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,15 +8,15 @@
|
|||||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@medusajs/medusa": "1.1.23-dev-1623081876060",
|
"@medusajs/medusa": "1.1.28-dev-1624965921528",
|
||||||
"medusa-interfaces": "1.1.10-dev-1623081876060",
|
"medusa-interfaces": "1.1.16-dev-1624965921528",
|
||||||
"typeorm": "^0.2.31"
|
"typeorm": "^0.2.31"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/cli": "^7.12.10",
|
"@babel/cli": "^7.12.10",
|
||||||
"@babel/core": "^7.12.10",
|
"@babel/core": "^7.12.10",
|
||||||
"@babel/node": "^7.12.10",
|
"@babel/node": "^7.12.10",
|
||||||
"babel-preset-medusa-package": "1.1.3-dev-1623168481467",
|
"babel-preset-medusa-package": "1.1.9-dev-1624965921528",
|
||||||
"jest": "^26.6.3"
|
"jest": "^26.6.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1215,10 +1215,10 @@
|
|||||||
"@types/yargs" "^15.0.0"
|
"@types/yargs" "^15.0.0"
|
||||||
chalk "^4.0.0"
|
chalk "^4.0.0"
|
||||||
|
|
||||||
"@medusajs/medusa@1.1.23-dev-1623081876060":
|
"@medusajs/medusa@1.1.28-dev-1624965921528":
|
||||||
version "1.1.23"
|
version "1.1.28-dev-1624965921528"
|
||||||
resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.23.tgz#420eae69b20bc3b5a4c8f81825ba46252a1f1c92"
|
resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.28-dev-1624965921528.tgz#f01db1e56c0dc98e02b64d197ca7df7f8041e5a1"
|
||||||
integrity sha512-1n9unNwt1jQV0SGd7053BIIb5P/PzPhX3fFgpwT4OzVbMOewnF6CLNMDaiQ1gI53JbkFY1rbjUPsRZk+9jVrYg==
|
integrity sha512-SBD+fe1neJSJ0EF1oZGsnspB1aU59+qhqxjMAnWung2VJ0VIcraT0cQLkKgsED2XCTkqJQwOYK7X1AW+fZYecg==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@hapi/joi" "^16.1.8"
|
"@hapi/joi" "^16.1.8"
|
||||||
"@types/lodash" "^4.14.168"
|
"@types/lodash" "^4.14.168"
|
||||||
@@ -1239,8 +1239,8 @@
|
|||||||
joi "^17.3.0"
|
joi "^17.3.0"
|
||||||
joi-objectid "^3.0.1"
|
joi-objectid "^3.0.1"
|
||||||
jsonwebtoken "^8.5.1"
|
jsonwebtoken "^8.5.1"
|
||||||
medusa-core-utils "^1.1.9"
|
medusa-core-utils "1.1.15-dev-1624965921528"
|
||||||
medusa-test-utils "^1.1.12"
|
medusa-test-utils "1.1.18-dev-1624965921528"
|
||||||
morgan "^1.9.1"
|
morgan "^1.9.1"
|
||||||
multer "^1.4.2"
|
multer "^1.4.2"
|
||||||
passport "^0.4.0"
|
passport "^0.4.0"
|
||||||
@@ -1696,10 +1696,10 @@ babel-preset-jest@^26.6.2:
|
|||||||
babel-plugin-jest-hoist "^26.6.2"
|
babel-plugin-jest-hoist "^26.6.2"
|
||||||
babel-preset-current-node-syntax "^1.0.0"
|
babel-preset-current-node-syntax "^1.0.0"
|
||||||
|
|
||||||
babel-preset-medusa-package@1.1.3-dev-1623168481467:
|
babel-preset-medusa-package@1.1.9-dev-1624965921528:
|
||||||
version "1.1.3-dev-1623168481467"
|
version "1.1.9-dev-1624965921528"
|
||||||
resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.3-dev-1623168481467.tgz#ae9167644267c52c1016c4695294d81059dfc2ff"
|
resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.9-dev-1624965921528.tgz#e8aaba99cdf6edd524a0db0346e030782da451e0"
|
||||||
integrity sha512-QombHh4IHvYll+DwUgeL93+uNCcFCSW6/rv/rrmcS4MMB+TeZ5iQrK+i1Gf/ns10v1WH2q0+VdExu9GDrdwU3Q==
|
integrity sha512-5waz8A/kFh9wNqr48m0pA85rEmModR+/yih4yVRJoP240ZcIoG793XCtDZPHhcy7vIxp6Ab+dv5+TnuhYIDBIQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/plugin-proposal-class-properties" "^7.12.1"
|
"@babel/plugin-proposal-class-properties" "^7.12.1"
|
||||||
"@babel/plugin-proposal-decorators" "^7.12.1"
|
"@babel/plugin-proposal-decorators" "^7.12.1"
|
||||||
@@ -4150,28 +4150,28 @@ media-typer@0.3.0:
|
|||||||
resolved "http://localhost:4873/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
resolved "http://localhost:4873/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
||||||
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
|
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
|
||||||
|
|
||||||
medusa-core-utils@^1.1.9:
|
medusa-core-utils@1.1.15-dev-1624965921528:
|
||||||
version "1.1.9"
|
version "1.1.15-dev-1624965921528"
|
||||||
resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.9.tgz#7b93c72d9c318ff4ab971381401158eee7d3edd9"
|
resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.15-dev-1624965921528.tgz#7da932df2423e9656ed4e800971fbc00d5124b3c"
|
||||||
integrity sha512-XBxwpCQT82gi/S92Bc0qfCSYyD5Hj+zstUbyOCKGp7nhwFPvYwJ0hp6NPKDSwHZ1uPEmb4rdHcW2qyf1bM4L1Q==
|
integrity sha512-2iuVowwgGfygjDGW1NwR2ubtG/TFVVqBiNECn+N7/rNajN2DIIa863bVTd9qZAy6uFX6CrT1gjBqQNdPUsTitA==
|
||||||
dependencies:
|
dependencies:
|
||||||
joi "^17.3.0"
|
joi "^17.3.0"
|
||||||
joi-objectid "^3.0.1"
|
joi-objectid "^3.0.1"
|
||||||
|
|
||||||
medusa-interfaces@1.1.10-dev-1623081876060:
|
medusa-interfaces@1.1.16-dev-1624965921528:
|
||||||
version "1.1.10"
|
version "1.1.16-dev-1624965921528"
|
||||||
resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.10.tgz#e81b885e11d6c2f05db8d2971edf30b8f8e7ddaa"
|
resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.16-dev-1624965921528.tgz#4fa2e25bba5ecb1bdcdaa8da788e204435c53e69"
|
||||||
integrity sha512-FJSpX3CE5jx2mYqRARFSp5C6x5Hq+MEZ6p2UikuWnm40qjGsbHNl4naZFdBS1u/vSnXq+607oHuZnCNnpRDrPQ==
|
integrity sha512-WKSfrCEAbkJ4I1NetiRULRURCmGY59Mue16cT7RZ2yb8U1Wnskui0OsG/EaS5JdV9HZq/iViEgzWIRTALNT1EQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
medusa-core-utils "^1.1.9"
|
medusa-core-utils "1.1.15-dev-1624965921528"
|
||||||
|
|
||||||
medusa-test-utils@^1.1.12:
|
medusa-test-utils@1.1.18-dev-1624965921528:
|
||||||
version "1.1.12"
|
version "1.1.18-dev-1624965921528"
|
||||||
resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.12.tgz#1a731a3bd0c7266105b75d88dce7c09657432002"
|
resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.18-dev-1624965921528.tgz#d94926109b349b62d5deb6329f0d46f5f5e953eb"
|
||||||
integrity sha512-h/xpN0Mq1DRS7pDzEDjHfkZtpw1iLDKnytwBd12Lzs9RsWpQOJArfqSocAqdDrIO7GbxykhkFDCdl3Yi/q59gw==
|
integrity sha512-P28P5pBV6HBXNiNUQbYgSaOrHcnq7GCNAIFuBmLYqDiZnNL8zfU+IxcWo3+xucXyLfX3flGCirpO4g7h65JGWQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/plugin-transform-classes" "^7.9.5"
|
"@babel/plugin-transform-classes" "^7.9.5"
|
||||||
medusa-core-utils "^1.1.9"
|
medusa-core-utils "1.1.15-dev-1624965921528"
|
||||||
randomatic "^3.1.1"
|
randomatic "^3.1.1"
|
||||||
|
|
||||||
merge-descriptors@1.0.1:
|
merge-descriptors@1.0.1:
|
||||||
|
|||||||
@@ -5,10 +5,11 @@
|
|||||||
export const MedusaErrorTypes = {
|
export const MedusaErrorTypes = {
|
||||||
/** Errors stemming from the database */
|
/** Errors stemming from the database */
|
||||||
DB_ERROR: "database_error",
|
DB_ERROR: "database_error",
|
||||||
|
DUPLICATE_ERROR: "duplicate_error",
|
||||||
INVALID_ARGUMENT: "invalid_argument",
|
INVALID_ARGUMENT: "invalid_argument",
|
||||||
INVALID_DATA: "invalid_data",
|
INVALID_DATA: "invalid_data",
|
||||||
NOT_FOUND: "not_found",
|
NOT_FOUND: "not_found",
|
||||||
NOT_ALLOWED: "not_allowed"
|
NOT_ALLOWED: "not_allowed",
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ export default () => {
|
|||||||
|
|
||||||
let statusCode = 500
|
let statusCode = 500
|
||||||
switch (err.name) {
|
switch (err.name) {
|
||||||
|
case MedusaError.Types.DUPLICATE_ERROR:
|
||||||
|
statusCode = 409
|
||||||
|
break
|
||||||
case MedusaError.Types.NOT_ALLOWED:
|
case MedusaError.Types.NOT_ALLOWED:
|
||||||
case MedusaError.Types.INVALID_DATA:
|
case MedusaError.Types.INVALID_DATA:
|
||||||
statusCode = 400
|
statusCode = 400
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /customers/{id}/addresses
|
* @oas [post] /customers/{id}/addresses
|
||||||
@@ -45,7 +46,8 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
let customer = await customerService.addAddress(id, value.address)
|
let customer = await customerService.addAddress(id, value.address)
|
||||||
customer = await customerService.retrieve(id, {
|
customer = await customerService.retrieve(id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ customer })
|
res.status(200).json({ customer })
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import jwt from "jsonwebtoken"
|
import jwt from "jsonwebtoken"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
import config from "../../../../config"
|
import config from "../../../../config"
|
||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /customers
|
* @oas [post] /customers
|
||||||
@@ -40,6 +41,7 @@ export default async (req, res) => {
|
|||||||
if (error) {
|
if (error) {
|
||||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const customerService = req.scope.resolve("customerService")
|
const customerService = req.scope.resolve("customerService")
|
||||||
let customer = await customerService.create(value)
|
let customer = await customerService.create(value)
|
||||||
@@ -50,7 +52,8 @@ export default async (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
customer = await customerService.retrieve(customer.id, {
|
customer = await customerService.retrieve(customer.id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ customer })
|
res.status(200).json({ customer })
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [delete] /customers/{id}/addresses/{address_id}
|
* @oas [delete] /customers/{id}/addresses/{address_id}
|
||||||
* operationId: DeleteCustomersCustomerAddressesAddress
|
* operationId: DeleteCustomersCustomerAddressesAddress
|
||||||
@@ -25,7 +27,8 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
await customerService.removeAddress(id, address_id)
|
await customerService.removeAddress(id, address_id)
|
||||||
customer = await customerService.retrieve(id, {
|
customer = await customerService.retrieve(id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.json({ customer })
|
res.json({ customer })
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /customers/{id}
|
* @oas [get] /customers/{id}
|
||||||
* operationId: GetCustomersCustomer
|
* operationId: GetCustomersCustomer
|
||||||
@@ -22,7 +24,8 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const customerService = req.scope.resolve("customerService")
|
const customerService = req.scope.resolve("customerService")
|
||||||
const customer = await customerService.retrieve(id, {
|
const customer = await customerService.retrieve(id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
res.json({ customer })
|
res.json({ customer })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -57,3 +57,39 @@ export default (app, container) => {
|
|||||||
|
|
||||||
return app
|
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 { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /customers/{id}/addresses/{address_id}
|
* @oas [post] /customers/{id}/addresses/{address_id}
|
||||||
@@ -50,7 +51,8 @@ export default async (req, res) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
customer = await customerService.retrieve(id, {
|
customer = await customerService.retrieve(id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.json({ customer })
|
res.json({ customer })
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
import { defaultRelations, defaultFields } from "./"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /customers/{id}
|
* @oas [post] /customers/{id}
|
||||||
@@ -24,6 +25,9 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
|||||||
* phone:
|
* phone:
|
||||||
* description: "The Customer's phone number."
|
* description: "The Customer's phone number."
|
||||||
* type: string
|
* type: string
|
||||||
|
* metadata:
|
||||||
|
* description: "Metadata about the customer."
|
||||||
|
* type: object
|
||||||
* tags:
|
* tags:
|
||||||
* - Customer
|
* - Customer
|
||||||
* responses:
|
* responses:
|
||||||
@@ -44,6 +48,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(),
|
||||||
|
metadata: Validator.object().optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(req.body)
|
const { value, error } = schema.validate(req.body)
|
||||||
@@ -56,7 +61,8 @@ export default async (req, res) => {
|
|||||||
let customer = await customerService.update(id, value)
|
let customer = await customerService.update(id, value)
|
||||||
|
|
||||||
customer = await customerService.retrieve(customer.id, {
|
customer = await customerService.retrieve(customer.id, {
|
||||||
relations: ["shipping_addresses"],
|
relations: defaultRelations,
|
||||||
|
select: defaultFields,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.status(200).json({ customer })
|
res.status(200).json({ customer })
|
||||||
|
|||||||
@@ -318,6 +318,13 @@ class CustomerService extends BaseService {
|
|||||||
|
|
||||||
const existing = await this.retrieveByEmail(email).catch(err => undefined)
|
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) {
|
if (existing && password && !existing.has_account) {
|
||||||
const hashedPassword = await this.hashPassword_(password)
|
const hashedPassword = await this.hashPassword_(password)
|
||||||
customer.password_hash = hashedPassword
|
customer.password_hash = hashedPassword
|
||||||
|
|||||||
Reference in new issue
Block a user