feat(server): add user existence check and optimize permission queries (#10402)

This commit is contained in:
fengmk2
2025-03-05 01:49:33 +00:00
parent b8ecfbdae6
commit 3d2c4fe007
4 changed files with 20 additions and 13 deletions

View File

@@ -297,6 +297,14 @@ test('should paginate users', async t => {
);
});
test('should check if user exists', async t => {
const user = await t.context.user.create({
email: 'test@affine.pro',
});
t.true(await t.context.user.exists(user.id));
t.false(await t.context.user.exists('non-existing-user'));
});
// #region ConnectedAccount
test('should create, get, update, delete connected account', async t => {

View File

@@ -102,7 +102,7 @@ export class PageInfo {
hasPreviousPage!: boolean;
}
export function Paginated<T>(classRef: Type<T>): any {
export function Paginated<T>(classRef: Type<T>) {
@ObjectType(`${classRef.name}Edge`)
abstract class EdgeType {
@Field(() => String)

View File

@@ -498,22 +498,14 @@ export class DocResolver {
]);
});
const users = new Map<string, PublicUserType>(
await Promise.all(
permissions.map(
async p =>
[p.userId, await this.models.user.getPublicUser(p.userId)] as [
string,
PublicUserType,
]
)
)
const publicUsers = await this.models.user.getPublicUsers(
permissions.map(p => p.userId)
);
const publicUsersMap = new Map(publicUsers.map(pu => [pu.id, pu]));
return paginate(
permissions.map(p => ({
...p,
user: users.get(p.userId),
user: publicUsersMap.get(p.userId) as PublicUserType,
})),
'createdAt',
pagination,

View File

@@ -61,6 +61,13 @@ export class UserModel extends BaseModel {
});
}
async exists(id: string) {
const count = await this.db.user.count({
where: { id },
});
return count > 0;
}
async getPublicUser(id: string): Promise<PublicUser | null> {
return this.db.user.findUnique({
select: publicUserSelect,