nodebb-plugin-niki-loyalty 1.0.7 → 1.0.9

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 +56 -12
  2. package/package.json +1 -1
package/library.js CHANGED
@@ -2,20 +2,21 @@
2
2
 
3
3
  const db = require.main.require('./src/database');
4
4
  const user = require.main.require('./src/user');
5
+ const routeHelpers = require.main.require('./src/controllers/helpers');
5
6
 
6
7
  const Plugin = {};
7
8
 
8
- // --- AYARLAR ---
9
9
  const SETTINGS = {
10
10
  pointsPerHeartbeat: 5,
11
- dailyCap: 250
11
+ dailyCap: 250,
12
+ coffeeCost: 250
12
13
  };
13
14
 
14
15
  Plugin.init = async function (params) {
15
16
  const router = params.router;
16
17
  const middleware = params.middleware;
17
18
 
18
- // 1. API: Kalp Atışı (Puan Kazanma - Client.js burayı kullanır)
19
+ // 1. HEARTBEAT (Puan Kazanma)
19
20
  router.post('/api/niki-loyalty/heartbeat', middleware.ensureLoggedIn, async (req, res) => {
20
21
  const uid = req.uid;
21
22
  const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
@@ -34,7 +35,7 @@ Plugin.init = async function (params) {
34
35
  return res.json({ earned: true, points: SETTINGS.pointsPerHeartbeat, total: newBalance });
35
36
  });
36
37
 
37
- // 2. YENİ API: Cüzdan Verisi Çekme (Custom Page burayı kullanacak)
38
+ // 2. WALLET DATA (Bilgi Çekme)
38
39
  router.get('/api/niki-loyalty/wallet-data', middleware.ensureLoggedIn, async (req, res) => {
39
40
  const uid = req.uid;
40
41
  const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
@@ -56,6 +57,56 @@ Plugin.init = async function (params) {
56
57
  dailyPercent: dailyPercent
57
58
  });
58
59
  });
60
+
61
+ // 3. QR TOKEN ÜRET (Öğrenci Butona Basınca)
62
+ router.post('/api/niki-loyalty/generate-qr', middleware.ensureLoggedIn, async (req, res) => {
63
+ const uid = req.uid;
64
+ 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
+ }
69
+
70
+ // Token Oluştur
71
+ const token = Math.random().toString(36).substring(2) + Date.now().toString(36);
72
+
73
+ // Kaydet (2 dakika geçerli)
74
+ await db.set(`niki:qr:${token}`, uid);
75
+ await db.expire(`niki:qr:${token}`, 120);
76
+
77
+ return res.json({ success: true, token: token });
78
+ });
79
+
80
+ // 4. QR OKUTMA (Personel Tarayınca)
81
+ router.post('/api/niki-loyalty/scan-qr', middleware.ensureLoggedIn, async (req, res) => {
82
+ const { token } = req.body;
83
+
84
+ // Sadece Admin/Mod yetkisi
85
+ const isAdmin = await user.isAdministrator(req.uid);
86
+ const isMod = await user.isGlobalModerator(req.uid);
87
+ if (!isAdmin && !isMod) return res.status(403).json({ success: false, message: 'Yetkisiz' });
88
+
89
+ const customerUid = await db.get(`niki:qr:${token}`);
90
+ if (!customerUid) return res.json({ success: false, message: 'Geçersiz Kod' });
91
+
92
+ // Puan Düş
93
+ const pts = parseInt(await user.getUserField(customerUid, 'niki_points')) || 0;
94
+ if (pts < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Bakiye Yetersiz' });
95
+
96
+ await user.decrementUserFieldBy(customerUid, 'niki_points', SETTINGS.coffeeCost);
97
+ await db.delete(`niki:qr:${token}`);
98
+
99
+ const customerData = await user.getUserFields(customerUid, ['username', 'picture']);
100
+ return res.json({ success: true, customer: customerData, message: 'Onaylandı!' });
101
+ });
102
+
103
+ // 5. NIKI KASA SAYFASI (Erişim Kontrolü)
104
+ routeHelpers.setupPageRoute(router, '/niki-kasa', middleware, [], async (req, res) => {
105
+ const isAdmin = await user.isAdministrator(req.uid);
106
+ const isMod = await user.isGlobalModerator(req.uid);
107
+ if (!isAdmin && !isMod) return res.render('403', {});
108
+ res.render('niki-kasa', { title: 'Niki Kasa' });
109
+ });
59
110
  };
60
111
 
61
112
  Plugin.addScripts = async function (scripts) {
@@ -63,15 +114,8 @@ Plugin.addScripts = async function (scripts) {
63
114
  return scripts;
64
115
  };
65
116
 
66
- // Menüye eklemeye devam edelim, Custom Page ile aynı linki vereceğiz
67
117
  Plugin.addNavigation = async function (nav) {
68
- nav.push({
69
- "route": "/niki-wallet",
70
- "title": "Niki Cüzdan",
71
- "enabled": true,
72
- "iconClass": "fa-coffee",
73
- "text": "Niki Cüzdan"
74
- });
118
+ nav.push({ route: "/niki-wallet", title: "Niki Cüzdan", enabled: true, iconClass: "fa-coffee", text: "Niki Cüzdan" });
75
119
  return nav;
76
120
  };
77
121
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-niki-loyalty",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Niki The Cat Coffee Loyalty System - Earn points while studying on IEU Forum.",
5
5
  "main": "library.js",
6
6
  "nbbpm": {