nodebb-plugin-niki-loyalty 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library.js +49 -41
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -12,28 +12,32 @@ const SETTINGS = {
|
|
|
12
12
|
coffeeCost: 250
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
// ---
|
|
16
|
-
async function
|
|
15
|
+
// --- LOG FONKSİYONLARI ---
|
|
16
|
+
async function addUserLog(uid, type, amount, desc) {
|
|
17
17
|
const logEntry = {
|
|
18
18
|
ts: Date.now(),
|
|
19
|
-
|
|
19
|
+
type: type, // 'earn' veya 'spend'
|
|
20
|
+
amt: amount,
|
|
21
|
+
txt: desc
|
|
22
|
+
};
|
|
23
|
+
// Listeye ekle ve son 50 işlemi tut
|
|
24
|
+
await db.listAppend(`niki:activity:${uid}`, logEntry);
|
|
25
|
+
await db.listTrim(`niki:activity:${uid}`, -50, -1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function addKasaLog(staffUid, customerName, customerUid) {
|
|
29
|
+
const logEntry = {
|
|
30
|
+
ts: Date.now(),
|
|
31
|
+
staff: staffUid,
|
|
20
32
|
cust: customerName,
|
|
21
|
-
|
|
33
|
+
cuid: customerUid,
|
|
22
34
|
amt: SETTINGS.coffeeCost
|
|
23
35
|
};
|
|
24
|
-
// Kasa geçmişine ekle
|
|
36
|
+
// Kasa geçmişine ekle ve son 100 işlemi tut
|
|
25
37
|
await db.listAppend('niki:kasa:history', logEntry);
|
|
26
|
-
// Son 100 işlemi tut, gerisini sil (Veritabanı temizliği)
|
|
27
38
|
await db.listTrim('niki:kasa:history', -100, -1);
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
// --- YARDIMCI: ÖĞRENCİ GÜNLÜĞÜNE EKLE ---
|
|
31
|
-
async function addUserLog(uid, type, amount, desc) {
|
|
32
|
-
const logEntry = { timestamp: Date.now(), type: type, amount: amount, desc: desc };
|
|
33
|
-
await db.listAppend(`niki:activity:${uid}`, logEntry);
|
|
34
|
-
await db.listTrim(`niki:activity:${uid}`, -50, -1);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
41
|
Plugin.init = async function (params) {
|
|
38
42
|
const router = params.router;
|
|
39
43
|
const middleware = params.middleware;
|
|
@@ -57,7 +61,7 @@ Plugin.init = async function (params) {
|
|
|
57
61
|
return res.json({ earned: true, points: SETTINGS.pointsPerHeartbeat, total: newBalance });
|
|
58
62
|
});
|
|
59
63
|
|
|
60
|
-
// 2. WALLET DATA (
|
|
64
|
+
// 2. WALLET DATA (Cüzdan Bilgisi + Geçmiş)
|
|
61
65
|
router.get('/api/niki-loyalty/wallet-data', middleware.ensureLoggedIn, async (req, res) => {
|
|
62
66
|
const uid = req.uid;
|
|
63
67
|
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
@@ -78,14 +82,34 @@ Plugin.init = async function (params) {
|
|
|
78
82
|
dailyScore: dailyScore,
|
|
79
83
|
dailyCap: SETTINGS.dailyCap,
|
|
80
84
|
dailyPercent: dailyPercent,
|
|
81
|
-
history: (history || []).reverse()
|
|
85
|
+
history: (history || []).reverse() // En yeniden eskiye
|
|
82
86
|
});
|
|
83
87
|
});
|
|
84
88
|
|
|
85
|
-
// 3.
|
|
89
|
+
// 3. KASA GEÇMİŞİ (Staff Dashboard)
|
|
90
|
+
router.get('/api/niki-loyalty/kasa-history', middleware.ensureLoggedIn, async (req, res) => {
|
|
91
|
+
const isAdmin = await user.isAdministrator(req.uid);
|
|
92
|
+
const isMod = await user.isGlobalModerator(req.uid);
|
|
93
|
+
|
|
94
|
+
if (!isAdmin && !isMod) return res.status(403).json([]);
|
|
95
|
+
|
|
96
|
+
const history = await db.getListRange('niki:kasa:history', 0, -1);
|
|
97
|
+
|
|
98
|
+
// Kullanıcı resimlerini de çekmek için zenginleştirme yapalım
|
|
99
|
+
const enrichedHistory = await Promise.all((history || []).reverse().map(async (item) => {
|
|
100
|
+
const uData = await user.getUserFields(item.cuid, ['picture']);
|
|
101
|
+
item.picture = uData.picture;
|
|
102
|
+
return item;
|
|
103
|
+
}));
|
|
104
|
+
|
|
105
|
+
res.json(enrichedHistory);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// 4. QR OLUŞTUR
|
|
86
109
|
router.post('/api/niki-loyalty/generate-qr', middleware.ensureLoggedIn, async (req, res) => {
|
|
87
110
|
const uid = req.uid;
|
|
88
111
|
const points = parseInt(await user.getUserField(uid, 'niki_points')) || 0;
|
|
112
|
+
|
|
89
113
|
if (points < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Yetersiz Puan' });
|
|
90
114
|
|
|
91
115
|
const token = Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
@@ -95,53 +119,37 @@ Plugin.init = async function (params) {
|
|
|
95
119
|
return res.json({ success: true, token: token });
|
|
96
120
|
});
|
|
97
121
|
|
|
98
|
-
//
|
|
122
|
+
// 5. QR OKUT (Ödeme Alma)
|
|
99
123
|
router.post('/api/niki-loyalty/scan-qr', middleware.ensureLoggedIn, async (req, res) => {
|
|
100
124
|
const { token } = req.body;
|
|
101
125
|
|
|
102
|
-
// Yetki Kontrolü
|
|
103
126
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
104
127
|
const isMod = await user.isGlobalModerator(req.uid);
|
|
105
128
|
if (!isAdmin && !isMod) return res.status(403).json({ success: false, message: 'Yetkisiz' });
|
|
106
129
|
|
|
107
|
-
// Token Doğrula
|
|
108
130
|
const customerUid = await db.get(`niki:qr:${token}`);
|
|
109
|
-
if (!customerUid) return res.json({ success: false, message: 'Geçersiz
|
|
131
|
+
if (!customerUid) return res.json({ success: false, message: 'Geçersiz Kod' });
|
|
110
132
|
|
|
111
|
-
// Puan Kontrolü
|
|
112
133
|
const pts = parseInt(await user.getUserField(customerUid, 'niki_points')) || 0;
|
|
113
134
|
if (pts < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Yetersiz Bakiye' });
|
|
114
135
|
|
|
115
|
-
//
|
|
136
|
+
// İŞLEM
|
|
116
137
|
await user.decrementUserFieldBy(customerUid, 'niki_points', SETTINGS.coffeeCost);
|
|
117
138
|
await db.delete(`niki:qr:${token}`);
|
|
118
139
|
|
|
119
|
-
//
|
|
140
|
+
// LOGLAMA (Detaylı metin ile)
|
|
120
141
|
const customerData = await user.getUserFields(customerUid, ['username', 'picture']);
|
|
121
142
|
|
|
122
|
-
//
|
|
123
|
-
await addUserLog(customerUid, 'spend', SETTINGS.coffeeCost, 'Kahve
|
|
143
|
+
// Öğrenciye: "Kahve Keyfi" yazsın
|
|
144
|
+
await addUserLog(customerUid, 'spend', SETTINGS.coffeeCost, 'Kahve Keyfi ☕');
|
|
124
145
|
|
|
125
|
-
//
|
|
126
|
-
await addKasaLog(req.uid, customerData.username,
|
|
146
|
+
// Kasaya: Kimin aldığı yazsın
|
|
147
|
+
await addKasaLog(req.uid, customerData.username, customerUid);
|
|
127
148
|
|
|
128
149
|
return res.json({ success: true, customer: customerData, message: 'Onaylandı!' });
|
|
129
150
|
});
|
|
130
151
|
|
|
131
|
-
//
|
|
132
|
-
router.get('/api/niki-loyalty/kasa-history', middleware.ensureLoggedIn, async (req, res) => {
|
|
133
|
-
const isAdmin = await user.isAdministrator(req.uid);
|
|
134
|
-
const isMod = await user.isGlobalModerator(req.uid);
|
|
135
|
-
if (!isAdmin && !isMod) return res.status(403).json([]);
|
|
136
|
-
|
|
137
|
-
// Tüm geçmişi çek
|
|
138
|
-
const history = await db.getListRange('niki:kasa:history', 0, -1);
|
|
139
|
-
|
|
140
|
-
// Ters çevir (En yeni en üstte)
|
|
141
|
-
res.json((history || []).reverse());
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// SAYFA ROUTE
|
|
152
|
+
// SAYFA ROTASI
|
|
145
153
|
routeHelpers.setupPageRoute(router, '/niki-kasa', middleware, [], async (req, res) => {
|
|
146
154
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
147
155
|
const isMod = await user.isGlobalModerator(req.uid);
|