• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alkem-io / server / #8050

16 Aug 2024 11:21AM UTC coverage: 13.92%. First build
#8050

Pull #4411

travis-ci

Pull Request #4411: Type added to authorization policy entity

80 of 4158 branches covered (1.92%)

Branch coverage included in aggregate %.

61 of 116 new or added lines in 50 files covered. (52.59%)

1945 of 10389 relevant lines covered (18.72%)

3.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

19.1
/src/domain/community/community/community.service.ts
1
import { IUserGroup } from '@domain/community/user-group/user-group.interface';
2
import { UserGroupService } from '@domain/community/user-group/user-group.service';
20✔
3
import { Inject, Injectable, LoggerService } from '@nestjs/common';
20✔
4
import { InjectRepository } from '@nestjs/typeorm';
20✔
5
import {
20✔
6
  EntityNotFoundException,
7
  EntityNotInitializedException,
8
  RelationshipNotFoundException,
9
} from '@common/exceptions';
10
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
20✔
11
import { FindOneOptions, FindOptionsRelations, Repository } from 'typeorm';
20✔
12
import { CreateUserGroupInput } from '@domain/community/user-group/dto';
13
import { Community, ICommunity } from '@domain/community/community';
20✔
14
import { AuthorizationPolicy } from '@domain/common/authorization-policy';
20✔
15
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
20✔
16
import { CommunicationService } from '@domain/communication/communication/communication.service';
20✔
17
import { ICommunication } from '@domain/communication/communication';
20✔
18
import { LogContext } from '@common/enums/logging.context';
19
import { CommunityResolverService } from '@services/infrastructure/entity-resolver/community.resolver.service';
20✔
20
import { StorageAggregatorResolverService } from '@services/infrastructure/storage-aggregator-resolver/storage.aggregator.resolver.service';
21
import { CreateCommunityInput } from './dto/community.dto.create';
22
import { AuthorizationPolicyType } from '@common/enums/authorization.policy.type';
23
import { RoleSetService } from '@domain/access/role-set/role.set.service';
20✔
24
import { IRoleSet } from '@domain/access/role-set';
25

26
@Injectable()
20✔
27
export class CommunityService {
28
  constructor(
20✔
29
    private authorizationPolicyService: AuthorizationPolicyService,
20✔
30
    private userGroupService: UserGroupService,
20✔
31
    private communicationService: CommunicationService,
20✔
32
    private communityResolverService: CommunityResolverService,
33
    private roleSetService: RoleSetService,
34
    private storageAggregatorResolverService: StorageAggregatorResolverService,
35
    @InjectRepository(Community)
36
    private communityRepository: Repository<Community>,
20✔
37
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
38
  ) {}
20✔
39

40
  async createCommunity(
41
    communityData: CreateCommunityInput
20✔
42
  ): Promise<ICommunity> {
43
    const community: ICommunity = new Community();
1✔
44
    community.authorization = new AuthorizationPolicy(
1✔
45
      AuthorizationPolicyType.COMMUNITY
1✔
46
    );
1✔
47
    community.roleSet = await this.roleSetService.createRoleSet(
1✔
48
      communityData.roleSetData
1✔
49
    );
1✔
50

1✔
51
    community.groups = [];
1✔
52
    community.communication =
1✔
53
      await this.communicationService.createCommunication(
1✔
54
        communityData.name,
55
        ''
1✔
56
      );
1✔
57
    return community;
58
  }
59

60
  async createGroup(groupData: CreateUserGroupInput): Promise<IUserGroup> {
61
    const communityID = groupData.parentID;
62
    const groupName = groupData.profile.displayName;
63

×
NEW
64
    this.logger.verbose?.(
×
65
      `Adding userGroup (${groupName}) to Community (${communityID})`,
66
      LogContext.COMMUNITY
67
    );
×
68

×
69
    // Try to find the Community
70
    const community = await this.getCommunityOrFail(communityID, {
71
      relations: { groups: true },
72
    });
73

74
    const storageAggregator =
×
75
      await this.storageAggregatorResolverService.getStorageAggregatorForCommunity(
76
        community.id
77
      );
78
    const group = await this.userGroupService.addGroupWithName(
79
      community,
×
80
      groupName,
81
      storageAggregator
82
    );
83
    await this.communityRepository.save(community);
×
84

×
85
    return group;
×
86
  }
87

×
88
  // Loads the group into the Community entity if not already present
×
89
  async getUserGroups(community: ICommunity): Promise<IUserGroup[]> {
90
    const communityWithGroups = await this.getCommunityOrFail(community.id, {
91
      relations: { groups: true },
92
    });
93
    if (!communityWithGroups.groups) {
×
94
      throw new EntityNotInitializedException(
95
        `Community not initialized: ${community.id}`,
96
        LogContext.COMMUNITY
97
      );
×
98
    }
×
99
    return communityWithGroups.groups;
100
  }
×
101

102
  // Loads the group into the Community entity if not already present
103
  async getUserGroup(
104
    community: ICommunity,
105
    groupID: string
106
  ): Promise<IUserGroup> {
×
107
    const communityWithGroups = await this.getCommunityOrFail(community.id, {
108
      relations: { groups: true },
109
    });
110
    if (!communityWithGroups.groups) {
111
      throw new EntityNotInitializedException(
×
112
        `Community not initialized: ${community.id}`,
113
        LogContext.COMMUNITY
114
      );
×
115
    }
116
    const result = communityWithGroups.groups.find(
117
      group => group.id === groupID
118
    );
119
    if (!result) {
×
120
      throw new EntityNotFoundException(
121
        `Unable to find group with ID: '${groupID}'`,
×
122
        LogContext.COMMUNITY,
123
        { communityId: community.id }
124
      );
125
    }
126
    return result;
×
127
  }
128

129
  async getCommunityOrFail(
×
130
    communityID: string,
×
131
    options?: FindOneOptions<Community>
132
  ): Promise<ICommunity | never> {
133
    const community = await this.communityRepository.findOne({
134
      where: { id: communityID },
135
      ...options,
×
136
    });
137
    if (!community)
138
      throw new EntityNotFoundException(
139
        `Unable to find Community with ID: ${communityID}`,
140
        LogContext.COMMUNITY
141
      );
142
    return community;
143
  }
×
144

145
  async removeCommunityOrFail(communityID: string): Promise<boolean | never> {
146
    // Note need to load it in with all contained entities so can remove fully
×
147
    const community = await this.getCommunityOrFail(communityID, {
×
148
      relations: {
149
        roleSet: true,
150
        groups: true,
151
        communication: true,
152
      },
×
153
    });
×
154
    if (
155
      !community.communication ||
×
156
      !community.communication.updates ||
×
157
      !community.roleSet ||
158
      !community.groups
159
    ) {
160
      throw new RelationshipNotFoundException(
161
        `Unable to load child entities for community for deletion: ${community.id} `,
162
        LogContext.COMMUNITY
×
163
      );
164
    }
165

166
    await this.roleSetService.removeRoleSetOrFail(community.roleSet.id);
167

168
    // Remove all groups
169
    for (const group of community.groups) {
×
170
      await this.userGroupService.removeUserGroup({
171
        ID: group.id,
172
      });
173
    }
×
174

×
175
    if (community.authorization)
176
      await this.authorizationPolicyService.delete(community.authorization);
177

178
    await this.communicationService.removeCommunication(
×
179
      community.communication.id
180
    );
181

182
    await this.communityRepository.remove(community as Community);
183
    return true;
×
184
  }
185

186
  async save(community: ICommunity): Promise<ICommunity> {
187
    return await this.communityRepository.save(community);
188
  }
189

190
  public async getDisplayName(community: ICommunity): Promise<string> {
191
    return await this.communityResolverService.getDisplayNameForRoleSetOrFail(
192
      community.id
193
    );
194
  }
×
195

×
196
  public async getRoleSet(community: ICommunity): Promise<IRoleSet> {
197
    const communityWithRoleSet = await this.getCommunityOrFail(community.id, {
198
      relations: { roleSet: true },
199
    });
200

201
    if (!communityWithRoleSet.roleSet) {
202
      throw new EntityNotInitializedException(
203
        `Unable to locate RoleSet for community: ${community.id}`,
204
        LogContext.COMMUNITY
205
      );
×
206
    }
207
    return communityWithRoleSet.roleSet;
208
  }
209

210
  async getCommunication(
211
    communityID: string,
212
    relations?: FindOptionsRelations<ICommunity>
×
213
  ): Promise<ICommunication> {
×
214
    const community = await this.getCommunityOrFail(communityID, {
215
      relations: { communication: true, ...relations },
216
    });
217

218
    const communication = community.communication;
×
219
    if (!communication) {
×
220
      throw new EntityNotInitializedException(
221
        `Unable to locate communication for community: ${community.id}`,
222
        LogContext.COMMUNITY
×
223
      );
×
224
    }
225
    return communication;
226
  }
227

228
  public async getLevelZeroSpaceIdForCommunity(
229
    community: ICommunity
×
230
  ): Promise<string> {
×
231
    return await this.communityResolverService.getLevelZeroSpaceIdForCommunity(
232
      community.id
233
    );
234
  }
235
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc