import { HttpException, Inject, Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; import { ConfigModuleOptions, MODULE_OPTIONS_TOKEN } from './auth.module-definition'; @Injectable() export class FxNotificationService { constructor(private httpService: HttpService, @Inject(MODULE_OPTIONS_TOKEN) private options: ConfigModuleOptions ) { } async create(token: any, body: any) { try { const { notificationUrl, clientId } = this.options const data = (await this.httpService.post(`${notificationUrl}/notification`, body, { params: { clientId }, headers: { 'Authorization': token } }).toPromise()).data } catch (error) { this.handleError(error) } } async createMany(token: any, body: any) { try { const { notificationUrl, clientId } = this.options const data = (await this.httpService.post(`${notificationUrl}/notification/many`, body, { params: { clientId }, headers: { 'Authorization': token } }).toPromise()).data } catch (error) { this.handleError(error) } } async getByUser(token: any, pagination) { try { const { notificationUrl, clientId } = this.options const data = (await this.httpService.get(`${notificationUrl}/notification/byUser`, { params: { clientId, pagination }, headers: { 'Authorization': token } }).toPromise()).data return data } catch (error) { this.handleError(error) } } async getUnreadCount(token: any) { try { const { notificationUrl, clientId } = this.options const data = (await this.httpService.get(`${notificationUrl}/notification/unreadCount`, { params: { clientId }, headers: { 'Authorization': token } }).toPromise()).data return data } catch (error) { this.handleError(error) } } async markAsRead(token: any, id: any) { try { const { notificationUrl, clientId } = this.options const data = (await this.httpService.put(`${notificationUrl}/notification/markasread/${id}`,{}, { params: { clientId }, headers: { 'Authorization': token } }).toPromise()).data return data } catch (error: any) { this.handleError(error) } } handleError(error) { if (error.response && error.response.data) { throw new HttpException(error.response.data.message, error.response.data.statusCode) } else { throw error } } }