-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add join favorites for getting workbook entries (#47)
* 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
1 parent
c2ad998
commit 344611c
Showing
12 changed files
with
246 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |
43 changes: 43 additions & 0 deletions
43
src/services/new/workbook/formatters/format-get-workbook-content.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.