vsDizzy / node-test-home

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NodeJS Take home test task

Setup

  • Install packages with npm i
  • Create .env file with PORT and MONGO_SERVER values corresponding to your environment.
  • Start sever with npm start

Test

npm test

API

Create new Comment

POST comments
Content-Type: application/json

{
    "_id":"000000000000000000000004",
    "body": "Last reply to the first comment.",
    "parentId": "000000000000000000000001",
    "replyTo": "000000000000000000000001",
    "threadId": "acd88360-e772-11ea-bc06-6ba89fdab63e",
    "createdAt": "2020-11-18T10:11Z"
}

Get thread

GET comments/{threadId}?skip=&limit=
Content-Type: application/json

Get comment

GET comments/{threadId}/{commentId}
Content-Type: application/json

Set moderation status

PATCH comments/{threadId}/{commentId}
Content-Type: application/json

{
"status": "declined" | "approved"
}

Found some inconsistencies

  1. inc-1 I believe these comments should be swapped in terms of parentId.
  2. _id or id field name. Such contracts should be strictly defined across the entire project.
  3. ThreadId is UUID, but CommentId is ObjectId. I'd suggest to use UUIDs everywhere.

Notes and thoughts

  • Nest and the project structure is okay. And because it is very small I don't really see the need to change anything right now.
  • @Type(() => Number) workaround took me a while, looks like number transforms are still not fixed.
  • Cannot really separate business and database logic e.g. with declined
  • The solution should scale well with mongo replicas because we're sending ton of db requests.
  • Also caching could be introduced. E.g. it should be possible to save entire thread tree snapshot at the specific date and then only patch it with the new data.
  • Infinite loop possible when getting comment replies
  • No auht roles for moderation

Security considerations

  • Moderator can only update status field to approved or declined
  • Skip & limit query parameters are constrained to int

TODO

  1. Build an API to get specific comment and all nested replies
  2. Each comment should have moderation status. Default value for new comments - pending (available statuses: pending, approved, declined)
  3. Build an API to update moderation status of a comment
  4. Build an API to retrieve comments per specific thread.
    1. Endpoint should contain skip & limit parameters.
    2. Endpoint should return specified limit of root comments and all nested replies to each of root comment.
    3. Root comments and replies should be sorted. For more datails about sort see the section - Comments sort explanation.
    4. Comments with declined status should not be returned from this API.
    5. Replies that are left to comments with declined status should not be returned.
  5. Add some tests

About


Languages

Language:TypeScript 94.4%Language:JavaScript 5.4%Language:Shell 0.2%