import { Inject, Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { Cron } from '@nestjs/schedule'; import { PubSub } from 'graphql-subscriptions'; @Injectable() export class TokenCodeScheduler { constructor( @Inject('PUBSUB') private readonly pubSub: PubSub, private readonly prisma: PrismaService, ) {} private readonly incrementEvery = 1000 * 60 * 10; private readonly values = [1, 2, 3, 5, 8]; @Cron('0 */1 * * * *') async handleIncrement() { const tasks = []; for (let i = this.values.length - 2; i >= 0; i++) { const task = this.prisma.tokenCode.updateMany({ where: { value: this.values[i], updatedAt: { lt: new Date(Date.now() - this.incrementEvery), }, }, data: { value: this.values[i + 1], }, }); tasks.push(task); } await this.prisma.$transaction(tasks); // TODO: send agent updates over pubsub } }