diff --git a/.changeset/healthy-ads-share.md b/.changeset/healthy-ads-share.md new file mode 100644 index 0000000000..263c3179fd --- /dev/null +++ b/.changeset/healthy-ads-share.md @@ -0,0 +1,6 @@ +--- +"@medusajs/modules-sdk": patch +"@medusajs/currency": patch +--- + +Remote Query "order" as options diff --git a/integration-tests/modules/__tests__/currency/admin/currency.spec.ts b/integration-tests/modules/__tests__/currency/admin/currency.spec.ts index 2a22543d69..edc09f5f92 100644 --- a/integration-tests/modules/__tests__/currency/admin/currency.spec.ts +++ b/integration-tests/modules/__tests__/currency/admin/currency.spec.ts @@ -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" }) + ) + }) }) }, }) diff --git a/integration-tests/modules/__tests__/promotion/admin/promotion-rules.spec.ts b/integration-tests/modules/__tests__/promotion/admin/promotion-rules.spec.ts index bde58a75dc..6c0aab2bee 100644 --- a/integration-tests/modules/__tests__/promotion/admin/promotion-rules.spec.ts +++ b/integration-tests/modules/__tests__/promotion/admin/promotion-rules.spec.ts @@ -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" }, ]) ) diff --git a/packages/currency/src/migrations/.snapshot-medusa-currency.json b/packages/currency/src/migrations/.snapshot-medusa-currency.json index cf10d6199f..296f2b87b2 100644 --- a/packages/currency/src/migrations/.snapshot-medusa-currency.json +++ b/packages/currency/src/migrations/.snapshot-medusa-currency.json @@ -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 diff --git a/packages/currency/src/migrations/InitialSetup20240228133303.ts b/packages/currency/src/migrations/InitialSetup20240228133303.ts index bad3e9af1b..53e6b89534 100644 --- a/packages/currency/src/migrations/InitialSetup20240228133303.ts +++ b/packages/currency/src/migrations/InitialSetup20240228133303.ts @@ -2,24 +2,27 @@ import { Migration } from "@mikro-orm/migrations" export class InitialSetup20240228133303 extends Migration { async up(): Promise { - 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; + `) } } diff --git a/packages/currency/src/models/currency.ts b/packages/currency/src/models/currency.ts index ac8d79a1e3..bb503992af 100644 --- a/packages/currency/src/models/currency.ts +++ b/packages/currency/src/models/currency.ts @@ -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 diff --git a/packages/medusa/src/utils/get-query-config.ts b/packages/medusa/src/utils/get-query-config.ts index 7d4cea51a3..94f6a2458b 100644 --- a/packages/medusa/src/utils/get-query-config.ts +++ b/packages/medusa/src/utils/get-query-config.ts @@ -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( obj: TModel | TModel[], @@ -24,6 +24,8 @@ export function prepareListQuery< T extends RequestQueryFields, TEntity extends BaseEntity >(validated: T, queryConfig: QueryConfig = {}) { + 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 { diff --git a/packages/modules-sdk/src/remote-query.ts b/packages/modules-sdk/src/remote-query.ts index a08b9e1e7a..ed1a036ace 100644 --- a/packages/modules-sdk/src/remote-query.ts +++ b/packages/modules-sdk/src/remote-query.ts @@ -181,6 +181,7 @@ export class RemoteQuery { "offset", "cursor", "sort", + "order", "withDeleted", ] const availableOptionsAlias = new Map([