Skip to content

Commit

Permalink
Sunset doesEmailAddressExist in favor of getUserByEmailAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjnelson committed Dec 18, 2024
1 parent 5f34198 commit 525424e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 47 deletions.
29 changes: 5 additions & 24 deletions src/components/user/user.edgedb.repository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Injectable } from '@nestjs/common';
import {
ID,
NotImplementedException,
PublicOf,
Session,
UnsecuredDto,
} from '~/common';
import { ID, NotImplementedException, PublicOf, Session } from '~/common';
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
import {
AssignOrganizationToUser,
Expand Down Expand Up @@ -44,26 +38,13 @@ export class UserEdgeDBRepository
},
);

async doesEmailAddressExist(email: string) {
const query = e.select(e.User, () => ({
async getUserByEmailAddress(email: string, _session: Session) {
const query = e.select(e.User, (user) => ({
...this.hydrate(user),
filter_single: { email },
}));
const result = await this.db.run(query);
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);
return await this.db.run(query);
}

protected listFilters(user: ScopeOf<typeof e.User>, input: UserListInput) {
Expand Down
17 changes: 1 addition & 16 deletions src/components/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,6 @@ export class UserRepository extends DtoRepository<typeof User, [Session | ID]>(
return result!; // result from paginate() will always have 1 row.
}

async doesEmailAddressExist(email: string) {
const result = await this.db
.query()
.matchNode('email', 'EmailAddress', { value: email })
.return('email.value')
.first();
return !!result;
}

async getUserByEmailAddress(email: string, session: Session) {
const query = this.db
.query()
Expand All @@ -260,13 +251,7 @@ export class UserRepository extends DtoRepository<typeof User, [Session | ID]>(
.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;
return (await query.first())?.dto ?? null;
}

async assignOrganizationToUser({
Expand Down
9 changes: 6 additions & 3 deletions src/components/user/user.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ export class UserResolver {
@Query(() => Boolean, {
description: 'Checks whether a provided email already exists',
})
async checkEmail(@Args() { email }: CheckEmailArgs): Promise<boolean> {
return await this.userService.checkEmail(email);
async checkEmail(
@AnonSession() session: Session,
@Args() { email }: CheckEmailArgs,
): Promise<boolean> {
return await this.userService.checkEmail(email, session);
}

@Query(() => User, {
Expand All @@ -136,7 +139,7 @@ export class UserResolver {
async userByEmail(
@LoggedInSession() session: Session,
@Args() { email }: CheckEmailArgs,
): Promise<User> {
): Promise<User | null> {
return await this.userService.getUserByEmailAddress(email, session);
}

Expand Down
9 changes: 5 additions & 4 deletions src/components/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,15 @@ export class UserService {
return await this.knownLanguages.list(userId);
}

async checkEmail(email: string): Promise<boolean> {
const exists = await this.userRepo.doesEmailAddressExist(email);
return !exists;
async checkEmail(email: string, session: Session): Promise<boolean> {
const exists = await this.userRepo.getUserByEmailAddress(email, session);
return !!exists;
}

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

return user ? this.secure(user, session) : null;
}

async assignOrganizationToUser(request: AssignOrganizationToUser) {
Expand Down

0 comments on commit 525424e

Please sign in to comment.