universo-platform-3d

Форк
0
108 строк · 2.7 Кб
1
import { Injectable } from '@nestjs/common'
2
import { InjectModel } from '@nestjs/mongoose'
3
import { Model, Types } from 'mongoose'
4
import { CreateUserGroupDto } from './dto/create-group.users.dto'
5
import { UpdateUserGroupDto } from './dto/update-group.users.dto'
6
import { UserGroup, UserGroupDocument } from './user-group.schema'
7

8
@Injectable()
9
export class UserGroupService {
10
  constructor(
11
    @InjectModel(UserGroup.name)
12
    private userGroupModel: Model<UserGroupDocument>
13
  ) {}
14

15
  public create(createUserGroupDto: CreateUserGroupDto): Promise<any> {
16
    const created = new this.userGroupModel(createUserGroupDto)
17
    return created.save()
18
  }
19

20
  public findOne(id: string): Promise<any> {
21
    return this.userGroupModel
22
      .aggregate<UserGroupDocument[]>()
23
      .append({ $match: { _id: new Types.ObjectId(id) } })
24
      .lookup({
25
        from: 'users',
26
        localField: 'creator',
27
        foreignField: '_id',
28
        as: 'creator'
29
      })
30
      .unwind({ path: '$creator' })
31
      .exec()
32
  }
33

34
  public update(
35
    id: string,
36
    updateUserGroupDto: UpdateUserGroupDto
37
  ): Promise<any> {
38
    return this.userGroupModel
39
      .findByIdAndUpdate(id, updateUserGroupDto, { new: true })
40
      .exec()
41
  }
42

43
  public remove(id: string): Promise<any> {
44
    return this.userGroupModel.findOneAndDelete({ _id: id }).exec()
45
  }
46

47
  /**
48
   * @description Returns all groups where the user is a creator, user, owner, or moderator
49
   */
50
  public findAllForUser(userId: string): Promise<any> {
51
    return this.userGroupModel
52
      .find()
53
      .where({
54
        $or: [
55
          {
56
            creator: { $in: [userId] }
57
          },
58
          {
59
            users: { $in: [userId] }
60
          },
61
          {
62
            owners: { $in: [userId] }
63
          },
64
          {
65
            moderators: { $in: [userId] }
66
          }
67
        ]
68
      })
69
      .exec()
70
  }
71

72
  public search(searchParams): Promise<any> {
73
    return this.userGroupModel
74
      .find({
75
        [searchParams.filterField]: {
76
          $regex: new RegExp(searchParams.filterValue),
77
          $options: 'i'
78
        }
79
      })
80
      .sort({ [searchParams.sortField]: searchParams.sortValue })
81
      .limit(searchParams.limit)
82
      .skip(searchParams.skip)
83
      .exec()
84
  }
85

86
  public removeMember(groupId, idUserRemove): Promise<any> {
87
    return this.userGroupModel
88
      .findByIdAndUpdate(
89
        groupId,
90
        {
91
          $pull: {
92
            users: idUserRemove,
93
            owners: idUserRemove,
94
            moderators: idUserRemove
95
          }
96
        },
97
        { new: true }
98
      )
99
      .exec()
100
  }
101

102
  public async findGroupsInformation(userLinks): Promise<any> {
103
    const groupsIds = []
104
    userLinks.map((userLink) => groupsIds.push(userLink.group))
105

106
    return await this.userGroupModel.find({ _id: groupsIds })
107
  }
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.