Skip to content

Commit

Permalink
Add getUserByEmail query
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjnelson committed Dec 17, 2024
1 parent 966b2db commit 818ffad
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/components/user/user.edgedb.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Injectable } from '@nestjs/common';
import { ID, NotImplementedException, PublicOf } from '~/common';
import {
ID,
NotImplementedException,
PublicOf,
Session,
UnsecuredDto,
} from '~/common';
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
import {
AssignOrganizationToUser,
Expand Down Expand Up @@ -46,6 +52,20 @@ export class UserEdgeDBRepository
return !!result;
}

getUserByEmailAddress(
email: string,
_session: Session,
): Promise<UnsecuredDto<User>> {
const query = e.assert_exists(
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
26 changes: 26 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,31 @@ 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();

if (!result) {
throw new ServerException('User not found');
}

return result.dto;
}

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

@Query(() => User, {
description: 'Returns a user for a given email address',
})
async userByEmail(
@LoggedInSession() session: Session,
@Args() { email }: CheckEmailArgs,
): Promise<User> {
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 this.secure(user, session);
}

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

0 comments on commit 818ffad

Please sign in to comment.