diff --git a/src/db/presentations/joined-entry-revision/index.ts b/src/db/presentations/joined-entry-revision/index.ts index e7707469..4349a2ef 100644 --- a/src/db/presentations/joined-entry-revision/index.ts +++ b/src/db/presentations/joined-entry-revision/index.ts @@ -1,5 +1,5 @@ import type {Knex} from 'knex'; -import {TransactionOrKnex} from 'objection'; +import {TransactionOrKnex, raw} from 'objection'; import {Model} from '../..'; import {Entry} from '../../models/new/entry'; import {RevisionModel} from '../../models/new/revision'; @@ -43,21 +43,24 @@ export const selectedColumns = [ export interface JoinRevisionArgs { revId?: RevisionModel['revId']; branch?: 'saved' | 'published'; - isPublishFallback?: boolean; } export const joinRevision = - ({revId, branch, isPublishFallback}: JoinRevisionArgs) => + ({revId, branch}: JoinRevisionArgs) => (builder: Knex.JoinClause) => { if (revId) { builder.on(`${Entry.tableName}.entryId`, `${RevisionModel.tableName}.entryId`); } else if (branch === 'published') { builder.on(`${Entry.tableName}.publishedId`, `${RevisionModel.tableName}.revId`); - } else { + } else if (branch === 'saved') { builder.on(`${Entry.tableName}.savedId`, `${RevisionModel.tableName}.revId`); - if (isPublishFallback) { - builder.orOn(`${Entry.tableName}.publishedId`, `${RevisionModel.tableName}.revId`); - } + } else { + builder.on( + raw( + `COALESCE(${Entry.tableName}.published_id, ${Entry.tableName}.saved_id)`, + ) as unknown as string, + `${RevisionModel.tableName}.revId`, + ); } }; @@ -75,11 +78,11 @@ export class JoinedEntryRevision extends Model { static find({ where, - joinRevisionArgs, + joinRevisionArgs = {}, trx, }: { where: Record | ((builder: Knex.QueryBuilder) => void); - joinRevisionArgs: JoinRevisionArgs; + joinRevisionArgs?: JoinRevisionArgs; trx: TransactionOrKnex; }) { return JoinedEntryRevision.query(trx) @@ -93,11 +96,11 @@ export class JoinedEntryRevision extends Model { static findOne({ where, - joinRevisionArgs, + joinRevisionArgs = {}, trx, }: { where: Record | ((builder: Knex.QueryBuilder) => void); - joinRevisionArgs: JoinRevisionArgs; + joinRevisionArgs?: JoinRevisionArgs; trx: TransactionOrKnex; }) { return JoinedEntryRevision.query(trx) diff --git a/src/services/entry/actions/copy-to-workbook.ts b/src/services/entry/actions/copy-to-workbook.ts index 85579d0e..0e7c10a8 100644 --- a/src/services/entry/actions/copy-to-workbook.ts +++ b/src/services/entry/actions/copy-to-workbook.ts @@ -81,9 +81,6 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { builder.whereIn(`${Entry.tableName}.entryId`, entryIds); }, - joinRevisionArgs: { - isPublishFallback: true, - }, trx: Entry.replica, }); @@ -269,9 +266,6 @@ export const copyToWorkbook = async (ctx: CTX, params: Params) => { builder.whereIn(`${Entry.tableName}.entryId`, Array.from(mapEntryIdsWithOldIds.keys())); }, - joinRevisionArgs: { - isPublishFallback: true, - }, trx: transactionTrx, }); diff --git a/src/services/new/entry/get-entry.ts b/src/services/new/entry/get-entry.ts index 828e30ef..3d00fd3a 100644 --- a/src/services/new/entry/get-entry.ts +++ b/src/services/new/entry/get-entry.ts @@ -50,7 +50,7 @@ export const getEntry = async ( {ctx, trx, skipValidation = false}: ServiceArgs, args: GetEntryArgs, ) => { - const {entryId, revId, branch, includePermissionsInfo, includeLinks} = args; + const {entryId, revId, branch = 'saved', includePermissionsInfo, includeLinks} = args; logInfo(ctx, 'GET_ENTRY_REQUEST', { entryId: Utils.encodeId(entryId),