Skip to content

feat: added complete-all functionality#14

Open
okaziya wants to merge 7 commits intomasterfrom
11-complete-all-in-list
Open

feat: added complete-all functionality#14
okaziya wants to merge 7 commits intomasterfrom
11-complete-all-in-list

Conversation

@okaziya
Copy link
Member

@okaziya okaziya commented Oct 24, 2024

Improved the functionality for completed lists. Slightly modified the updateTodoItem solution 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

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract fetching and validation into its own helper function

})
}

export const createTodoList = async (todoListData) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helpful during testing and can be used in the future

if (todoList) {
const hasIncompleteItems = todoList.items.some((item) => !item.completed)
if (!hasIncompleteItems || todoList.items.length === 0) {
await todoListRepository.update({ id: listId }, { completed: true })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of using update instead of the merge

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`)
}
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper function which marks the list as completed if all items are done, used in several places

return todoRepository.save(updatedTodoItem)
const updatedTodoItem = await todoRepository.save(todoRepository.merge(todoItem, updateData))

await handleListCompletionStatus(todoListRepository, listId, updateData.completed)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks if the list needs to be marked as completed or not within the helper function

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.
@okaziya okaziya force-pushed the 11-complete-all-in-list branch from a2eef47 to 86b4a17 Compare October 29, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Complete "all items completed in list" additional task

1 participant