itlab-internal-services 2.16.9 → 2.16.10

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,8 @@ export declare function LikeableDocument<TBase extends new (...args: any[]) => D
4
4
  requiredPermissions?: string[];
5
5
  }): {
6
6
  new (...args: any[]): {
7
- _likedBy: string[];
8
- likes: number;
7
+ _likedBy?: string[];
8
+ likes?: number;
9
9
  likedBy?: User[];
10
10
  hasLiked(accountId: string): boolean;
11
11
  addLike(accountId: string): Promise<number>;
@@ -18,20 +18,23 @@ const pipes_1 = require("../../pipes");
18
18
  function LikeableDocument(Base, options) {
19
19
  class LikeableDocument extends Base {
20
20
  hasLiked(accountId) {
21
- return this._likedBy.includes(accountId);
21
+ var _a;
22
+ return ((_a = this._likedBy) !== null && _a !== void 0 ? _a : []).includes(accountId);
22
23
  }
23
24
  async addLike(accountId) {
25
+ var _a;
24
26
  if (!(0, class_validator_1.isMongoId)(accountId))
25
27
  return this.likes;
26
- this._likedBy = Array.from(new Set([...this._likedBy, accountId]));
28
+ this._likedBy = Array.from(new Set([...((_a = this._likedBy) !== null && _a !== void 0 ? _a : []), accountId]));
27
29
  this.likes = this._likedBy.length;
28
30
  await this.save({ timestamps: false });
29
31
  return this.likes;
30
32
  }
31
33
  async removeLike(accountId) {
34
+ var _a;
32
35
  if (!(0, class_validator_1.isMongoId)(accountId))
33
36
  return this.likes;
34
- this._likedBy = this._likedBy.filter((id) => id !== accountId);
37
+ this._likedBy = ((_a = this._likedBy) !== null && _a !== void 0 ? _a : []).filter((id) => id !== accountId);
35
38
  this.likes = this._likedBy.length;
36
39
  await this.save({ timestamps: false });
37
40
  return this.likes;
@@ -4,8 +4,8 @@ export declare function ViewableDocument<TBase extends new (...args: any[]) => D
4
4
  requiredPermissions?: string[];
5
5
  }): {
6
6
  new (...args: any[]): {
7
- _viewedBy: string[];
8
- views: number;
7
+ _viewedBy?: string[];
8
+ views?: number;
9
9
  viewedBy?: User[];
10
10
  hasViewed(accountId: string): boolean;
11
11
  addView(accountId: string): Promise<number>;
@@ -18,20 +18,23 @@ const pipes_1 = require("../../pipes");
18
18
  function ViewableDocument(Base, options) {
19
19
  class ViewableDocument extends Base {
20
20
  hasViewed(accountId) {
21
- return this._viewedBy.includes(accountId);
21
+ var _a;
22
+ return ((_a = this._viewedBy) !== null && _a !== void 0 ? _a : []).includes(accountId);
22
23
  }
23
24
  async addView(accountId) {
25
+ var _a;
24
26
  if (!(0, class_validator_1.isMongoId)(accountId))
25
27
  return this.views;
26
- this._viewedBy = Array.from(new Set([...this._viewedBy, accountId]));
28
+ this._viewedBy = Array.from(new Set([...((_a = this._viewedBy) !== null && _a !== void 0 ? _a : []), accountId]));
27
29
  this.views = this._viewedBy.length;
28
30
  await this.save({ timestamps: false });
29
31
  return this.views;
30
32
  }
31
33
  async removeView(accountId) {
34
+ var _a;
32
35
  if (!(0, class_validator_1.isMongoId)(accountId))
33
36
  return this.views;
34
- this._viewedBy = this._viewedBy.filter((id) => id !== accountId);
37
+ this._viewedBy = ((_a = this._viewedBy) !== null && _a !== void 0 ? _a : []).filter((id) => id !== accountId);
35
38
  this.views = this._viewedBy.length;
36
39
  await this.save({ timestamps: false });
37
40
  return this.views;
@@ -17,5 +17,5 @@ export declare class EntityMap<T extends {
17
17
  * @param map Map of id -> user/account object
18
18
  * @returns Array of user/account objects
19
19
  */
20
- getMultiple(ids: string[]): T[];
20
+ getMultiple(ids?: string[]): T[];
21
21
  }
@@ -22,6 +22,8 @@ class EntityMap {
22
22
  * @returns Array of user/account objects
23
23
  */
24
24
  getMultiple(ids) {
25
+ if (!ids || ids.length === 0)
26
+ return [];
25
27
  return ids.map((id) => this.map.get(id)).filter(Boolean);
26
28
  }
27
29
  }
@@ -39,7 +39,8 @@ async function populateLikedBy(options, items, accountsService) {
39
39
  });
40
40
  }
41
41
  items.forEach((item) => {
42
- item.likes = Math.max(item._likedBy.length, item.likes); // TODO: Tmp Fix
42
+ var _a, _b, _c;
43
+ item.likes = (_c = (_b = (_a = item._likedBy) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : item.likes) !== null && _c !== void 0 ? _c : 0;
43
44
  if (!options.includeLikedBy)
44
45
  item.likedBy = undefined;
45
46
  if (!(options.includeLikedBy || options.includeLikedByIds)) {
@@ -1,3 +1,4 @@
1
+ import { Document } from 'mongoose';
1
2
  import { AccountsService } from '../../modules';
2
3
  import { IsViewable } from '../../types';
3
4
  export type ViewedByFetchOptions = {
@@ -18,4 +19,4 @@ export declare function populateViewedBy(options: {
18
19
  includeViewedBy?: boolean;
19
20
  includeViewedByIds?: boolean;
20
21
  viewedBy?: string;
21
- }, items: IsViewable[], accountsService: AccountsService): Promise<void>;
22
+ }, items: (Document & IsViewable)[], accountsService: AccountsService): Promise<void>;
@@ -11,9 +11,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ViewedByOptions = ViewedByOptions;
13
13
  exports.populateViewedBy = populateViewedBy;
14
+ const common_1 = require("@nestjs/common");
14
15
  const swagger_1 = require("@nestjs/swagger");
15
16
  const class_validator_1 = require("class-validator");
16
- const rxjs_1 = require("rxjs");
17
17
  const pipes_1 = require("../../pipes");
18
18
  const transform_1 = require("../../transform");
19
19
  function ViewedByOptions(Base, options) {
@@ -37,8 +37,18 @@ function ViewedByOptions(Base, options) {
37
37
  }
38
38
  async function populateViewedBy(options, items, accountsService) {
39
39
  if (options.viewedBy) {
40
+ const logger = new common_1.Logger('MongooseModule');
40
41
  items.forEach((item) => {
41
- item.addView(options.viewedBy).then(rxjs_1.noop);
42
+ var _a;
43
+ const viewedBy = Array.from(new Set([...((_a = item._viewedBy) !== null && _a !== void 0 ? _a : []), options.viewedBy]));
44
+ item
45
+ .updateOne({ $set: { _viewedBy: viewedBy, views: viewedBy.length } }, { timestamps: false })
46
+ .then(() => {
47
+ logger.log(`Added unique accountId '${options.viewedBy}' to '_viewedBy' for document ${item._id.toString()}`);
48
+ })
49
+ .catch((error) => {
50
+ logger.error(`Failed to add accountId '${options.viewedBy}' to attribute '_viewedBy' for document ${item._id.toString()}: ${error.stack}`);
51
+ });
42
52
  });
43
53
  }
44
54
  if (options.includeViewedBy) {
@@ -48,7 +58,8 @@ async function populateViewedBy(options, items, accountsService) {
48
58
  });
49
59
  }
50
60
  items.forEach((item) => {
51
- item.views = Math.max(item._viewedBy.length, item.views); // TODO: Tmp Fix
61
+ var _a, _b, _c;
62
+ item.views = (_c = (_b = (_a = item._viewedBy) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : item.views) !== null && _c !== void 0 ? _c : 0;
52
63
  if (!options.includeViewedBy)
53
64
  item.viewedBy = undefined;
54
65
  if (!(options.includeViewedBy || options.includeViewedByIds)) {
@@ -6,8 +6,8 @@ exports.ContentReturnTypeEndpointMap = exports.supportedContentReturnTypes = voi
6
6
  */
7
7
  exports.supportedContentReturnTypes = ['json', 'text', 'html', 'htmlPopulated'];
8
8
  exports.ContentReturnTypeEndpointMap = {
9
- json: 'v1/content/%resource%/%resourceId%/json',
10
- text: 'v1/content/%resource%/%resourceId%/text',
11
- html: 'v1/content/%resource%/%resourceId%/html',
12
- htmlPopulated: 'v1/content/%resource%/%resourceId%/html/populated',
9
+ json: 'v1/internal/content/%resource%/%resourceId%/json',
10
+ text: 'v1/internal/content/%resource%/%resourceId%/text',
11
+ html: 'v1/internal/content/%resource%/%resourceId%/html',
12
+ htmlPopulated: 'v1/internal/content/%resource%/%resourceId%/html/populated',
13
13
  };
@@ -81,6 +81,6 @@ export declare class NotificationsService extends BaseHttpService {
81
81
  *
82
82
  * @param resourceId Unique identifier of the resource.
83
83
  */
84
- deleteNotifications(resourceId: string): void;
84
+ deleteNotificationsV1(resourceId: string): void;
85
85
  }
86
86
  export {};
@@ -130,7 +130,7 @@ let NotificationsService = NotificationsService_1 = class NotificationsService e
130
130
  *
131
131
  * @param resourceId Unique identifier of the resource.
132
132
  */
133
- deleteNotifications(resourceId) {
133
+ deleteNotificationsV1(resourceId) {
134
134
  if (!this.options.resource) {
135
135
  this.logger.error('Cannot delete notifications: Resource type not provided.');
136
136
  return;
@@ -8,6 +8,9 @@ const swagger_1 = require("@nestjs/swagger");
8
8
  const class_validator_1 = require("class-validator");
9
9
  const itlab_functions_1 = require("itlab-functions");
10
10
  const exceptions_1 = require("../../exceptions");
11
+ function isValidHubId(value) {
12
+ return (0, class_validator_1.isString)(value) && (0, itlab_functions_1.isHubId)(value.toLowerCase());
13
+ }
11
14
  /**
12
15
  * Pipe that validates and transforms a route parameter into a valid HubId string.
13
16
  * Throws a `BadParameterException` if the input does not match the expected format.
@@ -22,8 +25,8 @@ class ParseHubIdPipe {
22
25
  * @throws {BadParameterException} - If validation fails.
23
26
  */
24
27
  transform(inputValue) {
25
- if ((0, itlab_functions_1.isHubId)(inputValue))
26
- return String(inputValue);
28
+ if (isValidHubId(inputValue))
29
+ return inputValue.toLowerCase();
27
30
  throw new exceptions_1.BadParameterException(this.parameterName);
28
31
  }
29
32
  }
@@ -59,7 +62,7 @@ function IsHubId(options) {
59
62
  return (0, class_validator_1.ValidateBy)({
60
63
  name: 'isHubId',
61
64
  validator: {
62
- validate: (input) => (0, itlab_functions_1.isHubId)(input),
65
+ validate: (input) => isValidHubId(input),
63
66
  defaultMessage: (0, class_validator_1.buildMessage)((each) => `${each}$property must be a valid HubId`, options),
64
67
  },
65
68
  }, options);
@@ -7,8 +7,8 @@ import { User } from '../models';
7
7
  * - `likedBy`: optional populated list of User objects.
8
8
  */
9
9
  export type IsLikeable = {
10
- _likedBy: string[];
11
- likes: number;
10
+ _likedBy?: string[];
11
+ likes?: number;
12
12
  likedBy?: User[];
13
13
  addLike(accountId: string): Promise<number>;
14
14
  removeLike(accountId: string): Promise<number>;
@@ -7,8 +7,8 @@ import { User } from '../models';
7
7
  * - `viewedBy`: optional populated list of User objects.
8
8
  */
9
9
  export type IsViewable = {
10
- _viewedBy: string[];
11
- views: number;
10
+ _viewedBy?: string[];
11
+ views?: number;
12
12
  viewedBy?: User[];
13
13
  addView(accountId: string): Promise<number>;
14
14
  removeView(accountId: string): Promise<number>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "name": "Timo Scheuermann",
5
5
  "email": "timo.scheuermann@sv-informatik.de"
6
6
  },
7
- "version": "2.16.9",
7
+ "version": "2.16.10",
8
8
  "type": "commonjs",
9
9
  "files": [
10
10
  "dist"