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

CHES Notifications & Logger Adjustment #2730

Merged
merged 10 commits into from
Oct 22, 2024
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: ['[email protected]'], bcc: ['[email protected]'] });
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: ['[email protected]'], bcc: ['[email protected]'] });
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
Loading