Skip to content

Commit

Permalink
fix: sms not being sent (#164)
Browse files Browse the repository at this point in the history
* fix: sms templates not being to able to parse json

* fix: the missing await
  • Loading branch information
naftis committed Jun 25, 2024
1 parent 2886911 commit 6aefb79
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/api/notification/sms-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -43,19 +42,13 @@ export type SMSTemplateType =
| keyof typeof otherTemplates
| keyof typeof informantTemplates

interface ISMSNotificationTemplate {
lang: string
displayName: string
messages: Record<SMSTemplateType, string>
}

export async function sendSMS(
type: SMSTemplateType,
variables: Record<string, string>,
recipient: string,
locale: string
) {
const message = compileMessages(type, variables, locale)
const message = await compileMessages(type, variables, locale)
const body = JSON.stringify({
messages: [
{
Expand Down Expand Up @@ -98,20 +91,19 @@ export async function sendSMS(
}
}

function compileMessages(
const compileMessages = async (
templateName: SMSTemplateType,
variables: Record<string, string>,
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)
Expand Down

0 comments on commit 6aefb79

Please sign in to comment.