UNPKG

@telstra/messaging

Version:
186 lines (177 loc) 6.62 kB
import { HttpClient } from '@telstra/core'; import { AssertionError } from 'assert'; import * as uuid from 'uuid'; import { MessagingErrorCode } from '../errors/ErrorCode.js'; import { Schemas } from '../schemas/index.js'; import { Validator } from './Validator.js'; export class Reports extends HttpClient { authConfig; constructor(authConfig) { super(authConfig); this.authConfig = authConfig; } validateReportIdParam(reportId) { if (!uuid.validate(reportId)) { throw new AssertionError({ ...MessagingErrorCode.MissingAttribute, message: `data.reportId should match UUID format string.`, }); } } validateReportDates(startDate, endDate) { const sDate = new Date(startDate); const eDate = new Date(endDate); const today = new Date(); const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); const reportStartRangeDate = new Date(today); reportStartRangeDate.setDate(today.getDate() - 90); if (sDate > tomorrow || eDate > tomorrow) { throw new AssertionError({ ...MessagingErrorCode.InvalidAttribute, message: `data.startDate, data.endDate should be in past.`, }); } if (sDate > eDate) { throw new AssertionError({ ...MessagingErrorCode.InvalidAttribute, message: `data.startDate should always be older than data.endDate.`, }); } if (sDate < reportStartRangeDate) { throw new AssertionError({ ...MessagingErrorCode.InvalidAttribute, message: `data.startDate should not be older than 90 days from today.`, }); } } /** * Fetch details of all reports recently generated for your account. * Use it to check the status of a report, plus fetch the report ID, * status, report type and expiry date. * @link https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Fetchallreports * @example ```typescript import { Report } from '@telstra/messaging' const report = new Report(); report.getAll() .then(result => { console.log(result); }) .catch(error => { console.error(error); }); ``` */ async getAll() { try { const result = await this.instance.get(`/messaging/v3/reports`); return result; } catch (error) { this.logger.error('Error fetching Reports', error); throw error; } } /** * Use the reportId to fetch a download link for a report generated. * @link https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FetchaReport * @example ```typescript import { Report } from '@telstra/messaging' const report = new Report(); report.get('6940c774-4335-4d2b-b758-4ecb19412e85') .then(result => { console.log(result); }) .catch(error => { console.error(error); }); ``` */ async get(reportId) { try { this.validateReportIdParam(reportId); const result = await this.instance.get(`/messaging/v3/reports/${reportId}`); return result; } catch (error) { this.logger.error('Error fetching Report', error); throw error; } } /** * Request a CSV report of messages (both incoming and outgoing) that have been sent to/from your account within the last three months. * @param createReport.startDate * @param createReport.endDate * @param createReport.reportCallbackUrl * @param createReport.filter * @link https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Submitarequestforamessagesreport * @example ```typescript import { Report } from '@telstra/messaging' const report = new Report(); report.create({ startDate: '2023-03-15', endDate: '2023-03-30', filter: '0412345678' }) .then(result => { console.log(result); }) .catch(error => { console.error(error); }); ``` */ async create(createReport) { try { const validate = new Validator(createReport); validate.schemaInline(Schemas.CREATE_MESSAGES_REPORT); this.validateReportDates(createReport.startDate, createReport.endDate); const result = await this.instance.post(`/messaging/v3/reports/messages`, createReport); return result; } catch (error) { this.logger.error('Error creating Report', error); throw error; } } /** * Request a CSV report of messages (both incoming and outgoing) that have been sent to/from your account within the last three months. * @param createReport.startDate * @param createReport.endDate * @param createReport.reportCallbackUrl * @param createReport.filter * @link https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Submitarequestforamessagesreport * @example ```typescript import { Report } from '@telstra/messaging' const report = new Report(); report.create({ startDate: '2023-03-15', endDate: '2023-03-30', filter: '0412345678' }) .then(result => { console.log(result); }) .catch(error => { console.error(error); }); ``` */ async createDailySummary(createReport) { try { const validate = new Validator(createReport); validate.schemaInline(Schemas.CREATE_MESSAGES_REPORT); this.validateReportDates(createReport.startDate, createReport.endDate); const result = await this.instance.post(`/messaging/v3/reports/messages/daily`, createReport); return result; } catch (error) { this.logger.error('Error creating Report', error); throw error; } } }