UNPKG

ministry-platform-provider

Version:

TypeScript client library for Ministry Platform API integration

59 lines (58 loc) 2.22 kB
export class CommunicationService { client; constructor(client) { this.client = client; } /** * Creates a new communication, immediately renders it and schedules for delivery. */ async createCommunication(communication, attachments) { try { await this.client.ensureValidToken(); if (attachments && attachments.length > 0) { return await this.createCommunicationWithAttachments(communication, attachments); } else { return await this.client.getHttpClient().post('/communications', { ...communication }); } } catch (error) { console.error('Error creating communication:', error); throw error; } } /** * Creates email messages from the provided information and immediately schedules them for delivery. */ async sendMessage(message, attachments) { try { await this.client.ensureValidToken(); if (attachments && attachments.length > 0) { return await this.sendMessageWithAttachments(message, attachments); } else { return await this.client.getHttpClient().post('/messages', { ...message }); } } catch (error) { console.error('Error sending message:', error); throw error; } } async createCommunicationWithAttachments(communication, attachments) { const formData = new FormData(); formData.append('communication', JSON.stringify(communication)); attachments.forEach((file, index) => { formData.append(`file-${index}`, file, file.name); }); return await this.client.getHttpClient().postFormData('/communications', formData); } async sendMessageWithAttachments(message, attachments) { const formData = new FormData(); formData.append('message', JSON.stringify(message)); attachments.forEach((file, index) => { formData.append(`file-${index}`, file, file.name); }); return await this.client.getHttpClient().postFormData('/messages', formData); } }