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

feat: add db cleanup cron #4482

Merged
merged 1 commit into from
Jan 4, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/cron-db-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Cron - Cleanup DB

on:
schedule:
- cron: '0 1 * * *'
workflow_dispatch:

jobs:
cleanup:
name: Cleanup
runs-on: ubuntu-latest
steps:
- name: Cleanup DB
env:
SECRET: ${{ secrets.SECRET }}
run: |
curl -X POST \
-H "Content-Type: application/json" \
-H "Referer: https://hey.xyz" \
-d '{"secret": "'"$SECRET"'"}' \
https://api.hey.xyz/internal/cleanup/db
55 changes: 55 additions & 0 deletions apps/api/src/routes/internal/cleanup/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { Handler } from 'express';

import { Errors } from '@hey/data/errors';
import logger from '@hey/lib/logger';
import catchedError from '@utils/catchedError';
import prisma from '@utils/prisma';
import { invalidBody, noBody } from '@utils/responses';
import { object, string } from 'zod';

type ExtensionRequest = {
secret: string;
};

const validationSchema = object({
secret: string()
});

export const post: Handler = async (req, res) => {
const { body } = req;

if (!body) {
return noBody(res);
}

const validation = validationSchema.safeParse(body);

if (!validation.success) {
return invalidBody(res);
}

const { secret } = body as ExtensionRequest;

if (secret !== process.env.SECRET) {
return res
.status(400)
.json({ error: Errors.InvalidSecret, success: false });
}

try {
// Cleanup ProfileRestriction
await prisma.profileRestriction.deleteMany({
where: { isFlagged: false, isSuspended: false }
});

// Cleanup Preference
await prisma.preference.deleteMany({
where: { highSignalNotificationFilter: false, isPride: false }
});
logger.info('Cleaned up DB');

return res.status(200).json({ success: true });
} catch (error) {
return catchedError(res, error);
}
};