-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvCard.js
More file actions
145 lines (128 loc) · 4.46 KB
/
vCard.js
File metadata and controls
145 lines (128 loc) · 4.46 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
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
const fs = require('fs');
const path = require('path');
class VCard {
/**
* Crée une nouvelle instance de VCard.
* @param {string} nom - Le nom de l'utilisateur.
* @param {string} prenom - Le prénom de l'utilisateur.
* @param {string} email - L'adresse e-mail de l'utilisateur.
* @param {string} telephone - Le numéro de téléphone de l'utilisateur.
* @param {string} organisation - Le nom de l'organisation (facultatif).
*/
constructor(nom, prenom, email, telephone, organisation = "SRYEM") {
if (!nom || !prenom || !email || !telephone) {
throw new Error("Tous les champs obligatoires doivent être fournis.");
}
if (!this.validerEmail(email)) {
throw new Error("Adresse e-mail invalide.");
}
this.nom = nom;
this.prenom = prenom;
this.email = email;
this.telephone = telephone;
this.organisation = organisation;
}
/**
* Valide le format d'une adresse e-mail.
* @param {string} email - L'adresse e-mail à valider.
* @returns {boolean} - True si l'adresse e-mail est valide, sinon False.
*/
validerEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
/**
* Génère la représentation VCard de l'utilisateur.
* @returns {string} - La VCard au format texte.
*/
genererVCard() {
return `BEGIN:VCARD
VERSION:4.0
FN:${this.prenom} ${this.nom}
EMAIL:${this.email}
TEL:${this.telephone}
ORG:${this.organisation}
END:VCARD`;
}
/**
* Enregistre la VCard dans un fichier.
* @param {string} chemin - Le chemin du fichier VCard.
*/
enregistrerVCard(chemin) {
fs.writeFileSync(chemin, this.genererVCard());
}
}
class GestionVCard {
constructor() {
// Chemins spécifiques
this.dossierData = path.resolve('data');
this.dossierVCard = path.join(this.dossierData, 'vCard');
this.fichierEmails = path.join(this.dossierData, 'emails_utilises.json');
// Assurer que les dossiers existent
this.creerDossiersSiNecessaire();
// Charger le stockage des e-mails
this.chargerStockage();
}
/**
* Crée les dossiers nécessaires si absents.
*/
creerDossiersSiNecessaire() {
if (!fs.existsSync(this.dossierData)) {
fs.mkdirSync(this.dossierData, { recursive: true });
}
if (!fs.existsSync(this.dossierVCard)) {
fs.mkdirSync(this.dossierVCard, { recursive: true });
}
}
/**
* Charge le fichier de stockage des adresses e-mail.
*/
chargerStockage() {
if (fs.existsSync(this.fichierEmails)) {
const contenu = fs.readFileSync(this.fichierEmails, 'utf-8');
this.emailsUtilises = JSON.parse(contenu);
} else {
this.emailsUtilises = [];
}
}
/**
* Sauvegarde les adresses e-mail utilisées dans le fichier de stockage.
*/
sauvegarderStockage() {
fs.writeFileSync(this.fichierEmails, JSON.stringify(this.emailsUtilises, null, 2));
}
/**
* Vérifie si une adresse e-mail est déjà utilisée.
* @param {string} email - L'adresse e-mail à vérifier.
* @returns {boolean} - True si l'e-mail est déjà utilisé, sinon False.
*/
verifierEmail(email) {
return this.emailsUtilises.includes(email);
}
/**
* Ajoute une adresse e-mail au fichier de stockage.
* @param {string} email - L'adresse e-mail à ajouter.
*/
ajouterEmail(email) {
if (this.verifierEmail(email)) {
throw new Error(`L'adresse e-mail ${email} est déjà utilisée.`);
}
this.emailsUtilises.push(email);
this.sauvegarderStockage();
}
/**
* Génère et sauvegarde une VCard après vérification des informations.
* @param {VCard} vcard - L'instance de VCard à enregistrer.
* @param {string} nomFichier - Nom du fichier vCard (sans extension).
*/
genererEtSauvegarder(vcard, nomFichier) {
if (this.verifierEmail(vcard.email)) {
throw new Error(`L'adresse e-mail ${vcard.email} est déjà utilisée.`);
}
const cheminFichier = path.join(this.dossierVCard, `${nomFichier}.vcf`);
vcard.enregistrerVCard(cheminFichier);
this.ajouterEmail(vcard.email);
console.log(`VCard générée et sauvegardée : ${cheminFichier}`);
}
}
module.exports = { VCard, GestionVCard };