Skip to content

Commit

Permalink
Merge pull request #3339 from SeedCompany/0322-user-by-email-query
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF authored Dec 18, 2024
2 parents 72c7acc + bb9b79c commit f802e63
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
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

0 comments on commit f802e63

Please sign in to comment.