38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import { client } from './client.js'
|
|
|
|
export async function getNotifications(userHash, options = {}) {
|
|
const params = {
|
|
fields: ['id', 'type', 'reference_id', 'read', 'date_created', 'user_hash'],
|
|
filter: { user_hash: { _eq: userHash } },
|
|
sort: ['-date_created'],
|
|
limit: options.limit || 50
|
|
}
|
|
if (options.unreadOnly) {
|
|
params.filter.read = { _eq: false }
|
|
}
|
|
const response = await client.get('/items/notifications', params)
|
|
return response.data
|
|
}
|
|
|
|
export async function getUnreadCount(userHash) {
|
|
const response = await client.get('/items/notifications', {
|
|
filter: { user_hash: { _eq: userHash }, read: { _eq: false } },
|
|
aggregate: { count: 'id' }
|
|
})
|
|
return parseInt(response.data?.[0]?.count?.id || '0', 10)
|
|
}
|
|
|
|
export async function markNotificationRead(id) {
|
|
return client.patch(`/items/notifications/${id}`, { read: true })
|
|
}
|
|
|
|
export async function markAllNotificationsRead(userHash) {
|
|
const unread = await getNotifications(userHash, { unreadOnly: true })
|
|
const updates = unread.map(n => markNotificationRead(n.id))
|
|
return Promise.all(updates)
|
|
}
|
|
|
|
export async function deleteNotification(id) {
|
|
return client.delete(`/items/notifications/${id}`)
|
|
}
|