Skip to content

Commit

Permalink
refactor: remove unused code and enhance redability
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaysah101 committed Sep 15, 2024
1 parent edf688c commit 7dd105b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/api/v1/token/token.dal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TokenModel } from './token.model';
import { CreateToken, Token } from './token.validation';
import { CreateToken, Token, UpdateToken } from './token.validation';

interface ITokenDal {
saveToken(payload: Token): Promise<CreateToken>;
Expand All @@ -23,7 +23,7 @@ export class TokenDAL implements ITokenDal {
return await TokenModel.find({ userId });
}

async updateToken(tokenId: string, payload: Token): Promise<Token | null> {
async updateToken(tokenId: string, payload: UpdateToken): Promise<Token | null> {
return await TokenModel.findByIdAndUpdate(tokenId, payload, { new: true });
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/v1/token/token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const TokenSchema: Schema<Token> = new Schema(
requestAttempts: {
type: Number,
default: 0,
}, // Track how many times the user has requested a token
},
metadata: {
type: Object, // Optional, for additional data like IP address, user agent, etc.
type: Object,
},
},
{ timestamps: true },
Expand Down
27 changes: 2 additions & 25 deletions src/api/v1/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,8 @@ export class TokenService {
throw new Error(ErrorTypeEnum.enum.TOKEN_EXPIRED);
}

return verifiedToken;
}

async markTokenAsUsed(token: string) {
const storedToken = await this.tokenDAL.getToken(token);
await this.tokenDAL.updateToken(storedToken.id, { isUsed: true });

if (!storedToken) {
throw new Error(ErrorTypeEnum.enum.INVALID_ACCESS);
}

storedToken.isUsed = true;
await this.tokenDAL.updateToken(storedToken.id as string, storedToken);
return verifiedToken;
}

// Method to handle rate-limiting and request attempts
// async handleRateLimiting(userId: string, tokenType: Token['tokenType']) {
// const tokens = await this.tokenDAL.getTokensByUserId(userId);

// const relevantTokens = tokens?.filter((token) => token.tokenType === tokenType);

// if (relevantTokens?.length && relevantTokens.length > 5) {
// // For example, max 5 attempts
// throw new Error(ErrorTypeEnum.enum.RATE_LIMIT_EXCEEDED);
// }

// // You can also set a specific timeframe for the token requests (e.g., 5 tokens in 1 hour)
// }
}
17 changes: 12 additions & 5 deletions src/api/v1/token/token.validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { objectIdSchema } from '../../../utils';
import { objectIdSchema } from '@/utils';

export enum TokenAction {
'resetPassword' = 'resetPassword',
Expand All @@ -25,9 +25,16 @@ export const createTokenSchema = tokenSchema.pick({
expiryTime: true,
});

export const updateTokenSchema = tokenSchema
.pick({ isUsed: true, requestAttempts: true })
.partial()
.refine((data) => Object.keys(data).length > 0, {
message: 'At least one field is required for token update',
});

export type Token = z.infer<typeof tokenSchema>;
export type CreateToken = z.infer<typeof createTokenSchema>;
export type getToken = z.infer<typeof tokenSchema>;
export type updateToken = z.infer<typeof tokenSchema>;
export type deleteToken = z.infer<typeof tokenSchema>;
export type getTokens = z.infer<typeof tokenSchema>;
export type GetToken = z.infer<typeof tokenSchema>;
export type UpdateToken = z.infer<typeof updateTokenSchema>;
export type DeleteToken = z.infer<typeof tokenSchema>;
export type GetTokens = z.infer<typeof tokenSchema>;
12 changes: 4 additions & 8 deletions src/api/v1/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export class UserService {
}

static async getUserByEmail(email: Email): Promise<GetUser> {
const validEmail = validateEmail(email);
validateEmail(email);

const user = await UserDAL.getUserByEmail(validEmail);
const user = await UserDAL.getUserByEmail(email);

if (!user) throw new Error(ErrorTypeEnum.enum.USER_NOT_FOUND);

Expand Down Expand Up @@ -111,13 +111,9 @@ export class UserService {
}

static async updateUser(userId: string, userData: UpdateUser): Promise<GetUser> {
const validateData = validateObjectId(userId);

const user = await UserDAL.getUserById(validateData);

if (!user) throw new Error(ErrorTypeEnum.enum.USER_NOT_FOUND);
validateObjectId(userId);

const updatedUser = await UserDAL.updateUser(validateData, userData);
const updatedUser = await UserDAL.updateUser(userId, userData);

if (!updatedUser) throw new Error(ErrorTypeEnum.enum.INTERNAL_SERVER_ERROR);

Expand Down

0 comments on commit 7dd105b

Please sign in to comment.