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

chore: make retention period configurable #5055

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
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 @@ -1319,7 +1319,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 @@ -1336,7 +1336,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
14 changes: 14 additions & 0 deletions apps/api/src/config/env-validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { bool, cleanEnv, json, makeValidator, port, str, num, url, ValidatorSpec } from 'envalid';
import {
DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
DEFAULT_NOTIFICATION_RETENTION_DAYS,
} from '@novu/shared';

const str32 = makeValidator((variable) => {
if (!(typeof variable === 'string') || variable.length != 32) {
Expand Down Expand Up @@ -88,6 +93,15 @@ const validators: { [K in keyof any]: ValidatorSpec<any[K]> } = {
default: 'false',
choices: ['false', 'true'],
}),
NOTIFICATION_RETENTION_DAYS: num({
default: DEFAULT_NOTIFICATION_RETENTION_DAYS,
}),
MESSAGE_GENERIC_RETENTION_DAYS: num({
default: DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
}),
MESSAGE_IN_APP_RETENTION_DAYS: num({
default: DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
}),
};

if (process.env.STORAGE_SERVICE === 'AZURE') {
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ declare global {
SENTRY_DSN: string;
STRIPE_API_KEY: string;
STRIPE_CONNECT_SECRET: string;
NOTIFICATION_RETENTION_DAYS?: number;
MESSAGE_GENERIC_RETENTION_DAYS?: number;
MESSAGE_IN_APP_RETENTION_DAYS?: number;
}
}
}
14 changes: 14 additions & 0 deletions apps/worker/src/config/env-validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { cleanEnv, json, port, str, num, ValidatorSpec, makeValidator } from 'envalid';
import {
DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
DEFAULT_NOTIFICATION_RETENTION_DAYS,
} from '@novu/shared';

const str32 = makeValidator((variable) => {
if (!(typeof variable === 'string') || variable.length != 32) {
Expand Down Expand Up @@ -53,6 +58,15 @@ const validators: { [K in keyof any]: ValidatorSpec<any[K]> } = {
STRIPE_API_KEY: str({
default: undefined,
}),
NOTIFICATION_RETENTION_DAYS: num({
default: DEFAULT_NOTIFICATION_RETENTION_DAYS,
}),
MESSAGE_GENERIC_RETENTION_DAYS: num({
default: DEFAULT_MESSAGE_GENERIC_RETENTION_DAYS,
}),
MESSAGE_IN_APP_RETENTION_DAYS: num({
default: DEFAULT_MESSAGE_IN_APP_RETENTION_DAYS,
}),
};

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 @@ -35,6 +35,9 @@ declare global {
SEGMENT_TOKEN?: string;
LAUNCH_DARKLY_SDK_KEY?: string;
STRIPE_API_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;
Loading