Skip to content

Commit

Permalink
Enable deletion of schedules. (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c authored Aug 2, 2024
1 parent 8e79dd3 commit d7a510e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Schedules" ADD COLUMN "name" TEXT NOT NULL DEFAULT E'';
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/modules/bot/bot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 29 additions & 6 deletions src/modules/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,50 @@ 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,
},
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);
Expand Down

0 comments on commit d7a510e

Please sign in to comment.