Skip to content

Commit

Permalink
feat: add db cleanup cron (heyxyz#4482)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint authored Jan 4, 2024
2 parents 31b45f3 + f20f6bf commit 78601b7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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);
}
};

0 comments on commit 78601b7

Please sign in to comment.