Skip to content

Commit

Permalink
feat(cron): implement permanent deletion of records
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Dec 31, 2024
1 parent 0e76f25 commit 0dce408
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cron/daily/96-permanant-delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Transaction } from 'sequelize';

import models, { ModelType, Op, sequelize } from '../../server/models';
import { runCronJob } from '../utils';

enum RetentionPeriod {
FINANCIAL = '7 year',
SENSITIVE = '1 year',
DEFAULT = '6 months',
REDUCED = '1 month',
}

const MODEL_RETENTION_PERIODS = new Map<ModelType, RetentionPeriod>([
[models.Comment, RetentionPeriod.SENSITIVE],
[models.ConnectedAccount, RetentionPeriod.DEFAULT],
[models.Conversation, RetentionPeriod.DEFAULT],
[models.Expense, RetentionPeriod.SENSITIVE],
[models.ExpenseItem, RetentionPeriod.SENSITIVE],
[models.HostApplication, RetentionPeriod.SENSITIVE],
[models.LegalDocument, RetentionPeriod.FINANCIAL],
[models.Location, RetentionPeriod.SENSITIVE],
[models.Member, RetentionPeriod.FINANCIAL],
[models.MemberInvitation, RetentionPeriod.DEFAULT],
[models.OAuthAuthorizationCode, RetentionPeriod.REDUCED],
[models.Order, RetentionPeriod.FINANCIAL],
[models.PaymentMethod, RetentionPeriod.FINANCIAL],
[models.PayoutMethod, RetentionPeriod.FINANCIAL],
[models.PaypalPlan, RetentionPeriod.DEFAULT],
[models.PaypalProduct, RetentionPeriod.DEFAULT],
[models.PersonalToken, RetentionPeriod.DEFAULT],
[models.RecurringExpense, RetentionPeriod.DEFAULT],
[models.RequiredLegalDocument, RetentionPeriod.FINANCIAL],
[models.Subscription, RetentionPeriod.FINANCIAL],
[models.SuspendedAsset, RetentionPeriod.DEFAULT],
[models.Tier, RetentionPeriod.DEFAULT],
[models.Transaction, RetentionPeriod.FINANCIAL],
[models.TransactionSettlement, RetentionPeriod.FINANCIAL],
[models.TransactionsImport, RetentionPeriod.FINANCIAL],
[models.Update, RetentionPeriod.DEFAULT],
[models.User, RetentionPeriod.FINANCIAL],
[models.UserToken, RetentionPeriod.SENSITIVE],
[models.VirtualCard, RetentionPeriod.FINANCIAL],
[models.VirtualCardRequest, RetentionPeriod.FINANCIAL],
]);

const run = async () => {
return sequelize.transaction(async (transaction: Transaction) => {
for (const [model, retentionPeriod] of MODEL_RETENTION_PERIODS) {
const result = await model.destroy({

Check failure on line 49 in cron/daily/96-permanant-delete.ts

View workflow job for this annotation

GitHub Actions / typescript

The 'this' context of type 'ModelType' is not assignable to method's 'this' of type 'ModelStatic<AccountingCategory>'.
transaction,
force: true,
where: {
deletedAt: {
[Op.not]: null,
[Op.lte]: `NOW() - INTERVAL(${retentionPeriod})`,
},
},
});

console.log(`Deleted ${result} records for ${model.name}`);
}

if (process.env.DRY_RUN !== 'false') {
await transaction.rollback();
}
});
};

if (require.main === module) {
runCronJob('checks', run, 24 * 60 * 60);
}
4 changes: 4 additions & 0 deletions server/models/ConnectedAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ConnectedAccount extends Model<
declare public CreatedByUserId: CreationOptional<number>;
declare public createdAt: CreationOptional<Date>;
declare public updatedAt: CreationOptional<Date>;
declare public deletedAt: CreationOptional<Date>;

declare public collective?: NonAttribute<Collective>;
declare public getCollective: BelongsToGetAssociationMixin<Collective>;
Expand Down Expand Up @@ -111,6 +112,9 @@ ConnectedAccount.init(
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
deletedAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
Expand Down

0 comments on commit 0dce408

Please sign in to comment.