-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajoutAccount.js
More file actions
80 lines (71 loc) · 2.69 KB
/
Copy pathajoutAccount.js
File metadata and controls
80 lines (71 loc) · 2.69 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
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 {hashPassword} from './passwordManager.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");
const data = await fs.readFile(jsonFile, "utf-8");
const accounts = JSON.parse(data);
const id = await rl.question("Identifiant : ");
const password = await hashPassword(await rl.question("Mot de passe : "));
const role = await demanderRole(rl);
const prenom = await rl.question("Prenom : ");
const nom = await rl.question("Nom : ");
const email = await rl.question("Email : ");
const telephone = await rl.question("Telephone : ");
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}`);
}
}
}