Home > Software engineering >  Get number of requisitions made by the user in a day
Get number of requisitions made by the user in a day

Time:10-16

I have a POST ENDPOINT in my API where i want to register all work journals a worker made.

export const registerPoint = async (req: Request, res: Response) => {

const user = res.locals.decoded;
const {id} = user;

const ponto = new Ponto;

ponto.datePoint = new Date();
ponto.user_id = id;

//this numPoint should restart every day for that user...
ponto.numPoint = 1; 


res.json({ msg: "Register point Route", user, id });
return;

}

how can i control how many times this requisition was made by a worker? I want to control the variable numPoint, when the user makes this requisition it should increase by 01 and then in the final of the day returns it to 0.

Anyone knows about a solution or about a npm package that can handle this?

EDIT: Im storing all the data with SEQUELIZE MYSQL.

CodePudding user response:

As a starting point you could use a simple database for storing the data such as https://www.npmjs.com/package/sqlite3 or MySQL.

Running jobs daily you could consider https://www.npmjs.com/package/node-cron , for example having a daily job (outside of an API call function);

var cron = require('node-cron');

cron.schedule('0 0 1 * *', () => {
  console.log('running every minute to 1 from 5');
});

CodePudding user response:

From what I understand, you need a logging/audit trail mechanism. Since you are using MySQL you can create a new table with columns like (datetime, user_id, action). Every time the user does any action, it will be logged here. Then you can easily find out the count by aggregation. You won't need to reset any count for any user, if data doesn't exist for a given date then it's count will be 0.

  • Related