Skip to content

Commit 364da4f

Browse files
committed
Refactoring of Notification type definitions and Object.serializer
1 parent 4ebc4ca commit 364da4f

14 files changed

+570
-279
lines changed

src/__mocks__/notification/captureFalse.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
}
2323
}
2424
]
25-
}
25+
}

src/__tests__/hmacValidator.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import HmacValidator from "../utils/hmacValidator";
2-
import {NotificationRequestItem} from "../typings/notification";
2+
import {AdditionalData, NotificationRequestItem} from "../typings/notification/models";
33
import {ApiConstants} from "../constants/apiConstants";
44

55
const key = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00";
6-
const expectedSign = "ipnxGCaUZ4l8TUW75a71/ghd2Fe5ffvX0pV4TLTntIc=";
6+
const expectedSign = "xhpHvYq2u2Np0rstbZ6QjLGu7JWhvf7Iwaa6EviJSX0=";
77
const notificationRequestItem: NotificationRequestItem = {
88
pspReference: "pspReference",
99
originalReference: "originalReference",
1010
merchantAccountCode: "merchantAccount",
1111
merchantReference: "reference",
1212
amount: {currency: "EUR", value: 1000},
13-
eventCode: "EVENT",
14-
eventDate: new Date("01-01-1970"),
13+
eventCode: NotificationRequestItem.EventCodeEnum.CAPTURE,
14+
eventDate: new Date("01-01-1970").toISOString(),
1515
paymentMethod: "VISA",
1616
reason: "reason",
17-
success: "true",
18-
additionalData: { [ApiConstants.HMAC_SIGNATURE]: expectedSign },
17+
success: NotificationRequestItem.SuccessEnum.True,
18+
additionalData: { [ApiConstants.HMAC_SIGNATURE as keyof AdditionalData]: expectedSign },
1919
};
2020

2121
describe("HMAC Validator", function (): void {
@@ -39,7 +39,7 @@ describe("HMAC Validator", function (): void {
3939
});
4040
it("should get correct data to sign", function (): void {
4141
const data = hmacValidator.getDataToSign(notificationRequestItem);
42-
expect(data).toEqual("pspReference:originalReference:merchantAccount:reference:1000:EUR:EVENT:true");
42+
expect(data).toEqual("pspReference:originalReference:merchantAccount:reference:1000:EUR:CAPTURE:true");
4343
});
4444

4545
it("should have valid hmac", function (): void {
@@ -51,7 +51,7 @@ describe("HMAC Validator", function (): void {
5151
it("should have invalid hmac", function (): void {
5252
const invalidNotification = {
5353
...notificationRequestItem,
54-
additionalData: { [ApiConstants.HMAC_SIGNATURE]: "notValidSign" }
54+
additionalData: { [ApiConstants.HMAC_SIGNATURE as keyof NotificationRequestItem]: "notValidSign" }
5555
};
5656
const result = hmacValidator.validateHMAC(invalidNotification, key);
5757
expect(result).toBeFalsy();

src/__tests__/notification.spec.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import captureFalse from "../__mocks__/notification/captureFalse.json";
44
import refundTrue from "../__mocks__/notification/refundTrue.json";
55
import refundFalse from "../__mocks__/notification/refundFalse.json";
66
import NotificationRequest from "../notification/notificationRequest";
7-
import {Notification, NotificationEnum, NotificationRequestItem} from "../typings/notification";
7+
import {Notification, NotificationRequestItem} from "../typings/notification/models";
8+
9+
import NotificationEnum = NotificationRequestItem.EventCodeEnum;
10+
import SuccessEnum = NotificationRequestItem.SuccessEnum;
811

912
describe("Notification Test", function (): void {
1013
it("should return authorisation success", function (): void {
@@ -13,8 +16,8 @@ describe("Notification Test", function (): void {
1316

1417
if (notificationRequest.notificationItems) {
1518
const notificationRequestItem: NotificationRequestItem = notificationRequest.notificationItems[0];
16-
expect(NotificationEnum.EVENT_CODE_AUTHORISATION).toEqual(notificationRequestItem.eventCode);
17-
expect(notificationRequestItem.success === "true").toBeTruthy();
19+
expect(NotificationEnum.AUTHORISATION).toEqual(notificationRequestItem.eventCode);
20+
expect(notificationRequestItem.success === SuccessEnum.True).toBeTruthy();
1821
expect(notificationRequestItem.pspReference).toEqual("123456789");
1922
} else {
2023
fail();
@@ -27,8 +30,8 @@ describe("Notification Test", function (): void {
2730

2831
if (notificationRequest.notificationItems) {
2932
const notificationRequestItem = notificationRequest.notificationItems[0];
30-
expect(NotificationEnum.EVENT_CODE_CAPTURE).toEqual(notificationRequestItem.eventCode);
31-
expect(notificationRequestItem.success === "true").toBeTruthy();
33+
expect(NotificationEnum.CAPTURE).toEqual(notificationRequestItem.eventCode);
34+
expect(notificationRequestItem.success === SuccessEnum.True).toBeTruthy();
3235
expect(notificationRequestItem.pspReference).toEqual("PSP_REFERENCE");
3336
expect(notificationRequestItem.originalReference).toEqual("ORIGINAL_PSP");
3437
} else {
@@ -42,8 +45,8 @@ describe("Notification Test", function (): void {
4245

4346
if (notificationRequest.notificationItems) {
4447
const notificationRequestItem = notificationRequest.notificationItems[0];
45-
expect(NotificationEnum.EVENT_CODE_CAPTURE).toEqual(notificationRequestItem.eventCode);
46-
expect(notificationRequestItem.success === "true").toBeFalsy();
48+
expect(NotificationEnum.CAPTURE).toEqual(notificationRequestItem.eventCode);
49+
expect(notificationRequestItem.success === SuccessEnum.True).toBeFalsy();
4750
expect(notificationRequestItem.pspReference).toEqual("PSP_REFERENCE");
4851
expect(notificationRequestItem.originalReference).toEqual("ORIGINAL_PSP");
4952
} else {
@@ -57,8 +60,8 @@ describe("Notification Test", function (): void {
5760

5861
if (notificationRequest.notificationItems) {
5962
const notificationRequestItem = notificationRequest.notificationItems[0];
60-
expect(NotificationEnum.EVENT_CODE_REFUND).toEqual(notificationRequestItem.eventCode);
61-
expect(notificationRequestItem.success === "true").toBeTruthy();
63+
expect(NotificationEnum.REFUND).toEqual(notificationRequestItem.eventCode);
64+
expect(notificationRequestItem.success === SuccessEnum.True).toBeTruthy();
6265
expect(notificationRequestItem.pspReference).toEqual("PSP_REFERENCE");
6366
expect(notificationRequestItem.originalReference).toEqual("ORIGINAL_PSP");
6467
expect(notificationRequestItem.eventDate).toBeDefined();
@@ -73,8 +76,8 @@ describe("Notification Test", function (): void {
7376

7477
if (notificationRequest.notificationItems) {
7578
const notificationRequestItem = notificationRequest.notificationItems[0];
76-
expect(NotificationEnum.EVENT_CODE_REFUND).toEqual(notificationRequestItem.eventCode);
77-
expect(notificationRequestItem.success === "true").toBeFalsy();
79+
expect(NotificationEnum.REFUND).toEqual(notificationRequestItem.eventCode);
80+
expect(notificationRequestItem.success === SuccessEnum.True).toBeFalsy();
7881
expect(notificationRequestItem.pspReference).toEqual("PSP_REFERENCE");
7982
expect(notificationRequestItem.originalReference).toEqual("ORIGINAL_PSP");
8083
expect(notificationRequestItem.eventDate).toBeDefined();

src/__tests__/platforms.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let accountHolderToUnSuspend: A.CreateAccountHolderResponse;
2323
let accountHolderToClose: A.CreateAccountHolderResponse;
2424
let notificationConfigurationToRetrieve: N.GetNotificationConfigurationResponse;
2525

26-
const generateRandomCode = () => Math.floor(Math.random() * Date.now()).toString();
26+
const generateRandomCode = (): string => Math.floor(Math.random() * Date.now()).toString();
2727
const accountHolderDetails: AccountHolderDetails = {
2828
2929
fullPhoneNumber: "312030291928",

src/notification/notificationRequest.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@
1818
* This file is open source and available under the MIT license.
1919
* See the LICENSE file for more info.
2020
*/
21-
import {Convert, Notification, NotificationItem, NotificationRequestItem} from "../typings/notification";
21+
import {
22+
Notification,
23+
NotificationItem,
24+
NotificationRequestItem,
25+
ObjectSerializer
26+
} from "../typings/notification/models";
2227

2328
class NotificationRequest {
2429
public constructor(json: Notification) {
25-
const notification = Convert.toNotification(JSON.stringify(json));
30+
const notification: Notification = ObjectSerializer.deserialize(json, "Notification");
2631
this.notificationItemContainers = notification.notificationItems;
2732
this.live = notification.live;
2833
}
2934

30-
public get notificationItems(): NotificationRequestItem[] | undefined {
35+
public get notificationItems(): Array<NotificationRequestItem> | undefined {
3136
if (!this.notificationItemContainers) {
3237
return undefined;
3338
}
3439

35-
return this.notificationItemContainers.map((container): NotificationRequestItem => container.NotificationRequestItem);
40+
return this.notificationItemContainers.map((container): NotificationRequestItem => container.notificationRequestItem);
3641
}
3742

3843
public live: string;
39-
public notificationItemContainers: NotificationItem[];
44+
public notificationItemContainers: Array<NotificationItem>;
4045
}
4146

4247
export default NotificationRequest;

src/typings/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// <reference path="enums/environment.ts" />
88
/// <reference path="enums/vatCategory.ts" />
99
/// <reference path="nexo.ts" />
10-
/// <reference path="notification.ts" />
10+
/// <reference path="notification/notification.ts" />
1111
/// <reference path="payments.ts" />
1212
/// <reference path="payouts.ts" />
1313
/// <reference path="recurring.ts" />

0 commit comments

Comments
 (0)