We have a web application using Fastify. There is a function in a handler that we need to call it from a cron job inside the same application.
index.js
async function registerRoutes(fastify) {
fastify
.post(
'/statements/generate',
{
schema: schemas.statements
},
handlers.generateStatements
)
};
handler.js
async function generateStatements(req, reply) {
...
...
reply.code(200).send({...});
}
Is there a direct way (without calling the actual endpoint by using axios or another library) to call the function generateStatements in the handler (or the route)?
I found a "hack":
const req = {
...
...
};
const response = { statusCode: 200 };
const context = {};
const reply = new Reply(response, { context });
reply.send = () => true; //the cron job doesn't need the response.
await generateStatements(req, reply);
But it is not ideal at all because we might need to generate a jwt token for some business logic and will fail.
const token = await reply.jwtSign(
{ account: req.account._id },
{
expiresIn: '1d'
}
);