Skip to content
Merged
19 changes: 11 additions & 8 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,23 @@ export interface AccountingFolder {
Event notifying the creation/addition of a document.

```ts
export enum DocumentKind {
DossierAnnuel = "AF",
DossierPermanent = "PF",
BaseDocumentaire = "DB",
ExternalDocument = "ED"
}
export const DOCUMENT_KIND = Object.freeze({
DossierAnnuel: "AF",
DossierPermanent: "PF",
BaseDocumentaire: "DB",
ExternalDocument: "ED",
MiscellaneousFlow: "MF"
});

export type DocumentKind = typeof DOCUMENT_KIND[keyof typeof DOCUMENT_KIND];

export interface Document {
name: "document";
scope: Scope;
operation: "CREATE";
data: {
id: string;
kind: DocumentKind;
kind: typeof DOCUMENT_KIND[keyof typeof DOCUMENT_KIND];
name: string;
};
}
Expand Down Expand Up @@ -256,7 +259,7 @@ export interface Document {
"type": "string"
},
"kind": {
"enum": ["AF", "PF", "DB", "ED"]
"enum": ["AF", "PF", "DB", "ED", "MF"]
},
"name": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@myunisoft/events",
"version": "7.0.6",
"version": "7.0.7",
"description": "MyUnisoft Events validation",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
18 changes: 9 additions & 9 deletions src/class/incomer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class Incomer <
#retryPublishLock = new Mutex({ concurrency: 1, keepReferencingTimers: false });
#registrationCbLock = new Mutex({ concurrency: 1, keepReferencingTimers: false });

#lastActivity: number;
private lastActivity: number;
#idleTime: number;
#eventsValidationFn: Map<string, ValidateFunction<Record<string, any>> | NestedValidationFunctions> | undefined;
#customValidationCbFn: ((event: T) => void) | undefined;
Expand Down Expand Up @@ -232,7 +232,7 @@ export class Incomer <
private async checkDispatcherState() {
const date = Date.now();

if ((Number(this.#lastActivity) + Number(this.#maxPingInterval)) < date) {
if ((Number(this.lastActivity) + Number(this.#maxPingInterval)) < date) {
this.dispatcherConnectionState = false;

return;
Expand Down Expand Up @@ -341,7 +341,7 @@ export class Incomer <
});

this.#checkDispatcherStateInterval = setInterval(() => {
if (!this.#lastActivity) {
if (!this.lastActivity) {
return;
}

Expand All @@ -350,7 +350,7 @@ export class Incomer <
}, this.#maxPingInterval).unref();

this.#checkTransactionsStateInterval = setInterval(() => {
if (!this.#lastActivity) {
if (!this.lastActivity) {
return;
}

Expand Down Expand Up @@ -422,7 +422,7 @@ export class Incomer <
});

this.#checkDispatcherStateInterval = setInterval(() => {
if (!this.#lastActivity) {
if (!this.lastActivity) {
return;
}

Expand All @@ -431,15 +431,15 @@ export class Incomer <
}, this.#maxPingInterval).unref();

this.#checkTransactionsStateInterval = setInterval(() => {
if (!this.#lastActivity) {
if (!this.lastActivity) {
return;
}

this.lazyRetryPublish()
.catch((error) => this.logger.error({ error: error.stack }, "failed while retry publishing"));
}, this.#publishInterval).unref();

this.#lastActivity = Date.now();
this.lastActivity = Date.now();
this.dispatcherConnectionState = true;

this.logger.info(`Incomer registered with uuid ${this.providedUUID}`);
Expand Down Expand Up @@ -715,7 +715,7 @@ export class Incomer <
const { redisMetadata } = message;
const { transactionId } = redisMetadata;

this.#lastActivity = Date.now();
this.lastActivity = Date.now();
this.dispatcherConnectionState = true;

const logData = {
Expand Down Expand Up @@ -906,7 +906,7 @@ export class Incomer <

await Promise.all(transactionToUpdate);

this.#lastActivity = Date.now();
this.lastActivity = Date.now();
this.dispatcherConnectionState = true;
this.emit("registered");
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema/events/document.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"pattern": "^[0-9]+"
},
"kind": {
"enum": ["AF", "PF", "DB", "ED"]
"enum": ["AF", "PF", "DB", "ED", "MF"]
},
"name": {
"type": "string"
Expand Down
15 changes: 9 additions & 6 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export interface AccountingFolder {
};
}

export enum DocumentKind {
DossierAnnuel = "AF",
DossierPermanent = "PF",
BaseDocumentaire = "DB",
ExternalDocument = "ED"
}
export const DOCUMENT_KIND = Object.freeze({
DossierAnnuel: "AF",
DossierPermanent: "PF",
BaseDocumentaire: "DB",
ExternalDocument: "ED",
MiscellaneousFlow: "MF"
});

export type DocumentKind = typeof DOCUMENT_KIND[keyof typeof DOCUMENT_KIND];

export interface Document {
name: "document";
Expand Down
10 changes: 6 additions & 4 deletions test/UT/class/dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe("Dispatcher", () => {
await subscriber.initialize();

dispatcher = new Dispatcher({
name: "foo",
redis,
subscriber,
logger,
Expand Down Expand Up @@ -161,7 +162,7 @@ describe("Dispatcher", () => {
});

describe("Publishing a well formed register event but multiple times", () => {
let channel;
let channel: Channel;
let incomerTransactionStore: TransactionStore<"incomer">;

const event = {
Expand Down Expand Up @@ -394,6 +395,7 @@ describe("Dispatcher", () => {
}

dispatcher = new Dispatcher({
name: "foo",
redis,
subscriber,
logger,
Expand Down Expand Up @@ -659,11 +661,11 @@ describe("Dispatcher", () => {
const secondIncomerName = "bar";
const secondUuid = randomUUID();
let firstIncomerProvidedUUID;
let secondIncomerProvidedUUID;
let secondIncomerProvidedUUID: string;
let hasDistributedEvents = false;
let firstIncomerTransactionStore: TransactionStore<"incomer">;
let secondIncomerTransactionStore: TransactionStore<"incomer">;
let mainTransactionId;
let mainTransactionId: string | null | undefined;

beforeAll(async() => {
await subscriber.subscribe("dispatcher");
Expand Down Expand Up @@ -835,7 +837,7 @@ describe("Dispatcher", () => {
test("it should have distributed the event & resolve the main transaction", async() => {
await timers.setTimeout(10_000);

const transaction = await firstIncomerTransactionStore.getTransactionById(mainTransactionId);
const transaction = await firstIncomerTransactionStore.getTransactionById(mainTransactionId!);

expect(transaction).toBeNull();

Expand Down
5 changes: 3 additions & 2 deletions test/UT/class/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
eventsValidationFn,
type EventOptions,
type Events,
DocumentKind
DOCUMENT_KIND
} from "../../../src/index.js";
import {
Transaction,
Expand Down Expand Up @@ -64,6 +64,7 @@ async function initDispatcherInstance(
const { pingInterval, checkTransactionInterval, idleTime, checkLastActivityInterval } = options;

const dispatcher = new Dispatcher<EventOptions<keyof Events>>({
name: "foo",
redis,
subscriber,
pingInterval,
Expand Down Expand Up @@ -448,7 +449,7 @@ describe("event", () => {
data: {
id: "1",
name: "foo",
kind: DocumentKind.DossierAnnuel
kind: DOCUMENT_KIND.DossierAnnuel
},
scope: {
schemaId: 1,
Expand Down
4 changes: 2 additions & 2 deletions test/UT/class/handle-inactive-no-backup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe("Publishing/exploiting a custom event & inactive incomer", () => {

Reflect.set(concernedIncomer, "newTransactionStore", firstIncomerTransactionStore);

concernedIncomer["lastPingDate"] = Date.now();
concernedIncomer["lastActivity"] = Date.now();
concernedIncomer.emit("registered");

handleApprovementIndex++;
Expand All @@ -146,7 +146,7 @@ describe("Publishing/exploiting a custom event & inactive incomer", () => {

Reflect.set(secondConcernedIncomer, "newTransactionStore", secondIncomerTransactionStore);

secondConcernedIncomer["lastPingDate"] = Date.now();
secondConcernedIncomer["lastActivity"] = Date.now();
secondConcernedIncomer.emit("registered");
}
});
Expand Down
4 changes: 2 additions & 2 deletions test/UT/class/handle-inactive-with-backup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Publishing/exploiting a custom event & inactive incomer", () => {

Reflect.set(concernedIncomer, "newTransactionStore", firstIncomerTransactionStore);

concernedIncomer["lastPingDate"] = Date.now();
concernedIncomer["lastActivity"] = Date.now();
concernedIncomer.emit("registered");

handleApprovementIndex++;
Expand All @@ -144,7 +144,7 @@ describe("Publishing/exploiting a custom event & inactive incomer", () => {

Reflect.set(secondConcernedIncomer, "newTransactionStore", secondIncomerTransactionStore);

secondConcernedIncomer["lastPingDate"] = Date.now();
secondConcernedIncomer["lastActivity"] = Date.now();
secondConcernedIncomer.emit("registered");
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/UT/class/incomer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe("Init Incomer with Dispatcher alive", () => {
pingInterval: pingInterval,
idleTime: kIdleTime,
incomerUUID: dispatcherIncomer.baseUUID,
instanceName: "node:Pulsar"
name: "node:Pulsar"
});

await dispatcher.initialize();
Expand Down
10 changes: 5 additions & 5 deletions test/UT/class/registration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe("Registration", () => {
}
};

function updateIncomerState(...args) {
incomer["lastPingDate"] = Date.now();
function updateIncomerState(...args: any[]) {
incomer["lastActivity"] = Date.now();
incomer["dispatcherConnectionState"] = true;
}

Expand Down Expand Up @@ -95,10 +95,10 @@ describe("Registration", () => {
});

describe("Initializing a new Incomer", () => {
let handlePingFn: (...any) => any;
let registerFn: (...any) => Promise<any>;
let handlePingFn: (...arg0: any[]) => any;
let registerFn: (...arg0: any[]) => Promise<any>;
let incomerProvidedUUID: string;
let callLength;
let callLength: number;

const eventComeBackHandler = jest.fn().mockImplementation(() => Ok({ status: "RESOLVED" }));

Expand Down
2 changes: 1 addition & 1 deletion test/UT/class/service/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe("EventsService", () => {
test("Calling forceDispatcherTakeLead as the only dispatcher, it should stay alive", async() => {
let incomers = await dispatcher.eventsService.getIncomers();

dispatcher.eventsService.forceDispatcherTakeLead(incomers, incomers[0]);
dispatcher.eventsService.forceDispatcherTakeLead(incomers, [...incomers.values()][0]);

await timers.setTimeout(2_000);

Expand Down
Loading
Loading