feat(customer): Introduce customer group (#6137)

**What**
Methods for:
- Bulk create customers
- Bulk and single create of customer groups
- Assigning customer <> group relationships
- listing of customers and customer groups

++ Uses new repository loading mechanism
This commit is contained in:
Sebastian Rindom
2024-01-22 11:24:22 +00:00
committed by GitHub
parent fd78f5e242
commit a52586880c
18 changed files with 632 additions and 119 deletions
@@ -0,0 +1,25 @@
import { DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { CustomerGroupCustomer } from "@models"
type CreateCustomerGroupCustomerDTO = {
customer_id: string
customer_group_id: string
created_by?: string
}
type InjectedDependencies = {
customerGroupRepository: DAL.RepositoryService
}
export default class CustomerGroupCustomerService<
TEntity extends CustomerGroupCustomer = CustomerGroupCustomer
> extends ModulesSdkUtils.abstractServiceFactory<
InjectedDependencies,
{ create: CreateCustomerGroupCustomerDTO }
>(CustomerGroupCustomer)<TEntity> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
}
}
@@ -1,7 +1,7 @@
import { DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { CustomerGroup } from "@models"
import { CreateCustomerGroupDTO, UpdateCustomerGroupDTO } from "@types"
import { CreateCustomerGroupDTO, UpdateCustomerGroupDTO } from "@medusajs/types"
type InjectedDependencies = {
customerGroupRepository: DAL.RepositoryService
@@ -8,7 +8,11 @@ import {
CustomerTypes,
} from "@medusajs/types"
import { InjectManager, MedusaContext } from "@medusajs/utils"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
} from "@medusajs/utils"
import { joinerConfig } from "../joiner-config"
import * as services from "../services"
@@ -17,6 +21,7 @@ type InjectedDependencies = {
customerService: services.CustomerService
addressService: services.AddressService
customerGroupService: services.CustomerGroupService
customerGroupCustomerService: services.CustomerGroupCustomerService
}
export default class CustomerModuleService implements ICustomerModuleService {
@@ -24,6 +29,7 @@ export default class CustomerModuleService implements ICustomerModuleService {
protected customerService_: services.CustomerService
protected addressService_: services.AddressService
protected customerGroupService_: services.CustomerGroupService
protected customerGroupCustomerService_: services.CustomerGroupCustomerService
constructor(
{
@@ -31,6 +37,7 @@ export default class CustomerModuleService implements ICustomerModuleService {
customerService,
addressService,
customerGroupService,
customerGroupCustomerService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
@@ -38,6 +45,7 @@ export default class CustomerModuleService implements ICustomerModuleService {
this.customerService_ = customerService
this.addressService_ = addressService
this.customerGroupService_ = customerGroupService
this.customerGroupCustomerService_ = customerGroupCustomerService
}
__joinerConfig(): ModuleJoinerConfig {
@@ -64,18 +72,189 @@ export default class CustomerModuleService implements ICustomerModuleService {
)
}
@InjectManager("baseRepository_")
async create(
data: CustomerTypes.CreateCustomerDTO,
sharedContext?: Context
): Promise<CustomerTypes.CustomerDTO>
async create(
data: CustomerTypes.CreateCustomerDTO[],
sharedContext?: Context
): Promise<CustomerTypes.CustomerDTO[]>
@InjectTransactionManager("baseRepository_")
async create(
dataOrArray:
| CustomerTypes.CreateCustomerDTO
| CustomerTypes.CreateCustomerDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CustomerTypes.CustomerDTO> {
const [customer] = await this.customerService_.create([data], sharedContext)
) {
const data = Array.isArray(dataOrArray) ? dataOrArray : [dataOrArray]
const customer = await this.customerService_.create(data, sharedContext)
if (Array.isArray(dataOrArray)) {
return await this.baseRepository_.serialize<CustomerTypes.CustomerDTO[]>(
customer,
{
populate: true,
}
)
}
return await this.baseRepository_.serialize<CustomerTypes.CustomerDTO>(
customer,
customer[0],
{
populate: true,
}
)
}
@InjectManager("baseRepository_")
async list(
filters: CustomerTypes.FilterableCustomerProps = {},
config: FindConfig<CustomerTypes.CustomerDTO> = {},
@MedusaContext() sharedContext: Context = {}
) {
const customers = await this.customerService_.list(
filters,
config,
sharedContext
)
return await this.baseRepository_.serialize<CustomerTypes.CustomerDTO[]>(
customers,
{
populate: true,
}
)
}
@InjectManager("baseRepository_")
async listAndCount(
filters: CustomerTypes.FilterableCustomerProps = {},
config: FindConfig<CustomerTypes.CustomerDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[CustomerTypes.CustomerDTO[], number]> {
const [customers, count] = await this.customerService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<CustomerTypes.CustomerDTO[]>(
customers,
{
populate: true,
}
),
count,
]
}
async createCustomerGroup(
dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO,
sharedContext?: Context
): Promise<CustomerTypes.CustomerGroupDTO>
async createCustomerGroup(
dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO[],
sharedContext?: Context
): Promise<CustomerTypes.CustomerGroupDTO[]>
@InjectTransactionManager("baseRepository_")
async createCustomerGroup(
dataOrArrayOfData:
| CustomerTypes.CreateCustomerGroupDTO
| CustomerTypes.CreateCustomerGroupDTO[],
@MedusaContext() sharedContext: Context = {}
) {
const data = Array.isArray(dataOrArrayOfData)
? dataOrArrayOfData
: [dataOrArrayOfData]
const groups = await this.customerGroupService_.create(data, sharedContext)
if (Array.isArray(dataOrArrayOfData)) {
return await this.baseRepository_.serialize<
CustomerTypes.CustomerGroupDTO[]
>(groups, {
populate: true,
})
}
return await this.baseRepository_.serialize<CustomerTypes.CustomerGroupDTO>(
groups[0],
{ populate: true }
)
}
async addCustomerToGroup(
groupCustomerPair: CustomerTypes.GroupCustomerPair,
sharedContext?: Context
): Promise<{ id: string }>
async addCustomerToGroup(
groupCustomerPairs: CustomerTypes.GroupCustomerPair[],
sharedContext?: Context
): Promise<{ id: string }[]>
@InjectTransactionManager("baseRepository_")
async addCustomerToGroup(
data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
@MedusaContext() sharedContext: Context = {}
): Promise<{ id: string } | { id: string }[]> {
const groupCustomers = await this.customerGroupCustomerService_.create(
Array.isArray(data) ? data : [data],
sharedContext
)
if (Array.isArray(data)) {
return groupCustomers.map((gc) => ({ id: gc.id }))
}
return { id: groupCustomers[0].id }
}
@InjectManager("baseRepository_")
async listCustomerGroups(
filters: CustomerTypes.FilterableCustomerGroupProps = {},
config: FindConfig<CustomerTypes.CustomerGroupDTO> = {},
@MedusaContext() sharedContext: Context = {}
) {
const groups = await this.customerGroupService_.list(
filters,
config,
sharedContext
)
return await this.baseRepository_.serialize<
CustomerTypes.CustomerGroupDTO[]
>(groups, {
populate: true,
})
}
@InjectManager("baseRepository_")
async listAndCountCustomerGroups(
filters: CustomerTypes.FilterableCustomerGroupProps = {},
config: FindConfig<CustomerTypes.CustomerGroupDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[CustomerTypes.CustomerGroupDTO[], number]> {
const [groups, count] = await this.customerGroupService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<CustomerTypes.CustomerGroupDTO[]>(
groups,
{
populate: true,
}
),
count,
]
}
}
+1
View File
@@ -2,3 +2,4 @@ export { default as AddressService } from "./address"
export { default as CustomerGroupService } from "./customer-group"
export { default as CustomerService } from "./customer"
export { default as CustomerModuleService } from "./customer-module"
export { default as CustomerGroupCustomerService } from "./customer-group-customer"