diff --git a/prisma/migrations/20240802101405_add_name_in_schedule_table/migration.sql b/prisma/migrations/20240802101405_add_name_in_schedule_table/migration.sql new file mode 100644 index 0000000..c975482 --- /dev/null +++ b/prisma/migrations/20240802101405_add_name_in_schedule_table/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Schedules" ADD COLUMN "name" TEXT NOT NULL DEFAULT E''; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7dd73ee..c1eca4b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -136,6 +136,7 @@ model ConversationLogic { model Schedules { id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid() + name String @default("") createdAt DateTime @default(now()) scheduledAt DateTime authToken String diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index 5a6d2d7..01f1973 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -241,6 +241,17 @@ export class BotController { return res; } + @Delete('/schedule/:id') + @UseInterceptors( + AddResponseObjectInterceptor, + AddAdminHeaderInterceptor, + AddOwnerInfoInterceptor, + AddROToResponseInterceptor, + ) + async deleteSchedule(@Param('id') id: string) { + await this.botService.deleteSchedule(id); + } + @Get('/:id/addUser/:userId') @UseInterceptors( AddResponseObjectInterceptor, diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index e8a7ddc..409e892 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -183,6 +183,12 @@ export class BotService implements OnModuleInit { // `description` "will" change if the data is modified. async scheduleNotification(botId: string, scheduledTime: Date, config: any, token: string, id?: string) { if (!id) id = randomUUID(); + const job = new CronJob(scheduledTime, () => { + this.start(botId, config, token); + }); + const scheduleName = `notification_${randomUUID()}`; + this.schedulerRegistry.addCronJob(scheduleName, job); + job.start(); await this.prisma.schedules.upsert({ where: { id: id, @@ -190,20 +196,37 @@ export class BotService implements OnModuleInit { update: {}, create: { authToken: token, + name: scheduleName, botId: botId, scheduledAt: scheduledTime, config: config, } }); - const job = new CronJob(scheduledTime, () => { - this.start(botId, config, token); - }); - this.schedulerRegistry.addCronJob(`notification_${randomUUID()}`, job); - job.start(); - this.logger.log(`Scheduled notification for: ${botId}, at: ${scheduledTime.toDateString()}`); + this.logger.log(`Scheduled notification for: ${botId}, at: ${scheduledTime.toDateString()}, name: ${scheduleName}`); await this.cacheManager.reset(); } + async deleteSchedule(scheduleId: string) { + if (!scheduleId) { + throw new BadRequestException(`Schedule id is required!`); + } + const existingSchedule = await this.prisma.schedules.findUnique({ + where: { + id: scheduleId, + } + }); + if (!existingSchedule) { + throw new BadRequestException('Schedule does not exist!'); + } + await this.prisma.schedules.delete({ + where: { + id: scheduleId, + } + }); + this.schedulerRegistry.deleteCronJob(existingSchedule.name); + this.logger.log(`Deleted schedule for bot: ${existingSchedule.botId}`); + } + // dateString = '2020-01-01' private getDateFromString(dateString: string) { return new Date(dateString);