feat: Add decimal digits and rounding info to currencies (#6991)
This commit is contained in:
1 parent
667c8609cc
commit
7302d078a9
9 files changed
+89
-49
No files matched your search
@@ -19,10 +19,12 @@ moduleIntegrationTestRunner({
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
code: "cad",
|
code: "cad",
|
||||||
name: "Canadian Dollar",
|
name: "Canadian Dollar",
|
||||||
|
decimal_digits: 2,
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
code: "usd",
|
code: "usd",
|
||||||
name: "US Dollar",
|
name: "US Dollar",
|
||||||
|
decimal_digits: 2,
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
@@ -116,7 +118,7 @@ moduleIntegrationTestRunner({
|
|||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
take: 1,
|
take: 1,
|
||||||
select: ["code"],
|
select: ["code", "rounding"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -126,6 +128,8 @@ moduleIntegrationTestRunner({
|
|||||||
expect(serialized).toEqual([
|
expect(serialized).toEqual([
|
||||||
{
|
{
|
||||||
code: "aed",
|
code: "aed",
|
||||||
|
rounding: 0,
|
||||||
|
raw_rounding: expect.any(Object),
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,6 +41,35 @@
|
|||||||
"primary": false,
|
"primary": false,
|
||||||
"nullable": false,
|
"nullable": false,
|
||||||
"mappedType": "text"
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"decimal_digits": {
|
||||||
|
"name": "decimal_digits",
|
||||||
|
"type": "int",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"default": "0",
|
||||||
|
"mappedType": "integer"
|
||||||
|
},
|
||||||
|
"rounding": {
|
||||||
|
"name": "rounding",
|
||||||
|
"type": "numeric",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"default": "0",
|
||||||
|
"mappedType": "decimal"
|
||||||
|
},
|
||||||
|
"raw_rounding": {
|
||||||
|
"name": "raw_rounding",
|
||||||
|
"type": "jsonb",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "json"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"name": "currency",
|
"name": "currency",
|
||||||
|
|||||||
@@ -2,8 +2,24 @@ import { Migration } from "@mikro-orm/migrations"
|
|||||||
|
|
||||||
export class InitialSetup20240228133303 extends Migration {
|
export class InitialSetup20240228133303 extends Migration {
|
||||||
async up(): Promise<void> {
|
async up(): Promise<void> {
|
||||||
|
const currencyTables = await this.execute(
|
||||||
|
"select * from information_schema.tables where table_name = 'currency' and table_schema = 'public'"
|
||||||
|
)
|
||||||
|
|
||||||
|
if (currencyTables.length > 0) {
|
||||||
|
// This is so we can still run the api tests, remove completely once that is not needed
|
||||||
|
this.addSql(
|
||||||
|
`alter table "currency" add column "decimal_digits" int not null default 0;`
|
||||||
|
)
|
||||||
|
this.addSql(
|
||||||
|
`alter table "currency" add column "rounding" numeric not null default 0;`
|
||||||
|
)
|
||||||
|
this.addSql(`alter table "currency" add column "raw_rounding" jsonb;`)
|
||||||
|
}
|
||||||
|
|
||||||
this.addSql(`create table if not exists "currency"
|
this.addSql(`create table if not exists "currency"
|
||||||
("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null,
|
("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null,
|
||||||
|
"decimal_digits" int not null default 0, "rounding" numeric not null default 0, "raw_rounding" jsonb not null,
|
||||||
constraint "currency_pkey" primary key ("code"));`)
|
constraint "currency_pkey" primary key ("code"));`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { BigNumberRawValue } from "@medusajs/types"
|
||||||
|
import { BigNumber, MikroOrmBigNumberProperty } from "@medusajs/utils"
|
||||||
import { Entity, PrimaryKey, Property } from "@mikro-orm/core"
|
import { Entity, PrimaryKey, Property } from "@mikro-orm/core"
|
||||||
|
|
||||||
@Entity({ tableName: "currency" })
|
@Entity({ tableName: "currency" })
|
||||||
@@ -13,6 +15,15 @@ class Currency {
|
|||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
name: string
|
name: string
|
||||||
|
|
||||||
|
@Property({ columnType: "int", default: 0 })
|
||||||
|
decimal_digits: number
|
||||||
|
|
||||||
|
@MikroOrmBigNumberProperty({ default: 0 })
|
||||||
|
rounding: BigNumber | number
|
||||||
|
|
||||||
|
@Property({ columnType: "jsonb" })
|
||||||
|
raw_rounding: BigNumberRawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Currency
|
export default Currency
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
import { defaultAdminCurrencyFields } from "../query-config"
|
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||||
@@ -10,7 +9,7 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
const queryObject = remoteQueryObjectFromString({
|
const queryObject = remoteQueryObjectFromString({
|
||||||
entryPoint: "currency",
|
entryPoint: "currency",
|
||||||
variables,
|
variables,
|
||||||
fields: defaultAdminCurrencyFields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const [currency] = await remoteQuery(queryObject)
|
const [currency] = await remoteQuery(queryObject)
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
import { transformQuery } from "../../../api/middlewares"
|
|
||||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
|
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
import {
|
import { AdminGetCurrencyParams, AdminGetCurrenciesParams } from "./validators"
|
||||||
AdminGetCurrenciesCurrencyParams,
|
|
||||||
AdminGetCurrenciesParams,
|
|
||||||
} from "./validators"
|
|
||||||
|
|
||||||
export const adminCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
|
export const adminCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
|
||||||
{
|
{
|
||||||
@@ -17,7 +14,7 @@ export const adminCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/currencies",
|
matcher: "/admin/currencies",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetCurrenciesParams,
|
AdminGetCurrenciesParams,
|
||||||
QueryConfig.listTransformQueryConfig
|
QueryConfig.listTransformQueryConfig
|
||||||
),
|
),
|
||||||
@@ -27,8 +24,8 @@ export const adminCurrencyRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
method: ["GET"],
|
method: ["GET"],
|
||||||
matcher: "/admin/currencies/:code",
|
matcher: "/admin/currencies/:code",
|
||||||
middlewares: [
|
middlewares: [
|
||||||
transformQuery(
|
validateAndTransformQuery(
|
||||||
AdminGetCurrenciesCurrencyParams,
|
AdminGetCurrencyParams,
|
||||||
QueryConfig.retrieveTransformQueryConfig
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,20 +1,19 @@
|
|||||||
export const defaultAdminCurrencyRelations = []
|
|
||||||
export const allowedAdminCurrencyRelations = []
|
|
||||||
export const defaultAdminCurrencyFields = [
|
export const defaultAdminCurrencyFields = [
|
||||||
"code",
|
"code",
|
||||||
"name",
|
"name",
|
||||||
"symbol",
|
"symbol",
|
||||||
"symbol_native",
|
"symbol_native",
|
||||||
|
"decimal_digits",
|
||||||
|
"rounding",
|
||||||
]
|
]
|
||||||
|
|
||||||
export const retrieveTransformQueryConfig = {
|
export const retrieveTransformQueryConfig = {
|
||||||
defaultFields: defaultAdminCurrencyFields,
|
defaults: defaultAdminCurrencyFields,
|
||||||
defaultRelations: defaultAdminCurrencyRelations,
|
|
||||||
allowedRelations: allowedAdminCurrencyRelations,
|
|
||||||
isList: false,
|
isList: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const listTransformQueryConfig = {
|
export const listTransformQueryConfig = {
|
||||||
|
...retrieveTransformQueryConfig,
|
||||||
defaultLimit: 50,
|
defaultLimit: 50,
|
||||||
isList: true,
|
isList: true,
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||||
import { defaultAdminCurrencyFields } from "./query-config"
|
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||||
@@ -9,11 +8,9 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
entryPoint: "currency",
|
entryPoint: "currency",
|
||||||
variables: {
|
variables: {
|
||||||
filters: req.filterableFields,
|
filters: req.filterableFields,
|
||||||
order: req.listConfig.order,
|
...req.remoteQueryConfig.pagination,
|
||||||
skip: req.listConfig.skip,
|
|
||||||
take: req.listConfig.take,
|
|
||||||
},
|
},
|
||||||
fields: defaultAdminCurrencyFields,
|
fields: req.remoteQueryConfig.fields,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { rows: currencies, metadata } = await remoteQuery(queryObject)
|
const { rows: currencies, metadata } = await remoteQuery(queryObject)
|
||||||
|
|||||||
@@ -1,30 +1,18 @@
|
|||||||
import { Type } from "class-transformer"
|
import { createFindParams, createSelectParams } from "../../utils/validators"
|
||||||
import { IsOptional, IsString, ValidateNested } from "class-validator"
|
import { z } from "zod"
|
||||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
|
||||||
|
|
||||||
export class AdminGetCurrenciesCurrencyParams extends FindParams {}
|
export const AdminGetCurrencyParams = createSelectParams()
|
||||||
/**
|
|
||||||
* Parameters used to filter and configure the pagination of the retrieved currencies.
|
export type AdminGetCurrenciesParamsType = z.infer<
|
||||||
*/
|
typeof AdminGetCurrenciesParams
|
||||||
export class AdminGetCurrenciesParams extends extendedFindParamsMixin({
|
>
|
||||||
limit: 50,
|
export const AdminGetCurrenciesParams = createFindParams({
|
||||||
offset: 0,
|
offset: 0,
|
||||||
}) {
|
limit: 50,
|
||||||
/**
|
}).merge(
|
||||||
* Search parameter for currencies.
|
z.object({
|
||||||
*/
|
code: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
@IsString({ each: true })
|
$and: z.lazy(() => AdminGetCurrenciesParams.array()).optional(),
|
||||||
@IsOptional()
|
$or: z.lazy(() => AdminGetCurrenciesParams.array()).optional(),
|
||||||
code?: string | string[]
|
})
|
||||||
|
)
|
||||||
// Additional filters from BaseFilterable
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetCurrenciesParams)
|
|
||||||
$and?: AdminGetCurrenciesParams[]
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ValidateNested({ each: true })
|
|
||||||
@Type(() => AdminGetCurrenciesParams)
|
|
||||||
$or?: AdminGetCurrenciesParams[]
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user