Let's get all your subordinates.
- One user can have only 1 role, as the given data structure.
- RoleIds and UserIds are unique.
Steps I thought of and executed for solving the get subordinates for given user:
- Get user by user id from list of users
- Get all child roles of the given users' role
- Get all users that have the roles from step 2
Step 2 has the main logic in my solution, it can be found in the role repository.
- No main dependency. It might have been easier/less code to do this with something like Lodash, but that would defeat the purpose of the coding task.
- Written in node with some ES6 syntax. tested on Node v12.13.1
- Uses Jest for testing
How to setup
Run the following commands to setup, given
npm is available:
- git clone firstname.lastname@example.org:geshan/subordinater.git
- cd subordinater
- npm install
- node index.js -> to see a test output
- To run the tests run
jeston the project root.
- For code coverage run
npm run test:cov, coverage files will be in the
The current coverage is 100%.
Decisions and tradeoffs
- Basic require is chosen in place of ES6 imports and class keyword for ease.
- I have chosen more of a functional/procedural path with more pure functions than object oriented pattern. I think it is better in this small problem space than trying to model or use a design pattern like composition design pattern where a user will have a role object.
- Setting users and roles as class variable (this.roles) can be one fast way to encapsulate the data. I was focused more on getting the result and testing the code with a high code coverage.
- There are places to improve. Still, I would not opt to do them all for a small problem domain like this one. As software engineers we need to find a balance.
If it was a bigger project
This is a coding challenge and scope is quite small. If it was a bigger real project, doing the following would be better:
- More focus on architecture and software design would be necesssary. For a solution of ~80 lines it would be over optimization IMHO.
- Choosing typescript or using some validator like validator or Joi for validation would be better.
- If a RDBMS is used, it would be easier to solve this with a relational database and a select SQL than on the code.
- It would be great to have some mutation testing in place like Stryker to know how effective the tests are in addition to the coverage.
- For a team project, it will be good to have the project dockerized.