diff --git a/__tests__/highlights.ts b/__tests__/highlights.ts index 81ee2b028e..60768c9b43 100644 --- a/__tests__/highlights.ts +++ b/__tests__/highlights.ts @@ -171,16 +171,19 @@ describe('query channelConfigurations', () => { channel: 'career', displayName: 'Career Growth', mode: 'shadow', + order: 1, }, { channel: 'backend', displayName: 'Backend Engineering', mode: 'publish', + order: 2, }, { channel: 'disabled', displayName: 'Disabled', mode: 'disabled', + order: 0, }, ]); @@ -199,6 +202,11 @@ describe('query channelConfigurations', () => { expect(res.errors).toBeFalsy(); expect(res.data.channelConfigurations).toEqual([ + { + channel: 'career', + displayName: 'Career Growth', + digest: null, + }, { channel: 'backend', displayName: 'Backend Engineering', @@ -211,11 +219,6 @@ describe('query channelConfigurations', () => { }, }, }, - { - channel: 'career', - displayName: 'Career Growth', - digest: null, - }, ]); }); }); diff --git a/src/common/channelHighlight/definitions.ts b/src/common/channelHighlight/definitions.ts index 3ad6c01daf..e44a44b69f 100644 --- a/src/common/channelHighlight/definitions.ts +++ b/src/common/channelHighlight/definitions.ts @@ -12,6 +12,7 @@ export const getChannelHighlightDefinitions = async ({ mode: Not('disabled'), }, order: { + order: 'ASC', channel: 'ASC', }, }); diff --git a/src/entity/ChannelHighlightDefinition.ts b/src/entity/ChannelHighlightDefinition.ts index e349dd7675..1dc5d021a8 100644 --- a/src/entity/ChannelHighlightDefinition.ts +++ b/src/entity/ChannelHighlightDefinition.ts @@ -15,6 +15,9 @@ export class ChannelHighlightDefinition { @Column({ type: 'text', default: '' }) displayName: string; + @Column({ type: 'smallint', default: 0 }) + order: number; + @Column({ type: 'text', default: 'disabled' }) mode: ChannelHighlightMode; diff --git a/src/migration/1776108702577-ChannelHighlightDefinitionOrder.ts b/src/migration/1776108702577-ChannelHighlightDefinitionOrder.ts new file mode 100644 index 0000000000..eed174ed39 --- /dev/null +++ b/src/migration/1776108702577-ChannelHighlightDefinitionOrder.ts @@ -0,0 +1,34 @@ +import type { MigrationInterface, QueryRunner } from 'typeorm'; + +export class ChannelHighlightDefinitionOrder1776108702577 + implements MigrationInterface +{ + name = 'ChannelHighlightDefinitionOrder1776108702577'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(/* sql */ ` + ALTER TABLE "channel_highlight_definition" + ADD COLUMN "order" smallint NOT NULL DEFAULT 0 + `); + + await queryRunner.query(/* sql */ ` + WITH ordered_definitions AS ( + SELECT + "channel", + ROW_NUMBER() OVER (ORDER BY "channel" ASC) - 1 AS "order" + FROM "channel_highlight_definition" + ) + UPDATE "channel_highlight_definition" AS definition + SET "order" = ordered_definitions."order" + FROM ordered_definitions + WHERE ordered_definitions."channel" = definition."channel" + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(/* sql */ ` + ALTER TABLE "channel_highlight_definition" + DROP COLUMN "order" + `); + } +} diff --git a/src/schema/highlights.ts b/src/schema/highlights.ts index 9b3b772573..a1b24ac1d8 100644 --- a/src/schema/highlights.ts +++ b/src/schema/highlights.ts @@ -124,7 +124,8 @@ export const resolvers: IResolvers = { .where(`"${builder.alias}"."mode" != :disabledMode`, { disabledMode: 'disabled', }) - .orderBy(`"${builder.alias}"."channel"`, 'ASC'); + .orderBy(`"${builder.alias}"."order"`, 'ASC') + .addOrderBy(`"${builder.alias}"."channel"`, 'ASC'); return builder; }, true,