All models are defined in
src/model.js
A profile can be either a client or a contractor.
Clients create contracts with contractors, while contractors perform jobs for clients and get paid.
Each profile has a balance property.
A contract exists between a client and a contractor.
Contracts have 3 statuses: new, in_progress, and terminated.
Contracts are considered active only when in the in_progress status.
Contracts group jobs within them.
Contractors get paid for jobs performed under a certain contract by clients.
The exercise requires Node.js to be installed. We recommend using the LTS version.
- Start by creating a local repository for this folder.
- In the repo's root directory, run
npm installto install all dependencies. - Next, run
npm run seedto seed the local SQLite database. Warning: This will drop the database if it exists. The database will be stored in a local file nameddatabase.sqlite3. - Then run
npm startto start both the server and the React client.
❗️ Make sure to commit all changes to the master branch!
- The server is running with nodemon, which will automatically restart whenever you modify and save a file.
- The database provider is SQLite, which will store data in a file local to your repository called
database.sqlite3. The ORM Sequelize is used on top of it. You should interact with Sequelize. Please spend some time reading the Sequelize documentation before starting the exercise. - To authenticate users, use the
getProfilemiddleware located undersrc/middleware/getProfile.js. Users are authenticated by passingprofile_idin the request header. Once authenticated, the user's profile will be available underreq.profile. Ensure that only users associated with a contract can access their respective contracts. - The server is running on port 3001.
Below is a list of the required APIs for the application.
-
GET
/contracts/:id- This API is broken 😵! It should return the contract only if it belongs to the profile making the request. Better fix that! -
GET
/contracts- Returns a list of contracts belonging to a user (client or contractor). The list should only contain non-terminated contracts. -
GET
/jobs/unpaid- Get all unpaid jobs for a user (either a client or contractor), but only for active contracts. -
POST
/jobs/:job_id/pay- Pay for a job. A client can only pay if their balance is greater than or equal to the amount due. The payment amount should be moved from the client's balance to the contractor's balance. -
POST
/balances/deposit/:userId- Deposit money into a client's balance. A client cannot deposit more than 25% of the total of jobs to pay at the time of deposit. -
GET
/admin/best-profession?start=<date>&end=<date>- Returns the profession that earned the most money (sum of jobs paid) for any contractor who worked within the specified time range. -
GET
/admin/best-clients?start=<date>&end=<date>&limit=<integer>- Returns the clients who paid the most for jobs within the specified time period. Thelimitquery parameter should be applied, and the default limit is 2.
[
{
"id": 1,
"fullName": "Reece Moyer",
"paid" : 100.3
},
{
"id": 200,
"fullName": "Debora Martin",
"paid" : 99
},
{
"id": 22,
"fullName": "Debora Martin",
"paid" : 21
}
]