feat(customer): admin addresses (#6235)

**What**
- GET /admin/customers/:id/addresses
- POST /admin/customers/:id/addresses
- POST /admin/customers/:id/addresses/:address_id
- DELETE /admin/customers/:id/addresses/:address_id
This commit is contained in:
Sebastian Rindom
2024-01-31 10:55:07 +01:00
committed by GitHub
parent ca0e0631af
commit 36ec3ea3aa
20 changed files with 990 additions and 34 deletions

View File

@@ -0,0 +1,34 @@
import {
ICustomerModuleService,
CreateCustomerAddressDTO,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const createCustomerAddressesStepId = "create-customer-addresses"
export const createCustomerAddressesStep = createStep(
createCustomerAddressesStepId,
async (data: CreateCustomerAddressDTO[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const addresses = await service.addAddresses(data)
return new StepResponse(
addresses,
addresses.map((address) => address.id)
)
},
async (ids, { container }) => {
if (!ids?.length) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.deleteAddress(ids)
}
)

View File

@@ -0,0 +1,32 @@
import { ICustomerModuleService } from "@medusajs/types"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
type DeleteCustomerAddressStepInput = string[]
export const deleteCustomerAddressesStepId = "delete-customer-addresses"
export const deleteCustomerAddressesStep = createStep(
deleteCustomerAddressesStepId,
async (ids: DeleteCustomerAddressStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const existing = await service.listAddresses({
id: ids,
})
await service.deleteAddress(ids)
return new StepResponse(void 0, existing)
},
async (prevAddresses, { container }) => {
if (!prevAddresses?.length) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await service.addAddresses(prevAddresses)
}
)

View File

@@ -1,3 +1,6 @@
export * from "./create-customers"
export * from "./update-customers"
export * from "./delete-customers"
export * from "./create-addresses"
export * from "./update-addresses"
export * from "./delete-addresses"

View File

@@ -0,0 +1,54 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CustomerAddressDTO,
FilterableCustomerAddressProps,
ICustomerModuleService,
} from "@medusajs/types"
import {
getSelectsAndRelationsFromObjectArray,
promiseAll,
} from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
type UpdateCustomerAddresseStepInput = {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
}
export const updateCustomerAddresseStepId = "update-customer-addresses"
export const updateCustomerAddressesStep = createStep(
updateCustomerAddresseStepId,
async (data: UpdateCustomerAddresseStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
data.update,
])
const prevCustomers = await service.listAddresses(data.selector, {
select: selects,
relations,
})
const customerAddresses = await service.updateAddress(
data.selector,
data.update
)
return new StepResponse(customerAddresses, prevCustomers)
},
async (prevCustomerAddresses, { container }) => {
if (!prevCustomerAddresses) {
return
}
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)
await promiseAll(
prevCustomerAddresses.map((c) => service.updateAddress(c.id, { ...c }))
)
}
)

View File

@@ -0,0 +1,13 @@
import { CreateCustomerAddressDTO, CustomerAddressDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createCustomerAddressesStep } from "../steps"
type WorkflowInput = { addresses: CreateCustomerAddressDTO[] }
export const createCustomerAddressesWorkflowId = "create-customer-addresses"
export const createCustomerAddressesWorkflow = createWorkflow(
createCustomerAddressesWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerAddressDTO[]> => {
return createCustomerAddressesStep(input.addresses)
}
)

View File

@@ -0,0 +1,12 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteCustomerAddressesStep } from "../steps"
type WorkflowInput = { ids: string[] }
export const deleteCustomerAddressesWorkflowId = "delete-customer-addresses"
export const deleteCustomerAddressesWorkflow = createWorkflow(
deleteCustomerAddressesWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteCustomerAddressesStep(input.ids)
}
)

View File

@@ -1,3 +1,6 @@
export * from "./create-customers"
export * from "./update-customers"
export * from "./delete-customers"
export * from "./create-addresses"
export * from "./update-addresses"
export * from "./delete-addresses"

View File

@@ -0,0 +1,19 @@
import {
FilterableCustomerAddressProps,
CustomerAddressDTO,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateCustomerAddressesStep } from "../steps"
type WorkflowInput = {
selector: FilterableCustomerAddressProps
update: Partial<CustomerAddressDTO>
}
export const updateCustomerAddressesWorkflowId = "update-customer-addresses"
export const updateCustomerAddressesWorkflow = createWorkflow(
updateCustomerAddressesWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<CustomerAddressDTO[]> => {
return updateCustomerAddressesStep(input)
}
)