-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
164 lines (122 loc) · 3.75 KB
/
models.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { randomUUID } from "node:crypto";
import { createUserObject, createUpdateUserObject } from "./utils/createUserObject.js";
import { validateEmail } from "./utils/validateEmail.js";
import { hashPassword } from "./utils/hashPassword.js";
import { handleError } from "./utils/handleError.js";
import dotenv from "dotenv";
// Variables de entorno
dotenv.config();
const PATH_USERS_FILE = process.env.PATH_USERS_FILE;
const PATH_USERS_ERROR = process.env.PATH_USERS_ERROR;
// Métodos
// Traer la lista de Usuarios
const getUsers = (urlFile) => {
try {
if (!urlFile) {
throw new Error("Access denied");
}
const existsFile = existsSync(urlFile);
if (!existsFile) {
writeFileSync(PATH_USERS_FILE, JSON.stringify([]));
return []
}
const users = JSON.parse(readFileSync(PATH_USERS_FILE));
return users;
}
catch (error) {
const objError = handleError(error, PATH_USERS_ERROR);
return objError;
}
};
// Buscar un Usuario por su ID
const getUserById = (id) => {
try {
if (!id) {
throw new Error("An ID is required");
}
const users = getUsers(PATH_USERS_FILE);
const user = users.find((user) => user.id === id);
if (!user) {
throw new Error("User doesn't exist, try a valid ID");
}
} catch (error) {
const objError = handleError(error, PATH_USERS_ERROR);
return objError;
}
};
// Agregar nuevo Usuario
const addUser = (userData) => {
try {
const { nombre, apellido, email, password } = userData;
if ( !nombre || !apellido || !email || !password ) {
throw new Error ("Missing data")
}
if (typeof(nombre || apellido || email !== string)) {
throw new Error ("Invalid type of data, must be a string")
}
validateEmail(email, users);
const newUser = {
id: randomUUID,
nombre,
apellido,
email,
password: hashPassword,
loggedIn: false
}
users.push(newUser);
writeFileSync(PATH_USERS_FILE, JSON.stringify(users));
return newUser;
} catch (error) {
const objError = handleError(error, PATH_USERS_ERROR);
return objError;
}
};
// Actualizar un Usuario buscando por ID
const updateUser = (id, userData) => {
try {
const { id, nombre, apellido, email, password } = userData;
if (!id || !userData) {
throw new Error("ID or data missing");
}
const users = getUsers();
const user = users.find((user) => user.id === id);
if (!user) {
throw new Error ("User not found")
}
const updateData = createUpdateUserObject(userData);
if (updateData.nombre) user.nombre = updateData.nombre;
if (updateData.apellido) user.apellido = updateData.apellido;
if (updateData.email) {
validateEmail(updateData.email, users);
user.email = updateData.email;
}
if (userData.password) {
user.password = hashPassword(userData.password);
}
writeFileSync(PATH_USERS_FILE, JSON.stringify(users));
return user;
} catch (error) {
const objError = handleError(error, PATH_USERS_ERROR);
return objError;}
};
// Borrar un Usuario buscando por su ID
const deleteUser = (id) => {
try {
if (!id) {
throw new Error("ID is missing");
}
const users = getUsers(PATH_FILE_USER);
const userToDelete = getUserById(id);
if (!userToDelete) {
throw new Error("User not found");
}
const filterUsers = users.filter((user) => user.id !== id);
writeFileSync(PATH_FILE_USER, JSON.stringify(filterUsers));
return userToDelete;
} catch (error) {
const objError = handleError(error, PATH_FILE_ERROR);
return objError;
}
};
export { getUsers, getUserById, addUser, updateUser, deleteUser };