Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): add currency to region responses #5857

Merged
merged 9 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-shoes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

chore(medusa): add currency to region responses
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/admin/regions Remove region from country on delete successfully creates a region with countries from a previously deleted region 1`] = `
exports[`/admin/regions Remove region from country on delete should successfully creates a region with countries from a previously deleted region 1`] = `
Object {
"deleted": true,
"id": "test-region",
"object": "region",
}
`;

exports[`/admin/regions Remove region from country on delete successfully creates a region with countries from a previously deleted region 2`] = `
exports[`/admin/regions Remove region from country on delete should successfully creates a region with countries from a previously deleted region 2`] = `
Object {
"automatic_taxes": true,
"countries": Array [
Expand All @@ -23,6 +23,12 @@ Object {
},
],
"created_at": Any<String>,
"currency": Object {
"code": "usd",
"name": "US Dollar",
"symbol": "$",
"symbol_native": "$",
},
"currency_code": "usd",
"deleted_at": null,
"fulfillment_providers": Array [
Expand Down
130 changes: 84 additions & 46 deletions integration-tests/api/__tests__/admin/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ describe("/admin/regions", () => {
await db.teardown()
})

it("successfully creates a region with countries from a previously deleted region", async () => {
it("should successfully creates a region with countries from a previously deleted region", async () => {
const api = useApi()

const response = await api
.delete(`/admin/regions/test-region`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.delete(`/admin/regions/test-region`, adminReqConfig)
.catch((err) => {
console.log(err)
})
Expand All @@ -85,11 +81,7 @@ describe("/admin/regions", () => {
fulfillment_providers: ["test-ful"],
countries: ["us"],
},
{
headers: {
"x-medusa-access-token": "test_token",
},
}
adminReqConfig
)

expect(newReg.status).toEqual(200)
Expand Down Expand Up @@ -153,15 +145,28 @@ describe("/admin/regions", () => {
await db.teardown()
})

it("only returns non-deleted regions", async () => {
it("should list the regions with expand currency relation", async () => {
const api = useApi()

const response = await api
.get(`/admin/regions`, {
headers: {
"x-medusa-access-token": "test_token",
},
const response = await api.get(
"/admin/regions?limit=1&offset=1&expand=currency",
adminReqConfig
)

expect(response.status).toEqual(200)

expect(response.data.regions[0].currency).toEqual(
expect.objectContaining({
code: "usd",
})
)
})

it("should only return non-deleted regions", async () => {
const api = useApi()

const response = await api
.get(`/admin/regions`, adminReqConfig)
.catch((err) => {
console.log(err)
})
Expand All @@ -183,29 +188,21 @@ describe("/admin/regions", () => {
expect(response.status).toEqual(200)
})

it("returns count of total regions", async () => {
it("should return count of total regions", async () => {
const api = useApi()

const response = await api.get(`/admin/regions?limit=2`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const response = await api.get(`/admin/regions?limit=2`, adminReqConfig)

expect(response.data.regions).toHaveLength(2)
expect(response.data.count).toEqual(3)
expect(response.status).toEqual(200)
})

it("filters correctly on update", async () => {
it("should filter correctly on update", async () => {
const api = useApi()

const response = await api
.get(`/admin/regions?updated_at[gt]=10-10-2005`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.get(`/admin/regions?updated_at[gt]=10-10-2005`, adminReqConfig)
.catch((err) => {
console.log(err)
})
Expand All @@ -225,6 +222,59 @@ describe("/admin/regions", () => {
})
})

describe("GET /admin/regions/:id", () => {
beforeEach(async () => {
const manager = dbConnection.manager
await adminSeeder(dbConnection)

await manager.insert(Region, {
id: "region-id",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should retrieve the region from ID", async () => {
const api = useApi()

const response = await api.get(
`/admin/regions/region-id?expand=currency`,
adminReqConfig
)

expect(response.status).toEqual(200)
expect(response.data.region).toEqual(
expect.objectContaining({
id: "region-id",
currency: expect.objectContaining({
code: "usd",
}),
})
)
})

it("should throw an error when region ID is invalid", async () => {
const api = useApi()

const error = await api
.get(`/admin/regions/invalid-region-id`, adminReqConfig)
.catch((e) => e)

expect(error.response.status).toEqual(404)

expect(error.response.data).toEqual({
type: "not_found",
message: "Region with invalid-region-id was not found",
})
})
})

describe("DELETE /admin/regions/:id", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
Expand All @@ -246,7 +296,7 @@ describe("/admin/regions", () => {
await db.teardown()
})

it("successfully deletes a region with a fulfillment provider", async () => {
it("should successfully deletes a region with a fulfillment provider", async () => {
const api = useApi()

// add fulfillment provider to the region
Expand All @@ -255,19 +305,11 @@ describe("/admin/regions", () => {
{
fulfillment_providers: ["test-ful"],
},
{
headers: {
"x-medusa-access-token": "test_token",
},
}
adminReqConfig
)

const response = await api
.delete(`/admin/regions/test-region`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.delete(`/admin/regions/test-region`, adminReqConfig)
.catch((err) => {
console.log(err)
})
Expand All @@ -280,7 +322,7 @@ describe("/admin/regions", () => {
expect(response.status).toEqual(200)
})

it("fails to create when countries exists in different region", async () => {
it("should fails to create when countries exists in different region", async () => {
const api = useApi()

try {
Expand All @@ -294,11 +336,7 @@ describe("/admin/regions", () => {
fulfillment_providers: ["test-ful"],
countries: ["us"],
},
{
headers: {
"x-medusa-access-token": "test_token",
},
}
adminReqConfig
)
} catch (error) {
expect(error.response.status).toEqual(422)
Expand Down
68 changes: 67 additions & 1 deletion integration-tests/api/__tests__/store/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("/store/carts", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
medusaProcess = await setupServer({ cwd, verbose: true })
})

afterAll(async () => {
Expand Down Expand Up @@ -71,5 +71,71 @@ describe("/store/carts", () => {
expect(response.data.offset).toEqual(1)
expect(response.data.limit).toEqual(1)
})

it("should list the store regions with expand relations", async () => {
const api = useApi()

const response = await api.get(
"/store/regions?limit=1&offset=1&expand=currency"
)

expect(response.status).toEqual(200)

expect(response.data.regions[0].currency).toEqual(
expect.objectContaining({
code: "usd",
})
)
})
})

describe("GET /store/regions/:id", () => {
beforeEach(async () => {
const manager = dbConnection.manager

await manager.insert(Region, {
id: "region-id",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
})
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should retrieve the region from ID", async () => {
const api = useApi()

const response = await api.get(`/store/regions/region-id?expand=currency`)

expect(response.status).toEqual(200)

expect(response.data.region).toEqual(
expect.objectContaining({
id: "region-id",
currency: expect.objectContaining({
code: "usd",
}),
})
)
})

it("should throw an error when region ID is invalid", async () => {
const api = useApi()

const error = await api
.get(`/store/regions/invalid-region-id`)
.catch((e) => e)

expect(error.response.status).toEqual(404)

expect(error.response.data).toEqual({
type: "not_found",
message: "Region with invalid-region-id was not found",
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const defaultFields = [

const defaultRelations = [
"countries",
"payment_providers",
"currency",
"fulfillment_providers",
"payment_providers",
]

describe("GET /admin/regions/:region_id", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IdMap } from "medusa-test-utils"
import { RegionServiceMock } from "../../../../../services/__mocks__/region"
import { request } from "../../../../../helpers/test-request"
import { RegionServiceMock } from "../../../../../services/__mocks__/region"

const defaultFields = [
"id",
Expand All @@ -19,8 +19,9 @@ const defaultFields = [

const defaultRelations = [
"countries",
"payment_providers",
"currency",
"fulfillment_providers",
"payment_providers",
]

describe("GET /admin/regions", () => {
Expand Down Expand Up @@ -54,6 +55,7 @@ describe("GET /admin/regions", () => {
relations: defaultRelations,
take: 50,
skip: 0,
order: { created_at: "DESC" },
}
)
})
Expand Down Expand Up @@ -89,6 +91,7 @@ describe("GET /admin/regions", () => {
relations: defaultRelations,
take: 20,
skip: 10,
order: { created_at: "DESC" },
}
)
})
Expand Down
11 changes: 5 additions & 6 deletions packages/medusa/src/api/routes/admin/regions/get-region.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defaultAdminRegionFields, defaultAdminRegionRelations } from "."

import RegionService from "../../../../services/region"
import { FindParams } from "../../../../types/common"

/**
* @oas [get] /admin/regions/{id}
Expand Down Expand Up @@ -57,10 +56,10 @@ import RegionService from "../../../../services/region"
export default async (req, res) => {
const { region_id } = req.params
const regionService: RegionService = req.scope.resolve("regionService")
const region = await regionService.retrieve(region_id, {
select: defaultAdminRegionFields,
relations: defaultAdminRegionRelations,
})

const region = await regionService.retrieve(region_id, req.retrieveConfig)

res.status(200).json({ region })
}

export class AdminGetRegionsRegionParams extends FindParams {}
Loading