Skip to content

feature/#38 Added reset password endpoint #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/custom-error/custom-error.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum CustomInternalCodes {
UserNotFound = 'user-002',
RolNotFound = 'user-003',
UnidadNotFound = 'user-004',
InvalidPassword = 'user-005',
}

export interface Error {
Expand Down
6 changes: 6 additions & 0 deletions src/pods/user/user.api-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ export interface SaveUserParams {
hashedPassword: string;
isTemporalPassword: boolean;
}

export interface ChangePasswordParams {
id: string;
contraseñaActual: string;
nuevaContraseña: string;
}
36 changes: 35 additions & 1 deletion src/pods/user/user.rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
mapUserFromApiToModel,
mapUserUpdateFromApiToModel,
} from './user.mappers.js';
import { validationPostUser, validationUpdateUser } from './validations/index.js';
import { validationPostUser, validationUpdateUser, validationChangePassword } from './validations/index.js';
import * as apiModel from './user.api-model.js';
import * as model from '#dals/user/user.model.js';

Expand Down Expand Up @@ -116,4 +116,38 @@ userApi
} catch (error) {
next(error);
}
})
.post('/change-password', async (req, res, next) => {
try {
const passwordData: apiModel.ChangePasswordParams = req.body;
const validationResult = await validationChangePassword(passwordData);

if (validationResult.succeded) {
const hashedPassword = await hash(passwordData.nuevaContraseña);
const user = await userRepository.getUser(passwordData.id);
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This call fetches the user again after validation already retrieved it; you could return the user from the validation step to reduce an extra database query.

Copilot uses AI. Check for mistakes.


await userRepository.saveUser({
...user,
contraseña: hashedPassword,
esContraseñaTemporal: false,
});

res.sendStatus(204);
} else {
const statusCode = (() => {
switch (validationResult.error?.error) {
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FieldNotInformed case currently falls through to the default status 400; mapping it explicitly to 422 (like UserNotFound) may better communicate input validation errors.

Copilot uses AI. Check for mistakes.

case CustomInternalCodes.UserNotFound:
return 422;
case CustomInternalCodes.InvalidPassword:
return 401;
default:
return 400;
}
})();

res.status(statusCode).send(validationResult.error);
}
} catch (error) {
next(error);
}
});
1 change: 1 addition & 0 deletions src/pods/user/validations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './user-validations.js';
export * from './update-user-validation.js';
export * from './password-validation.js';
24 changes: 24 additions & 0 deletions src/pods/user/validations/password-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CustomInternalCodes, ValidationInfo } from '#common/custom-error/index.js';
import { verifyHash } from '#common/helpers/index.js';
import { userRepository } from '#dals/user/user.repository.js';
import * as apiModel from '../user.api-model.js';

export const validationChangePassword = async (
passwordData: apiModel.ChangePasswordParams
): Promise<ValidationInfo> => {
if (!passwordData.id || !passwordData.contraseñaActual || !passwordData.nuevaContraseña) {
return { succeded: false, error: { error: CustomInternalCodes.FieldNotInformed } };
}

const user = await userRepository.getUser(passwordData.id);
if (!user) {
return { succeded: false, error: { error: CustomInternalCodes.UserNotFound } };
}

const isValidPassword = await verifyHash(passwordData.contraseñaActual, user.contraseña);
if (!isValidPassword) {
return { succeded: false, error: { error: CustomInternalCodes.InvalidPassword } };
}

return { succeded: true };
Comment on lines +10 to +23
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property succeded appears misspelled; it should likely be succeeded to match common spelling and avoid mismatches with the ValidationInfo interface.

Suggested change
return { succeded: false, error: { error: CustomInternalCodes.FieldNotInformed } };
}
const user = await userRepository.getUser(passwordData.id);
if (!user) {
return { succeded: false, error: { error: CustomInternalCodes.UserNotFound } };
}
const isValidPassword = await verifyHash(passwordData.contraseñaActual, user.contraseña);
if (!isValidPassword) {
return { succeded: false, error: { error: CustomInternalCodes.InvalidPassword } };
}
return { succeded: true };
return { succeeded: false, error: { error: CustomInternalCodes.FieldNotInformed } };
}
const user = await userRepository.getUser(passwordData.id);
if (!user) {
return { succeeded: false, error: { error: CustomInternalCodes.UserNotFound } };
}
const isValidPassword = await verifyHash(passwordData.contraseñaActual, user.contraseña);
if (!isValidPassword) {
return { succeeded: false, error: { error: CustomInternalCodes.InvalidPassword } };
}
return { succeeded: true };

Copilot uses AI. Check for mistakes.

};