Skip to content

Commit

Permalink
CHES Notifications & Logger Adjustment (#2730)
Browse files Browse the repository at this point in the history
Co-authored-by: LawrenceLau2020 <68400651+LawrenceLau2020@users.noreply.github.com>
  • Loading branch information
dbarkowsky and LawrenceLau2020 authored Oct 22, 2024
1 parent eaf7cdf commit 6c9077c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
10 changes: 9 additions & 1 deletion express-api/src/services/ches/chesServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ const sendAsync = async (endpoint: string, method: string, data: Record<string,
body: data ? JSON.stringify(data) : undefined,
});

if (!response.ok) {
logger.error({
message: 'CHES Error',
chesResponse: JSON.parse(await response.text()),
});
return null;
}

const text = await response.text();
if (text.length) {
return JSON.parse(text);
} else {
return {};
return null;
}
};

Expand Down
6 changes: 4 additions & 2 deletions express-api/src/utilities/winstonLogger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { format, createLogger, transports } from 'winston';
import constants from '@/constants';

const { timestamp, combine, json } = format;
const { timestamp, combine, json, prettyPrint } = format;
const { TESTING } = constants;
const { CONTAINERIZED } = process.env;

/**
* Creates a logger object that can be called to generate log messages.
Expand All @@ -19,7 +20,8 @@ const logger = createLogger({
timestamp({
format: 'YYYY-MM-DD hh:mm:ss.SSS A',
}),
json(),
CONTAINERIZED ? json() : prettyPrint(),
format.errors({ stack: true }),
),
transports: [new transports.Console()],
silent: TESTING,
Expand Down
36 changes: 35 additions & 1 deletion express-api/tests/unit/services/ches/chesServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`,
}));
const response = await chesServices.sendEmailAsync(email, keycloak);
Expand All @@ -35,10 +36,37 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: false,
text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`,
}));
expect(async () => await chesServices.sendEmailAsync(email, keycloak)).rejects.toThrow();
});
it('should return null if the CHES response is not OK', async () => {
const email = produceEmail({ cc: ['john@doe.com'], bcc: ['john@doe.com'] });
const keycloak = producePimsRequestUser();
_fetch.mockImplementationOnce(() => ({
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: false,
text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`,
}));
const result = await chesServices.sendEmailAsync(email, keycloak);
expect(result).toBeNull();
});
it('should return null if the CHES response has no text length', async () => {
const email = produceEmail({ cc: ['john@doe.com'], bcc: ['john@doe.com'] });
const keycloak = producePimsRequestUser();
_fetch.mockImplementationOnce(() => ({
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => ``,
}));
const result = await chesServices.sendEmailAsync(email, keycloak);
expect(result).toBeNull();
});
it('should send email with extra config', async () => {
const email: IEmail = produceEmail({});
const keycloak = producePimsRequestUser();
Expand All @@ -55,6 +83,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`,
}));
const response = await chesServices.sendEmailAsync(email, keycloak);
Expand All @@ -67,8 +96,8 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
{
"createdTS": 1560000000,
Expand Down Expand Up @@ -102,6 +131,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
[
{
Expand Down Expand Up @@ -143,6 +173,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
{
Expand Down Expand Up @@ -177,6 +208,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
{
Expand All @@ -202,6 +234,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
{
Expand Down Expand Up @@ -238,6 +271,7 @@ describe('UNIT - Ches Services', () => {
text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}',
}));
_fetch.mockImplementationOnce(() => ({
ok: true,
text: () => `
[
{
Expand Down

0 comments on commit 6c9077c

Please sign in to comment.