• 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

18.52
/src/domain/timeline/event/event.service.ts
1
import { Inject, Injectable, LoggerService } from '@nestjs/common';
18✔
2
import { InjectRepository } from '@nestjs/typeorm';
18✔
3
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
18✔
4
import { FindOneOptions, In, Repository } from 'typeorm';
18✔
5
import { EntityNotFoundException } from '@common/exceptions';
18✔
6
import { LogContext, ProfileType } from '@common/enums';
18✔
7
import { AuthorizationPolicy } from '@domain/common/authorization-policy';
18✔
8
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
18✔
9
import { DeleteCalendarEventInput } from './dto/event.dto.delete';
10
import { UpdateCalendarEventInput } from './dto/event.dto.update';
11
import { CreateCalendarEventInput } from './dto/event.dto.create';
12
import { CalendarEvent } from './event.entity';
18✔
13
import { ICalendarEvent } from './event.interface';
14
import { ProfileService } from '@domain/common/profile/profile.service';
18✔
15
import { IProfile } from '@domain/common/profile/profile.interface';
16
import { RoomService } from '@domain/communication/room/room.service';
18✔
17
import { RoomType } from '@common/enums/room.type';
18✔
18
import { TagsetReservedName } from '@common/enums/tagset.reserved.name';
18✔
19
import { IStorageAggregator } from '@domain/storage/storage-aggregator/storage.aggregator.interface';
20
import { AuthorizationPolicyType } from '@common/enums/authorization.policy.type';
18✔
21
import { ISpace } from '@domain/space/space/space.interface';
22
import { Calendar } from '@domain/timeline/calendar/calendar.entity';
23
import { Timeline } from '@domain/timeline/timeline/timeline.entity';
18✔
24
import { Collaboration } from '@domain/collaboration/collaboration';
25
import { Space } from '@domain/space/space/space.entity';
×
26
import { SpaceLevel } from '@common/enums/space.level';
×
27

×
28
@Injectable()
29
export class CalendarEventService {
×
30
  constructor(
×
31
    private authorizationPolicyService: AuthorizationPolicyService,
32
    private roomService: RoomService,
33
    private profileService: ProfileService,
34
    @InjectRepository(Space) private spaceRepository: Repository<Space>,
35
    @InjectRepository(CalendarEvent)
36
    private calendarEventRepository: Repository<CalendarEvent>,
37
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
38
  ) {}
39

×
40
  public async createCalendarEvent(
×
41
    calendarEventInput: CreateCalendarEventInput,
42
    storageAggregator: IStorageAggregator,
43
    userID: string
44
  ): Promise<ICalendarEvent> {
45
    const calendarEvent: ICalendarEvent =
×
46
      CalendarEvent.create(calendarEventInput);
47
    calendarEvent.profile = await this.profileService.createProfile(
×
48
      calendarEventInput.profileData,
NEW
49
      ProfileType.CALENDAR_EVENT,
×
50
      storageAggregator
51
    );
52
    await this.profileService.addOrUpdateTagsetOnProfile(
×
53
      calendarEvent.profile,
54
      {
×
55
        name: TagsetReservedName.DEFAULT,
56
        tags: calendarEventInput.tags || [],
57
      }
58
    );
59
    calendarEvent.authorization = new AuthorizationPolicy(
×
60
      AuthorizationPolicyType.CALENDAR_EVENT
61
    );
62
    calendarEvent.createdBy = userID;
63

×
64
    calendarEvent.comments = await this.roomService.createRoom(
65
      `calendarEvent-comments-${calendarEvent.nameID}`,
66
      RoomType.CALENDAR_EVENT
67
    );
68

69
    return await this.save(calendarEvent);
×
70
  }
×
71

72
  public async save(calendarEvent: ICalendarEvent): Promise<CalendarEvent> {
73
    return this.calendarEventRepository.save(calendarEvent);
×
74
  }
×
75

76
  public async deleteCalendarEvent(
×
77
    deleteData: DeleteCalendarEventInput
×
78
  ): Promise<ICalendarEvent> {
79
    const calendarEventID = deleteData.ID;
×
80
    const calendarEvent = await this.getCalendarEventOrFail(calendarEventID, {
×
81
      relations: { profile: true, comments: true },
82
    });
83
    if (calendarEvent.authorization) {
×
84
      await this.authorizationPolicyService.delete(calendarEvent.authorization);
85
    }
86
    if (calendarEvent.profile) {
×
87
      await this.profileService.deleteProfile(calendarEvent.profile.id);
×
88
    }
89
    if (calendarEvent.comments) {
90
      await this.roomService.deleteRoom(calendarEvent.comments);
91
    }
92

93
    const result = await this.calendarEventRepository.remove(
94
      calendarEvent as CalendarEvent
×
95
    );
96
    result.id = calendarEventID;
97
    return result;
98
  }
×
99

×
100
  public async getCalendarEventOrFail(
101
    calendarEventID: string,
102
    options?: FindOneOptions<CalendarEvent>
103
  ): Promise<ICalendarEvent | never> {
×
104
    const calendarEvent = await this.calendarEventRepository.findOne({
105
      where: { id: calendarEventID },
106
      ...options,
107
    });
108
    if (!calendarEvent)
109
      throw new EntityNotFoundException(
×
110
        `Not able to locate calendarEvent with the specified ID: ${calendarEventID}`,
111
        LogContext.CALENDAR
112
      );
113
    return calendarEvent;
114
  }
115

116
  public async updateCalendarEvent(
117
    calendarEventData: UpdateCalendarEventInput
×
118
  ): Promise<ICalendarEvent> {
×
119
    const calendarEvent = await this.getCalendarEventOrFail(
×
120
      calendarEventData.ID,
121
      {
122
        relations: { profile: true },
123
      }
124
    );
×
125

126
    // Copy over the received data
127
    if (calendarEventData.profileData) {
128
      if (!calendarEvent.profile) {
129
        throw new EntityNotFoundException(
×
130
          `CalendarEvent not initialised: ${calendarEvent.id}`,
×
131
          LogContext.CALENDAR
132
        );
×
133
      }
×
134
      calendarEvent.profile = await this.profileService.updateProfile(
135
        calendarEvent.profile,
×
136
        calendarEventData.profileData
×
137
      );
×
138
    }
139
    if (calendarEventData.durationDays) {
×
140
      calendarEvent.durationDays = calendarEventData.durationDays;
×
141
    }
142
    if (calendarEventData.durationMinutes) {
143
      calendarEvent.durationMinutes = calendarEventData.durationMinutes;
×
144
    }
145
    calendarEvent.wholeDay = calendarEventData.wholeDay;
×
146
    calendarEvent.multipleDays = calendarEventData.multipleDays;
147
    calendarEvent.startDate = calendarEventData.startDate;
148

149
    if (calendarEventData.type) {
150
      calendarEvent.type = calendarEventData.type;
151
    }
×
152

153
    calendarEvent.visibleOnParentCalendar =
154
      calendarEventData.visibleOnParentCalendar ??
155
      calendarEvent.visibleOnParentCalendar;
×
156

157
    return this.calendarEventRepository.save(calendarEvent);
158
  }
159

160
  public async saveCalendarEvent(
161
    calendarEvent: ICalendarEvent
×
162
  ): Promise<ICalendarEvent> {
×
163
    return await this.calendarEventRepository.save(calendarEvent);
164
  }
165

166
  public getCalendarEvent(
167
    calendarId: string,
×
168
    eventID: string
169
  ): Promise<ICalendarEvent> {
170
    return this.calendarEventRepository.findOneOrFail({
171
      where: [
×
172
        {
173
          id: eventID,
174
          calendar: {
175
            id: calendarId,
176
          },
177
        },
178
      ],
×
179
    });
×
180
  }
181

182
  public getCalendarEvents(eventIds: string[]): Promise<ICalendarEvent[]> {
183
    return this.calendarEventRepository.findBy({
184
      id: In(eventIds),
185
    });
×
186
  }
187

188
  public async getProfileOrFail(
189
    calendarEvent: ICalendarEvent
190
  ): Promise<IProfile> {
191
    const calendarEventLoaded = await this.getCalendarEventOrFail(
192
      calendarEvent.id,
193
      {
194
        relations: { profile: true },
195
      }
196
    );
197
    if (!calendarEventLoaded.profile)
198
      throw new EntityNotFoundException(
199
        `Post profile not initialised for calendarEvent: ${calendarEvent.id}`,
200
        LogContext.CALENDAR
201
      );
202

203
    return calendarEventLoaded.profile;
204
  }
205

206
  public async getSubspace(
207
    calendarEvent: ICalendarEvent
208
  ): Promise<ISpace | undefined> {
209
    const spaceParentOfTheEvent = await this.calendarEventRepository
210
      .createQueryBuilder('calendarEvent')
211
      .leftJoin(Calendar, 'calendar', 'calendar.id = calendarEvent.calendarId')
212
      .leftJoin(Timeline, 'timeline', 'timeline.calendarId = calendar.id')
213
      .leftJoin(
214
        Collaboration,
215
        'collaboration',
216
        'collaboration.timelineId = timeline.id'
217
      )
218
      .leftJoin(
219
        Space,
220
        'subspace',
221
        'subspace.collaborationId = collaboration.id'
222
      )
223
      .where('calendarEvent.id = :id', { id: calendarEvent.id })
224
      .andWhere('subspace.level != :level', { level: SpaceLevel.L0 })
225
      .select('subspace.id as spaceId')
226
      .getRawOne<{ spaceId: string }>();
227

228
    if (!spaceParentOfTheEvent) {
229
      return undefined;
230
    }
231

232
    const space = await this.spaceRepository.findOne({
233
      where: { id: spaceParentOfTheEvent.spaceId },
234
    });
235

236
    return space ?? undefined;
237
  }
238

239
  public async getComments(calendarEventID: string) {
240
    const calendarEventLoaded = await this.getCalendarEventOrFail(
241
      calendarEventID,
242
      {
243
        relations: { comments: true },
244
      }
245
    );
246

247
    if (!calendarEventLoaded.comments) {
248
      throw new EntityNotFoundException(
249
        `Comments not found on calendarEvent: ${calendarEventID}`,
250
        LogContext.CALENDAR
251
      );
252
    }
253

254
    return calendarEventLoaded.comments;
255
  }
256
}
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