-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa,pricing,types): added get endpoints for pricing rule types (
#6678) what: - adds list endpoint for rule types - adds get endpoint for rule types
- Loading branch information
Showing
14 changed files
with
312 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@medusajs/pricing": patch | ||
"@medusajs/medusa": patch | ||
"@medusajs/types": patch | ||
--- | ||
|
||
feat(medusa,pricing,types): added get endpoints for pricing rule types |
112 changes: 112 additions & 0 deletions
112
integration-tests/modules/__tests__/pricing/admin/rule-types.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IPricingModuleService, RuleTypeDTO } from "@medusajs/types" | ||
import { medusaIntegrationTestRunner } from "medusa-test-utils" | ||
import { createAdminUser } from "../../../../helpers/create-admin-user" | ||
|
||
jest.setTimeout(50000) | ||
|
||
const env = { MEDUSA_FF_MEDUSA_V2: true } | ||
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } | ||
|
||
medusaIntegrationTestRunner({ | ||
env, | ||
testSuite: ({ dbConnection, getContainer, api }) => { | ||
describe("Admin: Pricing Rule Types API", () => { | ||
let appContainer | ||
let pricingModule: IPricingModuleService | ||
let ruleTypes: RuleTypeDTO[] | ||
|
||
beforeAll(async () => { | ||
appContainer = getContainer() | ||
pricingModule = appContainer.resolve(ModuleRegistrationName.PRICING) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await createAdminUser(dbConnection, adminHeaders, appContainer) | ||
|
||
ruleTypes = await pricingModule.createRuleTypes([ | ||
{ name: "Customer Group ID", rule_attribute: "customer_group_id" }, | ||
{ name: "Region ID", rule_attribute: "region_id" }, | ||
]) | ||
}) | ||
|
||
describe("GET /admin/pricing", () => { | ||
it("should get all rule types and its prices with rules", async () => { | ||
let response = await api.get( | ||
`/admin/pricing/rule-types`, | ||
adminHeaders | ||
) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.count).toEqual(2) | ||
expect(response.data.rule_types).toEqual([ | ||
expect.objectContaining({ id: expect.any(String) }), | ||
expect.objectContaining({ id: expect.any(String) }), | ||
]) | ||
|
||
response = await api.get( | ||
`/admin/pricing/rule-types?fields=id,rule_attribute,created_at`, | ||
adminHeaders | ||
) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.count).toEqual(2) | ||
expect(response.data.rule_types).toEqual( | ||
expect.arrayContaining([ | ||
{ | ||
id: ruleTypes[0].id, | ||
rule_attribute: ruleTypes[0].rule_attribute, | ||
created_at: expect.any(String), | ||
}, | ||
{ | ||
id: ruleTypes[1].id, | ||
rule_attribute: ruleTypes[1].rule_attribute, | ||
created_at: expect.any(String), | ||
}, | ||
]) | ||
) | ||
}) | ||
}) | ||
|
||
describe("GET /admin/pricing/:id", () => { | ||
it("should retrieve a rule type and its prices with rules", async () => { | ||
const ruleType = ruleTypes[0] | ||
|
||
let response = await api.get( | ||
`/admin/pricing/rule-types/${ruleType.id}`, | ||
adminHeaders | ||
) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.rule_type).toEqual( | ||
expect.objectContaining({ | ||
id: ruleType.id, | ||
}) | ||
) | ||
|
||
response = await api.get( | ||
`/admin/pricing/rule-types/${ruleType.id}?fields=id,created_at`, | ||
adminHeaders | ||
) | ||
|
||
expect(response.data.rule_type).toEqual({ | ||
id: ruleType.id, | ||
created_at: expect.any(String), | ||
}) | ||
}) | ||
|
||
it("should throw an error when rule type is not found", async () => { | ||
const error = await api | ||
.get(`/admin/pricing/rule-types/does-not-exist`, adminHeaders) | ||
.catch((e) => e) | ||
|
||
expect(error.response.status).toBe(404) | ||
expect(error.response.data).toEqual({ | ||
type: "not_found", | ||
message: "RuleType with id: does-not-exist was not found", | ||
}) | ||
}) | ||
}) | ||
}) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { transformQuery } from "../../../api/middlewares" | ||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import { authenticate } from "../../../utils/authenticate-middleware" | ||
import * as QueryConfig from "./query-config" | ||
import { | ||
AdminGetPricingRuleTypesParams, | ||
AdminGetPricingRuleTypesRuleTypeParams, | ||
} from "./validators" | ||
|
||
export const adminPricingRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
matcher: "/admin/pricing*", | ||
middlewares: [authenticate("admin", ["bearer", "session"])], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/admin/pricing/rule-types", | ||
middlewares: [ | ||
transformQuery( | ||
AdminGetPricingRuleTypesParams, | ||
QueryConfig.listTransformQueryConfig | ||
), | ||
], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/admin/pricing/rule-types/:id", | ||
middlewares: [ | ||
transformQuery( | ||
AdminGetPricingRuleTypesRuleTypeParams, | ||
QueryConfig.retrieveTransformQueryConfig | ||
), | ||
], | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const defaultAdminPricingRuleTypeRelations = [] | ||
export const allowedAdminPricingRuleTypeRelations = [] | ||
export const defaultAdminPricingRuleTypeFields = [ | ||
"id", | ||
"name", | ||
"rule_attribute", | ||
"default_priority", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaultFields: defaultAdminPricingRuleTypeFields, | ||
defaultRelations: defaultAdminPricingRuleTypeRelations, | ||
allowedRelations: allowedAdminPricingRuleTypeRelations, | ||
isList: false, | ||
} | ||
|
||
export const listTransformQueryConfig = { | ||
...retrieveTransformQueryConfig, | ||
isList: true, | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/medusa/src/api-v2/admin/pricing/rule-types/[id]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IPricingModuleService } from "@medusajs/types" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../../types/routing" | ||
import { AdminGetPricingRuleTypesRuleTypeParams } from "../../validators" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest<AdminGetPricingRuleTypesRuleTypeParams>, | ||
res: MedusaResponse | ||
) => { | ||
const pricingModule: IPricingModuleService = req.scope.resolve( | ||
ModuleRegistrationName.PRICING | ||
) | ||
|
||
const ruleType = await pricingModule.retrieveRuleType(req.params.id, { | ||
select: req.retrieveConfig.select, | ||
relations: req.retrieveConfig.relations, | ||
}) | ||
|
||
res.status(200).json({ rule_type: ruleType }) | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/medusa/src/api-v2/admin/pricing/rule-types/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IPricingModuleService } from "@medusajs/types" | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../types/routing" | ||
import { AdminGetPricingRuleTypesParams } from "../validators" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest<AdminGetPricingRuleTypesParams>, | ||
res: MedusaResponse | ||
) => { | ||
const pricingModule: IPricingModuleService = req.scope.resolve( | ||
ModuleRegistrationName.PRICING | ||
) | ||
|
||
const [ruleTypes, count] = await pricingModule.listAndCountRuleTypes( | ||
req.filterableFields, | ||
req.listConfig | ||
) | ||
|
||
const { limit, offset } = req.validatedQuery | ||
|
||
res.json({ | ||
count, | ||
rule_types: ruleTypes, | ||
offset, | ||
limit, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IsOptional, IsString } from "class-validator" | ||
import { FindParams, extendedFindParamsMixin } from "../../../types/common" | ||
|
||
export class AdminGetPricingRuleTypesRuleTypeParams extends FindParams {} | ||
export class AdminGetPricingRuleTypesParams extends extendedFindParamsMixin({ | ||
limit: 100, | ||
offset: 0, | ||
}) { | ||
@IsString() | ||
@IsOptional() | ||
rule_attribute?: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.