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

alkem-io / server / #7097

21 May 2024 07:40AM UTC coverage: 13.69%. First build
#7097

Pull #3905

travis-ci

Pull Request #3905: Create & Read - Community Guidelines template

114 of 4228 branches covered (2.7%)

Branch coverage included in aggregate %.

15 of 64 new or added lines in 3 files covered. (23.44%)

1771 of 9541 relevant lines covered (18.56%)

2.8 hits per line

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

10.19
/src/domain/template/templates-set/templates.set.service.ts
1
import { Inject, Injectable, LoggerService } from '@nestjs/common';
17✔
2
import { InjectRepository } from '@nestjs/typeorm';
17✔
3
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
17✔
4
import { FindOneOptions, Repository } from 'typeorm';
17✔
5
import {
17✔
6
  EntityNotFoundException,
7
  ValidationException,
8
} from '@common/exceptions';
9
import { LogContext } from '@common/enums';
10
import { AuthorizationPolicy } from '@domain/common/authorization-policy/authorization.policy.entity';
17✔
11
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
17✔
12
import { TemplatesSet } from './templates.set.entity';
17✔
13
import { ITemplatesSet } from './templates.set.interface';
14
import { TemplateService } from '../template/template.service';
15
import { ITemplate } from '../template/template.interface';
17✔
16
import { CreateTemplateInput } from '../template/dto/template.dto.create';
17
import { IStorageAggregator } from '@domain/storage/storage-aggregator/storage.aggregator.interface';
17✔
18
import { StorageAggregatorResolverService } from '@services/infrastructure/storage-aggregator-resolver/storage.aggregator.resolver.service';
17✔
19
import { AuthorizationPolicyType } from '@common/enums/authorization.policy.type';
17✔
20
import { TemplateType } from '@common/enums/template.type';
17✔
21
import { NamingService } from '@services/infrastructure/naming/naming.service';
22
import { CreateTemplateFromSpaceOnTemplatesSetInput } from './dto/templates.set.dto.create.template.from.space';
23
import { CreateTemplateFromContentSpaceOnTemplatesSetInput } from './dto/templates.set.dto.create.template.from.space.content';
24
import { InputCreatorService } from '@services/api/input-creator/input.creator.service';
25

26
@Injectable()
27
export class TemplatesSetService {
28
  constructor(
29
    private authorizationPolicyService: AuthorizationPolicyService,
17✔
30
    @InjectRepository(TemplatesSet)
31
    private templatesSetRepository: Repository<TemplatesSet>,
32
    private storageAggregatorResolverService: StorageAggregatorResolverService,
17✔
33
    private templateService: TemplateService,
34
    private namingService: NamingService,
35
    private inputCreatorService: InputCreatorService,
17✔
36
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService
37
  ) {}
×
38

39
  async createTemplatesSet(): Promise<ITemplatesSet> {
×
40
    const templatesSet: ITemplatesSet = TemplatesSet.create();
×
41
    templatesSet.authorization = new AuthorizationPolicy(
×
42
      AuthorizationPolicyType.TEMPLATES_SET
×
43
    );
×
44
    templatesSet.templates = [];
×
NEW
45

×
46
    return await this.templatesSetRepository.save(templatesSet);
×
47
  }
48

49
  async getTemplatesSetOrFail(
50
    templatesSetID: string,
×
51
    options?: FindOneOptions<TemplatesSet>
×
52
  ): Promise<ITemplatesSet | never> {
×
53
    const templatesSet = await TemplatesSet.findOne({
×
54
      where: { id: templatesSetID },
×
NEW
55
      ...options,
×
56
    });
57
    if (!templatesSet)
×
58
      throw new EntityNotFoundException(
59
        `TemplatesSet with id(${templatesSetID}) not found!`,
60
        LogContext.TEMPLATES
61
      );
62
    return templatesSet;
63
  }
64

×
65
  async deleteTemplatesSet(templatesSetID: string): Promise<ITemplatesSet> {
66
    const templatesSet = await this.getTemplatesSetOrFail(templatesSetID, {
67
      relations: {
68
        authorization: true,
×
69
        templates: true,
×
70
      },
71
    });
72

73
    if (templatesSet.authorization)
×
74
      await this.authorizationPolicyService.delete(templatesSet.authorization);
75

76
    if (templatesSet.templates) {
77
      for (const template of templatesSet.templates) {
×
78
        await this.templateService.delete(template);
79
      }
80
    }
81

82
    return await this.templatesSetRepository.remove(
83
      templatesSet as TemplatesSet
84
    );
85
  }
86

87
  public getTemplate(
×
88
    templateId: string,
×
89
    templatesSetId: string
90
  ): Promise<ITemplate> {
×
91
    return this.templateService.getTemplateOrFail(templateId, {
×
92
      where: { templatesSet: { id: templatesSetId } },
×
93
      relations: { templatesSet: true, profile: true },
94
    });
95
  }
×
96

×
97
  public getTemplates(templatesSet: ITemplatesSet): Promise<ITemplate[]> {
×
98
    return this.templateService.getTemplatesInTemplatesSet(templatesSet.id);
99
  }
100

101
  public getTemplatesOfType(
102
    templatesSet: ITemplatesSet,
×
103
    templateType: TemplateType
×
104
  ): Promise<ITemplate[]> {
×
105
    return this.templateService.getTemplateTypeInTemplatesSet(
106
      templatesSet.id,
107
      templateType
108
    );
NEW
109
  }
×
NEW
110

×
NEW
111
  async createTemplate(
×
112
    templatesSet: ITemplatesSet,
113
    templateInput: CreateTemplateInput
114
  ): Promise<ITemplate> {
115
    const reservedNameIDs =
116
      await this.namingService.getReservedNameIDsInTemplatesSet(
×
117
        templatesSet.id
118
      );
119

120
    if (templateInput.nameID && templateInput.nameID.length > 0) {
121
      if (reservedNameIDs.includes(templateInput.nameID)) {
122
        throw new ValidationException(
123
          `Unable to create Template: the provided nameID is already taken: ${templateInput.nameID}`,
124
          LogContext.SPACES
×
125
        );
126
      }
127
      // Just use the provided nameID
128
    } else {
129
      templateInput.nameID =
130
        this.namingService.createNameIdAvoidingReservedNameIDs(
131
          `${templateInput.profileData.displayName}`,
132
          reservedNameIDs
133
        );
134
    }
×
135

×
136
    const storageAggregator = await this.getStorageAggregator(templatesSet);
137

138
    const template = await this.templateService.createTemplate(
139
      templateInput,
140
      storageAggregator
×
141
    );
142
    template.templatesSet = templatesSet;
143
    return await this.templateService.save(template);
144
  }
145

146
  async createTemplateFromSpace(
×
147
    templatesSet: ITemplatesSet,
148
    templateSpaceInput: CreateTemplateFromSpaceOnTemplatesSetInput
149
  ): Promise<ITemplate> {
150
    const createSpaceContentInput =
151
      await this.inputCreatorService.buildCreateTemplateContentSpaceInputFromSpace(
152
        templateSpaceInput.spaceID,
153
        templateSpaceInput.recursive
154
      );
155
    const templateInput: CreateTemplateInput = {
156
      ...templateSpaceInput,
×
157
      type: TemplateType.SPACE,
×
158
      contentSpaceData: createSpaceContentInput,
159
    };
160
    return await this.createTemplate(templatesSet, templateInput);
161
  }
162

×
163
  async createTemplateFromContentSpace(
164
    templatesSet: ITemplatesSet,
165
    templateSpaceInput: CreateTemplateFromContentSpaceOnTemplatesSetInput
166
  ): Promise<ITemplate> {
167
    const createContentSpaceInput =
168
      await this.inputCreatorService.buildCreateTemplateContentSpaceInputFromContentSpace(
NEW
169
        templateSpaceInput.contentSpaceID
×
170
      );
171

172
    const templateInput: CreateTemplateInput = {
173
      ...templateSpaceInput,
174
      type: TemplateType.SPACE,
175
      contentSpaceData: createContentSpaceInput,
176
    };
177
    return await this.createTemplate(templatesSet, templateInput);
178
  }
179

NEW
180
  private async getStorageAggregator(
×
181
    templatesSet: ITemplatesSet
182
  ): Promise<IStorageAggregator> {
183
    return await this.storageAggregatorResolverService.getStorageAggregatorForTemplatesSet(
184
      templatesSet.id
185
    );
186
  }
187

188
  public async save(templatesSet: ITemplatesSet): Promise<ITemplatesSet> {
189
    return await this.templatesSetRepository.save(templatesSet);
NEW
190
  }
×
NEW
191

×
192
  async getTemplatesCountForType(
193
    templatesSetID: string,
194
    type: TemplateType
195
  ): Promise<number> {
NEW
196
    return await this.templateService.getCountInTemplatesSet(
×
197
      templatesSetID,
198
      type
199
    );
200
  }
201

202
  async getTemplatesCount(templatesSetID: string): Promise<number> {
203
    const whiteboardTemplatesCount =
×
204
      await this.templateService.getCountInTemplatesSet(
205
        templatesSetID,
206
        TemplateType.WHITEBOARD
207
      );
208

209
    const postTemplatesCount =
210
      await this.templateService.getCountInTemplatesSet(
211
        templatesSetID,
212
        TemplateType.POST
213
      );
×
214

215
    const spaceTemplatesCount =
216
      await this.templateService.getCountInTemplatesSet(
217
        templatesSetID,
218
        TemplateType.SPACE
219
      );
220

221
    const calloutTemplatesCount =
222
      await this.templateService.getCountInTemplatesSet(
223
        templatesSetID,
224
        TemplateType.CALLOUT
225
      );
226

×
227
    const communityGuidelinesTemplatesCount =
228
      await this.templateService.getCountInTemplatesSet(
229
        templatesSetID,
230
        TemplateType.COMMUNITY_GUIDELINES
231
      );
232

233
    return (
234
      whiteboardTemplatesCount +
235
      postTemplatesCount +
236
      spaceTemplatesCount +
237
      calloutTemplatesCount +
238
      communityGuidelinesTemplatesCount
239
    );
×
240
  }
×
241
}
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