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

Fix db-cleanup #106

Merged
merged 3 commits into from
Dec 11, 2023
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
28 changes: 13 additions & 15 deletions apps/server/src/pages/api/cron/db-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sendAccountDeletionConfirmationEmail } from "../../../../src/utils/noti
// Run this cron every day once for max 60s.
export const config = {
maxDuration: 60,
};
};

// This cron will also help with GDPR compliance and data retention.

Expand Down Expand Up @@ -89,40 +89,38 @@ export default async function dbCleanup(req: NextApiRequest, res: NextApiRespons
}));

// item 4:
// Delete all SiteAlerts that have deletedAt date older than 30 days
promises.push(prisma.siteAlert.deleteMany({
// Delete all AlertMethods that have been soft-deleteted for longer than 7 days
promises.push(prisma.alertMethod.deleteMany({
where: {
deletedAt: {
lte: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000)
lte: new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)
}
}
}));
}))

// item 5:
// Delete all notifications that are older than 30 days and have been processed
promises.push(prisma.notification.deleteMany({
// Delete all SiteAlerts that have deletedAt date older than 30 days
promises.push(prisma.siteAlert.deleteMany({
where: {
isDelivered: true,
sentAt: {
lt: new Date(new Date().getTime() - 90 * 24 * 60 * 60 * 1000)
eventDate: {
lte: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000)
}
}
}));

// We do not delete notifications, as we will need notifications data in the future for further analysis

try {

const [deletedGeoEvent, deletedUsers, deletedSites, deletedSiteAlerts, deletedNotification] =
const [deletedGeoEvents, deletedUsers, deletedSites, deletedAlertMethods, deletedSiteAlerts] =
await Promise.all(promises);

// Deleted ${deletedNotifications.count} notifications for soft-deleted SiteAlerts

logger(`
Deleted ${deletedGeoEvent.count} geo events that are older than 30 days and have been processed
Deleted ${deletedGeoEvents.count} geo events that are older than 30 days and have been processed
Deleted ${deletedUsers.count} users who've requested to be deleted and have deletedAt date older than 7 days
Deleted ${deletedSites.count} soft-deleted Sites
Deleted ${deletedAlertMethods.count} soft-deleted AlertMethods
Deleted ${deletedSiteAlerts.count} soft-deleted SiteAlerts
Deleted ${deletedNotification.count} notifications that are older than 90 days and have been processed
`, 'info');

res.status(200).json({
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/server/api/routers/alertMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ export const alertMethodRouter = createTRPCRouter({
});
// If the existing alertMethod has been soft deleted, un-soft delete it, and return success
if (existingAlertMethod?.deletedAt) {
// This block re-creates the soft-deleted alertMethod.
// Check if the user has reached the maximum limit of alert methods for all alertMethods (e.g., 5)
// If limit has reached, the function will throw an error message
await limitAlertMethodBasedOnPlan({ctx, userId, userPlan: userPlan, method: input.method});
// Else, restore the soft-deleted alertMethod
const updatedAlertMethod = await ctx.prisma.alertMethod.update({
where: {
id: existingAlertMethod.id,
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/utils/routers/alertMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const limitSpecificAlertMethodPerUser = async ({
const specificAlertMethodCount = await ctx.prisma.alertMethod.count({
where: {
userId,
method
method,
deletedAt: null
},
});
if (specificAlertMethodCount >= count) {
Expand Down