-
Notifications
You must be signed in to change notification settings - Fork 0
feat: clean notifications #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5aca24a
feat: delete due and dandling notifications every day
alfetopito 55219ab
feat: delete notifications when associated notification-template is d…
alfetopito c85ca2e
chore: add .vscode to .gitignore
alfetopito cb2aa8b
chore: regenerate types
alfetopito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,56 @@ | ||
| export default { | ||
| /** | ||
| * Delete notifications that are over a week past their due date and orphaned notifications daily at midnight | ||
| */ | ||
| cleanupExpiredNotifications: { | ||
| task: async ({ strapi }) => { | ||
| console.log("Running cleanupExpiredNotifications task"); | ||
| try { | ||
| // Calculate date 1 week ago | ||
| const oneWeekAgo = new Date(); | ||
| oneWeekAgo.setDate(oneWeekAgo.getDate() - 7); | ||
|
|
||
| // Find all notifications with templates due over a week ago | ||
| const expiredNotifications = await strapi.db | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .query("api::notification.notification") | ||
| .findMany({ | ||
| where: { | ||
| $or: [ | ||
| { | ||
| notification_template: { | ||
| dueDate: { | ||
| $lt: oneWeekAgo, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| notification_template: null, | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| // Delete the expired and orphaned notifications | ||
| if (expiredNotifications.length > 0) { | ||
| await strapi.db.query("api::notification.notification").deleteMany({ | ||
| where: { | ||
| id: { | ||
| $in: expiredNotifications.map((n) => n.id), | ||
| }, | ||
| }, | ||
| }); | ||
| console.log( | ||
| `Deleted ${expiredNotifications.length} notifications that were over a week past due or orphaned` | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error("Error cleaning up notifications:", error); | ||
| } | ||
| console.log("Finished cleanupExpiredNotifications task"); | ||
| }, | ||
| options: { | ||
| rule: "0 0 * * *", // Runs daily at midnight | ||
| tz: "UTC", | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| import cronTasks from "./cron-tasks"; | ||
|
|
||
| export default ({ env }) => ({ | ||
| host: env('HOST', '0.0.0.0'), | ||
| port: env.int('PORT', 1337), | ||
| host: env("HOST", "0.0.0.0"), | ||
| port: env.int("PORT", 1337), | ||
| app: { | ||
| keys: env.array('APP_KEYS'), | ||
| keys: env.array("APP_KEYS"), | ||
| }, | ||
| http: { | ||
| serverOptions: { | ||
| timeout: 120_000, // 2 minutes | ||
| }, | ||
| }, | ||
| webhooks: { | ||
| populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false), | ||
| populateRelations: env.bool("WEBHOOKS_POPULATE_RELATIONS", false), | ||
| }, | ||
| cron: { | ||
| enabled: true, | ||
| tasks: cronTasks, | ||
| }, | ||
| }); |
67 changes: 67 additions & 0 deletions
67
src/api/notification-template/content-types/notification-template/lifecycles.ts
This file contains hidden or 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,67 @@ | ||
| export default { | ||
| async beforeDelete(event) { | ||
| const { where } = event.params; | ||
| const { id } = where; | ||
|
|
||
| try { | ||
| await deleteAssociatedNotifications([id]); | ||
| } catch (error) { | ||
| console.error(`Error in beforeDelete for template ${id}:`, error); | ||
| } | ||
| }, | ||
|
|
||
| async beforeDeleteMany(event) { | ||
| const { where } = event.params; | ||
| const { id } = where; | ||
|
|
||
| try { | ||
| // First get all template IDs that will be deleted | ||
| const templatesForDeletion = await strapi.db | ||
| .query("api::notification-template.notification-template") | ||
| .findMany({ | ||
| where, | ||
| select: ["id"], | ||
| }); | ||
|
|
||
| const templateIds = templatesForDeletion.map((template) => template.id); | ||
|
|
||
| if (templateIds.length > 0) { | ||
| await deleteAssociatedNotifications(templateIds); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error in beforeDeleteMany for templates:`, error); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Helper function to delete notifications for given template IDs | ||
| async function deleteAssociatedNotifications(templateIds: number[]) { | ||
| // Find all notifications associated with these templates | ||
| const notifications = await strapi.db | ||
| .query("api::notification.notification") | ||
| .findMany({ | ||
| where: { | ||
| notification_template: { | ||
| id: { | ||
| $in: templateIds, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (notifications.length > 0) { | ||
| // Delete all associated notifications | ||
| await strapi.db.query("api::notification.notification").deleteMany({ | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where: { | ||
| id: { | ||
| $in: notifications.map((n) => n.id), | ||
| }, | ||
| }, | ||
| }); | ||
| console.log( | ||
| `Deleted ${ | ||
| notifications.length | ||
| } notifications associated with template(s) ${templateIds.join(", ")}` | ||
| ); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generated files after a Changes not related to this PR |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.