Skip to content
Closed
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
17 changes: 16 additions & 1 deletion src/agenda/agenda.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { env } from '../config/config';
import mongoose from 'mongoose';
import testJob from './jobs/testJob';
import sendPrescriptions from './jobs/sendPrecriptions';
import deletePrescriptions from './jobs/deletePrescriptions';
import { Collection } from 'mongodb';

class AgendaService {
private static instance: AgendaService;
private agenda!: Agenda;
private isInitialized = false;

private constructor() {}
private constructor() { }

public static getInstance(): AgendaService {
if (!AgendaService.instance) {
Expand Down Expand Up @@ -63,6 +64,11 @@ class AgendaService {
this.agenda.define('send-prescriptions', { concurrency: 10 }, async (job: Job) => {
await sendPrescriptions(job);
});

// Job para eliminación física de recetas eliminadas lógicamente
this.agenda.define('delete-prescriptions', { concurrency: 1 }, async (job: Job) => {
await deletePrescriptions(job);
});
}

private getCollection(): Collection {
Expand Down Expand Up @@ -145,6 +151,15 @@ class AgendaService {
await this.scheduleJob('send prescription', {}, when);
}

// programar eliminación de recetas
public async scheduleDeletePrescriptionJob(data: {
days?: number;
when?: string | Date;
}): Promise<void> {
const { when, days } = data;
await this.scheduleJob('delete-prescriptions', { days }, when || '0 0 * * *'); // Default to every day at midnight if no 'when'
}

public async getAgendaInstance(): Promise<Agenda> {
await this.initialize();
return this.agenda;
Expand Down
28 changes: 28 additions & 0 deletions src/agenda/jobs/deletePrescriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Job } from 'agenda';
import Prescription from '../../models/prescription.model';
import moment = require('moment');

async function deletePrescriptions(job: Job) {
const { days = 30 } = job.attrs.data || {};
const limitDate = moment().subtract(days, 'days').toDate();

try {
const result = await Prescription.deleteMany({
status: 'eliminada',
$or: [
{ updatedAt: { $lt: limitDate } },
{ updatedAt: { $exists: false }, createdAt: { $lt: limitDate } },
{ updatedAt: { $exists: false }, createdAt: { $exists: false }, date: { $lt: limitDate } }
]
});

// eslint-disable-next-line no-console
console.log(`[Job: delete-prescriptions] Se han eliminado físicamente ${result.deletedCount} recetas marcadas como 'Eliminada' antes de ${limitDate}`);
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Job: delete-prescriptions] Error al eliminar las recetas:', error);
return job.fail(`Error en el trabajo delete-prescriptions: ${error}`);
}
}

export default deletePrescriptions;