fix(modules-sdk): Remote Query "order" as config options (#6925)

This commit is contained in:
Carlos R. L. Rodrigues
2024-04-08 08:17:53 +02:00
committed by GitHub
parent 4f88743591
commit dd35a4dbff
8 changed files with 93 additions and 40 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/currency": patch
---
Remote Query "order" as options

View File

@@ -1,5 +1,5 @@
import { createAdminUser } from "../../../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
@@ -41,6 +41,18 @@ medusaIntegrationTestRunner({
listResp.data.currencies.find((c) => c.code === "aud")
)
})
it("should correctly list currencies in the correct order", async () => {
const listResp = await api.get(
"/admin/currencies?order=-code",
adminHeaders
)
const first = listResp.data.currencies.shift()
expect(first).toEqual(
expect.objectContaining({ code: "zwl", name: "Zimbabwean Dollar" })
)
})
})
},
})

View File

@@ -798,7 +798,7 @@ medusaIntegrationTestRunner({
)
response = await api.get(
`/admin/promotions/rule-value-options/rules/currency?limit=2`,
`/admin/promotions/rule-value-options/rules/currency?limit=2&order=name`,
adminHeaders
)
@@ -806,14 +806,8 @@ medusaIntegrationTestRunner({
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{
label: "United Arab Emirates Dirham",
value: "aed",
},
{
label: "Afghan Afghani",
value: "afn",
},
{ label: "Afghan Afghani", value: "afn" },
{ label: "Albanian Lek", value: "all" },
])
)
@@ -862,8 +856,8 @@ medusaIntegrationTestRunner({
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{ label: "Andorra", value: "ad" },
{ label: "United Arab Emirates", value: "ae" },
{ label: "Afghanistan", value: "af" },
{ label: "Albania", value: "al" },
])
)

View File

@@ -1,7 +1,5 @@
{
"namespaces": [
"public"
],
"namespaces": ["public"],
"name": "public",
"tables": [
{
@@ -70,6 +68,28 @@
"primary": false,
"nullable": false,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
}
},
"name": "currency",
@@ -77,9 +97,7 @@
"indexes": [
{
"keyName": "currency_pkey",
"columnNames": [
"code"
],
"columnNames": ["code"],
"composite": false,
"primary": true,
"unique": true

View File

@@ -2,24 +2,27 @@ import { Migration } from "@mikro-orm/migrations"
export class InitialSetup20240228133303 extends Migration {
async up(): Promise<void> {
const currencyTables = await this.execute(
"select * from information_schema.tables where table_name = 'currency' and table_schema = 'public'"
)
this.addSql(`
create table if not exists "currency"
(
"code" text not null,
"symbol" text not null,
"symbol_native" text not null,
"decimal_digits" int not null default 0,
"rounding" numeric not null default 0,
"raw_rounding" jsonb not null,
"name" text not null,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
constraint "currency_pkey" primary key ("code")
);
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;`)
}
ALTER TABLE "currency" ADD COLUMN IF NOT EXISTS "created_at" TIMESTAMPTZ NOT NULL DEFAULT now();
ALTER TABLE "currency" ADD COLUMN IF NOT EXISTS "updated_at" TIMESTAMPTZ NULL DEFAULT now();
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,
"decimal_digits" int not null default 0, "rounding" numeric not null default 0, "raw_rounding" jsonb not null,
constraint "currency_pkey" primary key ("code"));`)
ALTER TABLE "currency" ADD COLUMN IF NOT EXISTS "decimal_digits" int not null default 0;
ALTER TABLE "currency" ADD COLUMN IF NOT EXISTS "rounding" numeric not null default 0;
ALTER TABLE "currency" ADD COLUMN IF NOT EXISTS "raw_rounding" jsonb;
`)
}
}

View File

@@ -24,6 +24,21 @@ class Currency {
@Property({ columnType: "jsonb" })
raw_rounding: BigNumberRawValue
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
}
export default Currency

View File

@@ -1,8 +1,8 @@
import { pick } from "lodash"
import { FindConfig, QueryConfig, RequestQueryFields } from "../types/common"
import { isDefined, MedusaError } from "medusa-core-utils"
import { BaseEntity } from "../interfaces"
import { getSetDifference, stringToSelectRelationObject } from "@medusajs/utils"
import { pick } from "lodash"
import { MedusaError, isDefined } from "medusa-core-utils"
import { BaseEntity } from "../interfaces"
import { FindConfig, QueryConfig, RequestQueryFields } from "../types/common"
export function pickByConfig<TModel extends BaseEntity>(
obj: TModel | TModel[],
@@ -24,6 +24,8 @@ export function prepareListQuery<
T extends RequestQueryFields,
TEntity extends BaseEntity
>(validated: T, queryConfig: QueryConfig<TEntity> = {}) {
const isMedusaV2 = process.env.MEDUSA_FF_MEDUSA_V2 == "true"
// TODO: this function will be simplified a lot once we drop support for the old api
const { order, fields, limit = 50, expand, offset = 0 } = validated
let {
@@ -182,7 +184,9 @@ export function prepareListQuery<
)
}
} else {
orderBy["created_at"] = "DESC"
if (!isMedusaV2) {
orderBy["created_at"] = "DESC"
}
}
return {

View File

@@ -181,6 +181,7 @@ export class RemoteQuery {
"offset",
"cursor",
"sort",
"order",
"withDeleted",
]
const availableOptionsAlias = new Map([