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

alkem-io / server / #9251

06 Feb 2025 03:39PM UTC coverage: 14.118%. First build
#9251

Pull #4883

travis-ci

Pull Request #4883: NameID usage

84 of 5018 branches covered (1.67%)

Branch coverage included in aggregate %.

4 of 266 new or added lines in 20 files covered. (1.5%)

2301 of 11875 relevant lines covered (19.38%)

7.04 hits per line

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

22.54
/src/domain/common/knowledge-base/knowledge.base.service.ts
1
import { ProfileType } from '@common/enums';
5✔
2
import { AuthorizationPolicyType } from '@common/enums/authorization.policy.type';
5✔
3
import { CalloutsSetType } from '@common/enums/callouts.set.type';
4
import { LogContext } from '@common/enums/logging.context';
5
import { TagsetReservedName } from '@common/enums/tagset.reserved.name';
6
import { TagsetType } from '@common/enums/tagset.type';
5✔
7
import { EntityNotFoundException } from '@common/exceptions/entity.not.found.exception';
5✔
8
import { ICalloutsSet } from '@domain/collaboration/callouts-set/callouts.set.interface';
5✔
9
import { CalloutsSetService } from '@domain/collaboration/callouts-set/callouts.set.service';
5✔
10
import { AuthorizationPolicy } from '@domain/common/authorization-policy/authorization.policy.entity';
5✔
11
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
5✔
12
import { IProfile } from '@domain/common/profile/profile.interface';
13
import { ProfileService } from '@domain/common/profile/profile.service';
5✔
14
import { CreateTagsetInput } from '@domain/common/tagset/dto/tagset.dto.create';
15
import { IStorageAggregator } from '@domain/storage/storage-aggregator/storage.aggregator.interface';
5✔
16
import { Injectable } from '@nestjs/common';
5✔
17
import { InjectRepository } from '@nestjs/typeorm';
5✔
18
import { FindOneOptions, FindOptionsRelations, Repository } from 'typeorm';
19
import { TagsetService } from '../tagset/tagset.service';
5✔
20
import { CreateKnowledgeBaseInput } from './dto/knowledge.base.dto.create';
5✔
21
import { UpdateKnowledgeBaseInput } from './dto/knowledge.base.dto.update';
22
import { KnowledgeBase } from './knowledge.base.entity';
5✔
23
import { IKnowledgeBase } from './knowledge.base.interface';
24

25
@Injectable()
5✔
26
export class KnowledgeBaseService {
27
  constructor(
×
28
    private authorizationPolicyService: AuthorizationPolicyService,
×
29
    private profileService: ProfileService,
×
30
    private tagsetService: TagsetService,
31
    private calloutsSetService: CalloutsSetService,
×
32
    @InjectRepository(KnowledgeBase)
33
    private knowledgeBaseRepository: Repository<KnowledgeBase>
34
  ) {}
35

36
  public async createKnowledgeBase(
37
    knowledgeBaseData: CreateKnowledgeBaseInput,
38
    storageAggregator: IStorageAggregator,
39
    userID: string | undefined
×
40
  ): Promise<IKnowledgeBase> {
41
    let knowledgeBase: IKnowledgeBase = KnowledgeBase.create(knowledgeBaseData);
×
42

43
    knowledgeBase.authorization = new AuthorizationPolicy(
44
      AuthorizationPolicyType.KNOWLEDGE_BASE
45
    );
×
46

47
    knowledgeBase.calloutsSet = this.calloutsSetService.createCalloutsSet(
48
      knowledgeBaseData.calloutsSetData,
49
      CalloutsSetType.KNOWLEDGE_BASE
50
    );
51

×
52
    // To consider also having the default tagset as a template tagset
53
    const defaultTagset: CreateTagsetInput = {
54
      name: TagsetReservedName.DEFAULT,
55
      type: TagsetType.FREEFORM,
56
      tags: [],
57
    };
×
58

59
    knowledgeBaseData.profile.tagsets = this.tagsetService.updateTagsetInputs(
60
      knowledgeBase.profile.tagsets,
61
      [defaultTagset]
62
    );
63

×
64
    knowledgeBase.profile = await this.profileService.createProfile(
65
      knowledgeBaseData.profile,
66
      ProfileType.KNOWLEDGE_BASE,
67
      storageAggregator
68
    );
69

×
70
    await this.save(knowledgeBase);
×
71
    knowledgeBase = await this.getKnowledgeBaseOrFail(knowledgeBase.id, {
72
      relations: {
73
        calloutsSet: { tagsetTemplateSet: true, callouts: true },
74
      },
75
    });
76

×
77
    if (!knowledgeBase.calloutsSet) {
×
78
      throw new EntityNotFoundException(
79
        `CalloutsSet not found for KnowledgeBase: ${knowledgeBase.id}`,
80
        LogContext.COLLABORATION
81
      );
82
    }
83

×
84
    if (knowledgeBaseData.calloutsSetData.calloutsData) {
×
85
      knowledgeBase.calloutsSet.callouts =
86
        await this.calloutsSetService.addCallouts(
87
          knowledgeBase.calloutsSet,
88
          knowledgeBaseData.calloutsSetData.calloutsData,
89
          storageAggregator,
90
          userID
91
        );
92
    }
93

×
94
    return knowledgeBase;
95
  }
96

97
  public async updateKnowledgeBase(
98
    knowledgeBase: IKnowledgeBase,
99
    knowledgeBaseData: UpdateKnowledgeBaseInput
100
  ): Promise<IKnowledgeBase> {
×
101
    if (knowledgeBaseData.profile) {
×
102
      knowledgeBase.profile = await this.profileService.updateProfile(
103
        knowledgeBase.profile,
104
        knowledgeBaseData.profile
105
      );
106
    }
107

×
108
    return knowledgeBase;
109
  }
110

111
  async delete(knowledgeBaseInput: IKnowledgeBase): Promise<IKnowledgeBase> {
×
112
    const knowledgeBaseID = knowledgeBaseInput.id;
×
113
    const knowledgeBase = await this.getKnowledgeBaseOrFail(knowledgeBaseID, {
114
      relations: {
115
        profile: true,
116
        calloutsSet: true,
117
      },
118
    });
×
119
    if (!knowledgeBase.profile || !knowledgeBase.calloutsSet) {
×
120
      throw new EntityNotFoundException(
121
        `Unable to load Profile or CalloutsSet on knowledgeBase:  ${knowledgeBase.id} `,
122
        LogContext.COLLABORATION
123
      );
124
    }
×
125
    await this.profileService.deleteProfile(knowledgeBase.profile.id);
×
126

127
    await this.calloutsSetService.deleteCalloutsSet(
×
128
      knowledgeBase.calloutsSet.id
129
    );
130

131
    if (knowledgeBase.authorization) {
×
132
      await this.authorizationPolicyService.delete(knowledgeBase.authorization);
×
133
    }
134

135
    const result = await this.knowledgeBaseRepository.remove(
×
136
      knowledgeBase as KnowledgeBase
137
    );
138
    result.id = knowledgeBaseID;
×
139
    return result;
×
140
  }
141

142
  async save(knowledgeBase: IKnowledgeBase): Promise<IKnowledgeBase> {
143
    return await this.knowledgeBaseRepository.save(knowledgeBase);
×
144
  }
145

146
  public async getKnowledgeBaseOrFail(
147
    knowledgeBaseID: string,
148
    options?: FindOneOptions<KnowledgeBase>
149
  ): Promise<IKnowledgeBase | never> {
NEW
150
    const knowledgeBase = await this.knowledgeBaseRepository.findOne({
×
151
      where: { id: knowledgeBaseID },
152
      ...options,
153
    });
154

155
    if (!knowledgeBase)
×
156
      throw new EntityNotFoundException(
×
157
        `No KnowledgeBase found with the given id: ${knowledgeBaseID}`,
158
        LogContext.COLLABORATION
159
      );
160
    return knowledgeBase;
×
161
  }
162

163
  public async getProfile(
164
    knowledgeBaseInput: IKnowledgeBase,
165
    relations?: FindOptionsRelations<IKnowledgeBase>
166
  ): Promise<IProfile> {
167
    const knowledgeBase = await this.getKnowledgeBaseOrFail(
×
168
      knowledgeBaseInput.id,
169
      {
170
        relations: { profile: true, ...relations },
171
      }
172
    );
173
    if (!knowledgeBase.profile)
×
174
      throw new EntityNotFoundException(
×
175
        `Callout profile not initialised: ${knowledgeBaseInput.id}`,
176
        LogContext.COLLABORATION
177
      );
178

179
    return knowledgeBase.profile;
×
180
  }
181

182
  public async getCalloutsSet(
183
    knowledgeBaseInput: IKnowledgeBase,
184
    relations?: FindOptionsRelations<IKnowledgeBase>
185
  ): Promise<ICalloutsSet> {
186
    const knowledgeBase = await this.getKnowledgeBaseOrFail(
×
187
      knowledgeBaseInput.id,
188
      {
189
        relations: { calloutsSet: true, ...relations },
190
      }
191
    );
192
    if (!knowledgeBase.calloutsSet)
×
193
      throw new EntityNotFoundException(
×
194
        `Callout profile not initialised: ${knowledgeBaseInput.id}`,
195
        LogContext.COLLABORATION
196
      );
197

198
    return knowledgeBase.calloutsSet;
×
199
  }
200
}
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

© 2026 Coveralls, Inc