refactor(server): remove never used column in page permission (#9985)

This commit is contained in:
forehalo
2025-02-06 10:52:05 +00:00
parent d7da12597a
commit 7c7febd495
9 changed files with 251 additions and 190 deletions
@@ -35,8 +35,8 @@ export class PermissionService {
const { workspaceId, docId, editor } = payload;
await this.prisma.$queryRaw`
INSERT INTO "workspace_page_user_permissions" ("workspace_id", "page_id", "user_id", "type", "accepted", "created_at", "updated_at")
VALUES (${workspaceId}, ${docId}, ${editor}, ${DocRole.Owner}, true, now(), now())
INSERT INTO "workspace_page_user_permissions" ("workspace_id", "page_id", "user_id", "type", "created_at")
VALUES (${workspaceId}, ${docId}, ${editor}, ${DocRole.Owner}, now())
ON CONFLICT ("workspace_id", "page_id", "user_id")
DO NOTHING
`;
@@ -576,7 +576,6 @@ export class PermissionService {
workspaceId: ws,
pageId: page,
userId: user,
accepted: true,
type: {
gte: role,
},
@@ -658,65 +657,48 @@ export class PermissionService {
});
}
async grantPage(
ws: string,
page: string,
user: string,
permission: DocRole
): Promise<string> {
const data = await this.prisma.workspacePageUserPermission.findFirst({
where: {
workspaceId: ws,
pageId: page,
userId: user,
accepted: true,
},
});
if (data) {
const [p] = await this.prisma.$transaction(
[
this.prisma.workspacePageUserPermission.update({
where: {
id: data.id,
async grantPage(ws: string, page: string, user: string, permission: DocRole) {
const [p] = await this.prisma.$transaction(
[
this.prisma.workspacePageUserPermission.upsert({
where: {
workspaceId_pageId_userId: {
workspaceId: ws,
pageId: page,
userId: user,
},
data: {
type: permission,
},
}),
},
update: {
type: permission,
},
create: {
workspaceId: ws,
pageId: page,
userId: user,
type: permission,
},
}),
// If the new permission is owner, we need to revoke old owner
permission === DocRole.Owner
? this.prisma.workspacePageUserPermission.updateMany({
where: {
workspaceId: ws,
pageId: page,
type: DocRole.Owner,
userId: {
not: user,
},
// If the new permission is owner, we need to revoke old owner
permission === DocRole.Owner
? this.prisma.workspacePageUserPermission.updateMany({
where: {
workspaceId: ws,
pageId: page,
type: DocRole.Owner,
userId: {
not: user,
},
data: {
type: DocRole.Manager,
},
})
: null,
].filter(Boolean) as Prisma.PrismaPromise<any>[]
);
},
data: {
type: DocRole.Manager,
},
})
: null,
].filter(Boolean) as Prisma.PrismaPromise<any>[]
);
return p.id;
}
return this.prisma.workspacePageUserPermission
.create({
data: {
workspaceId: ws,
pageId: page,
userId: user,
type: permission,
},
})
.then(p => p.id);
return p;
}
async revokePage(ws: string, page: string, users: string[]) {
@@ -746,14 +728,11 @@ export class PermissionService {
if (userIds.length === 0) {
return [];
}
if (role === DocRole.Owner) {
if (userIds.length > 1) {
throw new SpaceShouldHaveOnlyOneOwner({ spaceId: workspaceId });
}
return [await this.grantPage(workspaceId, pageId, userIds[0], role)];
if (role === DocRole.Owner && userIds.length > 1) {
throw new SpaceShouldHaveOnlyOneOwner({ spaceId: workspaceId });
}
const ret = await this.prisma.$transaction(async tx =>
return await this.prisma.$transaction(async tx =>
Promise.all(
userIds.map(id =>
tx.workspacePageUserPermission.upsert({
@@ -777,7 +756,6 @@ export class PermissionService {
)
)
);
return ret.map(p => p.id);
}
async updatePagePermission(
@@ -798,14 +776,17 @@ export class PermissionService {
return this.grantPage(workspaceId, pageId, userId, role);
}
const { id } = await this.prisma.workspacePageUserPermission.update({
return await this.prisma.workspacePageUserPermission.update({
where: {
id: permission.id,
workspaceId_pageId_userId: {
workspaceId,
pageId,
userId,
},
},
data: {
type: role,
},
});
return id;
}
}
@@ -3,7 +3,6 @@ import {
Args,
Field,
InputType,
Int,
Mutation,
ObjectType,
Parent,
@@ -21,6 +20,9 @@ import {
ExpectToRevokePublicPage,
ExpectToUpdateDocUserRole,
PageIsNotPublic,
paginate,
Paginated,
PaginationInput,
registerObjectType,
} from '../../../base';
import { CurrentUser } from '../../auth';
@@ -34,7 +36,6 @@ import {
PublicPageMode,
WorkspaceRole,
} from '../../permission';
import { UserType } from '../../user';
import { DocID } from '../../utils/doc';
import { WorkspaceType } from '../types';
@@ -73,65 +74,23 @@ class GrantDocUserRolesInput {
userIds!: string[];
}
@InputType()
class PageGrantedUsersInput {
@Field(() => Int)
first!: number;
@Field(() => Int)
offset?: number;
@Field(() => String, { description: 'Cursor', nullable: true })
after?: string;
@Field(() => String, { description: 'Cursor', nullable: true })
before?: string;
}
@ObjectType()
class GrantedDocUserType {
@Field(() => UserType)
user!: UserType;
@Field(() => DocRole)
role!: DocRole;
}
@ObjectType()
class PageInfo {
@Field(() => String, { nullable: true })
startCursor?: string;
@Field(() => String, { nullable: true })
endCursor?: string;
@Field(() => Boolean)
hasNextPage!: boolean;
@Field(() => Boolean)
hasPreviousPage!: boolean;
}
@ObjectType()
class GrantedDocUserEdge {
@Field(() => GrantedDocUserType)
user!: GrantedDocUserType;
@Field(() => String)
workspaceId!: string;
@Field(() => String)
cursor!: string;
pageId!: string;
@Field(() => String)
userId!: string;
@Field(() => DocRole, { name: 'role' })
type!: DocRole;
}
@ObjectType()
class GrantedDocUsersConnection {
@Field(() => Int)
totalCount!: number;
@Field(() => [GrantedDocUserEdge])
edges!: GrantedDocUserEdge[];
@Field(() => PageInfo)
pageInfo!: PageInfo;
}
class PaginatedGrantedDocUserType extends Paginated(GrantedDocUserType) {}
const DocPermissions = registerObjectType<DocActionPermissions>(
Object.fromEntries(
@@ -261,16 +220,15 @@ export class PagePermissionResolver {
};
}
@ResolveField(() => GrantedDocUsersConnection, {
@ResolveField(() => PaginatedGrantedDocUserType, {
description: 'Page granted users list',
complexity: 4,
})
async pageGrantedUsersList(
@Parent() workspace: WorkspaceType,
@Args('pageId') pageId: string,
@Args('pageGrantedUsersInput')
pageGrantedUsersInput: PageGrantedUsersInput
): Promise<GrantedDocUsersConnection> {
@Args('pagination') pagination: PaginationInput
): Promise<PaginatedGrantedDocUserType> {
const docId = new DocID(pageId, workspace.id);
const [permissions, totalCount] = await this.prisma.$transaction(tx => {
return Promise.all([
@@ -279,19 +237,11 @@ export class PagePermissionResolver {
workspaceId: workspace.id,
pageId: docId.guid,
},
include: {
user: true,
},
orderBy: {
createdAt: 'desc',
},
take: pageGrantedUsersInput.first,
skip: pageGrantedUsersInput.offset,
cursor: pageGrantedUsersInput.after
? {
id: pageGrantedUsersInput.after,
}
: undefined,
take: pagination.first,
skip: pagination.offset,
}),
tx.workspacePageUserPermission.count({
where: {
@@ -302,31 +252,7 @@ export class PagePermissionResolver {
]);
});
return {
totalCount,
edges: permissions.map(permission => ({
user: {
user: {
id: permission.user.id,
name: permission.user.name,
email: permission.user.email,
avatarUrl: permission.user.avatarUrl,
emailVerified: permission.user.emailVerifiedAt !== null,
hasPassword: permission.user.password !== null,
},
role: permission.type,
},
cursor: permission.id,
})),
pageInfo: {
startCursor: permissions.at(0)?.id,
endCursor: permissions.at(-1)?.id,
hasNextPage: totalCount > pageGrantedUsersInput.first,
hasPreviousPage:
pageGrantedUsersInput.offset !== undefined &&
pageGrantedUsersInput.offset > 0,
},
};
return paginate(permissions, 'createdAt', pagination, totalCount);
}
/**