• 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

29.31
/src/domain/communication/communication/communication.service.ts
1
import { Inject, Injectable, LoggerService } from '@nestjs/common';
21✔
2
import { InjectRepository } from '@nestjs/typeorm';
21✔
3
import {
21✔
4
  EntityNotFoundException,
5
  EntityNotInitializedException,
6
} from '@common/exceptions';
7
import { LogContext } from '@common/enums';
21✔
8
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
21✔
9
import { FindOneOptions, Repository } from 'typeorm';
21✔
10
import {
21✔
11
  Communication,
12
  ICommunication,
13
} from '@domain/communication/communication';
14
import { AuthorizationPolicy } from '@domain/common/authorization-policy';
21✔
15
import { CommunicationAdapter } from '@services/adapters/communication-adapter/communication.adapter';
21✔
16
import { IUser } from '@domain/community/user/user.interface';
17
import { RoomService } from '../room/room.service';
21✔
18
import { IRoom } from '../room/room.interface';
19
import { RoomType } from '@common/enums/room.type';
21✔
20
import { AuthorizationPolicyType } from '@common/enums/authorization.policy.type';
21✔
21

22
@Injectable()
23
export class CommunicationService {
21✔
24
  constructor(
25
    private roomService: RoomService,
1✔
26
    private communicationAdapter: CommunicationAdapter,
1✔
27
    @InjectRepository(Communication)
28
    private communicationRepository: Repository<Communication>,
1✔
29
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
1✔
30
  ) {}
31

32
  async createCommunication(
33
    displayName: string,
34
    spaceID: string
35
  ): Promise<ICommunication> {
36
    const communication: ICommunication = new Communication(displayName);
×
NEW
37
    communication.authorization = new AuthorizationPolicy(
×
38
      AuthorizationPolicyType.COMMUNICATION
39
    );
40
    communication.spaceID = spaceID;
×
41

42
    // save to get the id assigned
43
    await this.save(communication);
×
44

45
    communication.updates = await this.roomService.createRoom(
×
46
      `${displayName}-Updates`,
47
      RoomType.UPDATES
48
    );
49

50
    return communication;
×
51
  }
52

53
  async save(communication: ICommunication): Promise<ICommunication> {
54
    return await this.communicationRepository.save(communication);
×
55
  }
56

57
  getUpdates(communication: ICommunication): IRoom {
58
    if (!communication.updates) {
×
59
      throw new EntityNotInitializedException(
×
60
        `Communication not initialized, no Updates: ${communication.id}`,
61
        LogContext.COMMUNICATION
62
      );
63
    }
64
    return communication.updates;
×
65
  }
66

67
  async getCommunicationOrFail(
68
    communicationID: string,
69
    options?: FindOneOptions<Communication>
70
  ): Promise<ICommunication | never> {
71
    const communication = await this.communicationRepository.findOne({
×
72
      where: {
73
        id: communicationID,
74
      },
75
      ...options,
76
    });
77
    if (!communication)
×
78
      throw new EntityNotFoundException(
×
79
        `Unable to find Communication with ID: ${communicationID}`,
80
        LogContext.COMMUNICATION
81
      );
82
    return communication;
×
83
  }
84

85
  async removeCommunication(communicationID: string): Promise<boolean> {
86
    // Note need to load it in with all contained entities so can remove fully
87
    const communication = await this.getCommunicationOrFail(communicationID, {
×
88
      relations: { updates: true },
89
    });
90

91
    if (!communication.updates)
×
92
      throw new EntityNotFoundException(
×
93
        `Unable to find Communication with ID: ${communicationID}`,
94
        LogContext.COMMUNICATION
95
      );
96

97
    await this.roomService.deleteRoom(communication.updates);
×
98

99
    await this.communicationRepository.remove(communication as Communication);
×
100
    return true;
×
101
  }
102

103
  async addContributorToCommunications(
104
    communication: ICommunication,
105
    contributorCommunicationID: string
106
  ): Promise<boolean> {
107
    if (!contributorCommunicationID || contributorCommunicationID === '') {
×
108
      // no communication ID to manage, just return
109
      return true;
×
110
    }
111
    const communicationRoomIDs = await this.getRoomsUsed(communication);
×
112
    await this.communicationAdapter.userAddToRooms(
×
113
      communicationRoomIDs,
114
      contributorCommunicationID
115
    );
116

117
    return true;
×
118
  }
119

120
  async getRoomsUsed(communication: ICommunication): Promise<string[]> {
121
    const communicationRoomIDs: string[] = [
×
122
      this.getUpdates(communication).externalRoomID,
123
    ];
124

125
    return communicationRoomIDs;
×
126
  }
127

128
  async getCommunicationIDsUsed(): Promise<string[]> {
129
    const communicationMatches = await this.communicationRepository
×
130
      .createQueryBuilder('communication')
131
      .getMany();
132
    const communicationIDs: string[] = [];
×
133
    for (const communication of communicationMatches) {
×
134
      communicationIDs.push(communication.id);
×
135
    }
136
    return communicationIDs;
×
137
  }
138

139
  async removeUserFromCommunications(
140
    communication: ICommunication,
141
    user: IUser
142
  ): Promise<boolean> {
143
    // get the list of rooms to add the user to
144
    const communicationRoomIDs: string[] = [
×
145
      this.getUpdates(communication).externalRoomID,
146
    ];
147

148
    await this.communicationAdapter.removeUserFromRooms(
×
149
      communicationRoomIDs,
150
      user.communicationID
151
    );
152

153
    return true;
×
154
  }
155
}
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