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

Add pluginType and refactor eventType #1233

Open
wants to merge 9 commits into
base: feature/SFI-959-reliability-tracking
Choose a base branch
from
47 changes: 26 additions & 21 deletions jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,20 @@ jest.mock(
action: { type: 'mockedAction' },
})),
createRedirectUrl: jest.fn(() => 'mocked_RedirectUrl'),
getService: jest.fn(() => ({
getURL: jest.fn(() => 'mocked_service_url'),
setURL: jest.fn(),
addHeader: jest.fn(),
call: jest.fn(() => ({
status: 'success',
isOk: jest.fn(() => true),
object: {
getText: jest.fn(() =>
'{"data":"mocked api response"}'),

},
}))
}))
}),
{ virtual: true },
);
Expand All @@ -320,6 +334,7 @@ jest.mock(
getCreditCardInstallments: jest.fn(() => true),
getAdyenTokenisationEnabled: jest.fn(() => true),
getAdyenClientKey: jest.fn(() => 'mocked_client_key'),
getAdyenApiKey: jest.fn(() => 'mocked_api_key'),
getGoogleMerchantID: jest.fn(() => 'mocked_google_merchant_id'),
getAdyenCardholderNameEnabled: jest.fn(() => true),
getAdyenPayPalIntent: jest.fn(() => 'mocked_intent'),
Expand Down Expand Up @@ -396,27 +411,6 @@ jest.mock(
{ virtual: true },
);

jest.mock(
'*/cartridge/adyen/utils/lineItemHelper',
() => ({
getDescription: jest.fn((lineItem) => lineItem.productName),
getId: jest.fn((lineItem) => lineItem.productID),
getQuantity: jest.fn((lineItem) => lineItem.quantityValue),
getItemAmount: jest.fn((lineItem) => ({
divide: jest.fn((quantity) => ({
getValue: jest.fn(() => lineItem.adjustedNetPrice / quantity),
})),
})),
getVatAmount: jest.fn((lineItem) => ({
divide: jest.fn((quantity) => ({
getValue: jest.fn(() => lineItem.getAdjustedTax / quantity),
})),
})),
getAllLineItems: jest.fn((lineItem) => lineItem),
}),
{ virtual: true },
);

jest.mock(
'*/cartridge/adyen/utils/paypalHelper',
() => ({
Expand Down Expand Up @@ -479,3 +473,14 @@ jest.mock(
}),
{ virtual: true },
);

jest.mock(
'*/cartridge/adyen/logs/adyenCustomLogs',
() => ({
fatal_log: jest.fn(),
error_log: jest.fn(),
debug_log: jest.fn(),
info_log: jest.fn(),
}),
{ virtual: true },
);
14 changes: 14 additions & 0 deletions jest/sfccPathSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,17 @@ jest.mock(
require('../src/cartridges/int_adyen_SFRA/cartridge/adyen/analytics/constants'),
{ virtual: true },
);

jest.mock(
'*/cartridge/adyen/logs/adyenError',
() =>
require('../src/cartridges/int_adyen_SFRA/cartridge/adyen/logs/adyenError'),
{ virtual: true },
);

jest.mock(
'*/cartridge/adyen/logs/setErrorType',
() =>
require('../src/cartridges/int_adyen_SFRA/cartridge/adyen/logs/setErrorType'),
{ virtual: true },
);
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('analyticsHook', () => {
};
onStartHandler(req);
expect(createAnalyticsEvent).toHaveBeenCalled();
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'START', 'EXPECTED', 'info');
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'expectedStart', 'EXPECTED', 'info');
})
it('should not create Start event when adyen analytics is disabled in BM', () => {
const req = {
Expand All @@ -51,7 +51,7 @@ describe('analyticsHook', () => {
};
onCompleteHandler(req, res);
expect(createAnalyticsEvent).toHaveBeenCalled();
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'END', 'EXPECTED', 'info');
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'expectedEnd', 'EXPECTED', 'info');
})
it('should create End event with unexpected status when onCompleteHandler is called for error in route', () => {
const req = {
Expand All @@ -62,7 +62,7 @@ describe('analyticsHook', () => {
};
onCompleteHandler(req, res);
expect(createAnalyticsEvent).toHaveBeenCalled();
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'END', 'UNEXPECTED', 'info');
expect(createAnalyticsEvent).toHaveBeenCalledWith('mocked_sessionID','Adyen-GetPaymentMethods', 'unexpectedEnd', 'UNEXPECTED', 'info');
})
it('should not create End event when adyen analytics is disabled in BM', () => {
const req = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,55 @@
const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
const { createCheckoutAttemptId } = require('../analyticsService');

const AdyenHelper = {
getApplicationInfo: jest.fn(),
};

const execute = jest.fn();
const constants = {
SERVICE: {
ADYEN_ANALYTICS: 'ADYEN_ANALYTICS',
},
};

const AdyenLogs = {
error_log: jest.fn(),
};

global.AdyenHelper = AdyenHelper;
global.execute = execute;
global.constants = constants;
global.AdyenLogs = AdyenLogs;

describe('createCheckoutAttemptId', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return checkoutAttemptId when the execute function is successful', () => {
const mockCheckoutAttemptId = 'test-checkout-attempt-id';
const mockApplicationInfo = { name: 'testApp' };

AdyenHelper.getApplicationInfo.mockReturnValue(mockApplicationInfo);
execute.mockReturnValue({ checkoutAttemptId: mockCheckoutAttemptId });
AdyenHelper.getService.mockImplementationOnce(() => ({
getURL: jest.fn(() => 'mocked_service_url'),
setURL: jest.fn(),
addHeader: jest.fn(),
call: jest.fn(() => ({
status: '200',
isOk: jest.fn(() => true),
object: {
getText: jest.fn(() =>
`{"checkoutAttemptId":"${mockCheckoutAttemptId}"}`),

},
}))
}));

const result = createCheckoutAttemptId();

setTimeout(() => {
expect(AdyenHelper.getApplicationInfo).toHaveBeenCalled();
expect(execute).toHaveBeenCalledWith(constants.SERVICE.ADYEN_ANALYTICS, {
applicationInfo: mockApplicationInfo,
channel: 'Web',
platform: 'Web',
});
expect(result).toEqual({ data: mockCheckoutAttemptId });
}, 0)
expect(result).toEqual({ data: mockCheckoutAttemptId });
});

it('should return an error object and log error when execute throws an error', () => {
const mockError = new Error('Execution failed');
AdyenHelper.getApplicationInfo.mockReturnValue({});
execute.mockImplementation(() => {
throw mockError;
});

AdyenHelper.getService.mockImplementationOnce(() => ({
getURL: jest.fn(() => 'mocked_service_url'),
setURL: jest.fn(),
addHeader: jest.fn(),
call: jest.fn(() => ({
status: 'failed',
isOk: jest.fn(() => false),
getError: jest.fn(() => ({
toString: jest.fn(() => '500')
})),
getStatus: jest.fn(() => 'failed'),
getErrorMessage: jest.fn(() => 'Service error'),
getMsg: jest.fn(() => 'Service error'),
}))
}));
const mockError = new Error('AdyenAnalytics service call error code 500 Error => ResponseStatus: failed | ResponseErrorText: Service error | ResponseText: Service error');
const result = createCheckoutAttemptId();

setTimeout(() => {
expect(AdyenHelper.getApplicationInfo).toHaveBeenCalled();
expect(AdyenLogs.error_log).toHaveBeenCalledWith(
'createCheckoutAttemptId for /analytics call failed:',
mockError
'createCheckoutAttemptId for /analytics call failed:', mockError
);
expect(result).toEqual({ error: true });
}, 0)
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function onStartHandler(req) {
analyticsEvent.createAnalyticsEvent(
session.sessionID,
path.join('-'),
analyticsConstants.eventType.START,
analyticsConstants.eventType.EXPECTED_START,
analyticsConstants.eventStatus.EXPECTED,
analyticsConstants.eventCode.INFO,
);
Expand All @@ -75,15 +75,15 @@ function onCompleteHandler(req, res) {
analyticsEvent.createAnalyticsEvent(
session.sessionID,
path.join('-'),
analyticsConstants.eventType.END,
analyticsConstants.eventType.UNEXPECTED_END,
analyticsConstants.eventStatus.UNEXPECTED,
analyticsConstants.eventCode.INFO,
);
} else {
analyticsEvent.createAnalyticsEvent(
session.sessionID,
path.join('-'),
analyticsConstants.eventType.END,
analyticsConstants.eventType.EXPECTED_END,
analyticsConstants.eventStatus.EXPECTED,
analyticsConstants.eventCode.INFO,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const AdyenConfigs = require('*/cartridge/adyen/utils/adyenConfigs');
const constants = require('*/cartridge/adyen/config/constants');
const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
const analyticsConstants = require('*/cartridge/adyen/analytics/constants');

function execute(serviceType, requestObject, checkoutAttemptID = '') {
const service = AdyenHelper.getService(serviceType);
Expand All @@ -26,7 +27,7 @@ function execute(serviceType, requestObject, checkoutAttemptID = '') {
const callResult = service.call(JSON.stringify(requestObject));
if (!callResult.isOk()) {
throw new Error(
`${serviceType} service call error code${callResult
`${serviceType} service call error code ${callResult
.getError()
.toString()} Error => ResponseStatus: ${callResult.getStatus()} | ResponseErrorText: ${callResult.getErrorMessage()} | ResponseText: ${callResult.getMsg()}`,
);
Expand All @@ -42,6 +43,7 @@ function createCheckoutAttemptId() {
applicationInfo: AdyenHelper.getApplicationInfo(),
channel: 'Web',
platform: 'Web',
pluginType: analyticsConstants.pluginType,
};

const response = execute(constants.SERVICE.ADYEN_ANALYTICS, requestObject);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module.exports = {
analyticsEventObjectId: 'AdyenAnalyticsEvents',
pluginType: 'salesforceCommerceCloud',
eventType: {
START: 'START',
END: 'END',
EXPECTED_START: 'expectedStart',
UNEXPECTED_START: 'unexpectedStart',
EXPECTED_END: 'expectedEnd',
UNEXPECTED_END: 'unexpectedEnd',
},
eventStatus: {
EXPECTED: 'EXPECTED',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function createRequestObjectForAllReferenceIds(groupedObjects) {
const requestObject = {
channel: 'Web',
platform: 'Web',
pluginType: constants.pluginType,
};

// Iterate over all referenceIds and group events into one requestObject
Expand All @@ -17,20 +18,20 @@ function createRequestObjectForAllReferenceIds(groupedObjects) {
events.forEach((event) => {
const eventObject = {
timestamp: new Date(event.creationDate).getTime().toString(),
type: 'focus', // this has to be changed once API accepts our event types
type: event.eventType,
target: event.eventStatus,
id: event.eventId,
component: event.eventSource,
};

const eventCode = event.eventCode.toLowerCase();
const eventTypes = [
const eventCodeList = [
constants.eventCode.INFO,
constants.eventCode.ERROR,
constants.eventCode.LOG,
];

if (eventTypes.includes(eventCode)) {
if (eventCodeList.includes(eventCode)) {
if (!requestObject[eventCode]) {
requestObject[eventCode] = [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function AdyenError(message) {
this.message = message || 'Something went wrong!';
this.name = this.constructor.name;
const error = new Error(this.message);
this.stack = error.stack;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AdyenError);
}
}
AdyenError.prototype = Object.create(Error.prototype);
AdyenError.prototype.constructor = AdyenError;

module.exports = {
AdyenError,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { AdyenError } = require('*/cartridge/adyen/logs/adyenError');

module.exports = (error, res, data) => {
if (error instanceof AdyenError) {
res.json({ ...data, error: true, errorType: 'AdyenError' });
} else {
res.json({ ...data, error: true });
}
};
Loading
Loading