feat(server): add user existence check and optimize permission queries (#10402)
This commit is contained in:
@@ -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
|
// #region ConnectedAccount
|
||||||
|
|
||||||
test('should create, get, update, delete connected account', async t => {
|
test('should create, get, update, delete connected account', async t => {
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ export class PageInfo {
|
|||||||
hasPreviousPage!: boolean;
|
hasPreviousPage!: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Paginated<T>(classRef: Type<T>): any {
|
export function Paginated<T>(classRef: Type<T>) {
|
||||||
@ObjectType(`${classRef.name}Edge`)
|
@ObjectType(`${classRef.name}Edge`)
|
||||||
abstract class EdgeType {
|
abstract class EdgeType {
|
||||||
@Field(() => String)
|
@Field(() => String)
|
||||||
|
|||||||
@@ -498,22 +498,14 @@ export class DocResolver {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
const users = new Map<string, PublicUserType>(
|
const publicUsers = await this.models.user.getPublicUsers(
|
||||||
await Promise.all(
|
permissions.map(p => p.userId)
|
||||||
permissions.map(
|
|
||||||
async p =>
|
|
||||||
[p.userId, await this.models.user.getPublicUser(p.userId)] as [
|
|
||||||
string,
|
|
||||||
PublicUserType,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
const publicUsersMap = new Map(publicUsers.map(pu => [pu.id, pu]));
|
||||||
return paginate(
|
return paginate(
|
||||||
permissions.map(p => ({
|
permissions.map(p => ({
|
||||||
...p,
|
...p,
|
||||||
user: users.get(p.userId),
|
user: publicUsersMap.get(p.userId) as PublicUserType,
|
||||||
})),
|
})),
|
||||||
'createdAt',
|
'createdAt',
|
||||||
pagination,
|
pagination,
|
||||||
|
|||||||
@@ -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> {
|
async getPublicUser(id: string): Promise<PublicUser | null> {
|
||||||
return this.db.user.findUnique({
|
return this.db.user.findUnique({
|
||||||
select: publicUserSelect,
|
select: publicUserSelect,
|
||||||
|
|||||||
Reference in New Issue
Block a user