feat: customer group update (#1098)

This commit is contained in:
Frane Polić
2022-02-24 18:03:20 +01:00
committed by olivermrbl
parent f983cfada6
commit d80eaa172d
8 changed files with 226 additions and 15 deletions

View File

@@ -20,8 +20,8 @@ import { validator } from "../../../../utils/validator"
* application/json:
* schema:
* properties:
* customerGroup:
* $ref: "#/components/schemas/customergroup"
* customer_group:
* $ref: "#/components/schemas/customer_group"
*/
export default async (req, res) => {
@@ -32,7 +32,7 @@ export default async (req, res) => {
)
const customerGroup = await customerGroupService.create(validated)
res.status(200).json({ customerGroup })
res.status(200).json({ customer_group: customerGroup })
}
export class AdminPostCustomerGroupsReq {

View File

@@ -20,13 +20,16 @@ import { defaultAdminCustomerGroupsRelations } from "."
* application/json:
* schema:
* properties:
* customerGroup:
* $ref: "#/components/schemas/customer-group"
* customer_group:
* $ref: "#/components/schemas/customer_group"
*/
export default async (req, res) => {
const { id } = req.params
const validated = await validator(AdminGetCustomerGroupsGroupParams, req.query)
const validated = await validator(
AdminGetCustomerGroupsGroupParams,
req.query
)
const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService"
@@ -45,7 +48,7 @@ export default async (req, res) => {
const customerGroup = await customerGroupService.retrieve(id, findConfig)
res.json({ customerGroup })
res.json({ customer_group: customerGroup })
}
export class AdminGetCustomerGroupsGroupParams extends FindParams {}

View File

@@ -14,6 +14,11 @@ export default (app) => {
"/:id",
middlewares.wrap(require("./delete-customer-group").default)
)
route.post(
"/:id",
middlewares.wrap(require("./update-customer-group").default)
)
return app
}

View File

@@ -0,0 +1,70 @@
import { IsObject, IsOptional, IsString } from "class-validator"
import { defaultAdminCustomerGroupsRelations } from "."
import { CustomerGroupService } from "../../../../services"
import { FindParams } from "../../../../types/common"
import { validator } from "../../../../utils/validator"
/**
* @oas [post] /customer-groups/{id}
* operationId: "PostCustomerGroupsGroup"
* summary: "Update a CustomerGroup"
* description: "Update a CustomerGroup."
* x-authenticated: true
* parameters:
* - (path) id=* {string} The id of the customer group.
* - (body) name=* {string} Name of the customer group
* - (body) metadata {object} Metadata for the customer.
* tags:
* - CustomerGroup
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* properties:
* customer_group:
* $ref: "#/components/schemas/customer_group"
*/
export default async (req, res) => {
const { id } = req.params
const validatedBody = await validator(
AdminPostCustomerGroupsGroupReq,
req.body
)
const validatedQuery = await validator(FindParams, req.query)
const customerGroupService: CustomerGroupService = req.scope.resolve(
"customerGroupService"
)
await customerGroupService.update(id, validatedBody)
let expandFields: string[] = []
if (validatedQuery.expand) {
expandFields = validatedQuery.expand.split(",")
}
const findConfig = {
relations: expandFields.length
? expandFields
: defaultAdminCustomerGroupsRelations,
}
const customerGroup = await customerGroupService.retrieve(id, findConfig)
res.json({ customer_group: customerGroup })
}
export class AdminPostCustomerGroupsGroupReq {
@IsString()
@IsOptional()
name?: string
@IsObject()
@IsOptional()
metadata?: object
}

View File

@@ -4,7 +4,10 @@ import { DeepPartial, EntityManager } from "typeorm"
import { CustomerGroup } from ".."
import { CustomerGroupRepository } from "../repositories/customer-group"
import { FindConfig } from "../types/common"
import { FilterableCustomerGroupProps } from "../types/customer-groups"
import {
CustomerGroupUpdate,
FilterableCustomerGroupProps,
} from "../types/customer-groups"
type CustomerGroupConstructorProps = {
manager: EntityManager
@@ -42,14 +45,14 @@ class CustomerGroupService extends BaseService {
}
async retrieve(id: string, config = {}): Promise<CustomerGroup> {
const customerRepo = this.manager_.getCustomRepository(
const cgRepo = this.manager_.getCustomRepository(
this.customerGroupRepository_
)
const validatedId = this.validateId_(id)
const query = this.buildQuery_({ id: validatedId }, config)
const customerGroup = await customerRepo.findOne(query)
const customerGroup = await cgRepo.findOne(query)
if (!customerGroup) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
@@ -86,6 +89,38 @@ class CustomerGroupService extends BaseService {
})
}
/**
* Update a customer group.
*
* @param {string} customerGroupId - id of the customer group
* @param {CustomerGroupUpdate} update - customer group partial data
*/
async update(
customerGroupId: string,
update: CustomerGroupUpdate
): Promise<CustomerGroup[]> {
return this.atomicPhase_(async (manager) => {
const { metadata, ...properties } = update
const cgRepo: CustomerGroupRepository = manager.getCustomRepository(
this.customerGroupRepository_
)
const customerGroup = await this.retrieve(customerGroupId)
for (const key in properties) {
if (typeof properties[key] !== "undefined") {
customerGroup[key] = properties[key]
}
}
if (typeof metadata !== "undefined") {
customerGroup.metadata = this.setMetadata_(customerGroup, metadata)
}
return await cgRepo.save(customerGroup)
})
}
/**
* Remove customer group
*

View File

@@ -8,3 +8,8 @@ export class FilterableCustomerGroupProps {
@IsType([String, [String], StringComparisonOperator])
id?: string | string[] | StringComparisonOperator
}
export class CustomerGroupUpdate {
name?: string
metadata?: object
}