A sample payroll system, using Nest.JS + MongoDB
This project is part of a challenge that the main goal is to create a new payroll system API that have two goals:
- Upload a CSV file containing data on the number of hours worked per day per employee
- Retrieve a report detailing how much each employee should be paid in each pay period
I decided to use NestJS because I am more familiar with the Node.js / Javascript / Typescript ecosystem and I believe that NestJS, being an agnostic framework, gives us flexibility when creating a Rest API.
I also decided to use MongoDB because, as the number of information can grow exponentially with the import of CSV files, a non-relational database is good on these occasions because it is fast and has a more flexible schema.
The application is separated into a few layers:
- Controller - responsible for receiving client requests
- Service - works as an intermediary, processing information and communicating with the Controller and the Repository
- Repository - communicates with the database (in this case, MongoDB)
With this separation, it is possible to test the application more easily, besides making the maintainability of the code better. In addition, I separated the pure functions to be able to use throughout the project.
To make the project simpler, just created an entity called TimeReport where the information coming from the CSV is saved. From it, information search and manipulation operations are performed to return in the expected structure. However, for a better understanding of the project, types and interfaces were created.
To run the code, you need to have installed one of these options:
- Node.js + MongoDB
- or Docker Desktop for Mac / Windows
- or Docker Engine for Linux
- or Docker + Skaffold
To run the project with docker, you need to duplicate the .env-docker-sample file by renaming it to .env.
After that, run the code:
npm run docker:upTo run on your machine, first you need to have MongoDB installed.
Tip: you can download it HERE
Then install the dependencies running:
npm run installThen duplicate the .env-local-sample file by renaming it to .env.
Finally, there are three ways to run the application:
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prodThe easiest way to run the project with kubernets is to have:
- Skaffold installed
- Duplicate the
.env-k8s-samplefile by renaming it to.env. - Running the following command in the main project folder:
skaffold devAnd then, to know the server port use the command:
kubectl get servicesAfter start the API, you can open http://localhost:3000/api and the Swagger documentation:
The first endpoint listed is POST /payrolls/upload that we can send a CSV file:
But, if the report id was already used, the API returns an error to the client:
The second is the endpoint where we will get all the reports, with the appropriate sums, multiplications, and segmentation by period:
In the example below, we see the return of 2 two reports processed in the same return:
To check if the API is running, we can access http://localhost:3000/ and see if will return the version of it.
The project was covered using Jest. You can run some of these commands below:
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:covTip: you can see that report in
./coverage/lcov-report/index.htmlfile:
Tip²: at every commit in a PR on GitHub, automated tests are run in order to detect if everything is ok
If you like Postman, I there is a file in docs/payroll_system.postman_collection.json for you :)
- Add Swagger
- Add docker-compose
- Add GitHub Actions Jest configuration
- Fix e2e tests
- Deploy in somewhere
- Add more tests to cover more possibilities
- Add alias to common paths/modules
- Add filters in GET /payrolls/reports









