Skip to content

Commit

Permalink
Add join favorites for getting workbook entries (#47)
Browse files Browse the repository at this point in the history
* Add join favorites for getting workbook entries

* Add isFavorite to response object in testts

* Add isFavorite to response object in testts

* Add where with login and tenant  for query

* Add new method findWithPagination

* Add new class JoinedEntryFavorite

* Add updatedAt field for joined entry

* Add updatedBy field for joined entry

* Change fields for select

* Fix tests

* Fix private tests

* Add findPage to JoinedEntryRevisionFavorite, add formatter - formatGetWorkbookContent

* Fix tests

* Change format response

* Refactor

* Fix after review

* Rm projectId in where
  • Loading branch information
Sergey-weber authored Jan 23, 2024
1 parent c2ad998 commit 344611c
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 59 deletions.
4 changes: 3 additions & 1 deletion src/controllers/workbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
formatWorkbooksList,
formatSetWorkbookIsTemplate,
formatRestoreWorkbook,
formatGetWorkbookContent,
} from '../services/new/workbook/formatters';

export default {
Expand Down Expand Up @@ -84,7 +85,8 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const formattedResponse = formatGetWorkbookContent(result);
const {code, response} = prepareResponse({data: formattedResponse});
res.status(code).send(response);
},

Expand Down
21 changes: 21 additions & 0 deletions src/db/presentations/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const selectedEntryColumns = [
'scope',
'type',
'key',
'innerMeta',
'createdBy',
'createdAt',
'isDeleted',
'deletedAt',
'hidden',
'displayKey',
'entryId',
'savedId',
'publishedId',
'tenantId',
'name',
'sortName',
'public',
'unversionedData',
'workbookId',
] as const;
1 change: 1 addition & 0 deletions src/db/presentations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './joined-entry-revision-favorite';
export * from './joined-migration-tenant';
export * from './joined-entry-migration-tenant';
export * from './joined-embed-embedding-secret';
export * from './joined-entry-favorite';
89 changes: 89 additions & 0 deletions src/db/presentations/joined-entry-favorite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type {Knex} from 'knex';
import {TransactionOrKnex, raw, Modifier, Page} from 'objection';
import {Model} from '../..';
import {Entry} from '../../models/new/entry';
import {Favorite} from '../../models/new/favorite';

import {leftJoinFavorite} from '../utils';

import {selectedEntryColumns} from '../constants';

const selectedColumns = [
...selectedEntryColumns.map((col) => `${Entry.tableName}.${col}`),
raw(`CASE WHEN ${Favorite.tableName}.entry_id IS NULL THEN FALSE ELSE TRUE END AS is_favorite`),
];

export type JoinedEntryFavoriteColumns = Pick<Entry, ArrayElement<typeof selectedEntryColumns>> & {
isFavorite: boolean;
};

export class JoinedEntryFavorite extends Model {
static get tableName() {
return Entry.tableName;
}

static get idColumn() {
return Entry.idColumn;
}

static find({
where,
userLogin,
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
userLogin: string;
trx: TransactionOrKnex;
}) {
return JoinedEntryFavorite.query(trx)
.select(selectedColumns)
.leftJoin(Favorite.tableName, leftJoinFavorite(userLogin))
.where(where)
.timeout(JoinedEntryFavorite.DEFAULT_QUERY_TIMEOUT) as unknown as Promise<
JoinedEntryFavoriteColumns[]
>;
}

static findOne({
where,
userLogin,
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
userLogin: string;
trx: TransactionOrKnex;
}) {
return JoinedEntryFavorite.query(trx)
.select(selectedColumns)
.leftJoin(Favorite.tableName, leftJoinFavorite(userLogin))
.where(where)
.first()
.timeout(JoinedEntryFavorite.DEFAULT_QUERY_TIMEOUT) as unknown as Promise<
JoinedEntryFavoriteColumns | undefined
>;
}

static findPage({
where,
userLogin,
modify,
page,
pageSize,
trx,
}: {
where: Record<string, unknown> | ((builder: Knex.QueryBuilder) => void);
trx: TransactionOrKnex;
modify: Modifier;
userLogin: string;
page: number;
pageSize: number;
}) {
return JoinedEntryFavorite.query(trx)
.select(selectedColumns)
.leftJoin(Favorite.tableName, leftJoinFavorite(userLogin))
.where(where)
.modify(modify)
.page(page, pageSize)
.timeout(JoinedEntryFavorite.DEFAULT_QUERY_TIMEOUT) as unknown as Promise<Page<Entry>>;
}
}
41 changes: 34 additions & 7 deletions src/db/presentations/joined-entry-revision-favorite/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import type {Knex} from 'knex';
import {TransactionOrKnex, raw} from 'objection';
import {TransactionOrKnex, raw, Modifier, Page} from 'objection';
import {
selectedColumns as joinedEntryRevisionColumns,
joinRevision,
JoinedEntryRevision,
JoinedEntryRevisionColumns,
JoinRevisionArgs,
} from '../joined-entry-revision';

import {Entry} from '../../models/new/entry';

import {RevisionModel} from '../../models/new/revision';
import {Favorite} from '../../models/new/favorite';

import {leftJoinFavorite} from '../utils';

const selectedColumns = [
...joinedEntryRevisionColumns,
raw(`CASE WHEN ${Favorite.tableName}.entry_id IS NULL THEN FALSE ELSE TRUE END AS is_favorite`),
];

export const leftJoinFavorite = (userLogin: string) => (builder: Knex.JoinClause) => {
builder
.on(`${Favorite.tableName}.entryId`, `${Entry.tableName}.entryId`)
.andOnIn(`${Favorite.tableName}.login`, [userLogin]);
};

export type JoinedEntryRevisionFavoriteColumns = JoinedEntryRevisionColumns & {
isFavorite: boolean;
};
Expand Down Expand Up @@ -69,4 +67,33 @@ export class JoinedEntryRevisionFavorite extends JoinedEntryRevision {
JoinedEntryRevisionFavoriteColumns | undefined
>;
}

static findPage({
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 & JoinedEntryRevisionFavoriteColumns>
>;
}
}
22 changes: 1 addition & 21 deletions src/db/presentations/joined-entry-revision/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,7 @@ import {Model} from '../..';
import {Entry} from '../../models/new/entry';
import {RevisionModel} from '../../models/new/revision';

const selectedEntryColumns = [
'scope',
'type',
'key',
'innerMeta',
'createdBy',
'createdAt',
'isDeleted',
'deletedAt',
'hidden',
'displayKey',
'entryId',
'savedId',
'publishedId',
'tenantId',
'name',
'sortName',
'public',
'unversionedData',
'workbookId',
] as const;
import {selectedEntryColumns} from '../constants';

const selectedRevisionColumns = [
'data',
Expand Down
11 changes: 11 additions & 0 deletions src/db/presentations/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {Knex} from 'knex';

import {Entry} from '../models/new/entry';

import {Favorite} from '../models/new/favorite';

export const leftJoinFavorite = (userLogin: string) => (builder: Knex.JoinClause) => {
builder
.on(`${Favorite.tableName}.entryId`, `${Entry.tableName}.entryId`)
.andOnIn(`${Favorite.tableName}.login`, [userLogin]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {EntryPermissions} from '../../entry/types';
import {JoinedEntryRevisionFavoriteColumns} from '../../../../db/presentations/joined-entry-revision-favorite';

export type GetContentResult = {
permissions?: EntryPermissions;
isLocked: boolean;
} & JoinedEntryRevisionFavoriteColumns;

export const formatGetJoinedEntryRevisionFavorite = (
joinedEntryRevisionFavorite: GetContentResult,
) => {
return {
entryId: joinedEntryRevisionFavorite.entryId,
scope: joinedEntryRevisionFavorite.scope,
type: joinedEntryRevisionFavorite.type,
key: joinedEntryRevisionFavorite.displayKey,
createdBy: joinedEntryRevisionFavorite.createdBy,
createdAt: joinedEntryRevisionFavorite.createdAt,
updatedBy: joinedEntryRevisionFavorite.updatedBy,
updatedAt: joinedEntryRevisionFavorite.updatedAt,
savedId: joinedEntryRevisionFavorite.savedId,
publishedId: joinedEntryRevisionFavorite.publishedId,
meta: joinedEntryRevisionFavorite.meta,
hidden: joinedEntryRevisionFavorite.hidden,
workbookId: joinedEntryRevisionFavorite.workbookId,
isFavorite: joinedEntryRevisionFavorite.isFavorite,
isLocked: joinedEntryRevisionFavorite.isLocked,
permissions: joinedEntryRevisionFavorite.permissions,
};
};

export const formatGetWorkbookContent = ({
entries,
nextPageToken,
}: {
entries: GetContentResult[];
nextPageToken?: string;
}) => {
return {
entries: entries.map((entry) => formatGetJoinedEntryRevisionFavorite(entry)),
nextPageToken,
};
};
1 change: 1 addition & 0 deletions src/services/new/workbook/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './format-workbook-model-with-operation';
export * from './format-workbook-models-list';
export * from './format-set-workbook-is-template';
export * from './format-restore-workbook';
export * from './format-get-workbook-content';
Loading

0 comments on commit 344611c

Please sign in to comment.