Skip to content

Commit

Permalink
move config variable and .env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE committed Jan 8, 2022
1 parent 38e4653 commit 3ecee12
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 88 deletions.
7 changes: 0 additions & 7 deletions api/src/models/gcnotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,3 @@ export interface IgcNotifyGenericMessage {
body2: string;
footer: string;
}

export interface IgcNotifyConfig {
headers: {
Authorization: string;
'Content-Type': string;
};
}
37 changes: 24 additions & 13 deletions api/src/paths/gcnotify/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'
}
}
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 9 additions & 53 deletions api/src/services/gcnotify-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -17,40 +17,20 @@ describe('GCNotifyService', () => {

const emailAddress = '[email protected]';

const config = {
headers: {
Authorization: 'api_key',
'Content-Type': 'application/json'
}
};

const message = {
header: 'message.header',
body1: 'message.body1',
body2: 'message.body2',
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');
Expand All @@ -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');
Expand All @@ -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);
});
Expand All @@ -92,40 +68,20 @@ describe('GCNotifyService', () => {

const sms = '2501231234';

const config = {
headers: {
Authorization: 'api_key',
'Content-Type': 'application/json'
}
};

const message = {
header: 'message.header',
body1: 'message.body1',
body2: 'message.body2',
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');
Expand All @@ -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');
Expand All @@ -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);
});
Expand Down
27 changes: 12 additions & 15 deletions api/src/services/gcnotify-service.ts
Original file line number Diff line number Diff line change
@@ -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<IgcNotifyPostReturn> {
async sendEmailGCNotification(emailAddress: string, message: IgcNotifyGenericMessage): Promise<IgcNotifyPostReturn> {
const data = {
email_address: emailAddress,
template_id: EMAIL_TEMPLATE,
Expand Down Expand Up @@ -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<IgcNotifyPostReturn> {
async sendPhoneNumberGCNotification(sms: string, message: IgcNotifyGenericMessage): Promise<IgcNotifyPostReturn> {
const data = {
phone_number: sms,
template_id: SMS_TEMPLATE,
Expand Down

0 comments on commit 3ecee12

Please sign in to comment.