feat: update customer groups (#1075)

* update customer service prep

* expand on the update customer groups test case

* add the test case for customer group update

* docs for `groups` prop on update customer endpoint

* refactor, update comments

* expend on integration tests, add customer mock with a group

* refactor to use `customerGroupService.list` method

* update units

* remove `updateCustomerGroups`

* fix rebase conflict

* fix customer seed data, add JoinTable on groups

* group retrieval using the expand param

* fix: use `buildQuery_`

* fix: validation for `groups`, enable `expand`param on update customer endpoint

* fix: remove fileds form the `FilterableCustomerGroupProps`

* fix: spearate body/query validation

Co-authored-by: fPolic <frane@medusajs.com>
This commit is contained in:
Frane Polić
2022-02-21 16:24:38 +01:00
committed by GitHub
co-authored by fPolic
parent c2241d1101
commit 75fb2ce9c3
9 changed files with 286 additions and 19 deletions
@@ -1,7 +1,17 @@
import { IsEmail, IsObject, IsOptional, IsString } from "class-validator"
import {
IsArray,
IsEmail,
IsObject,
IsOptional,
IsString,
ValidateNested,
} from "class-validator"
import { MedusaError } from "medusa-core-utils"
import CustomerService from "../../../../services/customer"
import { validator } from "../../../../utils/validator"
import { defaultAdminCustomersRelations } from "."
import { Type } from "class-transformer"
import { FindParams } from "../../../../types/common"
/**
* @oas [post] /customers/{id}
@@ -31,6 +41,16 @@ import { validator } from "../../../../utils/validator"
* password:
* type: string
* description: The Customer's password.
* groups:
* type: array
* description: A list of customer groups to which the customer belongs.
* items:
* required:
* - id
* properties:
* id:
* description: The id of a customer group
* type: string
* metadata:
* type: object
* description: Metadata for the customer.
@@ -49,27 +69,43 @@ import { validator } from "../../../../utils/validator"
export default async (req, res) => {
const { id } = req.params
const validated = await validator(AdminPostCustomersCustomerReq, req.body)
const validatedBody = await validator(AdminPostCustomersCustomerReq, req.body)
const validatedQuery = await validator(FindParams, req.query)
const customerService: CustomerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id)
if (validated.email && customer.has_account) {
if (validatedBody.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, validated)
await customerService.update(id, validatedBody)
let expandFields: string[] = []
if (validatedQuery.expand) {
expandFields = validatedQuery.expand.split(",")
}
const findConfig = {
relations: expandFields.length
? expandFields
: defaultAdminCustomersRelations,
}
customer = await customerService.retrieve(id, findConfig)
customer = await customerService.retrieve(id, {
relations: ["orders"],
})
res.status(200).json({ customer })
}
class Group {
@IsString()
id: string
}
export class AdminPostCustomersCustomerReq {
@IsEmail()
@IsOptional()
@@ -94,4 +130,10 @@ export class AdminPostCustomersCustomerReq {
@IsObject()
@IsOptional()
metadata?: object
@IsArray()
@IsOptional()
@Type(() => Group)
@ValidateNested({ each: true })
groups?: Group[]
}