Skip to content

Commit

Permalink
Merge branch 'main' into feature/MOL-357/PICT-241
Browse files Browse the repository at this point in the history
  • Loading branch information
tdang1-shopmacher committed Sep 10, 2024
2 parents 231ec0f + 18de5c9 commit c9b5072
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Dockerfile → processor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ FROM node:18-alpine AS builder

WORKDIR /app

COPY ./processor/package*.json ./
COPY ./package*.json ./

RUN npm install --production --frozen-lockfile

COPY ./processor .
COPY . .

RUN npm run build

Expand Down
18 changes: 18 additions & 0 deletions processor/src/controllers/connector.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CustomError from '../errors/custom.error';
import { apiError } from '../api/error.api';
import { formatErrorResponse } from '../errors/mollie.error';
import { createExtensionAndCustomFields, removeExtension } from '../service/connector.service';
import { getProfile } from '../mollie/profile.mollie';

export const healthCheck = async (request: Request, response: Response) => {
try {
Expand All @@ -15,6 +16,23 @@ export const healthCheck = async (request: Request, response: Response) => {
}
};

export const mollieStatus = async (request: Request, response: Response) => {
try {
logger.debug('SCTM - mollieStatus - Checking Mollie API status.');
const profile = await getProfile();
logger.debug('SCTM - mollieStatus - Mollie API status is functioning.');
return response.status(200).json({
mode: profile.mode,
name: profile.name,
website: profile.website,
status: profile.status,
});
} catch (error) {
logger.error('SCTM - healthCheck - Unexpected error occurred when processing request', error);
return response.status(400).json(error).send();
}
};

export const install = async (request: Request, response: Response) => {
const protocol = request.secure ? 'https' : 'http';
const extensionUrl = `${protocol}://${request.hostname}/processor`;
Expand Down
22 changes: 22 additions & 0 deletions processor/src/mollie/profile.mollie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { initMollieClient } from '../client/mollie.client';
import { MollieApiError, Profile } from '@mollie/api-client';
import { logger } from '../utils/logger.utils';
import CustomError from '../errors/custom.error';

export const getProfile = async (): Promise<Profile> => {
try {
return await initMollieClient().profiles.getCurrent();
} catch (error) {
let errorMessage;
if (error instanceof MollieApiError) {
errorMessage = `SCTM - getProfile - error: ${error.message}, field: ${error.field}`;
} else {
errorMessage = `SCTM - getProfile - Failed to get Mollie profile with unknown errors`;
}

logger.error(errorMessage, {
error,
});
throw new CustomError(400, errorMessage);
}
};
4 changes: 3 additions & 1 deletion processor/src/routes/processor.route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Router } from 'express';
import { post } from '../controllers/processor.controller';
import { install, healthCheck, uninstall } from '../controllers/connector.controller';
import { install, healthCheck, uninstall, mollieStatus } from '../controllers/connector.controller';

const serviceRouter = Router();

serviceRouter.post('/', post);

serviceRouter.get('/health-check', healthCheck);

serviceRouter.get('/mollie/status', mollieStatus);

serviceRouter.post('/install', install);

serviceRouter.post('/uninstall', uninstall);
Expand Down
59 changes: 59 additions & 0 deletions processor/tests/mollie/profile.mollie.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals';
import { getProfile } from '../../src/mollie/profile.mollie';
import { MollieApiError, Profile } from '@mollie/api-client';
import { initMollieClient } from '../../src/client/mollie.client';

jest.mock('../../src/client/mollie.client', () => ({
initMollieClient: jest.fn(),
}));

describe('Test profile.mollie.ts', () => {
let mockProfile: Profile;

beforeEach(() => {
mockProfile = {
mode: 'test',
name: 'Test Profile',
website: 'https://example.com',
status: 'verified',
} as unknown as Profile;

(initMollieClient as jest.Mock).mockReturnValue({
profiles: {
getCurrent: jest.fn().mockReturnValue(mockProfile),
},
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return the current profile', async () => {
const profile = await getProfile();
expect(profile).toEqual(mockProfile);
expect(initMollieClient).toHaveBeenCalled();
});

it('should return Mollie API errors when fetching the profile', async () => {
const errorMessage = `SCTM - getProfile - error: Missing authentication, or failed to authenticate, field: undefined"`;
(initMollieClient as jest.Mock).mockReturnValue({
profiles: {
getCurrent: jest.fn().mockReturnValue(new MollieApiError(errorMessage)),
},
});

await expect(getProfile()).resolves.toThrow(errorMessage);
});

it('should return unknown errors when fetching the profile', async () => {
const errorMessage = 'SCTM - getProfile - Failed to get Mollie profile with unknown errors';
(initMollieClient as jest.Mock).mockReturnValue({
profiles: {
getCurrent: jest.fn().mockReturnValue(new Error(errorMessage)),
},
});

await expect(getProfile()).resolves.toThrow(errorMessage);
});
});

0 comments on commit c9b5072

Please sign in to comment.