nodebb-plugin-niki-loyalty 1.0.13 → 1.0.14
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 +21 -22
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -7,20 +7,20 @@ const routeHelpers = require.main.require('./src/controllers/helpers');
|
|
|
7
7
|
const Plugin = {};
|
|
8
8
|
|
|
9
9
|
const SETTINGS = {
|
|
10
|
-
pointsPerHeartbeat:
|
|
10
|
+
pointsPerHeartbeat: 50,
|
|
11
11
|
dailyCap: 2500000,
|
|
12
12
|
coffeeCost: 250
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
// --- LOG
|
|
15
|
+
// --- LOG FONKSİYONLARI ---
|
|
16
16
|
async function addUserLog(uid, type, amount, desc) {
|
|
17
17
|
const logEntry = {
|
|
18
18
|
ts: Date.now(),
|
|
19
|
-
type: type, // 'earn'
|
|
19
|
+
type: type, // 'earn' veya 'spend'
|
|
20
20
|
amt: amount,
|
|
21
|
-
txt: desc
|
|
21
|
+
txt: desc // "Kahve Keyfi ☕" gibi metin
|
|
22
22
|
};
|
|
23
|
-
//
|
|
23
|
+
// Öğrenci geçmişine ekle (Son 50 işlem)
|
|
24
24
|
await db.listAppend(`niki:activity:${uid}`, logEntry);
|
|
25
25
|
await db.listTrim(`niki:activity:${uid}`, -50, -1);
|
|
26
26
|
}
|
|
@@ -42,7 +42,7 @@ Plugin.init = async function (params) {
|
|
|
42
42
|
const router = params.router;
|
|
43
43
|
const middleware = params.middleware;
|
|
44
44
|
|
|
45
|
-
// 1. HEARTBEAT (
|
|
45
|
+
// 1. HEARTBEAT (Puan Kazanma)
|
|
46
46
|
router.post('/api/niki-loyalty/heartbeat', middleware.ensureLoggedIn, async (req, res) => {
|
|
47
47
|
const uid = req.uid;
|
|
48
48
|
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
@@ -61,7 +61,7 @@ Plugin.init = async function (params) {
|
|
|
61
61
|
return res.json({ earned: true, points: SETTINGS.pointsPerHeartbeat, total: newBalance });
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
-
// 2. WALLET DATA (
|
|
64
|
+
// 2. WALLET DATA (Cüzdan + Geçmiş)
|
|
65
65
|
router.get('/api/niki-loyalty/wallet-data', middleware.ensureLoggedIn, async (req, res) => {
|
|
66
66
|
const uid = req.uid;
|
|
67
67
|
const today = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
|
@@ -69,7 +69,7 @@ Plugin.init = async function (params) {
|
|
|
69
69
|
const [userData, dailyData, history] = await Promise.all([
|
|
70
70
|
user.getUserFields(uid, ['niki_points']),
|
|
71
71
|
db.getObject(`niki:daily:${uid}:${today}`),
|
|
72
|
-
db.getListRange(`niki:activity:${uid}`, 0, -1)
|
|
72
|
+
db.getListRange(`niki:activity:${uid}`, 0, -1)
|
|
73
73
|
]);
|
|
74
74
|
|
|
75
75
|
const currentPoints = parseInt(userData.niki_points) || 0;
|
|
@@ -82,21 +82,20 @@ Plugin.init = async function (params) {
|
|
|
82
82
|
dailyScore: dailyScore,
|
|
83
83
|
dailyCap: SETTINGS.dailyCap,
|
|
84
84
|
dailyPercent: dailyPercent,
|
|
85
|
-
history: (history || []).reverse() // En
|
|
85
|
+
history: (history || []).reverse() // En yeni en üstte
|
|
86
86
|
});
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
// 3. KASA GEÇMİŞİ (
|
|
89
|
+
// 3. KASA GEÇMİŞİ (Niki Kasa'nın Görmediği Kısım Burasıydı)
|
|
90
90
|
router.get('/api/niki-loyalty/kasa-history', middleware.ensureLoggedIn, async (req, res) => {
|
|
91
91
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
92
92
|
const isMod = await user.isGlobalModerator(req.uid);
|
|
93
93
|
|
|
94
94
|
if (!isAdmin && !isMod) return res.status(403).json([]);
|
|
95
95
|
|
|
96
|
-
// Veritabanından listeyi çek
|
|
97
96
|
const history = await db.getListRange('niki:kasa:history', 0, -1);
|
|
98
97
|
|
|
99
|
-
// Kullanıcı resimlerini de ekleyerek
|
|
98
|
+
// Kullanıcı resimlerini de ekleyerek gönder
|
|
100
99
|
const enrichedHistory = await Promise.all((history || []).reverse().map(async (item) => {
|
|
101
100
|
const uData = await user.getUserFields(item.cuid, ['picture']);
|
|
102
101
|
item.picture = uData.picture;
|
|
@@ -106,7 +105,7 @@ Plugin.init = async function (params) {
|
|
|
106
105
|
res.json(enrichedHistory);
|
|
107
106
|
});
|
|
108
107
|
|
|
109
|
-
// 4. QR
|
|
108
|
+
// 4. QR OLUŞTUR
|
|
110
109
|
router.post('/api/niki-loyalty/generate-qr', middleware.ensureLoggedIn, async (req, res) => {
|
|
111
110
|
const uid = req.uid;
|
|
112
111
|
const points = parseInt(await user.getUserField(uid, 'niki_points')) || 0;
|
|
@@ -115,12 +114,12 @@ Plugin.init = async function (params) {
|
|
|
115
114
|
|
|
116
115
|
const token = Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
117
116
|
await db.set(`niki:qr:${token}`, uid);
|
|
118
|
-
await db.expire(`niki:qr:${token}`, 120);
|
|
117
|
+
await db.expire(`niki:qr:${token}`, 120);
|
|
119
118
|
|
|
120
119
|
return res.json({ success: true, token: token });
|
|
121
120
|
});
|
|
122
121
|
|
|
123
|
-
// 5. QR
|
|
122
|
+
// 5. QR OKUT (Ödeme Alma + LOGLAMA)
|
|
124
123
|
router.post('/api/niki-loyalty/scan-qr', middleware.ensureLoggedIn, async (req, res) => {
|
|
125
124
|
const { token } = req.body;
|
|
126
125
|
|
|
@@ -129,25 +128,25 @@ Plugin.init = async function (params) {
|
|
|
129
128
|
if (!isAdmin && !isMod) return res.status(403).json({ success: false, message: 'Yetkisiz' });
|
|
130
129
|
|
|
131
130
|
const customerUid = await db.get(`niki:qr:${token}`);
|
|
132
|
-
if (!customerUid) return res.json({ success: false, message: '
|
|
131
|
+
if (!customerUid) return res.json({ success: false, message: 'Geçersiz Kod' });
|
|
133
132
|
|
|
134
133
|
const pts = parseInt(await user.getUserField(customerUid, 'niki_points')) || 0;
|
|
135
134
|
if (pts < SETTINGS.coffeeCost) return res.json({ success: false, message: 'Yetersiz Bakiye' });
|
|
136
135
|
|
|
137
|
-
//
|
|
136
|
+
// İŞLEM
|
|
138
137
|
await user.decrementUserFieldBy(customerUid, 'niki_points', SETTINGS.coffeeCost);
|
|
139
138
|
await db.delete(`niki:qr:${token}`);
|
|
140
139
|
|
|
141
|
-
//
|
|
140
|
+
// DETAYLI LOG KAYDI
|
|
142
141
|
const customerData = await user.getUserFields(customerUid, ['username', 'picture']);
|
|
143
142
|
|
|
144
|
-
//
|
|
143
|
+
// Cüzdan için açıklama: "Kahve Keyfi ☕"
|
|
145
144
|
await addUserLog(customerUid, 'spend', SETTINGS.coffeeCost, 'Kahve Keyfi ☕');
|
|
146
145
|
|
|
147
|
-
//
|
|
146
|
+
// Kasa için kayıt
|
|
148
147
|
await addKasaLog(req.uid, customerData.username, customerUid);
|
|
149
148
|
|
|
150
|
-
return res.json({ success: true, customer: customerData, message: '
|
|
149
|
+
return res.json({ success: true, customer: customerData, message: 'Onaylandı!' });
|
|
151
150
|
});
|
|
152
151
|
|
|
153
152
|
// SAYFA ROTASI
|
|
@@ -155,7 +154,7 @@ Plugin.init = async function (params) {
|
|
|
155
154
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
156
155
|
const isMod = await user.isGlobalModerator(req.uid);
|
|
157
156
|
if (!isAdmin && !isMod) return res.render('403', {});
|
|
158
|
-
res.render('niki-kasa', { title: 'Niki Kasa
|
|
157
|
+
res.render('niki-kasa', { title: 'Niki Kasa' });
|
|
159
158
|
});
|
|
160
159
|
};
|
|
161
160
|
|