forked from heyxyz/hey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add db cleanup cron (heyxyz#4482)
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 |
This file contains 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,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); | ||
} | ||
}; |