AletheiaFact / AletheiaFact/aletheia

Refactor User Queries and Modify Schema for Mongoose find Usage

Open
#1,112 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
55
Forks
20
Avg merge
2d 6h
Merged PRs (30d)
2

Description

## 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.

```typescript
@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.

```typescript
async findAll(userQuery): Promise {
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.