Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getUserByEmail query #3339

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/components/user/user.edgedb.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ID, NotImplementedException, PublicOf } from '~/common';
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
import { disableAccessPolicies, e, RepoFor, ScopeOf } from '~/core/edgedb';
import {
AssignOrganizationToUser,
RemoveOrganizationFromUser,
Expand Down Expand Up @@ -42,10 +42,19 @@ export class UserEdgeDBRepository
const query = e.select(e.User, () => ({
filter_single: { email },
}));
const result = await this.db.run(query);
const result = await this.db.withOptions(disableAccessPolicies).run(query);
return !!result;
}

getUserByEmailAddress(email: string) {
const query = e.select(e.User, (user) => ({
...this.hydrate(user),
filter_single: { email },
}));

return this.db.run(query);
}

protected listFilters(user: ScopeOf<typeof e.User>, input: UserListInput) {
if (!input.filter) return [];
return [
Expand Down
21 changes: 21 additions & 0 deletions src/components/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
matchProps,
merge,
paginate,
path,
property,
requestingUser,
sortWith,
Expand Down Expand Up @@ -243,6 +244,26 @@ export class UserRepository extends DtoRepository<typeof User, [Session | ID]>(
return !!result;
}

async getUserByEmailAddress(email: string, session: Session) {
const query = this.db
.query()
.matchNode('node', 'User')
.where(
path([
node('node'),
relation('out', '', 'email', ACTIVE),
node({
value: email,
}),
]),
)
.apply(this.privileges.forUser(session).filterToReadable())
.apply(this.hydrate(session));

const result = await query.first();
return result?.dto ?? null;
}

async assignOrganizationToUser({
userId,
orgId,
Expand Down
11 changes: 11 additions & 0 deletions src/components/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ export class UserResolver {
return await this.userService.checkEmail(email);
}

@Query(() => User, {
description: 'Returns a user for a given email address',
nullable: true,
})
async userByEmail(
@LoggedInSession() session: Session,
@Args() { email }: CheckEmailArgs,
): Promise<User | null> {
return await this.userService.getUserByEmailAddress(email, session);
}

@ResolveField(() => SecuredUnavailabilityList)
async unavailabilities(
@AnonSession() session: Session,
Expand Down
5 changes: 5 additions & 0 deletions src/components/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ export class UserService {
return !exists;
}

async getUserByEmailAddress(email: string, session: Session) {
const user = await this.userRepo.getUserByEmailAddress(email, session);
return user ? this.secure(user, session) : null;
}

async assignOrganizationToUser(request: AssignOrganizationToUser) {
await this.userRepo.assignOrganizationToUser(request);
}
Expand Down
Loading