Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sajdakabir/mar 271 sub item #541

Merged
merged 4 commits into from
Nov 5, 2024
Merged

Conversation

sajdakabir
Copy link
Member

@sajdakabir sajdakabir commented Nov 4, 2024

What did you ship?

sub item

Fixes:

  • #XXX (GitHub issue number)
  • MAR-271 (Linear issue number - should be visible at the bottom of the GitHub issue description)

Checklist:

  • I have self-reviewed the code (A decent size PR without self-review might be rejected)
  • I pinky swear that my codes gonna work as I have testing every possible scenario.
  • I ignored Coderabbit suggestion because it does not make any sense.
  • I took Coderabbit suggestion under consideration as some of it makes sense.
  • I have commented my code, particularly in hard-to-understand areas.

OR:

  • shut up and let me cook.

Copy link

coderabbitai bot commented Nov 4, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new controller function, getSubItemsController, which retrieves sub-items associated with a parent item based on a provided parentId. This function is integrated into the routing structure, allowing access via new routes in both the inbox and space contexts. Additionally, the ItemSchema in the model is updated to include a parent field, facilitating a hierarchical relationship between items. A corresponding service function, getSubItems, is also added to handle the retrieval logic.

Changes

File Path Change Summary
apps/backend/src/controllers/lib/item.controller.js Added getSubItemsController method and updated exports to include it.
apps/backend/src/models/lib/item.model.js Added parent field to ItemSchema for hierarchical relationships.
apps/backend/src/routers/lib/common.route.js Added route for retrieving sub-items in the inbox using getSubItemsController.
apps/backend/src/routers/lib/space.route.js Added route for retrieving sub-items under a specific item in a space using getSubItemsController.
apps/backend/src/services/lib/item.service.js Added getSubItems function and updated exports to include it.

Assessment against linked issues

Objective Addressed Explanation
Treat todo item in markdown as sub todo on the parent item (MAR-271) The implementation does not address the markdown treatment of sub-items.
They are taken as the cycleDate if it's in this week (MAR-271) It's unclear if the cycleDate handling is implemented in the current changes.
If they are marked as done, takes the date and shows in today as done (MAR-271) The changes do not clarify how completion status affects date handling.

Possibly related PRs

🐇 In the burrow, I hop with glee,
New sub-items for all to see!
With parents now, they find their way,
In the inbox, they dance and play.
A structure strong, a path so bright,
Hooray for changes, what a delight! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Nov 4, 2024

Hey there! 👋

We require pull request titles to follow the Conventional Commits specification.

Valid types:

  • feat: A new feature
  • fix: A bug fix
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance
  • test: Adding missing or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit

Format: type: description

Error details:

No release type found in pull request title "Sajdakabir/mar 271 sub item". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/

Available types:
 - feat
 - fix
 - docs
 - style
 - refactor
 - perf
 - test
 - build
 - ci
 - chore
 - revert

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🧹 Outside diff range and nitpick comments (4)
apps/backend/src/routers/lib/common.route.js (1)

26-26: Consider implementing complete CRUD operations for sub-items.

While the GET route for sub-items is correctly implemented, consider adding the following routes to support full CRUD operations:

 router.route("/inbox/:item/sub-items/").get(getSubItemsController);
+router.route("/inbox/:item/sub-items/").post(createSubItemController);
+router.route("/inbox/:item/sub-items/:subItem").put(updateSubItemController);
+router.route("/inbox/:item/sub-items/:subItem").delete(deleteSubItemController);

This would provide a complete API for managing sub-items, allowing:

  • Creation of new sub-items
  • Updates to existing sub-items
  • Deletion of sub-items

Consider adding API documentation for these endpoints to help other developers understand the available operations and expected request/response formats.

apps/backend/src/models/lib/item.model.js (2)

75-78: Consider adding validation for circular references.

The current implementation doesn't prevent circular parent-child relationships (e.g., item A → item B → item A), which could cause infinite loops in recursive operations.

Would you like me to provide an implementation for a pre-save middleware that validates the parent chain to prevent circular references?


75-78: Enhance schema for sub-item specific requirements.

Based on the PR objectives (MAR-271), sub-items need additional functionality:

  1. Scheduling within the current week
  2. Cycle date association
  3. Completion date tracking
  4. Inbox categorization for unscheduled items

Consider adding fields to support these requirements:

         parent: {
             type: Schema.Types.ObjectId,
             ref: 'Item',
+            index: true
         },
+        subItemMetadata: {
+            isScheduledInCurrentWeek: Boolean,
+            cycleDate: Date,
+            inboxCategory: String
+        },
apps/backend/src/controllers/lib/item.controller.js (1)

201-214: Consider enhancing error handling and validation.

While the implementation follows the established pattern, consider these improvements:

  1. Add specific error handling for common cases
  2. Validate the parentId before processing

Consider updating the implementation:

 const getSubItemsController = async (req, res, next) => {
     try {
         const user = req.user._id;
         const { item: parentId } = req.params;
 
+        if (!parentId) {
+            return res.status(400).json({
+                message: "Parent item ID is required"
+            });
+        }
+
         const subItems = await getSubItems(user, parentId);
 
         res.status(200).json({
             response: subItems
         });
     } catch (err) {
+        if (err.name === 'ValidationError') {
+            return res.status(400).json({
+                message: err.message
+            });
+        }
+        if (err.name === 'NotFoundError') {
+            return res.status(404).json({
+                message: 'Parent item not found'
+            });
+        }
         next(err);
     }
 };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f01ce26 and 8806a21.

📒 Files selected for processing (5)
  • apps/backend/src/controllers/lib/item.controller.js (3 hunks)
  • apps/backend/src/models/lib/item.model.js (1 hunks)
  • apps/backend/src/routers/lib/common.route.js (2 hunks)
  • apps/backend/src/routers/lib/space.route.js (2 hunks)
  • apps/backend/src/services/lib/item.service.js (2 hunks)
🔇 Additional comments (6)
apps/backend/src/routers/lib/common.route.js (1)

13-13: LGTM!

The import statement is correctly updated to include the new controller.

apps/backend/src/routers/lib/space.route.js (1)

16-17: LGTM! Import statements are properly structured.

The new imports follow the existing pattern and are correctly organized with other item-related controllers.

apps/backend/src/controllers/lib/item.controller.js (2)

1-1: LGTM: Import changes are well-organized.

The addition of getSubItems to the imports follows the established pattern and maintains code organization.


226-227: LGTM: Export changes are properly structured.

The new controller is correctly added to the exports, maintaining the existing pattern.

apps/backend/src/services/lib/item.service.js (2)

399-400: LGTM! Export statement correctly includes the new function.

The export statement has been properly updated to include the new getSubItems function while maintaining existing exports.


364-378: Verify Item model schema and controller integration.

Let's verify the implementation against the codebase.

✅ Verification successful

Let me gather more information about the Item model schema since the previous search didn't show the complete schema definition.


Let me check the complete Item schema definition to verify the parent field type and any other relevant fields.


Let me check one final thing to see the complete schema definition since it was cut off, specifically focusing on the parent field.


Implementation correctly matches the schema and is properly integrated

The verification confirms that:

  • Item model has the required parent field as ObjectId with reference to 'Item'
  • The schema includes isArchived, isDeleted fields used in the query
  • Controller and routes are properly integrated:
    • Controller at apps/backend/src/controllers/lib/item.controller.js
    • Routes defined at both /inbox/:item/sub-items/ and /:space/blocks/:block/items/:item/sub-items/
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify Item model schema and controller integration

# Test 1: Check if Item model has parent field
echo "Checking Item model schema..."
ast-grep --pattern 'const ItemSchema = new Schema({
  $$$
  parent: {
    $$$
  },
  $$$
})'

# Test 2: Check controller implementation
echo "Checking controller implementation..."
rg -A 5 "getSubItemsController.*=.*async.*\(.*\).*=>" 

# Test 3: Check route definitions
echo "Checking route definitions..."
rg "sub-items" --type js

Length of output: 1290


Script:

#!/bin/bash
# Search for Item model schema definition
echo "Searching for Item model schema..."
rg -A 20 "const ItemSchema = new Schema\(" --type js

# Check for any other references to parent field in Item related files
echo "Checking parent field references..."
rg "parent:" --type js

Length of output: 1678


Script:

#!/bin/bash
# Get the complete Item schema definition
echo "Getting complete Item schema..."
rg -A 50 "const ItemSchema = new Schema\(" apps/backend/src/models/lib/item.model.js

# Check for any validation or middleware on the parent field
echo "Checking for schema middleware..."
rg -B 5 -A 5 "ItemSchema\.(pre|post|virtual|path)" apps/backend/src/models/lib/item.model.js

Length of output: 1903


Script:

#!/bin/bash
# Search specifically for parent field definition in Item model
echo "Searching for parent field..."
rg -B 5 -A 5 "parent: \{" apps/backend/src/models/lib/item.model.js

# Check for any references to isArchived, isDeleted fields
echo "Checking for isArchived and isDeleted fields..."
rg "isArchived|isDeleted" apps/backend/src/models/lib/item.model.js

Length of output: 585

@@ -64,6 +65,7 @@ router.route("/:space/blocks/:block/items/").post(createItemController);
router.route("/:space/blocks/:block/items/").get(getAllItemsByBloackController);
router.route("/:space/blocks/:block/items/:item/").get(getItemController);
router.route("/:space/blocks/:block/items/:item/").put(updateItemController);
router.route("/:space/blocks/:block/items/:item/sub-items/").get(getSubItemsController);
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Parent-child relationship is properly implemented, but route documentation is missing

The implementation shows proper parent-child constraints through:

  • Schema definition with proper parent reference (parent: { type: Schema.Types.ObjectId, ref: 'Item' })
  • Service layer validation that checks for parent existence and user ownership
  • Controller correctly extracting parent ID from params and passing to service

However, the route lacks JSDoc documentation for its purpose and parameters.

  • apps/backend/src/routers/lib/space.route.js: Add JSDoc comments above the route definition to document:
    • Purpose of the sub-items endpoint
    • Required URL parameters (space, block, item)
    • Expected response format
    • Authentication requirements
🔗 Analysis chain

Consider adding route documentation and verify parent-child constraints.

The new sub-items route follows RESTful conventions and is well-structured. However, consider the following suggestions:

  1. Add JSDoc comments to document the route's purpose and expected parameters
  2. Verify that the parent-child relationship is properly enforced in the controller

Let's verify the implementation of parent-child constraints:


Let me try a different approach to verify the implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify parent-child relationship implementation

# Check controller implementation
echo "Checking controller implementation..."
ast-grep --pattern 'export const getSubItemsController = async (req, res) => {
  $$$
}'

# Check service implementation
echo "Checking service implementation..."
ast-grep --pattern 'export const getSubItems = async ($$$) => {
  $$$
}'

# Check model schema
echo "Checking parent field in ItemSchema..."
ast-grep --pattern 'const ItemSchema = new Schema({
  $$$
  parent: {
    $$$
  }
  $$$
})'

Length of output: 509


Script:

#!/bin/bash
# Search for the controller file and its content
echo "Searching for controller file..."
rg -l "getSubItemsController" --type js

echo "\nChecking controller implementation..."
rg "getSubItemsController" -A 10 --type js

echo "\nSearching for related service file..."
rg -l "getSubItems" --type js

echo "\nChecking service implementation..."
rg "getSubItems" -A 10 --type js

echo "\nSearching for schema definitions..."
rg "Schema.*subItems|parent" -A 5 --type js

Length of output: 15698

Comment on lines +75 to +78
parent: {
type: Schema.Types.ObjectId,
ref: 'Item'
},
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add an index to optimize parent-child queries.

Since the feature involves retrieving sub-items frequently, adding an index on the parent field would improve query performance.

         parent: {
             type: Schema.Types.ObjectId,
-            ref: 'Item'
+            ref: 'Item',
+            index: true
         },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
parent: {
type: Schema.Types.ObjectId,
ref: 'Item'
},
parent: {
type: Schema.Types.ObjectId,
ref: 'Item',
index: true
},

💡 Codebase verification

Parent-child relationship maintenance needs improvement

The codebase currently has basic parent-child relationship support through the parent field in the Item model and getSubItems service method, but lacks critical data integrity features:

  1. No middleware to handle orphaned sub-items when a parent is deleted
  2. No cascading status updates from parent to children
  3. No cleanup of parent references when items are archived or soft-deleted

Key evidence:

  • Item model has parent field referencing other items
  • getSubItems service method only queries children
  • No pre/post middleware for maintaining referential integrity
  • No service methods for handling parent-child relationship updates

Recommendation: Implement middleware and service methods to:

  • Clean up child references when parent is deleted/archived
  • Optionally propagate status changes to children
  • Maintain data consistency during parent-child operations
🔗 Analysis chain

Enhance middleware for parent-child relationship maintenance.

Consider adding middleware to maintain parent-child relationship integrity:

  1. Handle orphaned sub-items when a parent is deleted
  2. Optionally propagate status changes from parent to children

Let's verify if there are any existing cascade delete implementations:


The previous search might have been too specific. Let's broaden our search to:

  1. Find all mongoose middleware definitions
  2. Look for any delete operations
  3. Check how parent-child relationships are handled in the codebase

The previous search revealed delete operations but we need to specifically look at:

  1. The Item model schema and its middleware
  2. Any service layer handling of parent-child relationships

We need to:

  1. See the complete Item model to understand how the parent field is used
  2. Locate the item service file to check for any parent-child relationship handling

We need to:

  1. Check the item service implementation for any parent-child relationship handling
  2. Look for specific usage of the parent field in queries and updates
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for cascade delete patterns in mongoose middleware
rg -A 5 "pre\(['\"]deleteOne['\"]|pre\(['\"]findOneAndDelete['\"]" 

Length of output: 76


Script:

#!/bin/bash
# Let's try a broader search for mongoose middleware and schema definitions
# First, find all mongoose schema and middleware files
fd -e js -e ts | xargs rg -l "mongoose|Schema" | xargs rg -A 5 "pre\(|schema\.pre"

# Also search for any delete-related operations
rg -A 5 "remove|delete|findOneAndDelete|deleteOne|deleteMany"

# Look for parent-child relationship handling
rg -B 2 -A 5 "parent.*ObjectId|parent.*ref"

Length of output: 48166


Script:

#!/bin/bash
# Let's search for the Item model and its middleware implementations
rg -B 5 -A 10 "ItemSchema" apps/backend/src/models/lib/item.model.js

# Also search for any cascade operations in the item service
fd -e js -e ts | grep -i "item.*service" | xargs rg -A 5 "delete|remove|update.*parent"

Length of output: 1314


Script:

#!/bin/bash
# Let's look at the complete Item model schema to understand the parent field context
cat apps/backend/src/models/lib/item.model.js

# Also search for any item service files specifically
fd -t f -e js -e ts | grep -i "item.*service"

Length of output: 3354


Script:

#!/bin/bash
# Let's examine the item service implementation for parent-child handling
cat apps/backend/src/services/lib/item.service.js

# Also search for any usage of parent field in updates or queries
rg -B 2 -A 5 "parent.*:" apps/backend/src/services/lib/item.service.js

Length of output: 10860

const user = req.user._id;
const { item: parentId } = req.params;

const subItems = await getSubItems(user, parentId);
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Implementation does not meet PR requirements for scheduling and completion dates

The current getSubItems implementation does not fulfill the PR objectives:

  • It doesn't include logic for handling items scheduled within the current week
  • It doesn't handle completion dates, only filters them out with isCompleted: false

Required changes:

  • Add scheduling criteria to include items scheduled within current week
  • Include completion date handling for done items instead of filtering them out
🔗 Analysis chain

Verify handling of scheduling and completion dates.

Based on the PR objectives, sub-items need special handling for scheduling and completion dates. Please verify that the getSubItems service implementation includes:

  1. Logic for handling items scheduled within the current week
  2. Proper handling of completion dates for done items
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the service implementation handles scheduling and completion dates
echo "Checking item.service.js for scheduling and completion date handling..."
rg -A 10 "getSubItems.*=" "src/services/lib/item.service.js"

Length of output: 286


Script:

#!/bin/bash
# First, let's find the correct path for item service implementation
fd "item.service" --type f

# Then search for getSubItems implementation across the codebase
rg -A 10 "getSubItems.*=" 

Length of output: 1839


Script:

#!/bin/bash
# Let's check the Item model schema to understand available fields
rg -A 15 "new Schema\({" apps/backend/src/models/item.model.js

# Also check if there are any scheduling-related queries in the service
rg -B 2 -A 5 "scheduledFor|dueDate|completedAt" apps/backend/src/services/lib/item.service.js

Length of output: 3541


Script:

#!/bin/bash
# Let's check the Item schema with a different pattern
rg -A 15 "const.*Schema.*=" apps/backend/src/models/item.model.js

# And check the complete getSubItems implementation
ast-grep --pattern 'const getSubItems = async ($_, $_) => { $$$ }'

Length of output: 1367

Comment on lines +364 to +378
const getSubItems = async (user, parentId) => {
const subItems = await Item.find({
parent: parentId,
user,
isArchived: false,
isDeleted: false,
isCompleted: false
});
if (!subItems.length) {
const error = new Error("No sub-items found for this parent item.");
error.statusCode = 404;
throw error;
}
return subItems;
};
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add parent item validation and pagination support.

The getSubItems function has several areas for improvement:

  1. It should validate that the parent item exists and belongs to the user before querying sub-items.
  2. For scalability, consider adding pagination support to handle large sets of sub-items.

Consider this implementation:

 const getSubItems = async (user, parentId) => {
+    const parentItem = await Item.findOne({
+        _id: parentId,
+        user,
+        isArchived: false,
+        isDeleted: false
+    });
+
+    if (!parentItem) {
+        const error = new Error("Parent item not found or inaccessible.");
+        error.statusCode = 404;
+        throw error;
+    }
+
     const subItems = await Item.find({
         parent: parentId,
         user,
         isArchived: false,
         isDeleted: false,
-        isCompleted: false
-    });
+        isCompleted: false
+    })
+    .limit(100)  // Add pagination limit
+    .sort({ createdAt: -1 });  // Sort by creation date

     if (!subItems.length) {
         const error = new Error("No sub-items found for this parent item.");
         error.statusCode = 404;
         throw error;
     }
     return subItems;
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const getSubItems = async (user, parentId) => {
const subItems = await Item.find({
parent: parentId,
user,
isArchived: false,
isDeleted: false,
isCompleted: false
});
if (!subItems.length) {
const error = new Error("No sub-items found for this parent item.");
error.statusCode = 404;
throw error;
}
return subItems;
};
const getSubItems = async (user, parentId) => {
const parentItem = await Item.findOne({
_id: parentId,
user,
isArchived: false,
isDeleted: false
});
if (!parentItem) {
const error = new Error("Parent item not found or inaccessible.");
error.statusCode = 404;
throw error;
}
const subItems = await Item.find({
parent: parentId,
user,
isArchived: false,
isDeleted: false,
isCompleted: false
})
.limit(100) // Add pagination limit
.sort({ createdAt: -1 }); // Sort by creation date
if (!subItems.length) {
const error = new Error("No sub-items found for this parent item.");
error.statusCode = 404;
throw error;
}
return subItems;
};

⚠️ Potential issue

Implement cycle date and scheduling requirements.

Based on the PR objectives (MAR-271), the function should handle scheduled items and cycle dates. Currently, it's missing these key requirements:

  1. Sub-items scheduled within the current week should be treated as part of the cycle date
  2. Completed items should show completion dates in the "today" view
  3. Unscheduled items should be moved to inbox

Consider this enhanced implementation:

-const getSubItems = async (user, parentId) => {
+const getSubItems = async (user, parentId, options = {}) => {
+    const now = new Date();
+    const startOfWeek = new Date(now);
+    startOfWeek.setDate(now.getDate() - now.getDay());
+    startOfWeek.setHours(0, 0, 0, 0);
+
+    const endOfWeek = new Date(startOfWeek);
+    endOfWeek.setDate(startOfWeek.getDate() + 6);
+    endOfWeek.setHours(23, 59, 59, 999);
+
     const subItems = await Item.find({
         parent: parentId,
         user,
         isArchived: false,
         isDeleted: false,
-        isCompleted: false
+        $or: [
+            // Incomplete items
+            {
+                isCompleted: false,
+                $or: [
+                    // Items scheduled for this week
+                    { 
+                        "cycle.startsAt": { 
+                            $gte: startOfWeek,
+                            $lte: endOfWeek 
+                        }
+                    },
+                    // Unscheduled items (inbox)
+                    {
+                        "cycle.startsAt": null,
+                        spaces: { $exists: true, $eq: [] }
+                    }
+                ]
+            },
+            // Show completed items in today view
+            {
+                isCompleted: true,
+                completedAt: {
+                    $gte: new Date().setHours(0, 0, 0, 0),
+                    $lt: new Date().setHours(23, 59, 59, 999)
+                }
+            }
+        ]
     });

     if (!subItems.length) {
         const error = new Error("No sub-items found for this parent item.");
         error.statusCode = 404;
         throw error;
     }
     return subItems;
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const getSubItems = async (user, parentId) => {
const subItems = await Item.find({
parent: parentId,
user,
isArchived: false,
isDeleted: false,
isCompleted: false
});
if (!subItems.length) {
const error = new Error("No sub-items found for this parent item.");
error.statusCode = 404;
throw error;
}
return subItems;
};
const getSubItems = async (user, parentId, options = {}) => {
const now = new Date();
const startOfWeek = new Date(now);
startOfWeek.setDate(now.getDate() - now.getDay());
startOfWeek.setHours(0, 0, 0, 0);
const endOfWeek = new Date(startOfWeek);
endOfWeek.setDate(startOfWeek.getDate() + 6);
endOfWeek.setHours(23, 59, 59, 999);
const subItems = await Item.find({
parent: parentId,
user,
isArchived: false,
isDeleted: false,
$or: [
// Incomplete items
{
isCompleted: false,
$or: [
// Items scheduled for this week
{
"cycle.startsAt": {
$gte: startOfWeek,
$lte: endOfWeek
}
},
// Unscheduled items (inbox)
{
"cycle.startsAt": null,
spaces: { $exists: true, $eq: [] }
}
]
},
// Show completed items in today view
{
isCompleted: true,
completedAt: {
$gte: new Date().setHours(0, 0, 0, 0),
$lt: new Date().setHours(23, 59, 59, 999)
}
}
]
});
if (!subItems.length) {
const error = new Error("No sub-items found for this parent item.");
error.statusCode = 404;
throw error;
}
return subItems;
};

@sajdakabir sajdakabir merged commit 92cdd54 into preview Nov 5, 2024
9 of 17 checks passed
@sajdakabir sajdakabir deleted the sajdakabir/mar-271-sub-item branch November 5, 2024 04:15
@oliursahin oliursahin linked an issue Nov 5, 2024 that may be closed by this pull request
@coderabbitai coderabbitai bot mentioned this pull request Nov 6, 2024
8 tasks
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.

sub item
1 participant