import { IotCredentials, NotificationsResponse } from '../../interfaces' import { Api } from '../api' export const NOTIFICATION_API = '/service/user-notifications' export class Notifications { api: Api constructor(api: Api) { this.api = api } async getCredentials() { try { const response = await this.api.apisauce.get( `${NOTIFICATION_API}/credentials`, undefined, { cache: false, }, ) return response.data } catch (e) { console.warn('Notification credentials request failed.') return null } } getNotifications(organization: string, limit = 20, offset = 0) { return this.api.apisauce.get( `${NOTIFICATION_API}/notifications/${organization}`, { limit, offset, }, ) } getNotificationsCount(organization: string) { return this.api.apisauce.get( `${NOTIFICATION_API}/notifications/${organization}/count`, ) } updateSeenNotifications(organization: string, lastNotificationAt?: string) { return this.api.apisauce.patch<{ ok: number; n: number }, void>( `${NOTIFICATION_API}/notifications/${organization}`, undefined, { params: lastNotificationAt ? { lastNotification: lastNotificationAt, } : undefined } ) } updateOneNotification(organization: string, notificationId: string) { return this.api.apisauce.patch<{ ok: number; n: number }, void>( `${NOTIFICATION_API}/notifications/${organization}/${notificationId}`, ) } registerPushEndpoint(payload: { deviceToken: string platform: 'ios' | 'android' platformEndpoint: string }) { return this.api.apisauce.post( `${NOTIFICATION_API}/push/register`, payload, ) } }