feat(core-flows,payment,medusa,types): Refund reasons management API (#8436)

* feat(core-flows,payment,medusa,types): add ability to set and manage refund reasons

* fix(payment): validate total amount when refunding payment (#8437)

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>

* feature: introduce additional_data to the product endpoints (#8405)

* chore(docs): Generated References (#8440)

Generated the following references:
- `product`

* chore: align payment database schema

* Update packages/core/core-flows/src/payment-collection/steps/create-refund-reasons.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* chore: address review

---------

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Riqwan Thamir
2024-08-06 11:47:42 +02:00
committed by GitHub
parent 8fb079786d
commit 0ff5b975e7
36 changed files with 2412 additions and 9 deletions

View File

@@ -0,0 +1,31 @@
import { CreateRefundReasonDTO, IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const createRefundReasonStepId = "create-refund-reason"
export const createRefundReasonStep = createStep(
createRefundReasonStepId,
async (data: CreateRefundReasonDTO[], { container }) => {
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
const refundReasons = await service.createRefundReasons(data)
return new StepResponse(
refundReasons,
refundReasons.map((rr) => rr.id)
)
},
async (ids, { container }) => {
if (!ids?.length) {
return
}
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
await service.deleteRefundReasons(ids)
}
)

View File

@@ -0,0 +1,28 @@
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const deleteRefundReasonsStepId = "delete-refund-reasons"
export const deleteRefundReasonsStep = createStep(
deleteRefundReasonsStepId,
async (ids: string[], { container }) => {
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
await service.softDeleteRefundReasons(ids)
return new StepResponse(void 0, ids)
},
async (prevCustomerIds, { container }) => {
if (!prevCustomerIds?.length) {
return
}
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
await service.restoreRefundReasons(prevCustomerIds)
}
)

View File

@@ -1,4 +1,7 @@
export * from "./create-payment-session"
export * from "./create-refund-reasons"
export * from "./delete-payment-sessions"
export * from "./delete-refund-reasons"
export * from "./update-payment-collection"
export * from "./update-refund-reasons"
export * from "./validate-deleted-payment-sessions"

View File

@@ -0,0 +1,43 @@
import { IPaymentModuleService, UpdateRefundReasonDTO } from "@medusajs/types"
import {
ModuleRegistrationName,
getSelectsAndRelationsFromObjectArray,
promiseAll,
} from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const updateRefundReasonStepId = "update-refund-reasons"
export const updateRefundReasonsStep = createStep(
updateRefundReasonStepId,
async (data: UpdateRefundReasonDTO[], { container }) => {
const ids = data.map((d) => d.id)
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data)
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
const prevRefundReasons = await service.listRefundReasons(
{ id: ids },
{ select: selects, relations }
)
const reasons = await service.updateRefundReasons(data)
return new StepResponse(reasons, prevRefundReasons)
},
async (previousData, { container }) => {
if (!previousData) {
return
}
const service = container.resolve<IPaymentModuleService>(
ModuleRegistrationName.PAYMENT
)
await promiseAll(
previousData.map((refundReason) =>
service.updateRefundReasons(refundReason)
)
)
}
)

View File

@@ -0,0 +1,17 @@
import { CreateRefundReasonDTO, RefundReasonDTO } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { createRefundReasonStep } from "../steps/create-refund-reasons"
export const createRefundReasonsWorkflowId = "create-refund-reasons-workflow"
export const createRefundReasonsWorkflow = createWorkflow(
createRefundReasonsWorkflowId,
(
input: WorkflowData<{ data: CreateRefundReasonDTO[] }>
): WorkflowResponse<RefundReasonDTO[]> => {
return new WorkflowResponse(createRefundReasonStep(input.data))
}
)

View File

@@ -0,0 +1,14 @@
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { deleteRefundReasonsStep } from "../steps"
export const deleteRefundReasonsWorkflowId = "delete-refund-reasons-workflow"
export const deleteRefundReasonsWorkflow = createWorkflow(
deleteRefundReasonsWorkflowId,
(input: WorkflowData<{ ids: string[] }>): WorkflowResponse<void> => {
return new WorkflowResponse(deleteRefundReasonsStep(input.ids))
}
)

View File

@@ -1 +1,3 @@
export * from "./create-payment-session"
export * from "./create-refund-reasons"
export * from "./update-refund-reasons"

View File

@@ -0,0 +1,17 @@
import { RefundReasonDTO, UpdateRefundReasonDTO } from "@medusajs/types"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { updateRefundReasonsStep } from "../steps"
export const updateRefundReasonsWorkflowId = "update-refund-reasons"
export const updateRefundReasonsWorkflow = createWorkflow(
updateRefundReasonsWorkflowId,
(
input: WorkflowData<UpdateRefundReasonDTO[]>
): WorkflowResponse<RefundReasonDTO[]> => {
return new WorkflowResponse(updateRefundReasonsStep(input))
}
)