feat(customer): add softdelete/restore (#6203)

This commit is contained in:
Sebastian Rindom authored and GitHub committed 2024-01-25 12:38:02 +00:00
1 parent e84847be36
commit e9435a8680
8 files changed
+519 -21

No files matched your search

+2 -1
View File
@@ -1,10 +1,11 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { Customer } from "@models"
import { Customer, CustomerGroup } from "@models"
export const LinkableKeys = {
customer_id: Customer.name,
customer_group_id: CustomerGroup.name,
}
const entityLinkableKeysMap: MapToConfig = {}
@@ -1,6 +1,7 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
Cascade,
BeforeCreate,
ManyToOne,
Entity,
@@ -32,6 +33,7 @@ export default class CustomerGroupCustomer {
fieldName: "customer_id",
index: "IDX_customer_group_customer_customer_id",
nullable: true,
cascade: [Cascade.REMOVE],
})
customer: Customer | null
@@ -40,6 +42,7 @@ export default class CustomerGroupCustomer {
fieldName: "customer_group_id",
index: "IDX_customer_group_customer_group_id",
nullable: true,
cascade: [Cascade.REMOVE],
})
customer_group: CustomerGroup | null
@@ -1,5 +1,5 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
@@ -9,13 +9,15 @@ import {
Property,
ManyToMany,
Collection,
Filter,
} from "@mikro-orm/core"
import Customer from "./customer"
import CustomerGroupCustomer from "./customer-group-customer"
type OptionalGroupProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
type OptionalGroupProps = DAL.SoftDeletableEntityDateColumns // TODO: To be revisited when more clear
@Entity({ tableName: "customer_group" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class CustomerGroup {
[OptionalProps]: OptionalGroupProps
@@ -52,6 +54,9 @@ export default class CustomerGroup {
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cusgroup")
+4 -2
View File
@@ -1,10 +1,11 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
Index,
ManyToMany,
ManyToOne,
@@ -23,9 +24,10 @@ type OptionalCustomerProps =
| "addresses"
| "default_shipping_address"
| "default_billing_address"
| DAL.EntityDateColumns
| DAL.SoftDeletableEntityDateColumns
@Entity({ tableName: "customer" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Customer {
[OptionalProps]?: OptionalCustomerProps
@@ -6,16 +6,23 @@ import {
InternalModuleDeclaration,
ModuleJoinerConfig,
CustomerTypes,
SoftDeleteReturn,
RestoreReturn,
} from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
mapObjectTo,
isString,
isObject,
} from "@medusajs/utils"
import { joinerConfig } from "../joiner-config"
import {
entityNameToLinkableKeysMap,
LinkableKeys,
joinerConfig,
} from "../joiner-config"
import * as services from "../services"
type InjectedDependencies = {
@@ -264,6 +271,23 @@ export default class CustomerModuleService implements ICustomerModuleService {
return Array.isArray(dataOrArrayOfData) ? serialized : serialized[0]
}
@InjectManager("baseRepository_")
async retrieveCustomerGroup(
groupId: string,
config: FindConfig<CustomerTypes.CustomerGroupDTO> = {},
@MedusaContext() sharedContext: Context = {}
) {
const group = await this.customerGroupService_.retrieve(
groupId,
config,
sharedContext
)
return await this.baseRepository_.serialize<CustomerTypes.CustomerGroupDTO>(
group,
{ populate: true }
)
}
async updateCustomerGroup(
groupId: string,
data: Partial<CustomerTypes.CreateCustomerGroupDTO>,
@@ -331,6 +355,39 @@ export default class CustomerModuleService implements ICustomerModuleService {
>(groups, { populate: true })
}
deleteCustomerGroup(groupId: string, sharedContext?: Context): Promise<void>
deleteCustomerGroup(
groupIds: string[],
sharedContext?: Context
): Promise<void>
deleteCustomerGroup(
selector: CustomerTypes.FilterableCustomerGroupProps,
sharedContext?: Context
): Promise<void>
@InjectTransactionManager("baseRepository_")
async deleteCustomerGroup(
groupIdOrSelector:
| string
| string[]
| CustomerTypes.FilterableCustomerGroupProps,
@MedusaContext() sharedContext: Context = {}
) {
let toDelete = Array.isArray(groupIdOrSelector)
? groupIdOrSelector
: [groupIdOrSelector as string]
if (isObject(groupIdOrSelector)) {
const ids = await this.customerGroupService_.list(
groupIdOrSelector,
{ select: ["id"] },
sharedContext
)
toDelete = ids.map(({ id }) => id)
}
return await this.customerGroupService_.delete(toDelete, sharedContext)
}
async addCustomerToGroup(
groupCustomerPair: CustomerTypes.GroupCustomerPair,
sharedContext?: Context
@@ -382,6 +439,25 @@ export default class CustomerModuleService implements ICustomerModuleService {
)
}
@InjectManager("baseRepository_")
async listCustomerGroupRelations(
filters?: CustomerTypes.FilterableCustomerGroupCustomerProps,
config?: FindConfig<CustomerTypes.CustomerGroupCustomerDTO>,
@MedusaContext() sharedContext: Context = {}
) {
const groupCustomers = await this.customerGroupCustomerService_.list(
filters,
config,
sharedContext
)
return await this.baseRepository_.serialize<
CustomerTypes.CustomerGroupCustomerDTO[]
>(groupCustomers, {
populate: true,
})
}
@InjectManager("baseRepository_")
async listCustomerGroups(
filters: CustomerTypes.FilterableCustomerGroupProps = {},
@@ -423,4 +499,90 @@ export default class CustomerModuleService implements ICustomerModuleService {
count,
]
}
@InjectTransactionManager("baseRepository_")
async softDeleteCustomerGroup<
TReturnableLinkableKeys extends string = string
>(
groupIds: string[],
config: SoftDeleteReturn<TReturnableLinkableKeys> = {},
@MedusaContext() sharedContext: Context = {}
) {
const [_, cascadedEntitiesMap] =
await this.customerGroupService_.softDelete(groupIds, sharedContext)
return config.returnLinkableKeys
? mapObjectTo<Record<TReturnableLinkableKeys, string[]>>(
cascadedEntitiesMap,
entityNameToLinkableKeysMap,
{
pick: config.returnLinkableKeys,
}
)
: void 0
}
@InjectTransactionManager("baseRepository_")
async restoreCustomerGroup<TReturnableLinkableKeys extends string = string>(
groupIds: string[],
config: RestoreReturn<TReturnableLinkableKeys> = {},
@MedusaContext() sharedContext: Context = {}
) {
const [_, cascadedEntitiesMap] = await this.customerGroupService_.restore(
groupIds,
sharedContext
)
return config.returnLinkableKeys
? mapObjectTo<Record<TReturnableLinkableKeys, string[]>>(
cascadedEntitiesMap,
entityNameToLinkableKeysMap,
{
pick: config.returnLinkableKeys,
}
)
: void 0
}
@InjectTransactionManager("baseRepository_")
async softDelete<TReturnableLinkableKeys extends string = string>(
customerIds: string[],
config: SoftDeleteReturn<TReturnableLinkableKeys> = {},
@MedusaContext() sharedContext: Context = {}
) {
const [_, cascadedEntitiesMap] = await this.customerService_.softDelete(
customerIds,
sharedContext
)
return config.returnLinkableKeys
? mapObjectTo<Record<TReturnableLinkableKeys, string[]>>(
cascadedEntitiesMap,
entityNameToLinkableKeysMap,
{
pick: config.returnLinkableKeys,
}
)
: void 0
}
@InjectTransactionManager("baseRepository_")
async restore<TReturnableLinkableKeys extends string = string>(
customerIds: string[],
config: RestoreReturn<TReturnableLinkableKeys> = {},
@MedusaContext() sharedContext: Context = {}
) {
const [_, cascadedEntitiesMap] = await this.customerService_.restore(
customerIds,
sharedContext
)
return config.returnLinkableKeys
? mapObjectTo<Record<TReturnableLinkableKeys, string[]>>(
cascadedEntitiesMap,
entityNameToLinkableKeysMap,
{
pick: config.returnLinkableKeys,
}
)
: void 0
}
}