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

Refactor User Queries and Modify Schema for Mongoose find Usage #1112

Open
3 tasks
pepermao opened this issue Nov 28, 2023 · 0 comments
Open
3 tasks

Refactor User Queries and Modify Schema for Mongoose find Usage #1112

pepermao opened this issue Nov 28, 2023 · 0 comments

Comments

@pepermao
Copy link
Collaborator

Background Information

With the current implementation of user queries relying on MongoDB aggregations, there is a need to enhance the code's readability and maintainability by transitioning to Mongoose find queries. In this context, a namespaces field needs to be added to the User schema to support the use of Mongoose find. The Namespace schema already references users, but the User schema lacks this reference.

User Stories

  • As a developer, I want to replace MongoDB aggregations with Mongoose find queries for user-related operations to improve code quality and maintainability.
  • As a system user, I want the User schema to include a namespaces field so that I can efficiently retrieve user-related information when needed.

What

Modify the User schema to include a users field and refactor the user-related aggregation to use Mongoose find queries.

How

Add namespaces Field to User Schema:

  • Modify the User schema to include a namespaces field, establishing a reference to the Namespace schema.

    @Schema()
    export class User {
        // Existing properties...
    
        @Prop({
            type: [
                {
                    type: mongoose.Types.ObjectId,
                    ref: "Namespace",
                    required: false,
                },
            ],
        })
        namespaces: Namespace[];
    }

Refactor User Queries:

  • Rewrite the existing user queries using Mongoose's find method.

  • Utilize the added namespaces field in the User schema for the necessary reference.

    async findAll(userQuery): Promise<UserDocument[]> {
        const { searchName, filterOutRoles, badges, project, nameSpaceSlug } = userQuery;
        
        const query = {
            name: { $regex: searchName || "", $options: "i" },
            role: { $nin: [...(filterOutRoles || []), null] },
            ...(badges ? { badges } : {}),
        };
    
        const populateOptions = [
            { path: 'badges', model: 'Badge' },
            { path: 'namespaces', model: 'Namespace' },
        ];
    
        if (nameSpaceSlug && nameSpaceSlug !== NameSpaceEnum.Main) {
            populateOptions.push({
                path: 'namespaces',
                match: { slug: nameSpaceSlug },
            });
        }
    
        return this.UserModel.find(query)
            .populate(populateOptions)
            .select(project || { _id: 1, name: 1, role: 1 });
    }

Acceptance Criteria

  • Code Functionality: Verify that the modified User schema successfully establishes a reference to the Namespace schema.
  • Schema Modification: Confirm that the User schema includes the namespaces field as described in the modification.
  • Code Readability: Ensure that the refactored code is readable and adheres to best practices for Mongoose queries.
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

No branches or pull requests

1 participant