From 3ecee122c6a59e2816f4db3711edeb2dcf0dec9b Mon Sep 17 00:00:00 2001 From: KjartanE Date: Fri, 7 Jan 2022 17:21:51 -0800 Subject: [PATCH] move config variable and .env variables --- api/src/models/gcnotify.ts | 7 --- api/src/paths/gcnotify/send.ts | 37 +++++++++----- api/src/services/gcnotify-service.test.ts | 62 ++++------------------- api/src/services/gcnotify-service.ts | 27 +++++----- 4 files changed, 45 insertions(+), 88 deletions(-) diff --git a/api/src/models/gcnotify.ts b/api/src/models/gcnotify.ts index 44b8aa37a4..118ff04541 100644 --- a/api/src/models/gcnotify.ts +++ b/api/src/models/gcnotify.ts @@ -13,10 +13,3 @@ export interface IgcNotifyGenericMessage { body2: string; footer: string; } - -export interface IgcNotifyConfig { - headers: { - Authorization: string; - 'Content-Type': string; - }; -} diff --git a/api/src/paths/gcnotify/send.ts b/api/src/paths/gcnotify/send.ts index e81b1aa513..f6cc51eeae 100644 --- a/api/src/paths/gcnotify/send.ts +++ b/api/src/paths/gcnotify/send.ts @@ -5,12 +5,10 @@ import { SYSTEM_ROLE } from '../../constants/roles'; import { authorizeRequestHandler } from '../../request-handlers/security/authorization'; import { getLogger } from '../../utils/logger'; import { GCNotifyService } from '../../services/gcnotify-service'; -import { IgcNotifyPostReturn, IgcNotifyConfig } from '../../models/gcnotify'; +import { IgcNotifyPostReturn } from '../../models/gcnotify'; const defaultLog = getLogger('paths/gcnotify'); -const api_key = process.env.GCNOTIFY_SECRET_API_KEY; - export const POST: Operation = [ authorizeRequestHandler(() => { return { @@ -86,7 +84,27 @@ POST.apiDoc = { 'application/json': { schema: { title: 'User Response Object', - type: 'object' + type: 'object', + properties: { + content: { + type: 'object' + }, + id: { + type: 'string' + }, + reference: { + type: 'string' + }, + scheduled_for: { + type: 'string' + }, + template: { + type: 'object' + }, + uri: { + type: 'string' + } + } } } } @@ -151,19 +169,12 @@ export function sendNotification(): RequestHandler { const gcnotifyService = new GCNotifyService(); let response = {} as IgcNotifyPostReturn; - const config = { - headers: { - Authorization: api_key, - 'Content-Type': 'application/json' - } - } as IgcNotifyConfig; - if (recipient.emailAddress) { - response = await gcnotifyService.sendEmailGCNotification(recipient.emailAddress, config, message); + response = await gcnotifyService.sendEmailGCNotification(recipient.emailAddress, message); } if (recipient.phoneNumber) { - response = await gcnotifyService.sendPhoneNumberGCNotification(recipient.phoneNumber, config, message); + response = await gcnotifyService.sendPhoneNumberGCNotification(recipient.phoneNumber, message); } if (recipient.userId) { diff --git a/api/src/services/gcnotify-service.test.ts b/api/src/services/gcnotify-service.test.ts index 395986732b..870a9b1310 100644 --- a/api/src/services/gcnotify-service.test.ts +++ b/api/src/services/gcnotify-service.test.ts @@ -5,7 +5,7 @@ import sinonChai from 'sinon-chai'; import { ApiError } from '../errors/custom-error'; import { GCNotifyService } from './gcnotify-service'; import axios from 'axios'; -import { IgcNotifyGenericMessage, IgcNotifyConfig } from '../models/gcnotify'; +import { IgcNotifyGenericMessage } from '../models/gcnotify'; chai.use(sinonChai); @@ -17,13 +17,6 @@ describe('GCNotifyService', () => { const emailAddress = 'test@email.com'; - const config = { - headers: { - Authorization: 'api_key', - 'Content-Type': 'application/json' - } - }; - const message = { header: 'message.header', body1: 'message.body1', @@ -31,26 +24,13 @@ describe('GCNotifyService', () => { footer: 'message.footer' }; - it('should throw a 400 error when no url is given', async () => { - const gcNotifyServiece = new GCNotifyService(); - - sinon.stub(axios, 'post').resolves({ data: null }); - - try { - await gcNotifyServiece.sendEmailGCNotification('', config, message); - expect.fail(); - } catch (actualError) { - expect((actualError as ApiError).message).to.equal('Failed to send Notification'); - } - }); - - it('should throw a 400 error when no config is given', async () => { + it('should throw a 400 error when no email is given', async () => { const gcNotifyServiece = new GCNotifyService(); sinon.stub(axios, 'post').resolves({ data: null }); try { - await gcNotifyServiece.sendEmailGCNotification(emailAddress, {} as IgcNotifyConfig, message); + await gcNotifyServiece.sendEmailGCNotification('', message); expect.fail(); } catch (actualError) { expect((actualError as ApiError).message).to.equal('Failed to send Notification'); @@ -63,7 +43,7 @@ describe('GCNotifyService', () => { sinon.stub(axios, 'post').resolves({ data: null }); try { - await gcNotifyServiece.sendEmailGCNotification(emailAddress, config, message); + await gcNotifyServiece.sendEmailGCNotification(emailAddress, message); expect.fail(); } catch (actualError) { expect((actualError as ApiError).message).to.equal('Failed to send Notification'); @@ -75,11 +55,7 @@ describe('GCNotifyService', () => { sinon.stub(axios, 'post').resolves({ data: 201 }); - const result = await gcNotifyServiece.sendEmailGCNotification( - emailAddress, - config, - {} as IgcNotifyGenericMessage - ); + const result = await gcNotifyServiece.sendEmailGCNotification(emailAddress, {} as IgcNotifyGenericMessage); expect(result).to.eql(201); }); @@ -92,13 +68,6 @@ describe('GCNotifyService', () => { const sms = '2501231234'; - const config = { - headers: { - Authorization: 'api_key', - 'Content-Type': 'application/json' - } - }; - const message = { header: 'message.header', body1: 'message.body1', @@ -106,26 +75,13 @@ describe('GCNotifyService', () => { footer: 'message.footer' }; - it('should throw a 400 error when no url is given', async () => { - const gcNotifyServiece = new GCNotifyService(); - - sinon.stub(axios, 'post').resolves({ data: null }); - - try { - await gcNotifyServiece.sendPhoneNumberGCNotification('', config, message); - expect.fail(); - } catch (actualError) { - expect((actualError as ApiError).message).to.equal('Failed to send Notification'); - } - }); - - it('should throw a 400 error when no config is given', async () => { + it('should throw a 400 error when no phone number is given', async () => { const gcNotifyServiece = new GCNotifyService(); sinon.stub(axios, 'post').resolves({ data: null }); try { - await gcNotifyServiece.sendPhoneNumberGCNotification(sms, {} as IgcNotifyConfig, message); + await gcNotifyServiece.sendPhoneNumberGCNotification('', message); expect.fail(); } catch (actualError) { expect((actualError as ApiError).message).to.equal('Failed to send Notification'); @@ -138,7 +94,7 @@ describe('GCNotifyService', () => { sinon.stub(axios, 'post').resolves({ data: null }); try { - await gcNotifyServiece.sendPhoneNumberGCNotification(sms, config, message); + await gcNotifyServiece.sendPhoneNumberGCNotification(sms, message); expect.fail(); } catch (actualError) { expect((actualError as ApiError).message).to.equal('Failed to send Notification'); @@ -150,7 +106,7 @@ describe('GCNotifyService', () => { sinon.stub(axios, 'post').resolves({ data: 201 }); - const result = await gcNotifyServiece.sendPhoneNumberGCNotification(sms, config, {} as IgcNotifyGenericMessage); + const result = await gcNotifyServiece.sendPhoneNumberGCNotification(sms, {} as IgcNotifyGenericMessage); expect(result).to.eql(201); }); diff --git a/api/src/services/gcnotify-service.ts b/api/src/services/gcnotify-service.ts index 5bb33b1696..ee51d02f68 100644 --- a/api/src/services/gcnotify-service.ts +++ b/api/src/services/gcnotify-service.ts @@ -1,27 +1,29 @@ import axios from 'axios'; import { ApiError, ApiErrorType } from '../errors/custom-error'; -import { IgcNotifyPostReturn, IgcNotifyGenericMessage, IgcNotifyConfig } from '../models/gcnotify'; +import { IgcNotifyPostReturn, IgcNotifyGenericMessage } from '../models/gcnotify'; -const EMAIL_TEMPLATE: string = process.env.GCNOTIFY_ONBOARDING_REQUEST_EMAIL_TEMPLATE || ''; -const SMS_TEMPLATE: string = process.env.GCNOTIFY_ONBOARDING_REQUEST_SMS_TEMPLATE || ''; +const EMAIL_TEMPLATE = process.env.GCNOTIFY_ONBOARDING_REQUEST_EMAIL_TEMPLATE || ''; +const SMS_TEMPLATE = process.env.GCNOTIFY_ONBOARDING_REQUEST_SMS_TEMPLATE || ''; const EMAIL_URL = process.env.GCNOTIFY_EMAIL_URL || ''; const SMS_URL = process.env.GCNOTIFY_SMS_URL || ''; +const API_KEY = process.env.GCNOTIFY_SECRET_API_KEY || ''; +const config = { + headers: { + Authorization: API_KEY, + 'Content-Type': 'application/json' + } +}; export class GCNotifyService { /** * Send email notification to recipient * * * @param {string} emailAddress - * @param {IgcNotifyConfig} config * @param {IgcNotifyGenericMessage} message * @returns {IgcNotifyPostReturn} */ - async sendEmailGCNotification( - emailAddress: string, - config: IgcNotifyConfig, - message: IgcNotifyGenericMessage - ): Promise { + async sendEmailGCNotification(emailAddress: string, message: IgcNotifyGenericMessage): Promise { const data = { email_address: emailAddress, template_id: EMAIL_TEMPLATE, @@ -49,15 +51,10 @@ export class GCNotifyService { * * * @param {string} sms - * @param {IgcNotifyConfig} config * @param {IgcNotifyGenericMessage} message * @returns {IgcNotifyPostReturn} */ - async sendPhoneNumberGCNotification( - sms: string, - config: IgcNotifyConfig, - message: IgcNotifyGenericMessage - ): Promise { + async sendPhoneNumberGCNotification(sms: string, message: IgcNotifyGenericMessage): Promise { const data = { phone_number: sms, template_id: SMS_TEMPLATE,