feat(core-flows,medusa,pricing,types,utils): added price list workflows + endpoints (#6648)
Apologies for the giant PR in advance, but most of these are removing of files and migrations from old workflows and routes to new. What: - Adds CRUD endpoints for price lists - Migrate workflows from old sdk to new - Added missing updatePriceListPrices method to pricing module
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
IsDateString,
|
||||
@@ -9,10 +9,9 @@ import {
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
|
||||
import { CampaignBudgetType } from "@medusajs/utils"
|
||||
import { transformOptionalDate } from "../../../utils/validators/date-transform"
|
||||
|
||||
export class AdminGetCampaignsCampaignParams extends FindParams {}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import {
|
||||
removePriceListPricesWorkflow,
|
||||
upsertPriceListPricesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import { listPriceLists } from "../../queries"
|
||||
import {
|
||||
adminPriceListRemoteQueryFields,
|
||||
defaultAdminPriceListFields,
|
||||
} from "../../query-config"
|
||||
import {
|
||||
AdminDeletePriceListsPriceListPricesReq,
|
||||
AdminPostPriceListsPriceListPricesReq,
|
||||
} from "../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPriceListsPriceListPricesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { prices } = req.validatedBody
|
||||
const id = req.params.id
|
||||
const workflow = upsertPriceListPricesWorkflow(req.scope)
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
price_lists_data: [{ id, prices }],
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const [[priceList]] = await listPriceLists({
|
||||
container: req.scope,
|
||||
remoteQueryFields: adminPriceListRemoteQueryFields,
|
||||
apiFields: defaultAdminPriceListFields,
|
||||
variables: { filters: { id }, skip: 0, take: 1 },
|
||||
})
|
||||
|
||||
res.status(200).json({ price_list: priceList })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest<AdminDeletePriceListsPriceListPricesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { ids } = req.validatedBody
|
||||
const workflow = removePriceListPricesWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { ids },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids,
|
||||
object: "price_list_prices",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -1,10 +1,18 @@
|
||||
import {
|
||||
deletePriceListsWorkflow,
|
||||
updatePriceListsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { listPriceLists } from "../queries"
|
||||
import { adminPriceListRemoteQueryFields } from "../query-config"
|
||||
import {
|
||||
adminPriceListRemoteQueryFields,
|
||||
defaultAdminPriceListFields,
|
||||
} from "../query-config"
|
||||
import { AdminPostPriceListsPriceListReq } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
@@ -31,3 +39,52 @@ export const GET = async (
|
||||
|
||||
res.status(200).json({ price_list: priceList })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPriceListsPriceListReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = updatePriceListsWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { price_lists_data: [{ id, ...req.validatedBody }] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const [[priceList]] = await listPriceLists({
|
||||
container: req.scope,
|
||||
remoteQueryFields: adminPriceListRemoteQueryFields,
|
||||
apiFields: defaultAdminPriceListFields,
|
||||
variables: { filters: { id }, skip: 0, take: 1 },
|
||||
})
|
||||
|
||||
res.status(200).json({ price_list: priceList })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = deletePriceListsWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "price_list",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminDeletePriceListsPriceListPricesReq,
|
||||
AdminGetPriceListsParams,
|
||||
AdminGetPriceListsPriceListParams,
|
||||
AdminPostPriceListsPriceListPricesReq,
|
||||
AdminPostPriceListsPriceListReq,
|
||||
AdminPostPriceListsReq,
|
||||
} from "./validators"
|
||||
|
||||
export const adminPriceListsRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -33,4 +37,24 @@ export const adminPriceListsRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/price-lists",
|
||||
middlewares: [transformBody(AdminPostPriceListsReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/price-lists/:id",
|
||||
middlewares: [transformBody(AdminPostPriceListsPriceListReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/price-lists/:id/prices",
|
||||
middlewares: [transformBody(AdminPostPriceListsPriceListPricesReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/price-lists/:id/prices",
|
||||
middlewares: [transformBody(AdminDeletePriceListsPriceListPricesReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import {
|
||||
MedusaContainer,
|
||||
PriceListRuleDTO,
|
||||
PriceSetMoneyAmountDTO,
|
||||
ProductVariantDTO,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
buildPriceListRules,
|
||||
buildPriceSetPricesForCore,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
@@ -37,7 +34,7 @@ export async function listPriceLists({
|
||||
|
||||
for (const priceList of priceLists) {
|
||||
priceList.rules = buildPriceListRules(priceList.price_list_rules || [])
|
||||
priceList.prices = buildPriceSetPrices(
|
||||
priceList.prices = buildPriceSetPricesForCore(
|
||||
priceList.price_set_money_amounts || []
|
||||
)
|
||||
}
|
||||
@@ -48,43 +45,3 @@ export async function listPriceLists({
|
||||
|
||||
return [sanitizedPriceLists, metadata.count]
|
||||
}
|
||||
|
||||
function buildPriceListRules(
|
||||
priceListRules: PriceListRuleDTO[]
|
||||
): Record<string, string[]> {
|
||||
return priceListRules.reduce((acc, curr) => {
|
||||
const ruleAttribute = curr.rule_type.rule_attribute
|
||||
const ruleValues = curr.price_list_rule_values || []
|
||||
|
||||
if (ruleAttribute) {
|
||||
acc[ruleAttribute] = ruleValues.map((ruleValue) => ruleValue.value)
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
function buildPriceSetPrices(
|
||||
priceSetMoneyAmounts: (PriceSetMoneyAmountDTO & {
|
||||
price_set: PriceSetMoneyAmountDTO["price_set"] & {
|
||||
variant?: ProductVariantDTO
|
||||
}
|
||||
})[]
|
||||
): Record<string, any>[] {
|
||||
return priceSetMoneyAmounts.map((priceSetMoneyAmount) => {
|
||||
const productVariant = priceSetMoneyAmount.price_set?.variant
|
||||
const rules = priceSetMoneyAmount.price_rules?.reduce((acc, curr) => {
|
||||
if (curr.rule_type.rule_attribute) {
|
||||
acc[curr.rule_type.rule_attribute] = curr.value
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
return {
|
||||
...priceSetMoneyAmount.money_amount,
|
||||
variant_id: productVariant?.id ?? null,
|
||||
rules,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { createPriceListsWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { listPriceLists } from "./queries"
|
||||
import { adminPriceListRemoteQueryFields } from "./query-config"
|
||||
import {
|
||||
adminPriceListRemoteQueryFields,
|
||||
defaultAdminPriceListFields,
|
||||
} from "./query-config"
|
||||
import { AdminPostPriceListsReq } from "./validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
@@ -29,3 +34,31 @@ export const GET = async (
|
||||
limit,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPriceListsReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflow = createPriceListsWorkflow(req.scope)
|
||||
const { result, errors } = await workflow.run({
|
||||
input: { price_lists_data: [req.validatedBody] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const [[priceList]] = await listPriceLists({
|
||||
container: req.scope,
|
||||
apiFields: defaultAdminPriceListFields,
|
||||
remoteQueryFields: adminPriceListRemoteQueryFields,
|
||||
variables: {
|
||||
filters: { id: result[0].id },
|
||||
skip: 0,
|
||||
take: 1,
|
||||
},
|
||||
})
|
||||
|
||||
res.status(200).json({ price_list: priceList })
|
||||
}
|
||||
|
||||
@@ -1,4 +1,152 @@
|
||||
import { PriceListStatus, PriceListType } from "@medusajs/types"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
IsEnum,
|
||||
IsInt,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateIf,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { FindParams } from "../../../types/common"
|
||||
import { transformOptionalDate } from "../../../utils/validators/date-transform"
|
||||
|
||||
export class AdminGetPriceListsParams extends FindParams {}
|
||||
export class AdminGetPriceListsPriceListParams extends FindParams {}
|
||||
|
||||
export class AdminPostPriceListsReq {
|
||||
@IsString()
|
||||
title: string
|
||||
|
||||
@IsString()
|
||||
description: string
|
||||
|
||||
@IsOptional()
|
||||
@Transform(transformOptionalDate)
|
||||
starts_at?: string
|
||||
|
||||
@IsOptional()
|
||||
@Transform(transformOptionalDate)
|
||||
ends_at?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(PriceListStatus)
|
||||
status?: PriceListStatus
|
||||
|
||||
@IsEnum(PriceListType)
|
||||
type: PriceListType
|
||||
|
||||
@IsArray()
|
||||
@Type(() => AdminPriceListPricesCreateReq)
|
||||
@ValidateNested({ each: true })
|
||||
prices: AdminPriceListPricesCreateReq[]
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
rules?: Record<string, string[]>
|
||||
}
|
||||
|
||||
export class AdminPriceListPricesCreateReq {
|
||||
@IsString()
|
||||
currency_code: string
|
||||
|
||||
@IsInt()
|
||||
amount: number
|
||||
|
||||
@IsString()
|
||||
variant_id: string
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
min_quantity?: number
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
max_quantity?: number
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
rules?: Record<string, string>
|
||||
}
|
||||
|
||||
export class AdminPostPriceListsPriceListReq {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
title?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string
|
||||
|
||||
@IsOptional()
|
||||
@Transform(transformOptionalDate)
|
||||
starts_at?: string
|
||||
|
||||
@IsOptional()
|
||||
@Transform(transformOptionalDate)
|
||||
ends_at?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(PriceListStatus)
|
||||
status?: PriceListStatus
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(PriceListType)
|
||||
type?: PriceListType
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
prices: (AdminPriceListPricesCreateReq | AdminPriceListPricesUpdateReq)[]
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
rules?: Record<string, string[]>
|
||||
}
|
||||
|
||||
export class AdminPostPriceListsPriceListPricesReq {
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
prices: (AdminPriceListPricesCreateReq | AdminPriceListPricesUpdateReq)[]
|
||||
}
|
||||
|
||||
export class AdminDeletePriceListsPriceListPricesReq {
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
ids: string[]
|
||||
}
|
||||
|
||||
export class AdminPriceListPricesUpdateReq {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
id: string
|
||||
|
||||
@IsOptional()
|
||||
@ValidateIf((object) => !object.id)
|
||||
@IsString()
|
||||
currency_code?: string
|
||||
|
||||
@IsOptional()
|
||||
@ValidateIf((object) => !object.id)
|
||||
@IsInt()
|
||||
amount?: number
|
||||
|
||||
@IsOptional()
|
||||
@ValidateIf((object) => !object.id)
|
||||
@IsString()
|
||||
variant_id: string
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
min_quantity?: number
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
max_quantity?: number
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
rules?: Record<string, string>
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { updatePriceLists } from "@medusajs/core-flows"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
import { Type } from "class-transformer"
|
||||
import { IsArray, IsBoolean, IsOptional, ValidateNested } from "class-validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
@@ -9,7 +6,6 @@ import { PriceList } from "../../../.."
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { AdminPriceListPricesUpdateReq } from "../../../../types/price-list"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { getPriceListPricingModule } from "./modules-queries"
|
||||
|
||||
/**
|
||||
* @oas [post] /admin/price-lists/{id}/prices/batch
|
||||
@@ -124,48 +120,22 @@ import { getPriceListPricingModule } from "./modules-queries"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
let priceList
|
||||
const featureFlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
const validated = await validator(AdminPostPriceListPricesPricesReq, req.body)
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
const updatePriceListWorkflow = updatePriceLists(req.scope)
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.addPrices(id, validated.prices, validated.override)
|
||||
})
|
||||
|
||||
const input = {
|
||||
price_lists: [
|
||||
{
|
||||
id,
|
||||
...validated,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
await updatePriceListWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
|
||||
priceList = await getPriceListPricingModule(id, {
|
||||
container: req.scope as MedusaContainer,
|
||||
})
|
||||
} else {
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.addPrices(id, validated.prices, validated.override)
|
||||
})
|
||||
|
||||
priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
}
|
||||
const priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
|
||||
res.json({ price_list: priceList })
|
||||
}
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
import { MedusaContainer, PricingTypes, WorkflowTypes } from "@medusajs/types"
|
||||
import {
|
||||
FlagRouter,
|
||||
MedusaV2Flag,
|
||||
PriceListStatus,
|
||||
PriceListType,
|
||||
} from "@medusajs/utils"
|
||||
import { createPriceLists } from "@medusajs/core-flows"
|
||||
import { PriceListStatus, PriceListType } from "@medusajs/utils"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
@@ -26,7 +19,6 @@ import {
|
||||
CreatePriceListInput,
|
||||
} from "../../../../types/price-list"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { getPriceListPricingModule } from "./modules-queries"
|
||||
import { transformOptionalDate } from "../../../../utils/validators/date-transform"
|
||||
|
||||
/**
|
||||
@@ -155,57 +147,17 @@ export default async (req: Request, res) => {
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
let priceList
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
let priceList = await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.create(req.validatedBody as CreatePriceListInput)
|
||||
})
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const createPriceListWorkflow = createPriceLists(req.scope)
|
||||
const validatedInput = req.validatedBody as CreatePriceListInput
|
||||
const rules: PricingTypes.CreatePriceListRules = {}
|
||||
const customerGroups = validatedInput?.customer_groups || []
|
||||
delete validatedInput.customer_groups
|
||||
|
||||
if (customerGroups.length) {
|
||||
rules["customer_group_id"] = customerGroups.map((cg) => cg.id)
|
||||
}
|
||||
|
||||
const input = {
|
||||
price_lists: [
|
||||
{
|
||||
...validatedInput,
|
||||
rules,
|
||||
},
|
||||
],
|
||||
} as WorkflowTypes.PriceListWorkflow.CreatePriceListWorkflowInputDTO
|
||||
|
||||
const { result } = await createPriceListWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
|
||||
priceList = result[0]!.priceList
|
||||
|
||||
priceList = await getPriceListPricingModule(priceList.id, {
|
||||
container: req.scope as MedusaContainer,
|
||||
})
|
||||
} else {
|
||||
priceList = await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.create(req.validatedBody as CreatePriceListInput)
|
||||
})
|
||||
|
||||
priceList = await priceListService.retrieve(priceList.id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
}
|
||||
priceList = await priceListService.retrieve(priceList.id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
|
||||
res.json({ price_list: priceList })
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { WorkflowTypes } from "@medusajs/types"
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { removePriceLists } from "@medusajs/core-flows"
|
||||
import { EntityManager } from "typeorm"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
|
||||
@@ -86,34 +83,13 @@ import PriceListService from "../../../../services/price-list"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const removePriceListsWorkflow = removePriceLists(req.scope)
|
||||
|
||||
const input = {
|
||||
price_lists: [id],
|
||||
} as WorkflowTypes.PriceListWorkflow.RemovePriceListWorkflowInputDTO
|
||||
|
||||
await removePriceListsWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService.withTransaction(transactionManager).delete(id)
|
||||
})
|
||||
}
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService.withTransaction(transactionManager).delete(id)
|
||||
})
|
||||
|
||||
res.json({
|
||||
id,
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { ArrayNotEmpty, IsString } from "class-validator"
|
||||
import { EntityManager } from "typeorm"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { WorkflowTypes } from "@medusajs/types"
|
||||
import { removePriceListPrices } from "@medusajs/core-flows"
|
||||
|
||||
/**
|
||||
* @oas [delete] /admin/price-lists/{id}/prices/batch
|
||||
@@ -101,43 +98,20 @@ import { removePriceListPrices } from "@medusajs/core-flows"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
AdminDeletePriceListPricesPricesReq,
|
||||
req.body
|
||||
)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const deletePriceListPricesWorkflow = removePriceListPrices(req.scope)
|
||||
|
||||
const input = {
|
||||
price_list_id: id,
|
||||
money_amount_ids: validated.price_ids,
|
||||
} as WorkflowTypes.PriceListWorkflow.RemovePriceListPricesWorkflowInputDTO
|
||||
|
||||
await deletePriceListPricesWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deletePrices(id, validated.price_ids)
|
||||
})
|
||||
}
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deletePrices(id, validated.price_ids)
|
||||
})
|
||||
|
||||
res.json({ ids: validated.price_ids, object: "money-amount", deleted: true })
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { WorkflowTypes } from "@medusajs/types"
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { removePriceListProductPrices } from "@medusajs/core-flows"
|
||||
import { EntityManager } from "typeorm"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
|
||||
@@ -94,47 +91,18 @@ import PriceListService from "../../../../services/price-list"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const { id, product_id } = req.params
|
||||
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
|
||||
let deletedPriceIds: string[] = []
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const deletePriceListProductsWorkflow = removePriceListProductPrices(
|
||||
req.scope
|
||||
)
|
||||
|
||||
const input = {
|
||||
product_ids: [product_id],
|
||||
price_list_id: id,
|
||||
} as WorkflowTypes.PriceListWorkflow.RemovePriceListProductsWorkflowInputDTO
|
||||
|
||||
const { result } = await deletePriceListProductsWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
|
||||
deletedPriceIds = result
|
||||
} else {
|
||||
const [deletedIds] = await manager.transaction(
|
||||
async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteProductPrices(id, [product_id])
|
||||
}
|
||||
)
|
||||
deletedPriceIds = deletedIds
|
||||
}
|
||||
const [deletedIds] = await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteProductPrices(id, [product_id])
|
||||
})
|
||||
deletedPriceIds = deletedIds
|
||||
|
||||
return res.json({
|
||||
ids: deletedPriceIds,
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { removePriceListProductPrices } from "@medusajs/core-flows"
|
||||
import { ArrayNotEmpty, IsString } from "class-validator"
|
||||
import { Request, Response } from "express"
|
||||
import { EntityManager } from "typeorm"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { WorkflowTypes } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* @oas [delete] /admin/price-lists/{id}/products/prices/batch
|
||||
@@ -114,43 +111,16 @@ export default async (req: Request, res: Response) => {
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
|
||||
let deletedPriceIds: string[] = []
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const deletePriceListProductsWorkflow = removePriceListProductPrices(
|
||||
req.scope
|
||||
)
|
||||
const [deletedIds] = await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteProductPrices(id, validated.product_ids)
|
||||
})
|
||||
|
||||
const input = {
|
||||
product_ids: validated.product_ids,
|
||||
price_list_id: id,
|
||||
} as WorkflowTypes.PriceListWorkflow.RemovePriceListProductsWorkflowInputDTO
|
||||
|
||||
const { result } = await deletePriceListProductsWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
deletedPriceIds = result
|
||||
} else {
|
||||
const [deletedIds] = await manager.transaction(
|
||||
async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteProductPrices(id, validated.product_ids)
|
||||
}
|
||||
)
|
||||
|
||||
deletedPriceIds = deletedIds
|
||||
}
|
||||
deletedPriceIds = deletedIds
|
||||
|
||||
return res.json({
|
||||
ids: deletedPriceIds,
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { removePriceListVariantPrices } from "@medusajs/core-flows"
|
||||
import { EntityManager } from "typeorm"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { WorkflowTypes } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* @oas [delete] /admin/price-lists/{id}/variants/{variant_id}/prices
|
||||
@@ -97,46 +94,16 @@ export default async (req, res) => {
|
||||
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const isMedusaV2FlagEnabled = featureFlagRouter.isFeatureEnabled(
|
||||
MedusaV2Flag.key
|
||||
)
|
||||
|
||||
let deletedPriceIds: string[] = []
|
||||
|
||||
if (isMedusaV2FlagEnabled) {
|
||||
const deletePriceListProductsWorkflow = removePriceListVariantPrices(
|
||||
req.scope
|
||||
)
|
||||
|
||||
const input = {
|
||||
variant_ids: [variant_id],
|
||||
price_list_id: id,
|
||||
} as WorkflowTypes.PriceListWorkflow.RemovePriceListVariantsWorkflowInputDTO
|
||||
|
||||
const { result } = await deletePriceListProductsWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
deletedPriceIds = result
|
||||
} else {
|
||||
const [deletedIds] = await manager.transaction(
|
||||
async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteVariantPrices(id, [variant_id])
|
||||
}
|
||||
)
|
||||
|
||||
deletedPriceIds = deletedIds
|
||||
}
|
||||
const [deletedIds] = await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.deleteVariantPrices(id, [variant_id])
|
||||
})
|
||||
|
||||
return res.json({
|
||||
ids: deletedPriceIds,
|
||||
ids: deletedIds,
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
|
||||
@@ -11,13 +11,12 @@ import {
|
||||
extendedFindParamsMixin,
|
||||
} from "../../../../types/common"
|
||||
|
||||
import { FlagRouter, MedusaV2Flag } from "@medusajs/utils"
|
||||
import { Type } from "class-transformer"
|
||||
import { Request } from "express"
|
||||
import { ProductStatus } from "../../../../models"
|
||||
import PriceListService from "../../../../services/price-list"
|
||||
import { FilterableProductProps } from "../../../../types/product"
|
||||
import { IsType, listProducts } from "../../../../utils"
|
||||
import { IsType } from "../../../../utils"
|
||||
|
||||
/**
|
||||
* @oas [get] /admin/price-lists/{id}/products
|
||||
@@ -228,10 +227,6 @@ import { IsType, listProducts } from "../../../../utils"
|
||||
export default async (req: Request, res) => {
|
||||
const { id } = req.params
|
||||
const { offset, limit } = req.validatedQuery
|
||||
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
let products
|
||||
let count
|
||||
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
|
||||
@@ -240,19 +235,11 @@ export default async (req: Request, res) => {
|
||||
price_list_id: [id],
|
||||
}
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
;[products, count] = await listProducts(
|
||||
req.scope,
|
||||
filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
} else {
|
||||
;[products, count] = await priceListService.listProducts(
|
||||
id,
|
||||
filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
}
|
||||
const [products, count] = await priceListService.listProducts(
|
||||
id,
|
||||
filterableFields,
|
||||
req.listConfig
|
||||
)
|
||||
|
||||
res.json({
|
||||
products,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { MedusaContainer, PricingTypes, WorkflowTypes } from "@medusajs/types"
|
||||
import { MedusaV2Flag, PriceListStatus, PriceListType } from "@medusajs/utils"
|
||||
import { PriceListStatus, PriceListType } from "@medusajs/utils"
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
@@ -10,7 +9,6 @@ import {
|
||||
} from "class-validator"
|
||||
import { defaultAdminPriceListFields, defaultAdminPriceListRelations } from "."
|
||||
|
||||
import { updatePriceLists } from "@medusajs/core-flows"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { PriceList } from "../../../.."
|
||||
@@ -19,7 +17,6 @@ import PriceListService from "../../../../services/price-list"
|
||||
import { AdminPriceListPricesUpdateReq } from "../../../../types/price-list"
|
||||
import { FeatureFlagDecorators } from "../../../../utils/feature-flag-decorators"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { getPriceListPricingModule } from "./modules-queries"
|
||||
import { transformOptionalDate } from "../../../../utils/validators/date-transform"
|
||||
|
||||
/**
|
||||
@@ -119,8 +116,6 @@ import { transformOptionalDate } from "../../../../utils/validators/date-transfo
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
let priceList
|
||||
const featureFlagRouter = req.scope.resolve("featureFlagRouter")
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
const priceListService: PriceListService =
|
||||
req.scope.resolve("priceListService")
|
||||
@@ -130,48 +125,16 @@ export default async (req, res) => {
|
||||
req.body
|
||||
)
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
const updateVariantsWorkflow = updatePriceLists(req.scope)
|
||||
const customerGroups = validated.customer_groups
|
||||
delete validated.customer_groups
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.update(id, validated)
|
||||
})
|
||||
|
||||
const updatePriceListInput = {
|
||||
id,
|
||||
...validated,
|
||||
} as PricingTypes.UpdatePriceListDTO
|
||||
|
||||
if (Array.isArray(customerGroups)) {
|
||||
updatePriceListInput.rules = {
|
||||
customer_group_id: customerGroups.map((group) => group.id),
|
||||
}
|
||||
}
|
||||
|
||||
const input = {
|
||||
price_lists: [updatePriceListInput],
|
||||
} as WorkflowTypes.PriceListWorkflow.UpdatePriceListWorkflowInputDTO
|
||||
|
||||
await updateVariantsWorkflow.run({
|
||||
input,
|
||||
context: {
|
||||
manager,
|
||||
},
|
||||
})
|
||||
|
||||
priceList = await getPriceListPricingModule(id, {
|
||||
container: req.scope as MedusaContainer,
|
||||
})
|
||||
} else {
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
return await priceListService
|
||||
.withTransaction(transactionManager)
|
||||
.update(id, validated)
|
||||
})
|
||||
|
||||
priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
}
|
||||
const priceList = await priceListService.retrieve(id, {
|
||||
select: defaultAdminPriceListFields as (keyof PriceList)[],
|
||||
relations: defaultAdminPriceListRelations,
|
||||
})
|
||||
|
||||
res.json({ price_list: priceList })
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import express from "express"
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { PriceList } from "../models"
|
||||
import { CurrencyService, PriceListService } from "../services"
|
||||
import { PriceListService } from "../services"
|
||||
import { createDefaultRuleTypes } from "./utils/create-default-rule-types"
|
||||
import { migrateProductVariantPricing } from "./utils/migrate-money-amounts-to-pricing-module"
|
||||
|
||||
@@ -109,7 +109,7 @@ const migratePriceLists = async (container: AwilixContainer) => {
|
||||
pricingModuleService.addPriceListPrices(
|
||||
priceListsToUpdate.map((priceList) => {
|
||||
return {
|
||||
priceListId: priceList.id,
|
||||
price_list_id: priceList.id,
|
||||
prices: priceList.prices
|
||||
.filter((price) =>
|
||||
variantIdPriceSetIdMap.has(price.variants?.[0]?.id)
|
||||
@@ -136,7 +136,7 @@ const migratePriceLists = async (container: AwilixContainer) => {
|
||||
pricingModuleService.createPriceLists(
|
||||
priceListsToCreate.map(
|
||||
({ name: title, prices, customer_groups, ...priceList }) => {
|
||||
const createData: PricingTypes.CreatePriceListDTO = {
|
||||
const createData: any = {
|
||||
...priceList,
|
||||
title,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user