-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cron): implement permanent deletion of records
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters