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

feat(dal): change the month add and sub for days add and sub #4071

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/api/migrations/expire-at/expire-at.migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
EnvironmentRepository,
JobStatusEnum,
} from '@novu/dal';
import { addMinutes, addMonths } from 'date-fns';
import { addMinutes, addDays } from 'date-fns';
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../../src/app.module';

Expand All @@ -19,8 +19,8 @@ const executionDetailsRepository = new ExecutionDetailsRepository();
const organizationRepository = new OrganizationRepository();
const environmentRepository = new EnvironmentRepository();
const now = Date.now();
let expireAtOneMonth = addMonths(now, 1);
let expireAtOneYear = addMonths(now, 12);
let expireAtOneMonth = addDays(now, 30);
let expireAtOneYear = addDays(now, 365);

export async function createExpireAt() {
const app = await NestFactory.create(AppModule, {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/app/events/e2e/delay-events.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { expect } from 'chai';
import axios from 'axios';
import { addSeconds, differenceInMilliseconds, subMonths } from 'date-fns';
import { addSeconds, differenceInMilliseconds, subDays } from 'date-fns';
import {
MessageRepository,
NotificationTemplateEntity,
Expand Down Expand Up @@ -91,8 +91,8 @@ describe('Trigger event - Delay triggered events - /v1/events/trigger (POST)', f
const expireAt = new Date(delayedJob?.expireAt as string);
const createdAt = new Date(delayedJob?.createdAt as string);

const subExpireMonths = subMonths(expireAt, 1);
const diff = differenceInMilliseconds(subExpireMonths, createdAt);
const subExpire30Days = subDays(expireAt, 30);
const diff = differenceInMilliseconds(subExpire30Days, createdAt);

expect(diff).to.approximately(100, 200);

Expand Down
15 changes: 7 additions & 8 deletions apps/api/src/app/events/e2e/trigger-event.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from 'chai';
import axios from 'axios';
import { v4 as uuid } from 'uuid';
import { differenceInMilliseconds, subMonths } from 'date-fns';
import { differenceInMilliseconds, subDays } from 'date-fns';
import {
MessageRepository,
NotificationRepository,
Expand Down Expand Up @@ -30,12 +30,11 @@ import {
DelayTypeEnum,
PreviousStepTypeEnum,
InAppProviderIdEnum,
MESSAGE_IN_APP_RETENTION_DAYS,
MESSAGE_GENERIC_RETENTION_DAYS,
} from '@novu/shared';
import { EmailEventStatusEnum } from '@novu/stateless';

const IN_APP_MESSAGE_EXPIRE_MONTHS = 12;
const MESSAGE_EXPIRE_MONTHS = 1;

const axiosInstance = axios.create();

const eventTriggerPath = '/v1/events/trigger';
Expand Down Expand Up @@ -473,8 +472,8 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
let expireAt = new Date(message?.expireAt as string);
let createdAt = new Date(message?.createdAt as string);

let subExpireMonths = subMonths(expireAt, IN_APP_MESSAGE_EXPIRE_MONTHS);
let diff = differenceInMilliseconds(subExpireMonths, createdAt);
const subExpireYear = subDays(expireAt, MESSAGE_IN_APP_RETENTION_DAYS);
let diff = differenceInMilliseconds(subExpireYear, createdAt);

expect(diff).to.approximately(0, 100);

Expand All @@ -490,8 +489,8 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
expireAt = new Date(email?.expireAt as string);
createdAt = new Date(email?.createdAt as string);

subExpireMonths = subMonths(expireAt, MESSAGE_EXPIRE_MONTHS);
diff = differenceInMilliseconds(subExpireMonths, createdAt);
const subExpireMonth = subDays(expireAt, MESSAGE_GENERIC_RETENTION_DAYS);
diff = differenceInMilliseconds(subExpireMonth, createdAt);

expect(diff).to.approximately(0, 100);
});
Expand Down
13 changes: 9 additions & 4 deletions libs/dal/src/repositories/base-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ClassConstructor, plainToInstance } from 'class-transformer';
import { addMonths } from 'date-fns';
import { addDays } from 'date-fns';
import {
MESSAGE_GENERIC_RETENTION_DAYS,
MESSAGE_IN_APP_RETENTION_DAYS,
NOTIFICATION_RETENTION_DAYS,
} from '@novu/shared';
import { Model, Types, ProjectionType, FilterQuery, UpdateQuery, QueryOptions } from 'mongoose';
import { DalException } from '../shared';

Expand Down Expand Up @@ -98,12 +103,12 @@ export class BaseRepository<T_DBModel, T_MappedEntity, T_Enforcement = object> {
switch (modelName) {
case 'Message':
if (data.channel === 'in_app') {
return addMonths(startDate, 12);
return addDays(startDate, MESSAGE_IN_APP_RETENTION_DAYS);
} else {
return addMonths(startDate, 1);
return addDays(startDate, MESSAGE_GENERIC_RETENTION_DAYS);
}
case 'Notification':
return addMonths(startDate, 1);
return addDays(startDate, NOTIFICATION_RETENTION_DAYS);
default:
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions libs/shared/src/consts/data-retention/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NOTIFICATION_RETENTION_DAYS = 30;
export const MESSAGE_GENERIC_RETENTION_DAYS = 30;
export const MESSAGE_IN_APP_RETENTION_DAYS = 365;
1 change: 1 addition & 0 deletions libs/shared/src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data-retention';
export * from './providers';
export * from './notification-item-buttons';
export * from './handlebar-helpers';
Expand Down
Loading