import { NON_UNICODED_LANGUAGES , RESOURCES_URL } from '@notification/constants' ; import { internal } from '@hapi/boom' ; import { sendSMS } from '@notification/features/sms/service' ; import fetch from 'node-fetch' ; import * as Handlebars from 'handlebars' ; import * as Hapi from '@hapi/hapi' ; import { getDefaultLanguage } from '@notification/i18n/utils' ; interface ISendSMSPayload { name ? : string ; authCode ? : string ; trackingId ? : string ; username ? : string ; password ? : string ; crvsOffice ? : string ; registrationNumber ? : string ; } interface IMessageIdentifier { [ key ] : string ; } export interface ILanguage { lang : string ; displayName : string ; messages : IMessageIdentifier ; } interface ITranslationsResponse { languages : ILanguage [ ] ; } export interface ISMSPayload { msisdn : string ; } export interface IAuthHeader { Authorization : string ; } export async function getTranslations ( authHeader , messageKey , messagePayload , locale ) { const url = `${ RESOURCES_URL }/definitions/notification` ; const res = await fetch ( url , { method : 'GET' , headers : { 'Content-Type' : 'application/json' , ... authHeader } } ) . then ( response => { return response . json ( ) ; } ) . catch ( error => { return Promise . reject ( new Error ( ` request failed: ${ error . message }` ) ) ; } ) ; const language = res . languages . filter ( obj => { return obj . lang === locale ; } ) [ 0 ] ; const template = Handlebars . compile ( language . messages [ messageKey ] ) ; return template ( messagePayload ) ; } export async function buildAndSendSMS ( request , msisdn , message ) { try { return await sendSMS ( msisdn , message , NON_UNICODED_LANGUAGES . indexOf ( getDefaultLanguage ( ) ) < 0 ) ; } catch ( err ) { return internal ( err ) ; } }