hi everyone,
I'm trying to create a task in my application.
basically, I have an invite being created and when that happens, an event called OnInviteCreated is triggered
in this event, I send an email to the user with the link to accept the invite and I'd also like to call trigger.dev to start a task that, when the invite's expiresAt time is reached, should check if the status is still PENDING
if it is, trigger.dev should mark it as EXPIRED; otherwise, it should ignore it
I'm not sure how to implement this, since the docs mention that some props are required, but my project uses different ones
what would be the best approach?
This is my trigger task:
import { schedules } from "@trigger.dev/sdk/v3";
import { UniqueEntityID } from "@vtal/common/domain/unique-entity-id.vo";
import { InviteTaskService } from "@/modules/invites/application/services/invite-task.service";
export function checkInvitationExpiry(taskService: InviteTaskService) {
return schedules.task({
id: "check-invitation-expiry",
run: async (payload) => {
await taskService.expireInvite(new UniqueEntityID(payload.externalId));
},
});
}
This is my taskService:
import { Injectable } from "@nestjs/common";
import { UniqueEntityID } from "@vtal/common/domain/unique-entity-id.vo";
import { InvitesRepository } from "../repositories/invite.repository";
@Injectable()
export class InviteTaskService {
constructor(private readonly invitesRepository: InvitesRepository) {}
async expireInvite(inviteId: UniqueEntityID): Promise<void> {
// TODO: implement logger
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log("EXECUTING TASK WITH ID", inviteId);
const invite = await this.invitesRepository.findById(inviteId);
if (!invite || invite.isExpired()) return;
invite.expire();
await this.invitesRepository.update(inviteId, invite);
}
}
and this is the part of the event that it should control the expiration:
private async createTaskToExpireInvite(invite: Invite) {
const expiresAt = invite.expiresAt;
if (expiresAt.getTime() <= Date.now()) {
console.warn("Invite is already expired — skipping schedule.");
return;
}
const checkInvitationExpiryTask = checkInvitationExpiry(
this.inviteTaskService,
);
await checkInvitationExpiryTask.trigger({
scheduleId
});
}
tech stack: Nest.js, GraphQL, Typeorm