itlab-internal-services 2.4.0 → 2.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ let PermsGuard = class PermsGuard {
50
50
  // Retrieve the user from the request
51
51
  const { user } = ctx.switchToHttp().getRequest();
52
52
  // Check if the user is valid and has at least one permission
53
- const userValid = user && (0, class_validator_1.minLength)(user.perms, 1);
53
+ const userValid = user && (0, class_validator_1.arrayMinSize)(user.perms, 1);
54
54
  // If the user is valid and has the required permissions, allow access
55
55
  if (userValid && (0, itlab_functions_1.hasPermission)(perms, user.perms))
56
56
  return true;
@@ -61,7 +61,7 @@ __decorate([
61
61
  (0, swagger_1.ApiBody)({ description: 'Der zu speichernde Kommentar' }),
62
62
  (0, exceptions_1.ApiBadBody)('Validierung des Kommentars fehlgeschlagen'),
63
63
  (0, pipes_1.ApiId)('targetId'),
64
- (0, common_1.Post)(':targetId/comment'),
64
+ (0, common_1.Post)(':targetId'),
65
65
  __param(0, (0, pipes_1.Id)('targetId')),
66
66
  __param(1, (0, authentication_1.Account)('id')),
67
67
  __param(2, (0, common_1.Body)()),
@@ -71,8 +71,6 @@ __decorate([
71
71
  ], CommentController.prototype, "postComment", null);
72
72
  CommentController = __decorate([
73
73
  (0, authentication_1.Jwt)(),
74
- (0, swagger_1.ApiTags)('Comment'),
75
- (0, common_1.Controller)(''),
76
74
  __metadata("design:paramtypes", [comment_service_1.CommentService])
77
75
  ], CommentController);
78
76
  exports.CommentController = CommentController;
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule } from '@nestjs/common';
2
- import { Schema } from 'mongoose';
2
+ import { ModelDefinition } from '@nestjs/mongoose';
3
3
  import { Targets } from '../../pipes';
4
4
  /**
5
5
  * @class CommentModule
@@ -10,10 +10,10 @@ export declare class CommentModule {
10
10
  /**
11
11
  * Generate a CommentModule based on the given schema.
12
12
  *
13
- * @param {Schema} schema - the schema for the CommentModule
14
- * @param {string | string[]} prefix - the prefix for the CommentController (Defaults to "comment")
13
+ * @param {ModelDefinition} model - the model
14
+ * @param {string | string[]} suffix - additional suffix for the CommentController (Defaults to "")
15
15
  * @return {DynamicModule} the CommentModule generated for the given schema
16
16
  */
17
- static forFeature(schema: Schema, target: Targets, prefix?: string): DynamicModule;
17
+ static forFeature(model: ModelDefinition, target: Targets, ...suffix: string[]): DynamicModule;
18
18
  static register(target: Targets): DynamicModule;
19
19
  }
@@ -8,7 +8,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.CommentModule = void 0;
10
10
  const common_1 = require("@nestjs/common");
11
- const mongoose_1 = require("@nestjs/mongoose");
11
+ const swagger_1 = require("@nestjs/swagger");
12
+ const itlab_functions_1 = require("itlab-functions");
12
13
  const comment_controller_1 = require("./comment.controller");
13
14
  const comment_service_1 = require("./comment.service");
14
15
  /**
@@ -20,23 +21,28 @@ class CommentModule {
20
21
  /**
21
22
  * Generate a CommentModule based on the given schema.
22
23
  *
23
- * @param {Schema} schema - the schema for the CommentModule
24
- * @param {string | string[]} prefix - the prefix for the CommentController (Defaults to "comment")
24
+ * @param {ModelDefinition} model - the model
25
+ * @param {string | string[]} suffix - additional suffix for the CommentController (Defaults to "")
25
26
  * @return {DynamicModule} the CommentModule generated for the given schema
26
27
  */
27
- static forFeature(schema, target, prefix) {
28
+ static forFeature(model, target, ...suffix) {
28
29
  let CommentController = class CommentController extends comment_controller_1.CommentController {
29
30
  };
30
31
  CommentController = __decorate([
31
- (0, common_1.Controller)(prefix)
32
+ (0, swagger_1.ApiTags)(['Comment Module', ...suffix.map(itlab_functions_1.capitalize)].join(' - ')),
33
+ (0, common_1.Controller)(['comment', ...suffix].join('/'))
32
34
  ], CommentController);
33
35
  let CommentModuleTemplate = class CommentModuleTemplate {
34
36
  };
35
37
  CommentModuleTemplate = __decorate([
36
38
  (0, common_1.Module)({
37
- imports: [mongoose_1.MongooseModule.forFeature([{ name: 'COMMENT_SCHEMA', schema }])],
38
39
  controllers: [CommentController],
39
- providers: [comment_service_1.CommentService, { provide: 'COMMENT_TARGET', useValue: target }],
40
+ providers: [
41
+ comment_service_1.CommentService,
42
+ { provide: 'COMMENT_TARGET', useValue: target },
43
+ { provide: 'COMMENT_MODEL', useValue: model.name },
44
+ { provide: 'COMMENT_SCHEMA', useValue: model.schema },
45
+ ],
40
46
  exports: [comment_service_1.CommentService],
41
47
  })
42
48
  ], CommentModuleTemplate);
@@ -1,6 +1,6 @@
1
1
  import { ConfigService } from '@nestjs/config';
2
2
  import { ContentRichtext } from 'itlab-functions';
3
- import { Model } from 'mongoose';
3
+ import { Connection, Schema } from 'mongoose';
4
4
  import { Targets } from '../../pipes';
5
5
  import { Comment } from './comment.module-options';
6
6
  /**
@@ -9,20 +9,32 @@ import { Comment } from './comment.module-options';
9
9
  * @author Timo Scheuermann
10
10
  */
11
11
  export declare class CommentService {
12
- private readonly commentModel;
12
+ private readonly connection;
13
+ private readonly model;
14
+ private readonly schema;
13
15
  private readonly target;
14
16
  private readonly configService;
15
17
  /** The CommentService logger */
16
18
  private readonly logger;
17
19
  /**
18
20
  * @constructor Constructor for the CommentService class
19
- * @param commentModel - The injected model for the COMMENT_SCHEMA
20
- * @param target - The target for which comments are being posted
21
- * @param configService - The injected config service
21
+ * @param {Connection} connection - the database connection
22
+ * @param {string} model - the model name
23
+ * @param {string} schema - the schema
24
+ * @param {Targets} target - the target
25
+ * @param {ConfigService} configService - the config service
22
26
  */
23
- constructor(commentModel: Model<{}> | null, target: Targets, configService: ConfigService);
27
+ constructor(connection: Connection, model: string | undefined, schema: Schema<{
28
+ _url: string;
29
+ }>, target: Targets, configService: ConfigService);
24
30
  /** The axios instance */
25
31
  private axios;
32
+ /**
33
+ * Retrieve the model from the connection based on the schema name.
34
+ *
35
+ * @return {Model<{}> | undefined} The model retrieved from the connection.
36
+ */
37
+ private getModel;
26
38
  /**
27
39
  * Post a comment to a specific target.
28
40
  *
@@ -40,12 +40,16 @@ const pipes_1 = require("../../pipes");
40
40
  let CommentService = CommentService_1 = class CommentService {
41
41
  /**
42
42
  * @constructor Constructor for the CommentService class
43
- * @param commentModel - The injected model for the COMMENT_SCHEMA
44
- * @param target - The target for which comments are being posted
45
- * @param configService - The injected config service
43
+ * @param {Connection} connection - the database connection
44
+ * @param {string} model - the model name
45
+ * @param {string} schema - the schema
46
+ * @param {Targets} target - the target
47
+ * @param {ConfigService} configService - the config service
46
48
  */
47
- constructor(commentModel, target, configService) {
48
- this.commentModel = commentModel;
49
+ constructor(connection, model, schema, target, configService) {
50
+ this.connection = connection;
51
+ this.model = model;
52
+ this.schema = schema;
49
53
  this.target = target;
50
54
  this.configService = configService;
51
55
  /** The CommentService logger */
@@ -56,6 +60,17 @@ let CommentService = CommentService_1 = class CommentService {
56
60
  k8sToken: this.configService.getOrThrow(env_1.ENV_K8S_TOKEN),
57
61
  });
58
62
  }
63
+ /**
64
+ * Retrieve the model from the connection based on the schema name.
65
+ *
66
+ * @return {Model<{}> | undefined} The model retrieved from the connection.
67
+ */
68
+ getModel() {
69
+ if (!(this.model && this.schema))
70
+ return undefined;
71
+ this.logger.debug('Current DB', this.connection.db.databaseName);
72
+ return this.connection.model(this.model, this.schema);
73
+ }
59
74
  /**
60
75
  * Post a comment to a specific target.
61
76
  *
@@ -67,11 +82,11 @@ let CommentService = CommentService_1 = class CommentService {
67
82
  postComment(targetId, accountId, comment) {
68
83
  return __awaiter(this, void 0, void 0, function* () {
69
84
  // CommentModel is null inside manager modules
70
- if (!(0, class_validator_1.isDefined)(this.commentModel)) {
85
+ if (!(0, class_validator_1.isDefined)(this.getModel())) {
71
86
  this.logger.error('Kommentare können nicht vom Management aus erstell werden');
72
87
  return;
73
88
  }
74
- const exists = yield this.commentModel.findOne({ _id: targetId });
89
+ const exists = yield this.getModel().findOne({ _id: targetId });
75
90
  if (!exists)
76
91
  throw new exceptions_1.NotFoundException('Kommentar konnte nicht erstellt werden');
77
92
  this.logger.log(`Posting comment for ${this.target} (${targetId}) by ${accountId}`);
@@ -105,9 +120,12 @@ let CommentService = CommentService_1 = class CommentService {
105
120
  };
106
121
  CommentService = CommentService_1 = __decorate([
107
122
  (0, common_1.Injectable)(),
108
- __param(0, (0, common_1.Optional)()),
109
- __param(0, (0, mongoose_1.InjectModel)('COMMENT_SCHEMA')),
110
- __param(1, (0, common_1.Inject)('COMMENT_TARGET')),
111
- __metadata("design:paramtypes", [mongoose_2.Model, String, config_1.ConfigService])
123
+ __param(0, (0, mongoose_1.InjectConnection)()),
124
+ __param(1, (0, common_1.Optional)()),
125
+ __param(1, (0, common_1.Inject)('COMMENT_MODEL')),
126
+ __param(2, (0, common_1.Optional)()),
127
+ __param(2, (0, common_1.Inject)('COMMENT_SCHEMA')),
128
+ __param(3, (0, common_1.Inject)('COMMENT_TARGET')),
129
+ __metadata("design:paramtypes", [mongoose_2.Connection, String, mongoose_2.Schema, String, config_1.ConfigService])
112
130
  ], CommentService);
113
131
  exports.CommentService = CommentService;
@@ -112,8 +112,6 @@ __decorate([
112
112
  ], LikeController.prototype, "hasLiked", null);
113
113
  LikeController = __decorate([
114
114
  (0, authentication_1.Jwt)(),
115
- (0, swagger_1.ApiTags)('Like'),
116
- (0, common_1.Controller)('like'),
117
115
  __metadata("design:paramtypes", [like_service_1.LikeService])
118
116
  ], LikeController);
119
117
  exports.LikeController = LikeController;
@@ -1,6 +1,5 @@
1
1
  import { DynamicModule } from '@nestjs/common';
2
- import { Schema } from 'mongoose';
3
- import { LikeableDocument } from './like.module-options';
2
+ import { ModelDefinition } from '@nestjs/mongoose';
4
3
  /**
5
4
  * @class LikeModule
6
5
  * @description This module handles all operations related to likes.
@@ -10,9 +9,9 @@ export declare class LikeModule {
10
9
  /**
11
10
  * Generate a LikeModule based on the given schema.
12
11
  *
13
- * @param {Schema<LikeableDocument>} schema - the schema for the LikeModule
14
- * @param {string | string[]} prefix - the prefix for the LikeController (Defaults to "like")
12
+ * @param {ModelDefinition} model - the model
13
+ * @param {string | string[]} suffix - additional suffix for the LikeController (Defaults to "")
15
14
  * @return {DynamicModule} the LikeModule generated for the given schema
16
15
  */
17
- static forFeature(schema: Schema<LikeableDocument>, ...prefix: string[]): DynamicModule;
16
+ static forFeature(model: ModelDefinition, ...suffix: string[]): DynamicModule;
18
17
  }
@@ -8,7 +8,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.LikeModule = void 0;
10
10
  const common_1 = require("@nestjs/common");
11
- const mongoose_1 = require("@nestjs/mongoose");
11
+ const swagger_1 = require("@nestjs/swagger");
12
+ const itlab_functions_1 = require("itlab-functions");
12
13
  const like_controller_1 = require("./like.controller");
13
14
  const like_service_1 = require("./like.service");
14
15
  /**
@@ -20,23 +21,28 @@ class LikeModule {
20
21
  /**
21
22
  * Generate a LikeModule based on the given schema.
22
23
  *
23
- * @param {Schema<LikeableDocument>} schema - the schema for the LikeModule
24
- * @param {string | string[]} prefix - the prefix for the LikeController (Defaults to "like")
24
+ * @param {ModelDefinition} model - the model
25
+ * @param {string | string[]} suffix - additional suffix for the LikeController (Defaults to "")
25
26
  * @return {DynamicModule} the LikeModule generated for the given schema
26
27
  */
27
- static forFeature(schema, ...prefix) {
28
+ static forFeature(model, ...suffix) {
28
29
  let LikeController = class LikeController extends like_controller_1.LikeController {
29
30
  };
30
31
  LikeController = __decorate([
31
- (0, common_1.Controller)(prefix.concat('like').join('/'))
32
+ (0, swagger_1.ApiTags)(['Like Module', ...suffix.map(itlab_functions_1.capitalize)].join(' - ')),
33
+ (0, common_1.Controller)(['like', ...suffix].join('/'))
32
34
  ], LikeController);
33
35
  let LikeModuleTemplate = class LikeModuleTemplate {
34
36
  };
35
37
  LikeModuleTemplate = __decorate([
36
38
  (0, common_1.Module)({
37
- imports: [mongoose_1.MongooseModule.forFeature([{ name: 'LIKE_SCHEMA', schema }])],
38
39
  controllers: [LikeController],
39
- providers: [like_service_1.LikeService],
40
+ providers: [
41
+ like_service_1.LikeService,
42
+ { provide: 'LIKE_MODEL', useValue: model.name },
43
+ { provide: 'LIKE_SCHEMA', useValue: model.schema },
44
+ ],
45
+ exports: [like_service_1.LikeService],
40
46
  })
41
47
  ], LikeModuleTemplate);
42
48
  return { module: LikeModuleTemplate };
@@ -1,19 +1,31 @@
1
- import { Model } from 'mongoose';
2
- import { LikeableDocument } from './like.module-options';
1
+ import { Connection, Schema } from 'mongoose';
3
2
  /**
4
3
  * @class LikeService
5
4
  * @description The LikeService class is responsible for handling likes for a specified target. It provides methods to add, remove, and check likes of a target.
6
5
  * @author Timo Scheuermann
7
6
  */
8
7
  export declare class LikeService {
9
- private readonly likeModel;
8
+ private readonly connection;
9
+ private readonly model;
10
+ private readonly schema;
10
11
  /** The LikeService logger */
11
12
  private readonly logger;
12
13
  /**
13
14
  * @constructor Constructor for the LikeService class
14
- * @param likeModel - The injected model for the LIKE_SCHEMA
15
+ * @param {Connection} connection - the database connection
16
+ * @param {string} model - the model name
17
+ * @param {Schema} schema - the schema
15
18
  */
16
- constructor(likeModel: Model<LikeableDocument>);
19
+ constructor(connection: Connection, model: string, schema: Schema<{
20
+ _likedBy: string[];
21
+ _id: string;
22
+ }>);
23
+ /**
24
+ * Retrieve the model from the connection based on the schema name.
25
+ *
26
+ * @return {Model<{}>} The model retrieved from the connection.
27
+ */
28
+ private getModel;
17
29
  /**
18
30
  * Get the number of likes for the specified target
19
31
  *
@@ -34,13 +34,27 @@ const mongoose_2 = require("mongoose");
34
34
  let LikeService = LikeService_1 = class LikeService {
35
35
  /**
36
36
  * @constructor Constructor for the LikeService class
37
- * @param likeModel - The injected model for the LIKE_SCHEMA
37
+ * @param {Connection} connection - the database connection
38
+ * @param {string} model - the model name
39
+ * @param {Schema} schema - the schema
38
40
  */
39
- constructor(likeModel) {
40
- this.likeModel = likeModel;
41
+ constructor(connection, model, schema) {
42
+ this.connection = connection;
43
+ this.model = model;
44
+ this.schema = schema;
41
45
  /** The LikeService logger */
42
46
  this.logger = new common_1.Logger(LikeService_1.name);
43
47
  }
48
+ /**
49
+ * Retrieve the model from the connection based on the schema name.
50
+ *
51
+ * @return {Model<{}>} The model retrieved from the connection.
52
+ */
53
+ getModel() {
54
+ if (!this.model)
55
+ return undefined;
56
+ return this.connection.model(this.model, this.schema);
57
+ }
44
58
  /**
45
59
  * Get the number of likes for the specified target
46
60
  *
@@ -62,7 +76,7 @@ let LikeService = LikeService_1 = class LikeService {
62
76
  return __awaiter(this, void 0, void 0, function* () {
63
77
  this.logger.log(`Adding like to ${targetId} by ${accountId}`);
64
78
  // Find the target and update the likedBy array by adding the accountId
65
- const target = yield this.likeModel.findOneAndUpdate({ _id: targetId, draft: false }, { $addToSet: { _likedBy: accountId } }, { upsert: false, new: true });
79
+ const target = yield this.getModel().findOneAndUpdate({ _id: targetId, draft: false }, { $addToSet: { _likedBy: accountId } }, { upsert: false, new: true });
66
80
  // Return the like count after the addition
67
81
  return this.likeCount(target);
68
82
  });
@@ -78,7 +92,7 @@ let LikeService = LikeService_1 = class LikeService {
78
92
  return __awaiter(this, void 0, void 0, function* () {
79
93
  this.logger.log(`Removing like from ${targetId} by ${accountId}`);
80
94
  // Find the target and update the _likedBy array by removing the accountId
81
- const target = yield this.likeModel.findOneAndUpdate({ _id: targetId, draft: false }, { $pull: { _likedBy: { $in: [accountId] } } }, { upsert: false, new: true });
95
+ const target = yield this.getModel().findOneAndUpdate({ _id: targetId, draft: false }, { $pull: { _likedBy: { $in: [accountId] } } }, { upsert: false, new: true });
82
96
  // Return the like count after the removal
83
97
  return this.likeCount(target);
84
98
  });
@@ -92,8 +106,9 @@ let LikeService = LikeService_1 = class LikeService {
92
106
  */
93
107
  hasLiked(targetId, accountId) {
94
108
  return __awaiter(this, void 0, void 0, function* () {
109
+ this.logger.log(`Check if ${accountId} has liked ${targetId}`);
95
110
  // Find the target in the likeModel with the provided targetId and ensure it's not a draft.
96
- const target = yield this.likeModel.findOne({ _id: targetId, draft: false });
111
+ const target = yield this.getModel().findOne({ _id: targetId, draft: false });
97
112
  // Return true if the target exists and has the account in its _likedBy array, otherwise return false.
98
113
  return target && target._likedBy ? target._likedBy.includes(accountId) : false;
99
114
  });
@@ -101,7 +116,9 @@ let LikeService = LikeService_1 = class LikeService {
101
116
  };
102
117
  LikeService = LikeService_1 = __decorate([
103
118
  (0, common_1.Injectable)(),
104
- __param(0, (0, mongoose_1.InjectModel)('LIKE_SCHEMA')),
105
- __metadata("design:paramtypes", [mongoose_2.Model])
119
+ __param(0, (0, mongoose_1.InjectConnection)()),
120
+ __param(1, (0, common_1.Inject)('LIKE_MODEL')),
121
+ __param(2, (0, common_1.Inject)('LIKE_SCHEMA')),
122
+ __metadata("design:paramtypes", [mongoose_2.Connection, String, mongoose_2.Schema])
106
123
  ], LikeService);
107
124
  exports.LikeService = LikeService;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "email": "timo.scheuermann@sv-informatik.de",
6
6
  "url": "https://timos.design"
7
7
  },
8
- "version": "2.4.0",
8
+ "version": "2.4.2",
9
9
  "type": "commonjs",
10
10
  "files": [
11
11
  "dist"