-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvouch-system.js
More file actions
160 lines (129 loc) · 3.77 KB
/
Copy pathvouch-system.js
File metadata and controls
160 lines (129 loc) · 3.77 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const fs = require('fs');
const path = require('path');
class VouchSystem {
constructor() {
this.vouchesFile = path.join(__dirname, 'data', 'vouches.json');
this.vouches = this.loadVouches();
}
loadVouches() {
try {
if (fs.existsSync(this.vouchesFile)) {
const data = fs.readFileSync(this.vouchesFile, 'utf8');
return JSON.parse(data);
}
} catch (error) {
console.error('Error loading vouches:', error);
}
return {};
}
saveVouches() {
try {
// Ensure data directory exists
const dataDir = path.dirname(this.vouchesFile);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
fs.writeFileSync(this.vouchesFile, JSON.stringify(this.vouches, null, 2));
} catch (error) {
console.error('Error saving vouches:', error);
}
}
addVouch(userId, voucherId, message, type = 'positive') {
if (!this.vouches[userId]) {
this.vouches[userId] = {
positive: [],
negative: [],
total: 0,
score: 0
};
}
const vouch = {
id: Date.now().toString(),
voucherId: voucherId,
message: message,
timestamp: Date.now(),
type: type
};
this.vouches[userId][type].push(vouch);
this.updateScore(userId);
this.saveVouches();
return vouch;
}
removeVouch(userId, vouchId) {
if (!this.vouches[userId]) return false;
const positive = this.vouches[userId].positive;
const negative = this.vouches[userId].negative;
let removed = false;
// Check positive vouches
const posIndex = positive.findIndex(v => v.id === vouchId);
if (posIndex !== -1) {
positive.splice(posIndex, 1);
removed = true;
}
// Check negative vouches
const negIndex = negative.findIndex(v => v.id === vouchId);
if (negIndex !== -1) {
negative.splice(negIndex, 1);
removed = true;
}
if (removed) {
this.updateScore(userId);
this.saveVouches();
}
return removed;
}
updateScore(userId) {
if (!this.vouches[userId]) return;
const positive = this.vouches[userId].positive.length;
const negative = this.vouches[userId].negative.length;
this.vouches[userId].total = positive + negative;
this.vouches[userId].score = positive - negative;
}
getUserVouches(userId) {
return this.vouches[userId] || {
positive: [],
negative: [],
total: 0,
score: 0
};
}
getAllVouches() {
return this.vouches;
}
getTopVouched(limit = 10) {
const users = Object.entries(this.vouches)
.map(([userId, data]) => ({ userId, ...data }))
.sort((a, b) => b.score - a.score)
.slice(0, limit);
return users;
}
canVouch(voucherId, targetId) {
// Users cannot vouch for themselves
if (voucherId === targetId) {
return { canVouch: false, reason: 'You cannot vouch for yourself.' };
}
// Check if user has already vouched for this person
const targetVouches = this.getUserVouches(targetId);
const hasVouched = [...targetVouches.positive, ...targetVouches.negative]
.some(v => v.voucherId === voucherId);
if (hasVouched) {
return { canVouch: false, reason: 'You have already vouched for this user.' };
}
return { canVouch: true };
}
getVouchById(userId, vouchId) {
if (!this.vouches[userId]) return null;
const positive = this.vouches[userId].positive.find(v => v.id === vouchId);
const negative = this.vouches[userId].negative.find(v => v.id === vouchId);
return positive || negative || null;
}
clearUserVouches(userId) {
if (this.vouches[userId]) {
delete this.vouches[userId];
this.saveVouches();
return true;
}
return false;
}
}
module.exports = VouchSystem;