Skip to content
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ build
############################

data/*
!data/.gitkeep
!data/.gitkeep

############################
# IDE
############################
.vscode
56 changes: 56 additions & 0 deletions config/cron-tasks.ts
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
.query("api::notification.notification")
.findMany({
where: {
$or: [
{
notification_template: {
dueDate: {
$lt: oneWeekAgo,
},
},
},
{
notification_template: null,
},
],
},
});

// 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",
},
},
};
14 changes: 10 additions & 4 deletions config/server.ts
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,
},
});
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({
where: {
id: {
$in: notifications.map((n) => n.id),
},
},
});
console.log(
`Deleted ${
notifications.length
} notifications associated with template(s) ${templateIds.join(", ")}`
);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated files after a yarn build.

Changes not related to this PR

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2025-02-13T15:53:59.140Z"
"x-generation-date": "2025-03-07T17:29:38.606Z"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down Expand Up @@ -10516,6 +10516,9 @@
}
}
},
"isReducedBondingPool": {
"type": "boolean"
},
"createdAt": {
"type": "string",
"format": "date-time"
Expand Down Expand Up @@ -10565,6 +10568,17 @@
}
}
},
"isServiceFeeEnabled": {
"type": "boolean"
},
"isColocated": {
"type": "string",
"enum": [
"Yes",
"No",
"Partial"
]
},
"createdAt": {
"type": "string",
"format": "date-time"
Expand Down Expand Up @@ -10702,6 +10716,15 @@
}
}
},
"isWhiteListed": {
"type": "boolean"
},
"isVouched": {
"type": "boolean"
},
"isColocated": {
"type": "boolean"
},
"createdAt": {
"type": "string",
"format": "date-time"
Expand Down
Loading