Skip to content

Commit

Permalink
fix: πŸ› Corrige une erreur sentry 'b.includes is not a function' (#1433)
Browse files Browse the repository at this point in the history
* fix: πŸ› Corrige une erreur sentry 'b.includes is not a function'

βœ… Closes: #1426

* fix: πŸ› PR erreur sentry 'b.includes is not a function'
  • Loading branch information
benguedj authored Sep 13, 2022
1 parent e8256c0 commit 671fa57
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 20 deletions.
97 changes: 85 additions & 12 deletions front/src/utils/notifications/notification.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
} from "../../constants";
import type { Step } from "../../types";
import { NotificationUtils, StorageUtils } from "..";
import {
getNotificationTrigger,
NotificationType,
saveStepForCongratNotifScheduled,
} from "./notification.util";
import { NotificationType } from "./notification.util";

describe("Notification utils", () => {
describe("Build Articles Notification Content", () => {
Expand Down Expand Up @@ -107,22 +103,25 @@ describe("Notification utils", () => {
});

it("getNotificationTrigger with 0 article and no trigger", async () => {
const result = await getNotificationTrigger(0, null);
const result = await NotificationUtils.getNotificationTrigger(0, null);
const expected = NotificationConstants.MIN_TRIGGER;

expect(result).toEqual(expected);
});

it("getNotificationTrigger with articles", async () => {
const notifTrigger = { seconds: 20 };
const result = await getNotificationTrigger(5, notifTrigger);
const result = await NotificationUtils.getNotificationTrigger(
5,
notifTrigger
);
const expected = { seconds: 20 };

expect(result).toEqual(expected);
});

it("getNotificationTrigger with articles and no trigger", async () => {
const result = await getNotificationTrigger(5, null);
const result = await NotificationUtils.getNotificationTrigger(5, null);
const expected = new Date(
addDays(
new Date(),
Expand All @@ -139,13 +138,83 @@ describe("Notification utils", () => {
});
});

describe("hasBeenAlreadyNotifiedForArticles", () => {
afterEach(() => {
void AsyncStorage.clear();
});

it("hasBeenAlreadyNotifiedForArticles with currentStep is null", () => {
const CURRENT_STEP = null;
const stepsAlreadyCongratulatedForArticles = ["6"];
const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles(
CURRENT_STEP,
stepsAlreadyCongratulatedForArticles
);
expect(data).toBeFalsy();
});

it("hasBeenAlreadyNotifiedForArticles with stepsAlreadyCongratulatedForArticles is null", () => {
const CURRENT_STEP = {
active: true,
debut: 0,
description: null,
fin: 90,
id: 6,
nom: "De 0 Γ  3 mois",
ordre: 6,
};
const stepsAlreadyCongratulatedForArticles = null;
const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles(
CURRENT_STEP,
stepsAlreadyCongratulatedForArticles
);
expect(data).toBeFalsy();
});

it("hasBeenAlreadyNotifiedForArticles is false", () => {
const CURRENT_STEP = {
active: true,
debut: 0,
description: null,
fin: 90,
id: 6,
nom: "De 0 Γ  3 mois",
ordre: 6,
};
const stepsAlreadyCongratulatedForArticles = ["1"];
const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles(
CURRENT_STEP,
stepsAlreadyCongratulatedForArticles
);
expect(data).toBeFalsy();
});

it("hasBeenAlreadyNotifiedForArticles is true", () => {
const CURRENT_STEP = {
active: true,
debut: 0,
description: null,
fin: 90,
id: 6,
nom: "De 0 Γ  3 mois",
ordre: 6,
};
const stepsAlreadyCongratulatedForArticles = ["6"];
const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles(
CURRENT_STEP,
stepsAlreadyCongratulatedForArticles
);
expect(data).toBeTruthy();
});
});

describe("saveStepForCongratNotifScheduled", () => {
afterEach(() => {
void AsyncStorage.clear();
});

it("saveStepForCongratNotifScheduled with no article and currentStep is null", async () => {
await saveStepForCongratNotifScheduled(0, null, null);
await NotificationUtils.saveStepForCongratNotifScheduled(0, null, null);
await StorageUtils.getObjectValue(
StorageKeysConstants.stepsAlreadyCongratulatedForArticles
).then((data) => {
Expand All @@ -165,7 +234,11 @@ describe("Notification utils", () => {
};
const expected = ["6"];

await saveStepForCongratNotifScheduled(0, currentStep, null);
await NotificationUtils.saveStepForCongratNotifScheduled(
0,
currentStep,
null
);
await StorageUtils.getObjectValue(
StorageKeysConstants.stepsAlreadyCongratulatedForArticles
).then((data) => {
Expand All @@ -184,9 +257,9 @@ describe("Notification utils", () => {
ordre: 6,
};
const stepsAlreadyCongratulatedForArticles = ["2", "5"];
const expected = stepsAlreadyCongratulatedForArticles.length + 1;
const expected = ["2", "5", "6"];

await saveStepForCongratNotifScheduled(
await NotificationUtils.saveStepForCongratNotifScheduled(
0,
currentStep,
stepsAlreadyCongratulatedForArticles
Expand Down
32 changes: 24 additions & 8 deletions front/src/utils/notifications/notification.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,30 @@ export const saveStepForCongratNotifScheduled = async (
): Promise<void> => {
if (nbArticlesToRead === 0 && currentStep) {
const currentStepId = currentStep.id.toString();
const newValue = stepsAlreadyCongratulatedForArticles
? stepsAlreadyCongratulatedForArticles.push(currentStepId)
: [currentStepId];
if (stepsAlreadyCongratulatedForArticles) {
stepsAlreadyCongratulatedForArticles.push(currentStepId);
}
const newValue = stepsAlreadyCongratulatedForArticles ?? [currentStepId];
await StorageUtils.storeObjectValue(
StorageKeysConstants.stepsAlreadyCongratulatedForArticles,
newValue
);
}
};

export const hasBeenAlreadyNotifiedForArticles = (
currentStep: Step | null,
stepsAlreadyCongratulatedForArticles: string[] | null
): boolean => {
let hasBeenAlreadyNotified = false;
if (Array.isArray(stepsAlreadyCongratulatedForArticles)) {
hasBeenAlreadyNotified = stepsAlreadyCongratulatedForArticles.includes(
currentStep ? currentStep.id.toString() : ""
);
}
return hasBeenAlreadyNotified;
};

export const scheduleArticlesNotification = async (
notifTrigger?: NotificationTriggerInput
): Promise<void> => {
Expand All @@ -426,11 +440,13 @@ export const scheduleArticlesNotification = async (

if (content) {
await cancelAllNotificationsByType(NotificationType.articles);
const hasBeenAlreadyNotified =
stepsAlreadyCongratulatedForArticles?.includes(
currentStep ? currentStep.id.toString() : ""
);
if (!hasBeenAlreadyNotified) {

if (
!hasBeenAlreadyNotifiedForArticles(
currentStep,
stepsAlreadyCongratulatedForArticles
)
) {
await sendNotificationReminder(content, trigger);
await saveStepForCongratNotifScheduled(
nbArticlesToRead,
Expand Down

0 comments on commit 671fa57

Please sign in to comment.