Skip to content

Commit

Permalink
update user
Browse files Browse the repository at this point in the history
  • Loading branch information
NdekoCode committed Dec 17, 2022
1 parent 05cfa25 commit d2a958d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions access.log
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
::ffff:127.0.0.1 - - [17/Dec/2022:09:55:26 +0000] "GET /api/v1/auth/users HTTP/1.1" 404 156
::ffff:127.0.0.1 - - [17/Dec/2022:09:55:45 +0000] "GET /api/v1/auth/users HTTP/1.1" 404 156
::ffff:127.0.0.1 - - [17/Dec/2022:13:20:22 +0000] "GET /api/v1/auth/users/delete/639dc0a9838708774a29b560 HTTP/1.1" 404 188
::ffff:127.0.0.1 - - [17/Dec/2022:16:52:56 +0000] "GET /api/v1/auth/users/update/639df31399979ea92eb6a52f HTTP/1.1" 404 188
::ffff:127.0.0.1 - - [17/Dec/2022:16:53:22 +0000] "GET /api/v1/auth/users/update/639df31399979ea92eb6a52f HTTP/1.1" 404 188
::ffff:127.0.0.1 - - [17/Dec/2022:16:53:51 +0000] "GET /api/v1/auth/users/update/639df31399979ea92eb6a52f HTTP/1.1" 404 188
16 changes: 14 additions & 2 deletions controllers/UsersCTRL.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { hash } from "bcrypt";
import slugify from "slugify";
import UserMDL from "../models/UserMDL.js";
import Alert from "../utils/Alert.js";
import { consoleError } from "../utils/utils.js";
import Validator from "../utils/Validator.js";
import { varIsEmpty } from "../utils/validators.js";

export default class UsersCTRL {
async getUsers(req, res) {
Expand Down Expand Up @@ -104,20 +106,30 @@ export default class UsersCTRL {
const validator = new Validator();
const _id = req.params.id;
const alert = new Alert(req, res);
const bodyRequest = { ...body };
const bodyRequest = { ...req.body };
validator.validateFormBody(bodyRequest);
if (!validator.varIsEmpty(validator.errors)) {
return alert.danger("Veuiller entrer tous les change", 400);
}
try {
const testUser = await UserMDL.exists({ _id });
if (testUser) {
const user = UserMDL.updateOne({ _id }, bodyRequest);
console.log(bodyRequest);
if (
!varIsEmpty(bodyRequest.lastName) ||
!varIsEmpty(bodyRequest.firstName)
) {
bodyRequest.slug = slugify(
`${bodyRequest.firstName} ${bodyRequest.lastName}`.toLowerCase()
);
}
await UserMDL.updateOne({ _id }, bodyRequest);
return alert.success("Utilisateur modifier avec succés", 201);
}

return alert.danger("L'utilisateur n'existe pas", 404);
} catch (error) {
consoleError(error);
return alert.danger(error.messsage, 500);
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Validator {
}
varIsEmpty(value) {
return this.isEmpty(value) || typeof "object"
? Object.keys(value).length < 1 || JSON.stringify(value) === "{}"
? JSON.stringify(value) === "{}"
: value.length < 1;
}
validateRequiredFields(data, fields) {
Expand Down
2 changes: 1 addition & 1 deletion utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function validatePassword(password, confpassword) {
}
export function varIsEmpty(value) {
return isEmpty(value) || typeof "object"
? Object.keys(value).length < 1 || JSON.stringify(value) === "{}"
? JSON.stringify(value) === "{}"
: value.length < 1;
}
export function validateRequiredFields(data, fields) {
Expand Down

0 comments on commit d2a958d

Please sign in to comment.