Explorar el Código

fix issue with incorrect order in mostViewedVideos (#124)

Bartosz Dryl hace 1 año
padre
commit
2220c7aa68

+ 28 - 6
src/server-extension/resolvers/VideosResolver/index.ts

@@ -131,12 +131,34 @@ export class VideosResolver {
     }
 
     const connectionQuery = new ConnectionQuery(model, ctx.openreader.dialect, typeName, req)
-    const connectionQuerySql = extendClause(
-      connectionQuery.sql,
-      'WHERE',
-      `"video"."id" IN (${ids.map((id) => `'${id}'`).join(', ')})`,
-      'AND'
-    )
+
+    let connectionQuerySql: string
+
+    const hasPeriodDaysArgAndIsOrderedByViews =
+      args.periodDays &&
+      (args.orderBy.find((orderByArg) => orderByArg === 'viewsNum_DESC') ||
+        args.orderBy.find((orderByArg) => orderByArg === 'viewsNum_ASC'))
+
+    if (hasPeriodDaysArgAndIsOrderedByViews) {
+      connectionQuerySql = extendClause(
+        connectionQuery.sql,
+        'FROM',
+        `JOIN unnest('{${ids
+          .map((id) => `"${id}"`)
+          .join(', ')}}'::text[]) WITH ORDINALITY t(id, ord) USING ("id")`,
+        ''
+      )
+
+      connectionQuerySql = connectionQuerySql.replace('"video"."views_num" DESC', 't.ord')
+      connectionQuerySql = connectionQuerySql.replace('"video"."views_num" ASC', 't.ord DESC')
+    } else {
+      connectionQuerySql = extendClause(
+        connectionQuery.sql,
+        'WHERE',
+        `"video"."id" IN (${ids.map((id) => `'${id}'`).join(', ')})`,
+        'AND'
+      )
+    }
 
     // Override the raw `sql` string in `connectionQuery` with the modified query
     ;(connectionQuery as { sql: string }).sql = connectionQuerySql

+ 3 - 1
src/server-extension/resolvers/baseTypes.ts

@@ -48,7 +48,9 @@ export class Membership {
 }
 
 export enum VideoOrderByInput {
-  id_ASC,
+  id_ASC = 'id_ASC',
+  viewsNum_ASC = 'viewsNum_ASC',
+  viewsNum_DESC = 'viewsNum_DESC',
 }
 registerEnumType(VideoOrderByInput, { name: 'VideoOrderByInput' })