Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BHBC-1503: GCNotfiy #689

Merged
merged 12 commits into from
Jan 10, 2022
233 changes: 233 additions & 0 deletions api/src/gcnotfiy-services/gcnotify-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { ApiError } from '../errors/custom-error';
import { GCNotifyService } from './gcnotify-service';
import axios from 'axios';
import { IgcNotfiyGenericMessage } from '../models/gcnotify';

chai.use(sinonChai);

describe('GCNotifyService', () => {
describe('sendGCNotification', () => {
afterEach(() => {
sinon.restore();
});

const url = 'url';

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

const data = {
email_address: 'emailAddress',
template_id: 'template',
personalisation: {
header: 'message.header',
main_body1: 'message.body1',
main_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.sendGCNotification('', config, data);
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 () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendGCNotification(url, {}, data);
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should throw a 400 error when no data is given', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendGCNotification(url, config, {});
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should not throw an error on success', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: 201 });

const result = await gcNotifyServiece.sendGCNotification(url, config, data);

expect(result).to.eql(201);
});
});

describe('sendEmailGCNotification', () => {
afterEach(() => {
sinon.restore();
});

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 () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendEmailGCNotification(emailAddress, {}, message);
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should throw a 400 error when no data is given', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendEmailGCNotification(emailAddress, config, message);
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should not throw an error on success', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: 201 });

const result = await gcNotifyServiece.sendEmailGCNotification(
emailAddress,
config,
{} as IgcNotfiyGenericMessage
);

expect(result).to.eql(201);
});
});

describe('sendSmsGCNotification', () => {
afterEach(() => {
sinon.restore();
});

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.sendSmsGCNotification('', 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 () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendSmsGCNotification(sms, {}, message);
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should throw a 400 error when no data is given', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: null });

try {
await gcNotifyServiece.sendSmsGCNotification(sms, config, message);
expect.fail();
} catch (actualError) {
expect((actualError as ApiError).message).to.equal('Failed to send Notification');
}
});

it('should not throw an error on success', async () => {
const gcNotifyServiece = new GCNotifyService();

sinon.stub(axios, 'post').resolves({ data: 201 });

const result = await gcNotifyServiece.sendSmsGCNotification(sms, config, {} as IgcNotfiyGenericMessage);

expect(result).to.eql(201);
});
});
});
97 changes: 97 additions & 0 deletions api/src/gcnotfiy-services/gcnotify-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import axios from 'axios';
import { ApiError, ApiErrorType } from '../errors/custom-error';
import { IgcNotfiyPostReturn, IgcNotfiyGenericMessage } from '../models/gcnotify';

KjartanE marked this conversation as resolved.
Show resolved Hide resolved
export class GCNotifyService {
/**
* Sends api call to gcnotify to send notification
*
*
* @param {string} url
* @param {object} config
* @param {object} data
*/
async sendGCNotification(url: string, config: object, data: object): Promise<IgcNotfiyPostReturn> {
const response = await axios.post(url, data, config);

const result = (response && response.data) || null;

if (!result) {
throw new ApiError(ApiErrorType.UNKNOWN, 'Failed to send Notification');
}

return result;
}

/**
* Send email notification to recipient
*
*
* @param {string} emailAddress
* @param {object} config
* @param {object} message
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
*/
async sendEmailGCNotification(
emailAddress: string,
config: object,
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
message: IgcNotfiyGenericMessage
): Promise<IgcNotfiyPostReturn> {
const template = process.env.GCNOTIFY_ONBOARDING_REQUEST_EMAIL_TEMPLATE;
const data = {
email_address: emailAddress,
template_id: template,
personalisation: {
header: message.header,
main_body1: message.body1,
main_body2: message.body2,
footer: message.footer
}
};

const response = await axios.post('https://api.notification.canada.ca/v2/notifications/email', data, config);
KjartanE marked this conversation as resolved.
Show resolved Hide resolved

const result = (response && response.data) || null;

if (!result) {
throw new ApiError(ApiErrorType.UNKNOWN, 'Failed to send Notification');
}

return result;
}

KjartanE marked this conversation as resolved.
Show resolved Hide resolved
/**
* Send email notification to recipient
*
*
* @param {string} sms
* @param {object} config
* @param {object} message
*/
async sendSmsGCNotification(
sms: string,
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
config: object,
message: IgcNotfiyGenericMessage
): Promise<IgcNotfiyPostReturn> {
const template = process.env.GCNOTIFY_ONBOARDING_REQUEST_SMS_TEMPLATE;
const data = {
phone_number: sms,
template_id: template,
personalisation: {
header: message.header,
main_body1: message.body1,
main_body2: message.body2,
footer: message.footer
}
};

const response = await axios.post('https://api.notification.canada.ca/v2/notifications/sms', data, config);

const result = (response && response.data) || null;

if (!result) {
throw new ApiError(ApiErrorType.UNKNOWN, 'Failed to send Notification');
}

return result;
}
}
15 changes: 15 additions & 0 deletions api/src/models/gcnotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface IgcNotfiyPostReturn {
KjartanE marked this conversation as resolved.
Show resolved Hide resolved
content: object;
id: string;
reference: string;
scheduled_for: string;
template: object;
uri: string;
}

export interface IgcNotfiyGenericMessage {
header: string;
body1: string;
body2: string;
footer: string;
}
Loading