forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcampaignRepository.js
More file actions
332 lines (307 loc) · 8.43 KB
/
Copy pathcampaignRepository.js
File metadata and controls
332 lines (307 loc) · 8.43 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const { query } = require('./index');
/**
* Validates campaign input fields before creation.
* Requirements: 7.3
*/
function validateCampaign({ rewardRate, startDate, endDate }) {
const errors = [];
if (rewardRate === undefined || rewardRate === null || isNaN(Number(rewardRate))) {
errors.push('rewardRate must be a number');
} else if (Number(rewardRate) <= 0) {
errors.push('rewardRate must be greater than 0');
}
if (!startDate || !endDate) {
errors.push('startDate and endDate are required');
} else {
const start = new Date(startDate);
const end = new Date(endDate);
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
errors.push('startDate and endDate must be valid dates');
} else if (end <= start) {
errors.push('endDate must be strictly after startDate');
}
}
return { valid: errors.length === 0, errors };
}
/**
* Creates a new campaign row (on_chain_status defaults to 'pending').
* Requirements: 7.2, #868
*/
async function createCampaign({ merchantId, name, rewardRate, tokenAmount, rewardPerAction, startDate, endDate }) {
const result = await query(
`INSERT INTO campaigns (merchant_id, name, reward_rate, token_amount, reward_per_action, start_date, end_date)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *`,
[merchantId, name, rewardRate ?? rewardPerAction, tokenAmount, rewardPerAction, startDate, endDate]
);
return result.rows[0];
}
/**
* Stamps the on-chain fields after a successful Soroban transaction.
*/
async function confirmOnChain({ id, contractCampaignId, txHash }) {
const result = await query(
`UPDATE campaigns
SET contract_campaign_id = $1,
tx_hash = $2,
on_chain_status = 'confirmed'
WHERE id = $3
RETURNING *`,
[contractCampaignId, txHash, id]
);
return result.rows[0];
}
/**
* Marks a campaign as on-chain failed (used for rollback).
*/
async function markOnChainFailed(id) {
await query(
`UPDATE campaigns SET on_chain_status = 'failed' WHERE id = $1`,
[id]
);
}
/**
* Returns a single campaign by id, excluding soft-deleted rows.
*/
async function getCampaignById(campaignId) {
const result = await query(
'SELECT * FROM campaigns WHERE id = $1 AND deleted_at IS NULL',
[campaignId]
);
return result.rows[0] || null;
}
/**
* Returns all non-deleted campaigns for a merchant.
*/
async function getCampaignsByMerchant(merchantId) {
const result = await query(
`SELECT * FROM campaigns
WHERE merchant_id = $1 AND deleted_at IS NULL
ORDER BY created_at DESC`,
[merchantId]
);
return result.rows;
}
/**
* Returns a campaign only if it is active and not expired.
* Requirements: 7.4, 7.5
*/
async function getActiveCampaign(campaignId) {
const result = await query(
`SELECT * FROM campaigns
WHERE id = $1
AND is_active = TRUE
AND end_date >= CURRENT_DATE
AND deleted_at IS NULL`,
[campaignId]
);
return result.rows[0] || null;
}
/**
* Updates mutable campaign fields and refreshes on-chain tracking columns.
*
* @param {number} id
* @param {{ name?: string, rewardRate?: number, txHash?: string }} fields
*/
async function updateCampaign(id, { name, rewardRate, txHash }) {
const setClauses = [];
const values = [];
let idx = 1;
if (name !== undefined) {
setClauses.push(`name = $${idx++}`);
values.push(name);
}
if (rewardRate !== undefined) {
setClauses.push(`reward_rate = $${idx++}`);
values.push(rewardRate);
}
if (txHash !== undefined) {
setClauses.push(`tx_hash = $${idx++}`);
values.push(txHash);
setClauses.push(`on_chain_status = 'confirmed'`);
}
if (setClauses.length === 0) return getCampaignById(id);
values.push(id);
const result = await query(
`UPDATE campaigns SET ${setClauses.join(', ')} WHERE id = $${idx} AND deleted_at IS NULL RETURNING *`,
values
);
return result.rows[0] || null;
}
/**
* Soft-deletes a campaign and records the final tx_hash from the pause call.
*
* @param {number} id
* @param {string} txHash
*/
async function softDeleteCampaign(id, txHash) {
const result = await query(
`UPDATE campaigns
SET deleted_at = NOW(),
is_active = FALSE,
tx_hash = $1,
on_chain_status = 'confirmed'
WHERE id = $2 AND deleted_at IS NULL
RETURNING *`,
[txHash, id]
);
return result.rows[0] || null;
}
/**
* Returns a paginated list of public (non-deleted) campaigns joined with
* merchant name, with optional server-side filtering.
*
* Supports filtering by: category, rewardType, status, merchantId, search.
* Status is derived from is_active + end_date at query time.
*
* @param {{
* page?: number,
* limit?: number,
* category?: string,
* rewardType?: string,
* status?: 'active'|'paused'|'completed',
* merchantId?: number,
* search?: string,
* }} params
* @returns {Promise<{ campaigns: object[], total: number, hasMore: boolean }>}
*/
async function getPublicCampaigns({
page = 1,
limit = 12,
category,
rewardType,
status,
merchantId,
search,
} = {}) {
const offset = (Math.max(1, page) - 1) * limit;
const conditions = ['c.deleted_at IS NULL'];
const values = [];
let i = 1;
if (category) {
conditions.push(`c.category = $${i++}`);
values.push(category);
}
if (rewardType) {
conditions.push(`c.reward_type = $${i++}`);
values.push(rewardType);
}
if (merchantId) {
conditions.push(`c.merchant_id = $${i++}`);
values.push(merchantId);
}
if (search) {
conditions.push(`(c.name ILIKE $${i} OR m.name ILIKE $${i})`);
values.push(`%${search}%`);
i++;
}
// Status filter: derive from DB columns
if (status === 'active') {
conditions.push(`c.is_active = TRUE AND c.end_date >= CURRENT_DATE`);
} else if (status === 'paused') {
conditions.push(`c.is_active = FALSE AND c.end_date >= CURRENT_DATE`);
} else if (status === 'completed') {
conditions.push(`c.end_date < CURRENT_DATE`);
}
const where = `WHERE ${conditions.join(' AND ')}`;
// Count query (no LIMIT/OFFSET)
const countResult = await query(
`SELECT COUNT(*) AS total
FROM campaigns c
JOIN merchants m ON m.id = c.merchant_id
${where}`,
values
);
const total = parseInt(countResult.rows[0].total, 10);
// Data query
const dataResult = await query(
`SELECT
c.id,
c.name,
c.description,
c.category,
c.reward_type,
c.reward_rate,
c.start_date,
c.end_date,
c.is_active,
c.participant_count,
c.eligibility_rules,
c.tags,
c.merchant_id,
m.name AS merchant_name,
m.logo_url AS merchant_logo
FROM campaigns c
JOIN merchants m ON m.id = c.merchant_id
${where}
ORDER BY
CASE WHEN c.is_active = TRUE AND c.end_date >= CURRENT_DATE THEN 0 ELSE 1 END,
c.created_at DESC
LIMIT $${i++} OFFSET $${i++}`,
[...values, limit, offset]
);
return {
campaigns: dataResult.rows,
total,
hasMore: offset + dataResult.rows.length < total,
};
}
/**
* Returns a single public campaign by ID (no auth required).
* Returns null if not found or soft-deleted.
*
* @param {number} id
* @returns {Promise<object|null>}
*/
async function getPublicCampaignById(id) {
const result = await query(
`SELECT
c.id,
c.name,
c.description,
c.category,
c.reward_type,
c.reward_rate,
c.start_date,
c.end_date,
c.is_active,
c.participant_count,
c.eligibility_rules,
c.tags,
c.merchant_id,
m.name AS merchant_name,
m.logo_url AS merchant_logo
FROM campaigns c
JOIN merchants m ON m.id = c.merchant_id
WHERE c.id = $1 AND c.deleted_at IS NULL`,
[id]
);
return result.rows[0] || null;
}
/**
* Returns the distinct non-null categories across all non-deleted campaigns.
* @returns {Promise<string[]>}
*/
async function getPublicCampaignCategories() {
const result = await query(
`SELECT DISTINCT category
FROM campaigns
WHERE category IS NOT NULL AND deleted_at IS NULL
ORDER BY category ASC`
);
return result.rows.map((r) => r.category);
}
module.exports = {
validateCampaign,
createCampaign,
confirmOnChain,
markOnChainFailed,
getCampaignById,
getCampaignsByMerchant,
getActiveCampaign,
updateCampaign,
softDeleteCampaign,
getPublicCampaigns,
getPublicCampaignById,
getPublicCampaignCategories,
};