forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropRepository.js
More file actions
53 lines (48 loc) · 1.31 KB
/
Copy pathdropRepository.js
File metadata and controls
53 lines (48 loc) · 1.31 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
const { query } = require('./index');
/**
* Returns all active, non-expired drops.
* @returns {Promise<object[]>}
*/
async function getActiveDrops() {
const result = await query(
`SELECT * FROM drops WHERE is_active = TRUE AND expires_at > NOW()`,
[]
);
return result.rows;
}
/**
* Returns a single drop by ID.
* @param {number} dropId
* @returns {Promise<object|null>}
*/
async function getDropById(dropId) {
const result = await query('SELECT * FROM drops WHERE id = $1', [dropId]);
return result.rows[0] || null;
}
/**
* Counts how many times a user has claimed a specific drop.
* @param {number} dropId
* @param {number} userId
* @returns {Promise<number>}
*/
async function getClaimCount(dropId, userId) {
const result = await query(
'SELECT COUNT(*) AS cnt FROM drop_claims WHERE drop_id = $1 AND user_id = $2',
[dropId, userId]
);
return parseInt(result.rows[0].cnt, 10);
}
/**
* Records a claim for a user on a drop.
* @param {number} dropId
* @param {number} userId
* @returns {Promise<object>}
*/
async function recordClaim(dropId, userId) {
const result = await query(
`INSERT INTO drop_claims (drop_id, user_id) VALUES ($1, $2) RETURNING *`,
[dropId, userId]
);
return result.rows[0];
}
module.exports = { getActiveDrops, getDropById, getClaimCount, recordClaim };