Skip to content

Commit

Permalink
Add new method findWithPagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey-weber committed Dec 26, 2023
1 parent 0ee4d73 commit ced6a3e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 50 deletions.
31 changes: 30 additions & 1 deletion src/db/presentations/joined-entry-revision-favorite/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Knex} from 'knex';
import {TransactionOrKnex, raw} from 'objection';
import {TransactionOrKnex, raw, Modifier, Page} from 'objection';
import {
selectedColumns as joinedEntryRevisionColumns,
joinRevision,
Expand Down Expand Up @@ -48,6 +48,35 @@ export class JoinedEntryRevisionFavorite extends JoinedEntryRevision {
>;
}

static findWithPagination({
where,
modify,
joinRevisionArgs,
userLogin,
page,
pageSize,
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
modify: Modifier;
joinRevisionArgs: JoinRevisionArgs;
userLogin: string;
page: number;
pageSize: number;
trx: TransactionOrKnex;
}) {
return JoinedEntryRevisionFavorite.query(trx)
.select(selectedColumns)
.join(RevisionModel.tableName, joinRevision(joinRevisionArgs))
.leftJoin(Favorite.tableName, leftJoinFavorite(userLogin))
.where(where)
.modify(modify)
.page(page, pageSize)
.timeout(JoinedEntryRevisionFavorite.DEFAULT_QUERY_TIMEOUT) as unknown as Promise<
Page<Entry>
>;
}

static findOne({
where,
joinRevisionArgs,
Expand Down
100 changes: 51 additions & 49 deletions src/services/new/workbook/get-workbook-content.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {raw} from 'objection';
import {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {RETURN_NAVIGATION_COLUMNS, DEFAULT_PAGE, DEFAULT_PAGE_SIZE} from '../../../const';
import Navigation from '../../../db/models/navigation';
import {DEFAULT_PAGE, DEFAULT_PAGE_SIZE} from '../../../const';
import {EntryScope} from '../../../db/models/new/entry';
import Utils, {logInfo} from '../../../utils';
import {UsPermission} from '../../../types/models';
Expand All @@ -10,7 +8,8 @@ import {getReplica} from '../utils';
import {getWorkbook} from './get-workbook';
import {getEntryPermissionsByWorkbook} from './utils';
import {Feature, isEnabledFeature} from '../../../components/features';
import Favorite from '../../../db/models/favorite';

import {JoinedEntryRevisionFavorite} from '../../../db/presentations';

const validateArgs = makeSchemaValidator({
type: 'object',
Expand Down Expand Up @@ -59,6 +58,13 @@ const validateArgs = makeSchemaValidator({
scope: {
type: ['array', 'string'],
},
revId: {
type: 'string',
},
branch: {
type: 'string',
enum: ['saved', 'published'],
},
},
});

Expand All @@ -76,6 +82,8 @@ export interface GetWorkbookContentArgs {
direction: 'asc' | 'desc';
};
scope?: EntryScope | EntryScope[];
revId?: string;
branch?: 'saved' | 'published';
}

export const getWorkbookContent = async (
Expand All @@ -91,6 +99,8 @@ export const getWorkbookContent = async (
filters,
orderBy,
scope,
revId,
branch,
} = args;

logInfo(ctx, 'GET_WORKBOOK_CONTENT_START', {
Expand All @@ -110,7 +120,7 @@ export const getWorkbookContent = async (

const targetTrx = getReplica(trx);

const {user, tenantId} = ctx.get('info');
const {user} = ctx.get('info');

const workbook = await getWorkbook(
{ctx, trx, skipValidation: true, skipCheckPermissions},
Expand All @@ -120,29 +130,13 @@ export const getWorkbookContent = async (
},
);

const selectColumnNames = [
...RETURN_NAVIGATION_COLUMNS,
raw('CASE sub.entry_id WHEN sub.entry_id THEN TRUE ELSE FALSE END AS is_favorite'),
];

// TODO: Get rid of Navigation
const entriesPage = await Navigation.query(targetTrx)
.select(selectColumnNames)
.join('revisions', 'entries.savedId', 'revisions.revId')
.leftJoin(
Favorite.query(targetTrx)
.select('favorites.entryId')
.where('login', user.login)
.andWhere('tenantId', '=', tenantId)
.as('sub'),
'sub.entryId',
'entries.entryId',
)
.where({
workbookId: workbookId,
isDeleted: false,
})
.where((builder) => {
const entriesPage = await JoinedEntryRevisionFavorite.findWithPagination({
where: (builder) => {
builder.where({
workbookId: workbookId,
isDeleted: false,
});

if (isEnabledFeature(ctx, Feature.UseLimitedView) && !workbook.permissions?.view) {
builder.whereNotIn('scope', ['dataset', 'connection']);
}
Expand All @@ -159,8 +153,8 @@ export const getWorkbookContent = async (
if (scope) {
builder.whereIn('scope', Array.isArray(scope) ? scope : [scope]);
}
})
.modify((builder) => {
},
modify: (builder) => {
if (orderBy) {
switch (orderBy.field) {
case 'updatedAt':
Expand All @@ -174,28 +168,36 @@ export const getWorkbookContent = async (
break;
}
}
})
.page(page, pageSize)
.timeout(Navigation.DEFAULT_QUERY_TIMEOUT);
},
trx: targetTrx,
joinRevisionArgs: {
revId,
branch,
},
userLogin: user.login,
page,
pageSize,
});

const nextPageToken = Utils.getNextPageToken(page, pageSize, entriesPage.total);

const entries: Array<Navigation & {permissions?: UsPermission; isLocked?: boolean}> =
entriesPage.results.map((entry) => {
let permissions: Optional<UsPermission>;

if (includePermissionsInfo) {
permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}

entry.permissions = permissions;
entry.isLocked = false;
return entry;
});
const entries = entriesPage.results.map((entry) => {
let permissions: Optional<UsPermission>;

if (includePermissionsInfo) {
permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}

return {
...entry,
permissions,
isLocked: false,
};
});

ctx.log('GET_WORKBOOK_CONTENT_FINISH');

Expand Down

0 comments on commit ced6a3e

Please sign in to comment.