Skip to content

Commit

Permalink
refactor: rest of the error messages translated to spanish
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Apr 16, 2024
1 parent 12b92ac commit be604ef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
12 changes: 7 additions & 5 deletions src/presentation/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class AuthMiddleware {

// Check if refresh token and access token are present
if (!refreshToken && !accessToken)
throw new UnauthenticatedError('Please first login');
throw new UnauthenticatedError('Por favor loguea con tu cuenta');

if (accessToken) {
// Validate access token
const payload = this.jwt.validateToken(accessToken);
if (!payload) throw new UnauthorizedError('Invalid token validation');
if (!payload) throw new UnauthorizedError('Token JWT inválido');
req.user = payload.user;
const wsToken = this.jwt.generateToken({ user: payload.user }, '1d');
req.user.wsToken = wsToken;
Expand All @@ -42,15 +42,15 @@ export class AuthMiddleware {

// Validate refresh token
const payload = this.jwt.validateToken(refreshToken);
if (!payload) throw new UnauthorizedError('Invalid token validation');
if (!payload) throw new UnauthorizedError('Token JWT inválido');

const existingToken = await prisma.token.findUnique({
where: { userId: payload.user.id, refreshToken: payload.refreshToken },
});

// Check if existing token is valid
if (!existingToken || !existingToken?.isValid)
throw new UnauthenticatedError('Authentication Invalid');
throw new UnauthenticatedError('No autentificado');

const { user } = payload;

Expand Down Expand Up @@ -84,7 +84,9 @@ export class AuthMiddleware {
if (role === 'admin') return next();

if (!roles.includes(role!))
throw new UnauthorizedError('Unauthorized to access this resource');
throw new UnauthorizedError(
'No estás autorizado para acceder a este recurso'
);

next();
};
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/middlewares/error-handler.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ErrorHandlerMiddleware {
// Handle jwt expired errors
if (err?.name && err?.name === 'TokenExpiredError') {
statusCode = 401;
message = 'JWT token expired';
message = 'Sin autentificar';
}

// Handle CustomAPIError
Expand Down
6 changes: 3 additions & 3 deletions src/presentation/middlewares/file-upload.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class FileUploadMiddleware {
include: { shelter: { select: { images: true } } },
});

if (!user) throw new NotFoundError('User not found');
if (!user) throw new NotFoundError('Usuario no encontrado');

name = user.username;
id = user.id;
Expand All @@ -89,15 +89,15 @@ export class FileUploadMiddleware {
name: true,
},
});
if (!animal) throw new NotFoundError('Animal not found');
if (!animal) throw new NotFoundError('Animal no encontrado');
name = animal.name;
id = animal.createdBy;
resourceId = animal.id;
break;

default:
throw new BadRequestError(
`Invalid resource: ${resource}, valid ones: users, animals`
`Recurso invalido: ${resource}, validos: users, animals`
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/presentation/middlewares/not-found.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class NotFoundMiddleware {
static init(req: Request, res: Response) {
res
.status(HttpCodes.NOT_FOUND)
.json({ message: `Route ${req.url} not found` });
.json({ message: `Ruta ${req.url} no encontrada` });
}
}
28 changes: 15 additions & 13 deletions src/presentation/users/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ export class UserService {
if (city) {
filters.cityId = city.id;
} else {
throw new NotFoundError(`City ${shelterFilterDto.city} not found`);
throw new NotFoundError(
`Ciudad ${shelterFilterDto.city} no encontrada`
);
}
}

Expand Down Expand Up @@ -309,7 +311,7 @@ export class UserService {
},
});

if (!user) throw new NotFoundError('User not found');
if (!user) throw new NotFoundError('Usuario no encontrado');

const userEntity = UserEntity.fromObject(user);

Expand Down Expand Up @@ -371,7 +373,7 @@ export class UserService {
public async getSingleUser(term: string) {
const user = await this.getUserFromTerm(term);

if (!user) throw new NotFoundError('User not found');
if (!user) throw new NotFoundError('Usuario no encontrado');

const userEntity = UserEntity.fromObject(user);

Expand Down Expand Up @@ -407,7 +409,7 @@ export class UserService {
}),
]);

if (!userToDelete) throw new NotFoundError('User not found');
if (!userToDelete) throw new NotFoundError('Usuario no encontrado');

CheckPermissions.check(user, userToDelete.id);

Expand Down Expand Up @@ -465,14 +467,14 @@ export class UserService {
) {
const user = await prisma.user.findUnique({ where: { id: userId } });

if (!user) throw new NotFoundError('User not found');
if (!user) throw new NotFoundError('Usuario no encontrado');

if (user) {
const isValid = prisma.user.validatePassword({
password: oldPassword,
hash: user.password,
});
if (!isValid) throw new BadRequestError('Invalid password');
if (!isValid) throw new BadRequestError('Password no valido');
const hashPassword = prisma.user.hashPassword(newPassword);
await prisma.user.update({
where: { id: userId },
Expand All @@ -495,7 +497,7 @@ export class UserService {
where: { email: user.email },
});

if (!userFound) throw new NotFoundError('User or shelter not found');
if (!userFound) throw new NotFoundError('Usuaro o refugio no encontrados');

CheckPermissions.check(user, userFound.id);

Expand Down Expand Up @@ -539,7 +541,7 @@ export class UserService {
where: { email: user.email },
});

if (!userToUpdate) throw new NotFoundError('User not found');
if (!userToUpdate) throw new NotFoundError('Usuario no encontrado');

CheckPermissions.check(user, userToUpdate.id);

Expand Down Expand Up @@ -624,7 +626,7 @@ export class UserService {
},
});

if (!userToUpdate) throw new NotFoundError('User not found');
if (!userToUpdate) throw new NotFoundError('Usuario no encontrado');

let updateQuery: {
avatar?: string[];
Expand All @@ -633,7 +635,7 @@ export class UserService {

if (userToUpdate.role === 'shelter') {
if (!userToUpdate.shelter)
throw new NotFoundError('Shelter not found for user');
throw new NotFoundError('El usuario no es un refugio');

const images = userToUpdate.shelter.images;
const resultImages = await this.buildImages(images, deleteImages, files);
Expand Down Expand Up @@ -852,9 +854,9 @@ export class UserService {
},
});

if (!notification) throw new NotFoundError('Notification not found');
if (!notification) throw new NotFoundError('Notificacion no encontrada');
if (notification.isRead)
throw new BadRequestError('Notification is already read');
throw new BadRequestError('La notificacion ya ha sido leida');

CheckPermissions.check(user, notification.userId);

Expand All @@ -878,7 +880,7 @@ export class UserService {
},
});

if (!notification) throw new NotFoundError('Notification not found');
if (!notification) throw new NotFoundError('Notificacion no encontrada');

CheckPermissions.check(user, notification.userId);

Expand Down

0 comments on commit be604ef

Please sign in to comment.