• 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

21.92
/src/domain/collaboration/post/post.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, FindOptionsRelations, Repository } from 'typeorm';
18✔
5
import { EntityNotFoundException } from '@common/exceptions';
18✔
6
import { LogContext, ProfileType } from '@common/enums';
18✔
7
import { IPost } from '@domain/collaboration/post/post.interface';
8
import { Post } from '@domain/collaboration/post/post.entity';
18✔
9
import { AuthorizationPolicy } from '@domain/common/authorization-policy';
18✔
10
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
18✔
11
import { UpdatePostInput } from './dto/post.dto.update';
12
import { CreatePostInput } from './dto/post.dto.create';
13
import { IProfile } from '@domain/common/profile/profile.interface';
14
import { ProfileService } from '@domain/common/profile/profile.service';
18✔
15
import { VisualType } from '@common/enums/visual.type';
18✔
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

22
@Injectable()
23
export class PostService {
18✔
24
  constructor(
25
    private authorizationPolicyService: AuthorizationPolicyService,
×
26
    private roomService: RoomService,
×
27
    private profileService: ProfileService,
×
28
    @InjectRepository(Post)
29
    private postRepository: Repository<Post>,
×
30
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
×
31
  ) {}
32

33
  public async createPost(
34
    postInput: CreatePostInput,
35
    storageAggregator: IStorageAggregator,
36
    userID: string
37
  ): Promise<IPost> {
38
    const post: IPost = Post.create(postInput);
×
39
    post.profile = await this.profileService.createProfile(
×
40
      postInput.profileData,
41
      ProfileType.POST,
42
      storageAggregator
43
    );
44
    await this.profileService.addVisualsOnProfile(
×
45
      post.profile,
×
46
      postInput.profileData.visuals,
×
47
      [VisualType.BANNER, VisualType.CARD]
48
    );
×
49
    await this.profileService.addOrUpdateTagsetOnProfile(post.profile, {
NEW
50
      name: TagsetReservedName.DEFAULT,
×
51
      tags: postInput.tags || [],
×
52
    });
53
    post.authorization = new AuthorizationPolicy(AuthorizationPolicyType.POST);
×
54
    post.createdBy = userID;
55

56
    post.comments = await this.roomService.createRoom(
57
      `post-comments-${post.nameID}`,
58
      RoomType.POST
×
59
    );
60

61
    return post;
62
  }
×
63

64
  public async deletePost(postId: string): Promise<IPost> {
65
    const post = await this.getPostOrFail(postId, {
×
66
      relations: { profile: true },
×
67
    });
68
    if (post.authorization) {
×
69
      await this.authorizationPolicyService.delete(post.authorization);
×
70
    }
71
    if (post.profile) {
×
72
      await this.profileService.deleteProfile(post.profile.id);
×
73
    }
74
    if (post.comments) {
75
      await this.roomService.deleteRoom(post.comments);
×
76
    }
×
77

×
78
    const result = await this.postRepository.remove(post as Post);
79
    result.id = postId;
80
    return result;
81
  }
82

83
  public async getPostOrFail(
84
    postID: string,
×
85
    options?: FindOneOptions<Post>
86
  ): Promise<IPost | never> {
87
    const post = await this.postRepository.findOne({
88
      where: { id: postID },
×
89
      ...options,
×
90
    });
91
    if (!post)
92
      throw new EntityNotFoundException(
93
        `Not able to locate post with the specified ID: ${postID}`,
×
94
        LogContext.SPACES
95
      );
96
    return post;
97
  }
×
98

99
  public async updatePost(postData: UpdatePostInput): Promise<IPost> {
100
    const post = await this.getPostOrFail(postData.ID, {
101
      relations: { profile: true },
102
    });
103

×
104
    // Copy over the received data
×
105

×
106
    if (postData.profileData) {
107
      if (!post.profile) {
108
        throw new EntityNotFoundException(
109
          `Post not initialised: ${post.id}`,
110
          LogContext.COLLABORATION
×
111
        );
112
      }
113
      post.profile = await this.profileService.updateProfile(
114
        post.profile,
115
        postData.profileData
×
116
      );
×
117
    }
118

119
    await this.savePost(post);
×
120

121
    return post;
×
122
  }
123

124
  public async savePost(post: IPost): Promise<IPost> {
125
    return await this.postRepository.save(post);
×
126
  }
127

128
  public async getProfile(
129
    post: IPost,
130
    relations?: FindOptionsRelations<IPost>
131
  ): Promise<IProfile> {
132
    const postLoaded = await this.getPostOrFail(post.id, {
×
133
      relations: { profile: true, ...relations },
134
    });
135
    if (!postLoaded.profile)
×
136
      throw new EntityNotFoundException(
×
137
        `Post profile not initialised for post: ${post.id}`,
138
        LogContext.COLLABORATION
139
      );
140

141
    return postLoaded.profile;
×
142
  }
143

144
  public async getComments(postID: string) {
145
    const { commentsId } = await this.postRepository
×
146
      .createQueryBuilder('post')
147
      .select('post.commentsId', 'commentsId')
148
      .where({ id: postID })
149
      .getRawOne();
150

151
    if (!commentsId) {
×
152
      throw new EntityNotFoundException(
×
153
        `Comments not found on post: ${postID}`,
154
        LogContext.COLLABORATION
155
      );
156
    }
157

158
    return this.roomService.getRoomOrFail(commentsId);
×
159
  }
160
}
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