Pragmatical ways to determine a week and automatic functions on a Node.js back end

60 Views Asked by At

I’m developing a backend server, using Postgres as my database for a friend’s game project. Everything is going well but there are a couple of features that I want to add which I’m not knowledgeable about yet.

The game works with ranks. Every day people may play short matches. Everyone who plays it will score ranks toward goals. There is a monthly rank, weekly rank and daily rank. Every game contributes to each respective rank. Weeks are measured by starting on Mondays and ending on Sundays.

Month and daily measures are easy but I’m wondering on a more pragmatic day to make the weekly rank. How to make sure I can easily delineate a week from month to month and etc with the various JavaScript functions? For example, we are in the 10th week of 2022, or 15th and etc.

Also, I would like to make a function that will automatically run every 24 hours at 00:00 of each day and email the daily ranks, and also the weekly ones on Sundays(‘when the weekly ranks resets’) and by the end of each month. Possibly deleting the old and non-useful past ranks in this process.

So the goals are:

  • Best ways to detect present weak, even when transitioning from month to month, and keeping track of each rank
  • How to email such ranks to the game owner (my friend)
  • How to make a function that will autorun on the server every 24 hours at 00:00 to tab such ranks.

I hope I’ve elaborated the question clearly enough. I would appreciate the help, please ask me anything if I can help make the question clearer

Dependencies of the projects:

jest: "^27.4.1", bcryptjs: "^2.4.3", csv-parse: "^5.0.4", express: "^4.17.2", express-async-errors: "^3.1.1", jest: "^27.5.1", jsonwebtoken: "^8.5.1", multer: "^1.4.4", pg: "^8.7.3", reflect-metadata: "^0.1.13", swagger-ui-express: "^4.3.0", ts-jest: "^27.1.3", tsyringe: "^4.6.0", typeorm: "^0.2.43", uuid: "^8.3.2"

There are also some more dev-dependencies but I'm not sure if they are relevant.

Thanks in advance everybody!

1

There are 1 best solutions below

1
On

Postgres provides a direct method for week number, providing you can put up with the year end idiosyncrasies of the ISO 8601 week definition. Use the extract function. However, it does not take account the particular month.
As far as scheduling tasks it is unfortunate but Postgres does not provide a built in scheduler. However see here for a few options.