feat: Add HTTP endpoints and workflows for price preference management (#7960)

REF CORE-2376

Remaining pieces are adding UI to manage the flag, showing the flag in price editor, plugging it in cart calculations, and https://github.com/medusajs/medusa/pull/7827
This commit is contained in:
Stevche Radevski
2024-07-05 08:47:01 +00:00
committed by GitHub
parent 1162b1625d
commit 3e86cb6ac3
27 changed files with 702 additions and 10 deletions
@@ -18,9 +18,9 @@ type OrderOutput =
metadata: any
}
export const getOrdersListlWorkflowId = "get-orders-list"
export const getOrdersListlWorkflow = createWorkflow(
getOrdersListlWorkflowId,
export const getOrdersListWorkflowId = "get-orders-list"
export const getOrdersListWorkflow = createWorkflow(
getOrdersListWorkflowId,
(
input: WorkflowData<{
fields: string[]
@@ -1 +1,2 @@
export * from "./steps"
export * from "./workflows"
@@ -0,0 +1,34 @@
import { IPricingModuleService } from "@medusajs/types"
import { PricingWorkflow } from "@medusajs/types/dist/workflow"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type StepInput = PricingWorkflow.CreatePricePreferencesWorkflowInput[]
export const createPricePreferencesStepId = "create-price-preferences"
export const createPricePreferencesStep = createStep(
createPricePreferencesStepId,
async (data: StepInput, { container }) => {
const pricingModule = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
const pricePreferences = await pricingModule.createPricePreferences(data)
return new StepResponse(
pricePreferences,
pricePreferences.map((pricePreference) => pricePreference.id)
)
},
async (pricePreferences, { container }) => {
if (!pricePreferences?.length) {
return
}
const pricingModule = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
await pricingModule.deletePricePreferences(pricePreferences)
}
)
@@ -0,0 +1,28 @@
import { IPricingModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const deletePricePreferencesStepId = "delete-price-preferences"
export const deletePricePreferencesStep = createStep(
deletePricePreferencesStepId,
async (ids: string[], { container }) => {
const service = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
await service.softDeletePricePreferences(ids)
return new StepResponse(void 0, ids)
},
async (prevIds, { container }) => {
if (!prevIds?.length) {
return
}
const service = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
await service.restorePricePreferences(prevIds)
}
)
@@ -1,2 +1,5 @@
export * from "./create-price-sets"
export * from "./update-price-sets"
export * from "./create-price-preferences"
export * from "./update-price-preferences"
export * from "./delete-price-preferences"
@@ -0,0 +1,45 @@
import { PricingWorkflow, IPricingModuleService } from "@medusajs/types"
import {
ModuleRegistrationName,
getSelectsAndRelationsFromObjectArray,
} from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type StepInput = PricingWorkflow.UpdatePricePreferencesWorkflowInput
export const updatePricePreferencesStepId = "update-price-preferences"
export const updatePricePreferencesStep = createStep(
updatePricePreferencesStepId,
async (input: StepInput, { container }) => {
const service = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
input.update,
])
const prevData = await service.listPricePreferences(input.selector, {
select: selects,
relations,
})
const updatedPricePreferences = await service.updatePricePreferences(
input.selector,
input.update
)
return new StepResponse(updatedPricePreferences, prevData)
},
async (prevData, { container }) => {
if (!prevData?.length) {
return
}
const service = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)
await service.upsertPricePreferences(prevData)
}
)
@@ -0,0 +1,13 @@
import { PricingWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createPricePreferencesStep } from "../steps"
type WorkflowInputData = PricingWorkflow.CreatePricePreferencesWorkflowInput[]
export const createPricePreferencesWorkflowId = "create-price-preferences"
export const createPricePreferencesWorkflow = createWorkflow(
createPricePreferencesWorkflowId,
(input: WorkflowData<WorkflowInputData>) => {
return createPricePreferencesStep(input)
}
)
@@ -0,0 +1,10 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deletePricePreferencesStep } from "../steps"
export const deletePricePreferencesWorkflowId = "delete-price-preferences"
export const deletePricePreferencesWorkflow = createWorkflow(
deletePricePreferencesWorkflowId,
(input: WorkflowData<string[]>) => {
return deletePricePreferencesStep(input)
}
)
@@ -0,0 +1,3 @@
export * from "./create-price-preferences"
export * from "./update-price-preferences"
export * from "./delete-price-preferences"
@@ -0,0 +1,13 @@
import { PricingWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updatePricePreferencesStep } from "../steps"
type WorkflowInputData = PricingWorkflow.UpdatePricePreferencesWorkflowInput
export const updatePricePreferencesWorkflowId = "update-price-preferences"
export const updatePricePreferencesWorkflow = createWorkflow(
updatePricePreferencesWorkflowId,
(input: WorkflowData<WorkflowInputData>) => {
return updatePricePreferencesStep(input)
}
)
@@ -29,3 +29,13 @@ export interface AdminPrice {
updated_at: string
deleted_at: string | null
}
export interface AdminPricePreference {
id: string
attribute: string | null
value: string | null
is_tax_inclusive: boolean
created_at: string
updated_at: string
deleted_at: null | string
}
@@ -1 +1,4 @@
export * from "./entities"
export * from "./payloads"
export * from "./queries"
export * from "./responses"
@@ -0,0 +1,11 @@
export interface AdminCreatePricePreference {
attribute?: string
value?: string
is_tax_inclusive?: boolean
}
export interface AdminUpdatePricePreference {
attribute?: string | null
value?: string | null
is_tax_inclusive?: boolean
}
@@ -0,0 +1,12 @@
import { BaseFilterable } from "../../../dal"
import { FindParams, SelectParams } from "../../common"
export interface AdminPricePreferenceListParams
extends FindParams,
BaseFilterable<AdminPricePreferenceListParams> {
id?: string | string[]
attribute?: string | string[]
value?: string | string[]
}
export interface AdminPricePreferenceParams extends SelectParams {}
@@ -0,0 +1,14 @@
import { DeleteResponse, PaginatedResponse } from "../../common"
import { AdminPricePreference } from "./entities"
export interface AdminPricePreferenceResponse {
price_preference: AdminPricePreference
}
export interface AdminPricePreferenceListResponse
extends PaginatedResponse<{
price_preferences: AdminPricePreference[]
}> {}
export interface AdminPricePreferenceDeleteResponse
extends DeleteResponse<"price_preference"> {}
@@ -10,3 +10,4 @@ export * as RegionWorkflow from "./region"
export * as ReservationWorkflow from "./reservation"
export * as UserWorkflow from "./user"
export * as OrderWorkflow from "./order"
export * as PricingWorkflow from "./pricing"
@@ -0,0 +1,18 @@
import { FilterablePricePreferenceProps } from "../../pricing"
export interface CreatePricePreferencesWorkflowInput {
attribute?: string
value?: string
is_tax_inclusive?: boolean
}
interface UpdatePricePreferences {
attribute?: string | null
value?: string | null
is_tax_inclusive?: boolean
}
export interface UpdatePricePreferencesWorkflowInput {
selector: FilterablePricePreferenceProps
update: UpdatePricePreferences
}