fix: Remove deleted flows from Redis

This commit is contained in:
Faruk AYDIN
2023-08-24 12:48:37 +02:00
committed by Ali BARIN
parent a909966562
commit 4b77f2f590
2 changed files with 43 additions and 3 deletions

View File

@@ -1,17 +1,44 @@
import { Duration } from 'luxon';
import Context from '../../types/express/context';
import deleteUserQueue from '../../queues/delete-user.ee';
import flowQueue from '../../queues/flow';
import Flow from '../../models/flow';
const deleteCurrentUser = async (_parent: unknown, params: never, context: Context) => {
const deleteCurrentUser = async (
_parent: unknown,
params: never,
context: Context
) => {
const id = context.currentUser.id;
const flows = await context.currentUser.$relatedQuery('flows').where({
status: 'active',
});
const repeatableJobs = await flowQueue.getRepeatableJobs();
for (const flow of flows) {
const job = repeatableJobs.find((job) => job.id === flow.id);
if (job) {
await flowQueue.removeRepeatableByKey(job.key);
}
}
await context.currentUser.$query().delete();
await Flow.query()
.whereIn(
'id',
flows.map((flow) => flow.id)
)
.delete();
const jobName = `Delete user - ${id}`;
const jobPayload = { id };
const millisecondsFor30Days = Duration.fromObject({ days: 30 }).toMillis();
const jobOptions = {
delay: millisecondsFor30Days
delay: millisecondsFor30Days,
};
await deleteUserQueue.add(jobName, jobPayload, jobOptions);