Skip to content

Commit

Permalink
review: throw an error if the user doesn't have login activity record…
Browse files Browse the repository at this point in the history
… when getLoginActivity method has been executed.
  • Loading branch information
YunhwanJeong committed Oct 8, 2024
1 parent 4b76572 commit 59e6339
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/login/login-activity/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NotFound } from '@curveball/http-errors';
import { UserLoginActivityRecord } from 'knex/types/tables.js';
import db from '../../database.js';
import { User } from '../../types.js';
Expand All @@ -8,10 +9,14 @@ export function reachedMaxAttempts(attempts: number) {
return attempts >= MAX_FAILED_ATTEMPTS;
}

async function getLoginActivity(user: User): Promise<UserLoginActivityRecord | undefined> {
return await db<UserLoginActivityRecord>('user_login_activity')
async function getLoginActivity(user: User): Promise<UserLoginActivityRecord> {
const loginActivity = await db<UserLoginActivityRecord>('user_login_activity')
.where({ principal_id: user.id })
.first();

if (!loginActivity) throw new NotFound(`Login activity record for user with ID ${user.id} was not found.`);

return loginActivity;
}

async function ensureUserLoginActivityRecord(user: User): Promise<void> {
Expand Down Expand Up @@ -61,5 +66,5 @@ export async function isAccountLocked(user: User): Promise<boolean> {
await ensureUserLoginActivityRecord(user);

const loginActivity = await getLoginActivity(user);
return !!loginActivity?.account_locked;
return !!loginActivity.account_locked;
}

0 comments on commit 59e6339

Please sign in to comment.