-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**What** - Tax Module Migration file. - Skeleton for API routes and integrations tests for tax API in v2
- Loading branch information
Showing
15 changed files
with
912 additions
and
6 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,77 @@ | ||
import path from "path" | ||
import { ITaxModuleService } from "@medusajs/types" | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
|
||
import { createAdminUser } from "../../../helpers/create-admin-user" | ||
import { initDb, useDb } from "../../../../environment-helpers/use-db" | ||
import { getContainer } from "../../../../environment-helpers/use-container" | ||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" | ||
import { useApi } from "../../../../environment-helpers/use-api" | ||
|
||
jest.setTimeout(50000) | ||
|
||
const env = { MEDUSA_FF_MEDUSA_V2: true } | ||
const adminHeaders = { | ||
headers: { "x-medusa-access-token": "test_token" }, | ||
} | ||
|
||
describe("Taxes - Admin", () => { | ||
let dbConnection | ||
let appContainer | ||
let shutdownServer | ||
let service: ITaxModuleService | ||
|
||
beforeAll(async () => { | ||
const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) | ||
dbConnection = await initDb({ cwd, env } as any) | ||
shutdownServer = await startBootstrapApp({ cwd, env }) | ||
appContainer = getContainer() | ||
service = appContainer.resolve(ModuleRegistrationName.TAX) | ||
}) | ||
|
||
beforeEach(async () => { | ||
await createAdminUser(dbConnection, adminHeaders) | ||
}) | ||
|
||
afterAll(async () => { | ||
const db = useDb() | ||
await db.shutdown() | ||
await shutdownServer() | ||
}) | ||
|
||
afterEach(async () => { | ||
const db = useDb() | ||
await db.teardown() | ||
}) | ||
|
||
it("can retrieve a tax rate", async () => { | ||
const region = await service.createTaxRegions({ | ||
country_code: "us", | ||
}) | ||
const rate = await service.create({ | ||
tax_region_id: region.id, | ||
code: "test", | ||
rate: 2.5, | ||
name: "Test Rate", | ||
}) | ||
|
||
const api = useApi() as any | ||
const response = await api.get(`/admin/tax-rates/${rate.id}`, adminHeaders) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data).toEqual({ | ||
tax_rate: { | ||
id: rate.id, | ||
code: "test", | ||
rate: 2.5, | ||
name: "Test Rate", | ||
metadata: null, | ||
tax_region_id: region.id, | ||
created_at: expect.any(String), | ||
updated_at: expect.any(String), | ||
deleted_at: null, | ||
created_by: null, | ||
}, | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaResponse, | ||
} from "../../../../types/routing" | ||
|
||
import { defaultAdminTaxRateFields } from "../query-config" | ||
import { remoteQueryObjectFromString } from "@medusajs/utils" | ||
|
||
export const GET = async ( | ||
req: AuthenticatedMedusaRequest, | ||
res: MedusaResponse | ||
) => { | ||
const remoteQuery = req.scope.resolve("remoteQuery") | ||
|
||
const variables = { id: req.params.id } | ||
|
||
const queryObject = remoteQueryObjectFromString({ | ||
entryPoint: "tax_rate", | ||
variables, | ||
fields: defaultAdminTaxRateFields, | ||
}) | ||
|
||
const [taxRate] = await remoteQuery(queryObject) | ||
|
||
res.status(200).json({ tax_rate: taxRate }) | ||
} |
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,25 @@ | ||
import * as QueryConfig from "./query-config" | ||
|
||
import { AdminGetTaxRatesTaxRateParams } from "./validators" | ||
import { transformQuery } from "../../../api/middlewares" | ||
|
||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types" | ||
import { authenticate } from "../../../utils/authenticate-middleware" | ||
|
||
export const adminTaxRateRoutesMiddlewares: MiddlewareRoute[] = [ | ||
{ | ||
method: ["ALL"], | ||
matcher: "/admin/tax-rates*", | ||
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])], | ||
}, | ||
{ | ||
method: ["GET"], | ||
matcher: "/admin/tax-rates/:id", | ||
middlewares: [ | ||
transformQuery( | ||
AdminGetTaxRatesTaxRateParams, | ||
QueryConfig.retrieveTransformQueryConfig | ||
), | ||
], | ||
}, | ||
] |
26 changes: 26 additions & 0 deletions
26
packages/medusa/src/api-v2/admin/tax-rates/query-config.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,26 @@ | ||
export const defaultAdminTaxRateRelations = [] | ||
export const allowedAdminTaxRateRelations = [] | ||
export const defaultAdminTaxRateFields = [ | ||
"id", | ||
"name", | ||
"code", | ||
"rate", | ||
"tax_region_id", | ||
"created_by", | ||
"created_at", | ||
"updated_at", | ||
"deleted_at", | ||
"metadata", | ||
] | ||
|
||
export const retrieveTransformQueryConfig = { | ||
defaultFields: defaultAdminTaxRateFields, | ||
defaultRelations: defaultAdminTaxRateRelations, | ||
allowedRelations: allowedAdminTaxRateRelations, | ||
isList: false, | ||
} | ||
|
||
export const listTransformQueryConfig = { | ||
defaultLimit: 20, | ||
isList: true, | ||
} |
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,3 @@ | ||
import { FindParams } from "../../../types/common" | ||
|
||
export class AdminGetTaxRatesTaxRateParams extends FindParams {} |
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.