mirror of
https://github.com/docmost/docmost.git
synced 2025-11-18 18:41:11 +10:00
include userRole in space object
This commit is contained in:
@ -10,7 +10,7 @@ import {
|
|||||||
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
import { validate as isValidUUID } from 'uuid';
|
import { validate as isValidUUID } from 'uuid';
|
||||||
import { ExpressionBuilder } from 'kysely';
|
import { ExpressionBuilder, sql } from 'kysely';
|
||||||
import { DB } from '@docmost/db/types/db';
|
import { DB } from '@docmost/db/types/db';
|
||||||
import { jsonObjectFrom } from 'kysely/helpers/postgres';
|
import { jsonObjectFrom } from 'kysely/helpers/postgres';
|
||||||
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
||||||
@ -27,6 +27,7 @@ export class ShareRepo {
|
|||||||
'key',
|
'key',
|
||||||
'pageId',
|
'pageId',
|
||||||
'includeSubPages',
|
'includeSubPages',
|
||||||
|
'searchIndexing',
|
||||||
'creatorId',
|
'creatorId',
|
||||||
'spaceId',
|
'spaceId',
|
||||||
'workspaceId',
|
'workspaceId',
|
||||||
@ -133,7 +134,7 @@ export class ShareRepo {
|
|||||||
.selectFrom('shares')
|
.selectFrom('shares')
|
||||||
.select(this.baseFields)
|
.select(this.baseFields)
|
||||||
.select((eb) => this.withPage(eb))
|
.select((eb) => this.withPage(eb))
|
||||||
.select((eb) => this.withSpace(eb))
|
.select((eb) => this.withSpace(eb, userId))
|
||||||
.select((eb) => this.withCreator(eb))
|
.select((eb) => this.withCreator(eb))
|
||||||
.where('spaceId', 'in', userSpaceIds)
|
.where('spaceId', 'in', userSpaceIds)
|
||||||
.orderBy('updatedAt', 'desc');
|
.orderBy('updatedAt', 'desc');
|
||||||
@ -157,15 +158,55 @@ export class ShareRepo {
|
|||||||
).as('page');
|
).as('page');
|
||||||
}
|
}
|
||||||
|
|
||||||
withSpace(eb: ExpressionBuilder<DB, 'shares'>) {
|
withSpace(eb: ExpressionBuilder<DB, 'shares'>, userId?: string) {
|
||||||
return jsonObjectFrom(
|
return jsonObjectFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('spaces')
|
.selectFrom('spaces')
|
||||||
.select(['spaces.id', 'spaces.name', 'spaces.slug'])
|
.select(['spaces.id', 'spaces.name', 'spaces.slug'])
|
||||||
|
.$if(Boolean(userId), (qb) =>
|
||||||
|
qb.select((eb) => this.withUserSpaceRole(eb, userId)),
|
||||||
|
)
|
||||||
.whereRef('spaces.id', '=', 'shares.spaceId'),
|
.whereRef('spaces.id', '=', 'shares.spaceId'),
|
||||||
).as('space');
|
).as('space');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withUserSpaceRole(eb: ExpressionBuilder<DB, 'spaces'>, userId: string) {
|
||||||
|
return eb
|
||||||
|
.selectFrom(
|
||||||
|
eb
|
||||||
|
.selectFrom('spaceMembers')
|
||||||
|
.select(['spaceMembers.role'])
|
||||||
|
.whereRef('spaceMembers.spaceId', '=', 'spaces.id')
|
||||||
|
.where('spaceMembers.userId', '=', userId)
|
||||||
|
.unionAll(
|
||||||
|
eb
|
||||||
|
.selectFrom('spaceMembers')
|
||||||
|
.innerJoin(
|
||||||
|
'groupUsers',
|
||||||
|
'groupUsers.groupId',
|
||||||
|
'spaceMembers.groupId',
|
||||||
|
)
|
||||||
|
.select(['spaceMembers.role'])
|
||||||
|
.whereRef('spaceMembers.spaceId', '=', 'spaces.id')
|
||||||
|
.where('groupUsers.userId', '=', userId),
|
||||||
|
)
|
||||||
|
.as('roles_union'),
|
||||||
|
)
|
||||||
|
.select('roles_union.role')
|
||||||
|
.orderBy(
|
||||||
|
sql`CASE roles_union.role
|
||||||
|
WHEN 'admin' THEN 3
|
||||||
|
WHEN 'writer' THEN 2
|
||||||
|
WHEN 'reader' THEN 1
|
||||||
|
ELSE 0
|
||||||
|
END`,
|
||||||
|
|
||||||
|
'desc',
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
.as('userRole');
|
||||||
|
}
|
||||||
|
|
||||||
withCreator(eb: ExpressionBuilder<DB, 'shares'>) {
|
withCreator(eb: ExpressionBuilder<DB, 'shares'>) {
|
||||||
return jsonObjectFrom(
|
return jsonObjectFrom(
|
||||||
eb
|
eb
|
||||||
|
|||||||
Reference in New Issue
Block a user