cron to inactivate expired subscriptions
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { Cron } from '@nestjs/schedule';
|
||||||
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
|
import { Restaurant } from '../entities/restaurant.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class RestaurantCrone {
|
||||||
|
private readonly logger = new Logger(RestaurantCrone.name);
|
||||||
|
|
||||||
|
constructor(private readonly em: EntityManager) { }
|
||||||
|
|
||||||
|
// run every day at 03:00 (3:00 AM)
|
||||||
|
@Cron('0 3 * * *', {
|
||||||
|
name: 'deactivateExpiredSubscriptions',
|
||||||
|
timeZone: 'UTC',
|
||||||
|
})
|
||||||
|
async handleCron() {
|
||||||
|
try {
|
||||||
|
this.logger.debug('Starting daily subscription expiration check');
|
||||||
|
|
||||||
|
// Get current date
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
// Find restaurants where subscription has expired and they're still active
|
||||||
|
const expiredRestaurants = await this.em.find(Restaurant, {
|
||||||
|
subscriptionEndDate: { $lt: now },
|
||||||
|
isActive: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!expiredRestaurants || expiredRestaurants.length === 0) {
|
||||||
|
this.logger.debug('No restaurants with expired subscriptions found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Found ${expiredRestaurants.length} restaurants with expired subscriptions`);
|
||||||
|
|
||||||
|
// Deactivate expired restaurants
|
||||||
|
for (const restaurant of expiredRestaurants) {
|
||||||
|
restaurant.isActive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist all changes
|
||||||
|
await this.em.persistAndFlush(expiredRestaurants);
|
||||||
|
|
||||||
|
this.logger.log(`Successfully deactivated ${expiredRestaurants.length} restaurants with expired subscriptions`);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(`RestaurantCrone failed: ${err?.message}`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,12 +8,13 @@ import { ScheduleRepository } from './repositories/schedule.repository';
|
|||||||
import { Schedule } from './entities/schedule.entity';
|
import { Schedule } from './entities/schedule.entity';
|
||||||
import { ScheduleService } from './providers/schedule.service';
|
import { ScheduleService } from './providers/schedule.service';
|
||||||
import { ScheduleController } from './controllers/schedule.controller';
|
import { ScheduleController } from './controllers/schedule.controller';
|
||||||
|
import { RestaurantCrone } from './crone/restaurant.crone';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
import { AuthModule } from '../auth/auth.module';
|
import { AuthModule } from '../auth/auth.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [RestaurantsController, ScheduleController],
|
controllers: [RestaurantsController, ScheduleController],
|
||||||
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService],
|
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone],
|
||||||
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
|
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
|
||||||
exports: [RestRepository, ScheduleRepository, ScheduleService],
|
exports: [RestRepository, ScheduleRepository, ScheduleService],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user