Add expand to /admin/customers/:id (#1070)

* fix: 1055 add expand to /admin/customers/:id

* fix tests

* fix tests
This commit is contained in:
Oliver Windall Juhl
2022-02-16 23:41:30 +01:00
committed by olivermrbl
parent 1e4cc2fc80
commit 7c31acb064
6 changed files with 189 additions and 14 deletions
@@ -0,0 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/customers GET /admin/customers/:id fetches a customer 1`] = `
Object {
"billing_address_id": "test-address",
"created_at": Any<String>,
"deleted_at": null,
"email": "test1@email.com",
"first_name": null,
"has_account": false,
"id": Any<String>,
"last_name": null,
"metadata": null,
"orders": Array [],
"phone": null,
"shipping_addresses": Array [
Object {
"address_1": null,
"address_2": null,
"city": null,
"company": null,
"country_code": null,
"created_at": Any<String>,
"customer_id": "test-customer-1",
"deleted_at": null,
"first_name": "Lebron",
"id": "test-address",
"last_name": "James",
"metadata": null,
"phone": null,
"postal_code": null,
"province": null,
"updated_at": Any<String>,
},
],
"updated_at": Any<String>,
}
`;
exports[`/admin/customers GET /admin/customers/:id fetches a customer with expand query 1`] = `
Object {
"billing_address": Object {
"address_1": null,
"address_2": null,
"city": null,
"company": null,
"country_code": null,
"created_at": Any<String>,
"customer_id": "test-customer-1",
"deleted_at": null,
"first_name": "Lebron",
"id": "test-address",
"last_name": "James",
"metadata": null,
"phone": null,
"postal_code": null,
"province": null,
"updated_at": Any<String>,
},
"billing_address_id": "test-address",
"created_at": Any<String>,
"deleted_at": null,
"email": "test1@email.com",
"first_name": null,
"has_account": false,
"id": "test-customer-1",
"last_name": null,
"metadata": null,
"phone": null,
"updated_at": Any<String>,
}
`;
@@ -229,4 +229,75 @@ describe("/admin/customers", () => {
)
})
})
describe("GET /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("fetches a customer", async () => {
const api = useApi()
const response = await api
.get("/admin/customers/test-customer-1", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer).toMatchSnapshot({
id: expect.any(String),
shipping_addresses: [
{
id: "test-address",
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
created_at: expect.any(String),
updated_at: expect.any(String),
})
})
it("fetches a customer with expand query", async () => {
const api = useApi()
const response = await api
.get("/admin/customers/test-customer-1?expand=billing_address", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.customer).toMatchSnapshot({
id: "test-customer-1",
billing_address: {
id: "test-address",
created_at: expect.any(String),
updated_at: expect.any(String),
},
created_at: expect.any(String),
updated_at: expect.any(String),
})
})
})
})
@@ -3,11 +3,23 @@ const { Customer, Address } = require("@medusajs/medusa")
module.exports = async (connection, data = {}) => {
const manager = connection.manager
await manager.insert(Customer, {
const testAddr = await manager.create(Address, {
id: "test-address",
first_name: "Lebron",
last_name: "James",
})
await manager.save(testAddr)
const customer = await manager.create(Customer, {
id: "test-customer-1",
email: "test1@email.com",
})
customer.billing_address = testAddr
customer.shipping_addresses = [testAddr]
await manager.save(customer)
await manager.insert(Customer, {
id: "test-customer-2",
email: "test2@email.com",
@@ -23,11 +35,4 @@ module.exports = async (connection, data = {}) => {
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",
})
}
@@ -1,4 +1,7 @@
import { defaultAdminCustomersRelations } from "."
import CustomerService from "../../../../services/customer"
import { FindParams } from "../../../../types/common"
import { validator } from "../../../../utils/validator"
/**
* @oas [get] /customers/{id}
@@ -22,10 +25,23 @@ import CustomerService from "../../../../services/customer"
*/
export default async (req, res) => {
const { id } = req.params
const validated = await validator(FindParams, req.query)
const customerService: CustomerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
relations: ["orders", "shipping_addresses"],
})
let expandFields: string[] = []
if (validated.expand) {
expandFields = validated.expand.split(",")
}
const findConfig = {
relations: expandFields.length
? expandFields
: defaultAdminCustomersRelations,
}
const customer = await customerService.retrieve(id, findConfig)
res.json({ customer })
}
@@ -26,6 +26,8 @@ export type AdminCustomersListRes = PaginatedResponse & {
customers: Customer[]
}
export const defaultAdminCustomersRelations = ["orders", "shipping_addresses"]
export * from "./create-customer"
export * from "./get-customer"
export * from "./list-customers"
+12 -3
View File
@@ -1,8 +1,7 @@
import { Transform } from "class-transformer"
import { transformDate } from "../utils/validators/date-transform"
import { Type } from "class-transformer"
import { Transform, Type } from "class-transformer"
import { IsDate, IsNumber, IsOptional, IsString } from "class-validator"
import "reflect-metadata"
import { transformDate } from "../utils/validators/date-transform"
export type PartialPick<T, K extends keyof T> = {
[P in K]?: T[P]
@@ -169,3 +168,13 @@ export class AddressCreatePayload {
@IsString()
postal_code: string
}
export class FindParams {
@IsString()
@IsOptional()
expand?: string
@IsString()
@IsOptional()
fields?: string
}