-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajoutAccount.js
More file actions
115 lines (99 loc) · 3.83 KB
/
Copy pathajoutAccount.js
File metadata and controls
115 lines (99 loc) · 3.83 KB
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
import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import path from "node:path";
import fs from "node:fs/promises";
import { colors } from '../utils/colors.js';
import { isValidName, isValidEmail, isValidPhone } from '../utils/validators.js';
export default async function ajoutAccount(rl = null) {
console.log("\n Ajout d'un compte : \n");
let ownRl = false;
if (!rl) {
rl = readline.createInterface({ input, output });
ownRl = true;
}
try {
const jsonFile = path.join(process.cwd(), "actions", "account.json");
let accounts = [];
try {
const data = await fs.readFile(jsonFile, "utf-8");
accounts = JSON.parse(data);
} catch (readErr) {
// Si le fichier n'existe pas, on part d'une liste vide (ou on pourrait gérer l'erreur autrement)
if (readErr.code !== 'ENOENT') {
throw readErr;
}
}
const id = await rl.question("Identifiant : ");
const password = await rl.question("Mot de passe : ");
const role = await demanderRole(rl);
let prenom;
while (true) {
prenom = await rl.question("Prenom : ");
if (isValidName(prenom)) break;
console.log(`${colors.red}Prénom invalide. Caractères interdits : < > * ? " / \\ : , ; .${colors.reset}`);
}
let nom;
while (true) {
nom = await rl.question("Nom : ");
if (isValidName(nom)) break;
console.log(`${colors.red}Nom invalide. Caractères interdits : < > * ? " / \\ : , ; .${colors.reset}`);
}
let email;
while (true) {
email = await rl.question("Email : ");
if (isValidEmail(email)) break;
console.log(`${colors.red}Email invalide. Format attendu : exemple@domaine.com${colors.reset}`);
}
let telephone;
while (true) {
telephone = await rl.question("Telephone : ");
if (isValidPhone(telephone)) break;
console.log(`${colors.red}Téléphone invalide. Utilisez uniquement des chiffres, espaces, +, -, (, ).${colors.reset}`);
}
const rue = await rl.question("Rue : ");
const ville = await rl.question("Ville : ");
const region = await rl.question("Region : ");
const codePostal = await rl.question("Code postal : ");
const newAccount = {
id,
password,
role,
prenom,
nom,
email,
telephone,
adresse: {
rue,
ville,
region,
codePostal
}
};
accounts.push(newAccount);
await fs.writeFile(jsonFile, JSON.stringify(accounts, null, 2), "utf8");
console.log("\n Le nouveau compte est enregistré");
if (role === "enseignant") {
console.log(`${colors.blue}Vous devrez retaper "1" si vous souhaitez générer le fichier VCard du nouvel enseignant enregistré.${colors.reset}`);
}
} catch (err) {
console.error(`${colors.red}Erreur lors de l'ajout du compte :${colors.reset}`, err);
} finally {
if (ownRl && rl) {
rl.close();
}
}
}
async function demanderRole(rl) {
while (true) {
const role = await rl.question(`Role 1 : enseignant / Rôle 2 : etudiant) :\n${colors.blue}(taper "1" si vous êtes enseignant, taper "2" si vous êtes étudiant)${colors.reset} `);
if (role === "1") {
return "enseignant";
}
else if (role === "2") {
return "etudiant";
}
else {
console.log(`${colors.red}Veuillez entrer un nombre valide. 1 pour enseignant | 2 pour étudiant.${colors.reset}`);
}
}
}