Skip to content

Commit bc8df79

Browse files
committed
refactor: improve types and add validation for targetId and targetModel
1 parent ea57f73 commit bc8df79

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

server/history/history.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export class HistoryController {
1919
@Query() query: HistoryQuery
2020
): Promise<HistoryResponse> {
2121
const { targetId, targetModel } = param;
22+
23+
if (!targetId || !targetModel) {
24+
throw new Error("targetId and targetModel are required");
25+
}
26+
2227
try {
2328
const response = await this.historyService.getHistoryForTarget(
2429
targetId,

server/history/history.service.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable, Logger } from "@nestjs/common";
22
import { InjectModel } from "@nestjs/mongoose";
33
import { Model, Types, isValidObjectId } from "mongoose";
4-
import { History, HistoryDocument, HistoryType } from "./schema/history.schema";
4+
import { History, HistoryDocument, HistoryType, TargetModel } from "./schema/history.schema";
55
import {
66
AfterAndBeforeType,
77
HEX24,
@@ -44,12 +44,16 @@ export class HistoryService {
4444
*/
4545
getHistoryParams(
4646
dataId: string,
47-
targetModel: string,
47+
targetModel: TargetModel,
4848
performedBy: PerformedBy,
49-
type: string,
49+
type: HistoryType,
5050
latestChange: AfterAndBeforeType,
5151
previousChange?: AfterAndBeforeType
5252
) {
53+
if (!isValidObjectId(dataId)) {
54+
throw new Error(`Invalid dataId received: ${dataId}`);
55+
}
56+
5357
const date = new Date();
5458
const targetId = Types.ObjectId(dataId);
5559
let currentPerformedBy = null;
@@ -60,9 +64,6 @@ export class HistoryService {
6064
currentPerformedBy = performedBy;
6165
}
6266

63-
if (!isValidObjectId(dataId)) {
64-
throw new Error(`Invalid dataId received: ${dataId}`);
65-
}
6667

6768
return {
6869
targetId: targetId,
@@ -82,14 +83,16 @@ export class HistoryService {
8283
* @param data Object with the history data
8384
* @returns Returns a new history document to database
8485
*/
85-
async createHistory(data) {
86+
async createHistory(
87+
data: Partial<HistoryDocument>
88+
): Promise<HistoryDocument> {
8689
const newHistory = new this.HistoryModel(data);
8790
return newHistory.save();
8891
}
8992

9093
async getHistoryForTarget(
9194
targetId: string,
92-
targetModel: string,
95+
targetModel: TargetModel,
9396
query: HistoryQuery
9497
): Promise<HistoryResponse> {
9598
const page = Math.max(Number(query.page) || 0, 0);
@@ -167,8 +170,8 @@ export class HistoryService {
167170
}
168171

169172
async getDescriptionForHide(
170-
content: IHideableContent,
171-
target: string
173+
content: IHideableContent,
174+
target: TargetModel
172175
) {
173176
if (!content?._id || !content?.isHidden) {
174177
return "";

server/history/schema/history.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class History {
6868
type: Date,
6969
required: true,
7070
})
71-
date: mongoose.Date;
71+
date: Date;
7272
}
7373

7474
export const HistorySchema = SchemaFactory.createForClass(History);

server/history/types/history.interfaces.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import { Roles } from "auth/ability/ability.factory";
2+
import { HistoryType, TargetModel } from "history/schema/history.schema";
13
import { Types } from "mongoose";
24

35
export const HEX24 = /^[0-9a-fA-F]{24}$/;
46

57
interface HistoryParams {
68
targetId: string;
7-
targetModel: string;
9+
targetModel: TargetModel;
810
}
911

1012
interface HistoryQuery {
1113
page?: number;
1214
pageSize?: number;
1315
order?: "asc" | "desc";
14-
type?: string;
16+
type?: HistoryType;
1517
}
1618
interface HistoryResponse {
1719
history: HistoryItem[];
@@ -23,9 +25,9 @@ interface HistoryResponse {
2325
interface HistoryItem {
2426
_id?: string;
2527
targetId: Types.ObjectId;
26-
targetModel: string;
28+
targetModel: TargetModel;
2729
user?: PerformedBy;
28-
type?: string;
30+
type?: HistoryType;
2931
details?: HistoryDetails;
3032
date?: Date | string;
3133
}
@@ -37,7 +39,7 @@ interface M2M {
3739
subject: string;
3840
scopes: string[];
3941
role: {
40-
main: string;
42+
main: Roles.Integration;
4143
};
4244
namespace: string;
4345
}

0 commit comments

Comments
 (0)