Open
Conversation
okaziya
commented
Oct 29, 2024
| where: { id: itemId, list: { id: listId } } | ||
| }) | ||
| if (!todoItem) throw new Error(`Todo item ${itemId} not found in list ${listId}`) | ||
| const todoItem = await fetchAndValidateTodoItem(todoRepository, listId, itemId) |
Member
Author
There was a problem hiding this comment.
Extract fetching and validation into its own helper function
okaziya
commented
Oct 29, 2024
| }) | ||
| } | ||
|
|
||
| export const createTodoList = async (todoListData) => { |
Member
Author
There was a problem hiding this comment.
Helpful during testing and can be used in the future
okaziya
commented
Oct 29, 2024
| if (todoList) { | ||
| const hasIncompleteItems = todoList.items.some((item) => !item.completed) | ||
| if (!hasIncompleteItems || todoList.items.length === 0) { | ||
| await todoListRepository.update({ id: listId }, { completed: true }) |
Member
Author
There was a problem hiding this comment.
Example of using update instead of the merge
okaziya
commented
Oct 29, 2024
Comment on lines
+61
to
+74
| const markListAsCompletedIfAllItemsCompleted = async (todoListRepository, listId) => { | ||
| const todoList = await todoListRepository.findOne({ | ||
| where: { id: listId }, | ||
| relations: ['items'], | ||
| }) | ||
|
|
||
| if (todoList) { | ||
| const hasIncompleteItems = todoList.items.some((item) => !item.completed) | ||
| if (!hasIncompleteItems || todoList.items.length === 0) { | ||
| await todoListRepository.update({ id: listId }, { completed: true }) | ||
| console.log(`List ${listId} marked as completed`) | ||
| } | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
Helper function which marks the list as completed if all items are done, used in several places
okaziya
commented
Oct 29, 2024
| return todoRepository.save(updatedTodoItem) | ||
| const updatedTodoItem = await todoRepository.save(todoRepository.merge(todoItem, updateData)) | ||
|
|
||
| await handleListCompletionStatus(todoListRepository, listId, updateData.completed) |
Member
Author
There was a problem hiding this comment.
Checks if the list needs to be marked as completed or not within the helper function
... and other tests!
And, better save implementation.
1. deleting items and having list updated with the "allCompleted: true" if applicable 2. updating TodoList "allCompleted: false" when adding new items to a list Also, add helper functions write more brief tests.
Also, refactor tests to be hierarchical, and collect different seed IDs at the root level test suite.
a2eef47 to
86b4a17
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improved the functionality for completed lists. Slightly modified the
updateTodoItemsolution after the interview. Added functionality to automatically switch the completed status after deleting or adding new Todo items. When a new item is added, the list’s completed status is set to false. If an item is deleted and all remaining items are completed, the entire list is marked as complete accordingly.All changes are covered with tests.
Resolves #11