Skip to content

Commit

Permalink
chore: make retention period configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
katyaterletskaya committed Jan 8, 2024
1 parent 4bdb723 commit 2785507
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
8 changes: 4 additions & 4 deletions apps/api/src/app/events/e2e/trigger-event.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import {
DelayTypeEnum,
PreviousStepTypeEnum,
InAppProviderIdEnum,
MESSAGE_IN_APP_RETENTION_DAYS,
MESSAGE_GENERIC_RETENTION_DAYS,
DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
ActorTypeEnum,
SystemAvatarIconEnum,
} from '@novu/shared';
Expand Down Expand Up @@ -1247,7 +1247,7 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
let expireAt = new Date(message?.expireAt as string);
let createdAt = new Date(message?.createdAt as string);

const subExpireYear = subDays(expireAt, MESSAGE_IN_APP_RETENTION_DAYS);
const subExpireYear = subDays(expireAt, DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS);
let diff = differenceInMilliseconds(subExpireYear, createdAt);

expect(diff).to.approximately(0, 100);
Expand All @@ -1264,7 +1264,7 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
expireAt = new Date(email?.expireAt as string);
createdAt = new Date(email?.createdAt as string);

const subExpireMonth = subDays(expireAt, MESSAGE_GENERIC_RETENTION_DAYS);
const subExpireMonth = subDays(expireAt, DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS);
diff = differenceInMilliseconds(subExpireMonth, createdAt);

expect(diff).to.approximately(0, 100);
Expand Down
9 changes: 9 additions & 0 deletions apps/worker/src/config/env-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ const validators: { [K in keyof any]: ValidatorSpec<any[K]> } = {
LAUNCH_DARKLY_SDK_KEY: str({
default: '',
}),
NOTIFICATION_RETENTION_DAYS: num({
default: 30,
}),
MESSAGE_GENERIC_RETENTION_DAYS: num({
default: 30,
}),
MESSAGE_IN_APP_RETENTION_DAYS: num({
default: 365,
}),
};

if (process.env.STORAGE_SERVICE === 'AZURE') {
Expand Down
3 changes: 3 additions & 0 deletions apps/worker/src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ declare global {
NEW_RELIC_LICENSE_KEY: string;
SEGMENT_TOKEN?: string;
LAUNCH_DARKLY_SDK_KEY?: string;
NOTIFICATION_RETENTION_DAYS?: number;
MESSAGE_GENERIC_RETENTION_DAYS?: number;
MESSAGE_IN_APP_RETENTION_DAYS?: number;
}
}
}
21 changes: 15 additions & 6 deletions libs/dal/src/repositories/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { ClassConstructor, plainToInstance } from 'class-transformer';
import { addDays } from 'date-fns';
import {
MESSAGE_GENERIC_RETENTION_DAYS,
MESSAGE_IN_APP_RETENTION_DAYS,
NOTIFICATION_RETENTION_DAYS,
DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
DEFAULT_NOTIFICATION_RETENTION_DAYS,
} from '@novu/shared';
import { Model, Types, ProjectionType, FilterQuery, UpdateQuery, QueryOptions } from 'mongoose';
import { DalException } from '../shared';
Expand Down Expand Up @@ -101,12 +101,21 @@ export class BaseRepository<T_DBModel, T_MappedEntity, T_Enforcement> {
switch (modelName) {
case 'Message':
if (data.channel === 'in_app') {
return addDays(startDate, MESSAGE_IN_APP_RETENTION_DAYS);
return addDays(
startDate,
Number(process.env.MESSAGE_IN_APP_RETENTION_DAYS || DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS)
);
} else {
return addDays(startDate, MESSAGE_GENERIC_RETENTION_DAYS);
return addDays(
startDate,
Number(process.env.MESSAGE_GENERIC_RETENTION_DAYS || DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS)
);
}
case 'Notification':
return addDays(startDate, NOTIFICATION_RETENTION_DAYS);
return addDays(
startDate,
Number(process.env.NOTIFICATION_RETENTION_DAYS || DEFAULT_NOTIFICATION_RETENTION_DAYS)
);
default:
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions libs/dal/src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ declare namespace NodeJS {
NODE_ENV: 'test' | 'production' | 'dev';
MONGO_MIN_POOL_SIZE: number;
MONGO_MAX_POOL_SIZE: number;
NOTIFICATION_RETENTION_DAYS?: number;
MESSAGE_GENERIC_RETENTION_DAYS?: number;
MESSAGE_IN_APP_RETENTION_DAYS?: number;
}
}
6 changes: 3 additions & 3 deletions libs/shared/src/consts/data-retention/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const NOTIFICATION_RETENTION_DAYS = 30;
export const MESSAGE_GENERIC_RETENTION_DAYS = 30;
export const MESSAGE_IN_APP_RETENTION_DAYS = 365;
export const DEFAULT_NOTIFICATION_RETENTION_DAYS = 30;
export const DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS = 30;
export const DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS = 365;

0 comments on commit 2785507

Please sign in to comment.