From 6aefb792c4f20b501cffef6c9b2bec3860d61bbf Mon Sep 17 00:00:00 2001 From: Pyry Rouvila Date: Tue, 25 Jun 2024 16:15:34 +0300 Subject: [PATCH] fix: sms not being sent (#164) * fix: sms templates not being to able to parse json * fix: the missing await --- src/api/notification/sms-service.ts | 30 +++++++++++------------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/api/notification/sms-service.ts b/src/api/notification/sms-service.ts index 5a613f04f..a4d2016cb 100644 --- a/src/api/notification/sms-service.ts +++ b/src/api/notification/sms-service.ts @@ -15,10 +15,9 @@ import { } from './constant' import { logger } from '@countryconfig/logger' import fetch from 'node-fetch' -import { readFileSync } from 'fs' import * as Handlebars from 'handlebars' -import { join } from 'path' import { internal } from '@hapi/boom' +import { getLanguages } from '../content/service' export const informantTemplates = { birthInProgressNotification: 'birthInProgressNotification', @@ -43,19 +42,13 @@ export type SMSTemplateType = | keyof typeof otherTemplates | keyof typeof informantTemplates -interface ISMSNotificationTemplate { - lang: string - displayName: string - messages: Record -} - export async function sendSMS( type: SMSTemplateType, variables: Record, recipient: string, locale: string ) { - const message = compileMessages(type, variables, locale) + const message = await compileMessages(type, variables, locale) const body = JSON.stringify({ messages: [ { @@ -98,20 +91,19 @@ export async function sendSMS( } } -function compileMessages( +const compileMessages = async ( templateName: SMSTemplateType, variables: Record, locale: string -): string { - const smsNotificationTemplate = JSON.parse( - readFileSync( - join(__dirname, '../languages/content/notification/notification.json') - ).toString() - ).data as ISMSNotificationTemplate[] +) => { + const languages = await getLanguages('notification') + const language = languages.find(({ lang }) => lang === locale) - const language = smsNotificationTemplate.filter((obj) => { - return obj.lang === locale - })[0] + if (!language) { + throw new Error( + `Locale "${locale}" not found while compiling notification messages.` + ) + } const template = Handlebars.compile(language.messages[templateName]) return template(variables)