AletheiaFact / AletheiaFact/aletheia

Refactor User Queries and Modify Schema for Mongoose find Usage

オープン
#1,112 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
55
フォーク
20
平均マージ
2日 6時間
マージ済み PR(30日)
2

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。