forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailLogRepository.js
More file actions
160 lines (146 loc) · 3.62 KB
/
Copy pathemailLogRepository.js
File metadata and controls
160 lines (146 loc) · 3.62 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
const { query } = require('./index');
/**
* Records an email log entry.
* Requirements: #184
*
* @param {object} params
* @param {string} params.recipientEmail
* @param {string} params.emailType - 'redemption_confirmation' | 'milestone_achieved' | 'welcome' | 'password_reset'
* @param {string} params.subject
* @returns {Promise<object>} The inserted log row
*/
async function createEmailLog({ recipientEmail, emailType, subject }) {
const result = await query(
`INSERT INTO email_logs
(recipient_email, email_type, subject)
VALUES ($1, $2, $3)
RETURNING *`,
[recipientEmail, emailType, subject]
);
return result.rows[0];
}
/**
* Updates email log status to 'sent'.
* Requirements: #184
*
* @param {number} logId
* @returns {Promise<object>}
*/
async function markEmailSent(logId) {
const result = await query(
`UPDATE email_logs
SET status = 'sent', sent_at = NOW()
WHERE id = $1
RETURNING *`,
[logId]
);
return result.rows[0];
}
/**
* Updates email log status to 'delivered'.
* Requirements: #184
*
* @param {number} logId
* @returns {Promise<object>}
*/
async function markEmailDelivered(logId) {
const result = await query(
`UPDATE email_logs
SET status = 'delivered', delivered_at = NOW()
WHERE id = $1
RETURNING *`,
[logId]
);
return result.rows[0];
}
/**
* Updates email log status to 'failed' with error message.
* Requirements: #184
*
* @param {number} logId
* @param {string} errorMessage
* @returns {Promise<object>}
*/
async function markEmailFailed(logId, errorMessage) {
const result = await query(
`UPDATE email_logs
SET status = 'failed', error_message = $2
WHERE id = $1
RETURNING *`,
[logId, errorMessage]
);
return result.rows[0];
}
/**
* Gets email logs with pagination and filtering.
* Requirements: #184
*
* @param {object} params
* @param {string} [params.recipientEmail]
* @param {string} [params.emailType]
* @param {string} [params.status]
* @param {number} params.page
* @param {number} params.limit
* @returns {Promise<{data: object[], total: number, page: number, limit: number}>}
*/
async function getEmailLogs({ recipientEmail, emailType, status, page = 1, limit = 20 }) {
const offset = (page - 1) * limit;
const conditions = [];
const params = [];
let paramIndex = 1;
if (recipientEmail) {
conditions.push(`recipient_email = $${paramIndex++}`);
params.push(recipientEmail);
}
if (emailType) {
conditions.push(`email_type = $${paramIndex++}`);
params.push(emailType);
}
if (status) {
conditions.push(`status = $${paramIndex++}`);
params.push(status);
}
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
// Get total count
const countResult = await query(
`SELECT COUNT(*) as total FROM email_logs ${whereClause}`,
params
);
const total = parseInt(countResult.rows[0].total, 10);
// Get paginated data
const dataResult = await query(
`SELECT * FROM email_logs
${whereClause}
ORDER BY created_at DESC
LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
[...params, limit, offset]
);
return {
data: dataResult.rows,
total,
page,
limit,
};
}
/**
* Gets an email log by ID.
* Requirements: #184
*
* @param {number} logId
* @returns {Promise<object|null>}
*/
async function getEmailLogById(logId) {
const result = await query(
'SELECT * FROM email_logs WHERE id = $1',
[logId]
);
return result.rows[0] || null;
}
module.exports = {
createEmailLog,
markEmailSent,
markEmailDelivered,
markEmailFailed,
getEmailLogs,
getEmailLogById,
};