feat: new workspace apis (#2825)

This commit is contained in:
DarkSky
2023-06-26 22:12:58 +08:00
committed by GitHub
parent e3ffd04804
commit d46b6c4863
7 changed files with 341 additions and 0 deletions

View File

@@ -11,4 +11,6 @@ import { AuthService } from './service';
controllers: [NextAuthController],
})
export class AuthModule {}
export * from './guard';
export { TokenType } from './resolver';

View File

@@ -40,6 +40,18 @@ export class AuthResolver {
};
}
@Mutation(() => UserType)
async register(
@Context() ctx: { req: Request },
@Args('name') name: string,
@Args('email') email: string,
@Args('password') password: string
) {
const user = await this.auth.register(name, email, password);
ctx.req.user = user;
return user;
}
@Mutation(() => UserType)
async signIn(
@Context() ctx: { req: Request },

View File

@@ -8,3 +8,5 @@ import { UserResolver } from './resolver';
providers: [UserResolver],
})
export class UsersModule {}
export { UserType } from './resolver';

View File

@@ -117,6 +117,21 @@ export class PermissionService {
});
}
async accept(ws: string, user: string) {
const result = await this.prisma.userWorkspacePermission.updateMany({
where: {
workspaceId: ws,
userId: user,
accepted: false,
},
data: {
accepted: true,
},
});
return result.count > 0;
}
async revoke(ws: string, user: string) {
const result = await this.prisma.userWorkspacePermission.deleteMany({
where: {

View File

@@ -111,6 +111,29 @@ export class WorkspaceResolver {
return data.user;
}
@ResolveField(() => [UserType], {
description: 'Members of workspace',
complexity: 2,
})
async members(
@CurrentUser() user: UserType,
@Parent() workspace: WorkspaceType
) {
const data = await this.prisma.userWorkspacePermission.findMany({
where: {
workspaceId: workspace.id,
accepted: true,
userId: {
not: user.id,
},
},
include: {
user: true,
},
});
return data.map(({ user }) => user);
}
@Query(() => [WorkspaceType], {
description: 'Get all accessible workspaces for current user',
complexity: 2,
@@ -203,4 +226,61 @@ export class WorkspaceResolver {
return true;
}
@Mutation(() => Boolean)
async invite(
@CurrentUser() user: User,
@Args('workspaceId') workspaceId: string,
@Args('email') email: string,
@Args('permission', { type: () => Permission }) permission: Permission
) {
await this.permissionProvider.check(workspaceId, user.id, Permission.Admin);
if (permission === Permission.Owner) {
throw new ForbiddenException('Cannot change owner');
}
const target = await this.prisma.user.findUnique({
where: {
email,
},
});
if (!target) {
throw new NotFoundException("User doesn't exist");
}
await this.permissionProvider.grant(workspaceId, target.id, permission);
return true;
}
@Mutation(() => Boolean)
async revoke(
@CurrentUser() user: User,
@Args('workspaceId') workspaceId: string,
@Args('userId') userId: string
) {
await this.permissionProvider.check(workspaceId, user.id, Permission.Admin);
return this.permissionProvider.revoke(workspaceId, userId);
}
@Mutation(() => Boolean)
async acceptInvite(
@CurrentUser() user: User,
@Args('workspaceId') workspaceId: string
) {
return this.permissionProvider.accept(workspaceId, user.id);
}
@Mutation(() => Boolean)
async leaveWorkspace(
@CurrentUser() user: User,
@Args('workspaceId') workspaceId: string
) {
await this.permissionProvider.check(workspaceId, user.id);
return this.permissionProvider.revoke(workspaceId, user.id);
}
}