refactor: extract the WITHIN_PAST_WEEK condition

This commit is contained in:
siiky 2024-02-14 14:39:07 +00:00
parent 0249005314
commit 374a4272a5

View file

@ -53,10 +53,12 @@ const getValidNotifications = (type, detail) => {
return db.any(sql, [type, detail]).catch(logger.error)
}
const WITHIN_PAST_WEEK = `created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)`
const getNotifications = () => {
const sql = `
SELECT * FROM notifications
WHERE valid AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY)
WHERE valid AND ${WITHIN_PAST_WEEK}
ORDER BY created DESC
`
return db.any(sql).catch(logger.error)
@ -75,14 +77,18 @@ const hasUnreadNotifications = () => {
const sql = `
SELECT EXISTS
(SELECT * FROM notifications
WHERE valid AND NOT read AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY))
WHERE valid AND NOT read AND ${WITHIN_PAST_WEEK})
`
return db.oneOrNone(sql).then(res => res.exists).catch(logger.error)
}
const getAlerts = () => {
const types = ['fiatBalance', 'cryptoBalance', 'error']
const sql = `SELECT * FROM notifications WHERE valid = 't' AND created > (CURRENT_TIMESTAMP - INTERVAL '7' DAY) AND type IN ($1:list) ORDER BY created DESC`
const sql = `
SELECT * FROM notifications
WHERE valid AND ${WITHIN_PAST_WEEK} AND type IN ($1:list)
ORDER BY created DESC
`
return db.any(sql, [types]).catch(logger.error)
}