nodebb-plugin-niki-loyalty 1.0.9 → 1.0.11

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.
Files changed (2) hide show
  1. package/library.js +59 -18
  2. package/package.json +1 -1
package/library.js CHANGED
@@ -12,6 +12,28 @@ const SETTINGS = {
12
12
  coffeeCost: 250
13
13
  };
14
14
 
15
+ // --- YARDIMCI: KASA GÜNLÜĞÜNE EKLE ---
16
+ async function addKasaLog(staffUid, customerName, customerPicture) {
17
+ const logEntry = {
18
+ ts: Date.now(),
19
+ staff: staffUid, // İşlemi yapan personel ID'si
20
+ cust: customerName,
21
+ img: customerPicture,
22
+ amt: SETTINGS.coffeeCost
23
+ };
24
+ // Kasa geçmişine ekle (Sonuna ekler)
25
+ await db.listAppend('niki:kasa:history', logEntry);
26
+ // Son 100 işlemi tut, gerisini sil (Veritabanı temizliği)
27
+ await db.listTrim('niki:kasa:history', -100, -1);
28
+ }
29
+
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
+
15
37
  Plugin.init = async function (params) {
16
38
  const router = params.router;
17
39
  const middleware = params.middleware;
@@ -35,14 +57,15 @@ Plugin.init = async function (params) {
35
57
  return res.json({ earned: true, points: SETTINGS.pointsPerHeartbeat, total: newBalance });
36
58
  });
37
59
 
38
- // 2. WALLET DATA (Bilgi Çekme)
60
+ // 2. WALLET DATA (Öğrenci Cüzdanı)
39
61
  router.get('/api/niki-loyalty/wallet-data', middleware.ensureLoggedIn, async (req, res) => {
40
62
  const uid = req.uid;
41
63
  const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
42
64
 
43
- const [userData, dailyData] = await Promise.all([
65
+ const [userData, dailyData, history] = await Promise.all([
44
66
  user.getUserFields(uid, ['niki_points']),
45
- db.getObject(`niki:daily:${uid}:${today}`)
67
+ db.getObject(`niki:daily:${uid}:${today}`),
68
+ db.getListRange(`niki:activity:${uid}`, 0, -1)
46
69
  ]);
47
70
 
48
71
  const currentPoints = parseInt(userData.niki_points) || 0;
@@ -54,53 +77,71 @@ Plugin.init = async function (params) {
54
77
  points: currentPoints,
55
78
  dailyScore: dailyScore,
56
79
  dailyCap: SETTINGS.dailyCap,
57
- dailyPercent: dailyPercent
80
+ dailyPercent: dailyPercent,
81
+ history: (history || []).reverse()
58
82
  });
59
83
  });
60
84
 
61
- // 3. QR TOKEN ÜRET (Öğrenci Butona Basınca)
85
+ // 3. QR TOKEN ÜRET
62
86
  router.post('/api/niki-loyalty/generate-qr', middleware.ensureLoggedIn, async (req, res) => {
63
87
  const uid = req.uid;
64
88
  const points = parseInt(await user.getUserField(uid, 'niki_points')) || 0;
65
-
66
- if (points < SETTINGS.coffeeCost) {
67
- return res.json({ success: false, message: 'Yetersiz Puan' });
68
- }
89
+ if (points < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Yetersiz Puan' });
69
90
 
70
- // Token Oluştur
71
91
  const token = Math.random().toString(36).substring(2) + Date.now().toString(36);
72
-
73
- // Kaydet (2 dakika geçerli)
74
92
  await db.set(`niki:qr:${token}`, uid);
75
93
  await db.expire(`niki:qr:${token}`, 120);
76
94
 
77
95
  return res.json({ success: true, token: token });
78
96
  });
79
97
 
80
- // 4. QR OKUTMA (Personel Tarayınca)
98
+ // 4. QR OKUTMA (KASA İŞLEMİ)
81
99
  router.post('/api/niki-loyalty/scan-qr', middleware.ensureLoggedIn, async (req, res) => {
82
100
  const { token } = req.body;
83
101
 
84
- // Sadece Admin/Mod yetkisi
102
+ // Yetki Kontrolü
85
103
  const isAdmin = await user.isAdministrator(req.uid);
86
104
  const isMod = await user.isGlobalModerator(req.uid);
87
105
  if (!isAdmin && !isMod) return res.status(403).json({ success: false, message: 'Yetkisiz' });
88
106
 
107
+ // Token Doğrula
89
108
  const customerUid = await db.get(`niki:qr:${token}`);
90
- if (!customerUid) return res.json({ success: false, message: 'Geçersiz Kod' });
109
+ if (!customerUid) return res.json({ success: false, message: 'Geçersiz/Süresi Dolmuş Kod' });
91
110
 
92
- // Puan Düş
111
+ // Puan Kontrolü
93
112
  const pts = parseInt(await user.getUserField(customerUid, 'niki_points')) || 0;
94
- if (pts < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Bakiye Yetersiz' });
113
+ if (pts < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Yetersiz Bakiye' });
95
114
 
115
+ // İŞLEMİ YAP
96
116
  await user.decrementUserFieldBy(customerUid, 'niki_points', SETTINGS.coffeeCost);
97
117
  await db.delete(`niki:qr:${token}`);
98
118
 
119
+ // LOGLARI GİR
99
120
  const customerData = await user.getUserFields(customerUid, ['username', 'picture']);
121
+
122
+ // 1. Öğrencinin Geçmişine Ekle
123
+ await addUserLog(customerUid, 'spend', SETTINGS.coffeeCost, 'Kahve Harcaması');
124
+
125
+ // 2. Kasa Defterine Ekle (YENİ)
126
+ await addKasaLog(req.uid, customerData.username, customerData.picture);
127
+
100
128
  return res.json({ success: true, customer: customerData, message: 'Onaylandı!' });
101
129
  });
102
130
 
103
- // 5. NIKI KASA SAYFASI (Erişim Kontrolü)
131
+ // 5. KASA GEÇMİŞİNİ GETİR (YENİ API)
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
104
145
  routeHelpers.setupPageRoute(router, '/niki-kasa', middleware, [], async (req, res) => {
105
146
  const isAdmin = await user.isAdministrator(req.uid);
106
147
  const isMod = await user.isGlobalModerator(req.uid);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-niki-loyalty",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Niki The Cat Coffee Loyalty System - Earn points while studying on IEU Forum.",
5
5
  "main": "library.js",
6
6
  "nbbpm": {