itlab-internal-services 2.4.1 → 2.4.3

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.
@@ -4,8 +4,9 @@ exports.PropertyLimit = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const swagger_1 = require("@nestjs/swagger");
6
6
  const class_validator_1 = require("class-validator");
7
+ const transform_1 = require("../../transform");
7
8
  /**
8
9
  * Creates a PropertyLimitDecorator with the given options.
9
10
  */
10
- const PropertyLimit = () => (0, common_1.applyDecorators)((0, swagger_1.ApiProperty)({ required: false, minimum: 0, example: 10, description: 'Die maximale Anzahl der Ergebnisse' }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.Min)(0));
11
+ const PropertyLimit = () => (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({ minimum: 0, default: 10, description: 'Die maximale Anzahl der Ergebnisse' }), (0, transform_1.TransformNumber)({ default: 10, min: 0 }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.Min)(0));
11
12
  exports.PropertyLimit = PropertyLimit;
@@ -4,13 +4,9 @@ exports.PropertySkip = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const swagger_1 = require("@nestjs/swagger");
6
6
  const class_validator_1 = require("class-validator");
7
+ const transform_1 = require("../../transform");
7
8
  /**
8
9
  * Creates a PropertySkipDecorator with the given options.
9
10
  */
10
- const PropertySkip = () => (0, common_1.applyDecorators)((0, swagger_1.ApiProperty)({
11
- required: false,
12
- minimum: 0,
13
- example: 0,
14
- description: 'Die Anzahl der zu überspringenden Ergebnisse',
15
- }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.Min)(0));
11
+ const PropertySkip = () => (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({ minimum: 0, default: 0, description: 'Die Anzahl der zu überspringenden Ergebnisse' }), (0, transform_1.TransformNumber)({ default: 0, min: 0 }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.Min)(0));
16
12
  exports.PropertySkip = PropertySkip;
@@ -4,13 +4,15 @@ exports.PropertySortDirection = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const swagger_1 = require("@nestjs/swagger");
6
6
  const class_validator_1 = require("class-validator");
7
+ const transform_1 = require("../../transform");
7
8
  /**
8
9
  * Creates a PropertySortDirectionDecorator with the given options.
9
10
  */
10
- const PropertySortDirection = () => (0, common_1.applyDecorators)((0, swagger_1.ApiProperty)({
11
- required: false,
12
- description: 'Die Reihenfolge der Ergebnisse (1 = aufsteigend, -1 = absteigend)',
13
- examples: [-1, 1],
14
- default: -1,
15
- }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsIn)([-1, 1]));
11
+ const PropertySortDirection = () => (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({
12
+ description: 'Die Reihenfolge der Ergebnisse (1 = aufsteigend, -1 = absteigend [default])',
13
+ examples: {
14
+ asc: { value: 1, summary: 'Aufsteigend' },
15
+ desc: { value: -1, summary: 'Absteigend (Standard)' },
16
+ },
17
+ }), (0, transform_1.TransformNumber)({ default: -1, min: -1, max: 1 }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsIn)([-1, 1]));
16
18
  exports.PropertySortDirection = PropertySortDirection;
@@ -7,11 +7,9 @@ const class_validator_1 = require("class-validator");
7
7
  /**
8
8
  * Creates a PropertySortFieldDecorator with the given options.
9
9
  */
10
- const PropertySortField = () => (0, common_1.applyDecorators)((0, swagger_1.ApiProperty)({
11
- required: false,
10
+ const PropertySortField = () => (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({
12
11
  description: 'Das Feld, nach dem sortiert werden soll',
13
- examples: ['title', 'timestamp', 'createdAt', 'updatedAt', '_id'],
14
- example: 'title',
15
12
  default: '_id',
13
+ example: 'title',
16
14
  }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.MinLength)(1));
17
15
  exports.PropertySortField = PropertySortField;
@@ -1,3 +1,4 @@
1
+ import { Type } from '@nestjs/common';
1
2
  /**
2
3
  * Represents the result of a search operation.
3
4
  *
@@ -6,9 +7,10 @@
6
7
  * @property {T[]} results - Array of found objects.
7
8
  * @property {number} total - Total number of objects without pagination.
8
9
  */
9
- export declare class SearchResult<T> {
10
+ export declare class SearchResult<TData> {
10
11
  /** Array of found objects. */
11
- results: T[];
12
+ results: TData[];
12
13
  /** Total number of objects without pagination. */
13
14
  total: number;
14
15
  }
16
+ export declare const ApiSearchResultResponse: <TModel extends Type<any>>(model: TModel) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
@@ -9,7 +9,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.SearchResult = void 0;
12
+ exports.ApiSearchResultResponse = exports.SearchResult = void 0;
13
+ const common_1 = require("@nestjs/common");
13
14
  const swagger_1 = require("@nestjs/swagger");
14
15
  /**
15
16
  * Represents the result of a search operation.
@@ -22,11 +23,29 @@ const swagger_1 = require("@nestjs/swagger");
22
23
  class SearchResult {
23
24
  }
24
25
  __decorate([
25
- (0, swagger_1.ApiProperty)({ description: 'Gefundene Events', type: [Object] }),
26
+ (0, swagger_1.ApiProperty)({ description: 'Gefundene Elemente', type: [Object] }),
26
27
  __metadata("design:type", Array)
27
28
  ], SearchResult.prototype, "results", void 0);
28
29
  __decorate([
29
- (0, swagger_1.ApiProperty)({ description: 'Insgesamt auffindbare Events', example: 17 }),
30
+ (0, swagger_1.ApiProperty)({ description: 'Insgesamt verfügbare Elemente', example: 17 }),
30
31
  __metadata("design:type", Number)
31
32
  ], SearchResult.prototype, "total", void 0);
32
33
  exports.SearchResult = SearchResult;
34
+ const ApiSearchResultResponse = (model) => (0, common_1.applyDecorators)((0, swagger_1.ApiExtraModels)(SearchResult, model), (0, swagger_1.ApiOkResponse)({
35
+ description: 'Gefundene Elemente',
36
+ schema: {
37
+ title: `SearchResultOf${model.name}`,
38
+ allOf: [
39
+ { $ref: (0, swagger_1.getSchemaPath)(SearchResult) },
40
+ {
41
+ properties: {
42
+ results: {
43
+ type: 'array',
44
+ items: { $ref: (0, swagger_1.getSchemaPath)(model) },
45
+ },
46
+ },
47
+ },
48
+ ],
49
+ },
50
+ }));
51
+ exports.ApiSearchResultResponse = ApiSearchResultResponse;
@@ -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;
@@ -58,14 +58,7 @@ exports.Hid = Hid;
58
58
  * @param {string} param - the parameter for the API HID
59
59
  * @return A decorator that creates an API HID with the given parameter
60
60
  */
61
- const ApiHid = (param) => (0, common_1.applyDecorators)((0, swagger_1.ApiParam)({
62
- name: param,
63
- example: 'hallo-welt',
64
- examples: {
65
- 'Hallo Welt': { value: 'hallo-welt' },
66
- 'Heute ist ein toller Tag': { value: 'heute-ist-ein-toller-tag' },
67
- },
68
- }), (0, swagger_1.ApiResponse)({ description, status }));
61
+ const ApiHid = (param) => (0, common_1.applyDecorators)((0, swagger_1.ApiParam)({ name: param, example: 'hallo-welt' }), (0, swagger_1.ApiResponse)({ description, status }));
69
62
  exports.ApiHid = ApiHid;
70
63
  /**
71
64
  * Check if the given value is a valid hub ID.
@@ -79,12 +79,5 @@ exports.Target = Target;
79
79
  * @param {string} param - the parameter for the API target
80
80
  * @return A decorator that creates an API target with the given parameter
81
81
  */
82
- const ApiTarget = (param) => (0, common_1.applyDecorators)((0, swagger_1.ApiParam)({
83
- name: param,
84
- example: Targets.NEWSROOM,
85
- examples: Object.entries(Targets).reduce((map, [value, key]) => {
86
- map[key] = { value };
87
- return map;
88
- }, {}),
89
- }), (0, swagger_1.ApiResponse)({ description, status }));
82
+ const ApiTarget = (param) => (0, common_1.applyDecorators)((0, swagger_1.ApiParam)({ name: param, enum: Targets }), (0, swagger_1.ApiResponse)({ description, status }));
90
83
  exports.ApiTarget = ApiTarget;
@@ -1,4 +1,6 @@
1
+ export { TransformBoolean } from './transformBoolean';
1
2
  export { TransformImage } from './transformImage';
3
+ export { TransformNumber } from './transformNumber';
2
4
  export { TransformString } from './transformString';
3
5
  export { TransformStringArray } from './transformStringArray';
4
6
  export { isStringLike, isStringLikeMin, TransformStringLike } from './transformStringLike';
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransformStringSet = exports.TransformStringLowerCase = exports.TransformStringLike = exports.isStringLikeMin = exports.isStringLike = exports.TransformStringArray = exports.TransformString = exports.TransformImage = void 0;
3
+ exports.TransformStringSet = exports.TransformStringLowerCase = exports.TransformStringLike = exports.isStringLikeMin = exports.isStringLike = exports.TransformStringArray = exports.TransformString = exports.TransformNumber = exports.TransformImage = exports.TransformBoolean = void 0;
4
+ var transformBoolean_1 = require("./transformBoolean");
5
+ Object.defineProperty(exports, "TransformBoolean", { enumerable: true, get: function () { return transformBoolean_1.TransformBoolean; } });
4
6
  var transformImage_1 = require("./transformImage");
5
7
  Object.defineProperty(exports, "TransformImage", { enumerable: true, get: function () { return transformImage_1.TransformImage; } });
8
+ var transformNumber_1 = require("./transformNumber");
9
+ Object.defineProperty(exports, "TransformNumber", { enumerable: true, get: function () { return transformNumber_1.TransformNumber; } });
6
10
  var transformString_1 = require("./transformString");
7
11
  Object.defineProperty(exports, "TransformString", { enumerable: true, get: function () { return transformString_1.TransformString; } });
8
12
  var transformStringArray_1 = require("./transformStringArray");
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This function transforms the input value to a trimmed number if it's a string
3
+ * or a number, otherwise it returns the original value.
4
+ */
5
+ export declare function TransformBoolean(): PropertyDecorator;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransformBoolean = void 0;
4
+ const class_transformer_1 = require("class-transformer");
5
+ const transformStringLike_1 = require("./transformStringLike");
6
+ const _trim_1 = require("./_trim");
7
+ /**
8
+ * This function transforms the input value to a trimmed number if it's a string
9
+ * or a number, otherwise it returns the original value.
10
+ */
11
+ function TransformBoolean() {
12
+ return (0, class_transformer_1.Transform)(({ value }) => {
13
+ if ((0, transformStringLike_1.isStringLike)(value))
14
+ value = (0, _trim_1.trimStringLike)(value).toLowerCase();
15
+ if (value === true || value === 'true' || value === 1 || value === '1')
16
+ return true;
17
+ if (value === false || value === 'false' || value === 0 || value === '0')
18
+ return false;
19
+ return value;
20
+ });
21
+ }
22
+ exports.TransformBoolean = TransformBoolean;
@@ -0,0 +1,11 @@
1
+ interface NumberOptions {
2
+ default?: number;
3
+ min?: number;
4
+ max?: number;
5
+ }
6
+ /**
7
+ * This function transforms the input value to a trimmed number if it's a string
8
+ * or a number, otherwise it returns the original value.
9
+ */
10
+ export declare function TransformNumber(opts?: NumberOptions): PropertyDecorator;
11
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransformNumber = void 0;
4
+ const class_transformer_1 = require("class-transformer");
5
+ /**
6
+ * This function transforms the input value to a trimmed number if it's a string
7
+ * or a number, otherwise it returns the original value.
8
+ */
9
+ function TransformNumber(opts = {}) {
10
+ return (0, class_transformer_1.Transform)(({ value }) => {
11
+ let number = Number.parseInt(value || String(opts.default), 10);
12
+ if (Number.isNaN(number) && !opts.default)
13
+ return value;
14
+ if (Number.isNaN(number))
15
+ number = opts.default;
16
+ if (opts.min && number < opts.min)
17
+ number = opts.min;
18
+ if (opts.max && number > opts.max)
19
+ number = opts.max;
20
+ return number;
21
+ });
22
+ }
23
+ exports.TransformNumber = TransformNumber;
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.1",
8
+ "version": "2.4.3",
9
9
  "type": "commonjs",
10
10
  "files": [
11
11
  "dist"