Files
mercado.clube67.com/backend/models/Notification.js
T

42 lines
1.0 KiB
JavaScript

'use strict'
const { execute, queryOne, query } = require('../database/postgres')
class Notification {
static async create({ user_cpf, title, message, type }) {
return execute(
`INSERT INTO notifications (user_cpf, title, message, type)
VALUES ($1, $2, $3, $4) RETURNING id`,
[user_cpf, title, message, type || 'info']
)
}
static async getUserNotifications(user_cpf) {
return query(
`SELECT * FROM notifications
WHERE user_cpf = $1
ORDER BY created_at DESC LIMIT 50`,
[user_cpf]
)
}
static async getUnreadCount(user_cpf) {
const row = await queryOne(
`SELECT COUNT(*) as total FROM notifications
WHERE user_cpf = $1 AND read_at IS NULL`,
[user_cpf]
)
return row ? parseInt(row.total) : 0
}
static async markAsRead(id, user_cpf) {
return execute(
`UPDATE notifications SET read_at = NOW()
WHERE id = $1 AND user_cpf = $2 AND read_at IS NULL`,
[id, user_cpf]
)
}
}
module.exports = Notification