@trafficgroup/knex-rel 0.1.9 → 0.1.11-rc0
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.
- package/.claude/settings.local.json +5 -2
- package/CLAUDE.md +2 -11
- package/dist/constants/video.constants.d.ts +12 -0
- package/dist/constants/video.constants.js +22 -0
- package/dist/constants/video.constants.js.map +1 -0
- package/dist/dao/chat/chat.dao.d.ts +4 -1
- package/dist/dao/chat/chat.dao.js +21 -12
- package/dist/dao/chat/chat.dao.js.map +1 -1
- package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
- package/dist/dao/video/video.dao.d.ts +2 -1
- package/dist/dao/video/video.dao.js +18 -2
- package/dist/dao/video/video.dao.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/migrations/20250717160737_migration.ts +1 -1
- package/migrations/20250717160908_migration.ts +2 -5
- package/migrations/20250717161310_migration.ts +1 -1
- package/migrations/20250717161406_migration.ts +3 -3
- package/migrations/20250717162431_migration.ts +1 -1
- package/migrations/20250717173228_migration.ts +2 -2
- package/migrations/20250717204731_migration.ts +1 -1
- package/migrations/20250722210109_migration.ts +4 -8
- package/migrations/20250722211019_migration.ts +1 -1
- package/migrations/20250723153852_migration.ts +10 -13
- package/migrations/20250723162257_migration.ts +7 -4
- package/migrations/20250723171109_migration.ts +7 -4
- package/migrations/20250723205331_migration.ts +9 -6
- package/migrations/20250724191345_migration.ts +11 -8
- package/migrations/20250730180932_migration.ts +13 -14
- package/migrations/20250730213625_migration.ts +11 -8
- package/migrations/20250804124509_migration.ts +21 -26
- package/migrations/20250804132053_migration.ts +8 -5
- package/migrations/20250804164518_migration.ts +7 -7
- package/migrations/20250823223016_migration.ts +21 -32
- package/migrations/20250910015452_migration.ts +6 -18
- package/migrations/20250911000000_migration.ts +4 -18
- package/migrations/20250917144153_migration.ts +7 -14
- package/migrations/20250930200521_migration.ts +4 -8
- package/migrations/20251010143500_migration.ts +6 -27
- package/migrations/20251020225758_migration.ts +15 -51
- package/migrations/20251112120000_migration.ts +2 -10
- package/migrations/20251112120200_migration.ts +7 -19
- package/migrations/20251112120300_migration.ts +2 -7
- package/package.json +1 -1
- package/src/constants/video.constants.ts +19 -0
- package/src/d.types.ts +14 -18
- package/src/dao/VideoMinuteResultDAO.ts +49 -72
- package/src/dao/auth/auth.dao.ts +55 -58
- package/src/dao/batch/batch.dao.ts +98 -101
- package/src/dao/camera/camera.dao.ts +121 -124
- package/src/dao/chat/chat.dao.ts +60 -42
- package/src/dao/folder/folder.dao.ts +56 -65
- package/src/dao/location/location.dao.ts +87 -109
- package/src/dao/message/message.dao.ts +32 -32
- package/src/dao/report-configuration/report-configuration.dao.ts +342 -370
- package/src/dao/study/study.dao.ts +63 -88
- package/src/dao/user/user.dao.ts +50 -52
- package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +49 -83
- package/src/dao/video/video.dao.ts +339 -357
- package/src/entities/BaseEntity.ts +1 -1
- package/src/index.ts +23 -27
- package/src/interfaces/auth/auth.interfaces.ts +10 -10
- package/src/interfaces/batch/batch.interfaces.ts +1 -1
- package/src/interfaces/camera/camera.interfaces.ts +9 -9
- package/src/interfaces/chat/chat.interfaces.ts +4 -4
- package/src/interfaces/folder/folder.interfaces.ts +2 -2
- package/src/interfaces/location/location.interfaces.ts +7 -7
- package/src/interfaces/message/message.interfaces.ts +3 -3
- package/src/interfaces/report-configuration/report-configuration.interfaces.ts +16 -16
- package/src/interfaces/study/study.interfaces.ts +3 -3
- package/src/interfaces/user/user.interfaces.ts +9 -9
- package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
- package/src/interfaces/video/video.interfaces.ts +34 -34
|
@@ -4,118 +4,115 @@ import { IBatch } from "../../interfaces/batch/batch.interfaces";
|
|
|
4
4
|
import KnexManager from "../../KnexConnection";
|
|
5
5
|
|
|
6
6
|
export class BatchDAO implements IBaseDAO<IBatch> {
|
|
7
|
-
|
|
7
|
+
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return createdBatch;
|
|
14
|
-
}
|
|
9
|
+
async create(item: IBatch): Promise<IBatch> {
|
|
10
|
+
const [createdBatch] = await this._knex("video_batch").insert(item).returning("*");
|
|
11
|
+
return createdBatch;
|
|
12
|
+
}
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
async getById(id: number): Promise<IBatch | null> {
|
|
15
|
+
const batch = await this._knex("video_batch as b")
|
|
16
|
+
.innerJoin("folders as f", "b.folderId", "f.id")
|
|
17
|
+
.select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
|
|
18
|
+
.where("b.id", id)
|
|
19
|
+
.first();
|
|
20
|
+
return batch || null;
|
|
21
|
+
}
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
async getByUuid(uuid: string): Promise<IBatch | null> {
|
|
24
|
+
const batch = await this._knex("video_batch as b")
|
|
25
|
+
.innerJoin("folders as f", "b.folderId", "f.id")
|
|
26
|
+
.select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
|
|
27
|
+
.where("b.uuid", uuid)
|
|
28
|
+
.first();
|
|
29
|
+
return batch || null;
|
|
30
|
+
}
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.returning("*");
|
|
39
|
-
return updatedBatch || null;
|
|
40
|
-
}
|
|
32
|
+
async update(id: number, item: Partial<IBatch>): Promise<IBatch | null> {
|
|
33
|
+
const [updatedBatch] = await this._knex("video_batch").where({ id }).update(item).returning("*");
|
|
34
|
+
return updatedBatch || null;
|
|
35
|
+
}
|
|
41
36
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
async delete(id: number): Promise<boolean> {
|
|
38
|
+
const result = await this._knex("video_batch").where({ id }).del();
|
|
39
|
+
return result > 0;
|
|
40
|
+
}
|
|
46
41
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
limit: number,
|
|
50
|
-
folderId?: number | null,
|
|
51
|
-
): Promise<IDataPaginator<IBatch>> {
|
|
52
|
-
const offset = (page - 1) * limit;
|
|
42
|
+
async getAll(page: number, limit: number, folderId?: number | null): Promise<IDataPaginator<IBatch>> {
|
|
43
|
+
const offset = (page - 1) * limit;
|
|
53
44
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
45
|
+
const query = this._knex("video_batch as b")
|
|
46
|
+
.innerJoin("folders as f", "b.folderId", "f.id")
|
|
47
|
+
.select("b.*", this._knex.raw("to_jsonb(f.*) as folder"));
|
|
48
|
+
if (folderId !== undefined && folderId !== null) {
|
|
49
|
+
query.where("b.folderId", folderId);
|
|
50
|
+
}
|
|
60
51
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
52
|
+
const [countResult] = await query.clone().clearSelect().count("* as count");
|
|
53
|
+
const totalCount = +countResult.count;
|
|
54
|
+
const batches = await query.clone().limit(limit).offset(offset);
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
56
|
+
return {
|
|
57
|
+
success: true,
|
|
58
|
+
data: batches,
|
|
59
|
+
page,
|
|
60
|
+
limit,
|
|
61
|
+
count: batches.length,
|
|
62
|
+
totalCount,
|
|
63
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
75
66
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Get batches by folder ID
|
|
69
|
+
*/
|
|
70
|
+
async getByFolderId(folderId: number): Promise<IBatch[]> {
|
|
71
|
+
return this._knex("video_batch as b")
|
|
72
|
+
.innerJoin("folders as f", "b.folderId", "f.id")
|
|
73
|
+
.select("b.*", this._knex.raw("to_jsonb(f.*) as folder"))
|
|
74
|
+
.where("b.folderId", folderId)
|
|
75
|
+
.orderBy("b.created_at", "desc");
|
|
76
|
+
}
|
|
86
77
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Update batch progress
|
|
80
|
+
*/
|
|
81
|
+
async updateProgress(
|
|
82
|
+
id: number,
|
|
83
|
+
completedVideos: number,
|
|
84
|
+
failedVideos: number
|
|
85
|
+
): Promise<void> {
|
|
86
|
+
await this._knex("video_batch")
|
|
87
|
+
.where({ id })
|
|
88
|
+
.update({
|
|
89
|
+
completedVideos,
|
|
90
|
+
failedVideos,
|
|
91
|
+
updated_at: this._knex.fn.now()
|
|
92
|
+
});
|
|
93
|
+
}
|
|
101
94
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Mark batch as completed
|
|
97
|
+
*/
|
|
98
|
+
async markCompleted(id: number): Promise<void> {
|
|
99
|
+
await this._knex("video_batch")
|
|
100
|
+
.where({ id })
|
|
101
|
+
.update({
|
|
102
|
+
status: "COMPLETED",
|
|
103
|
+
updated_at: this._knex.fn.now()
|
|
104
|
+
});
|
|
105
|
+
}
|
|
111
106
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Mark batch as failed
|
|
109
|
+
*/
|
|
110
|
+
async markFailed(id: number): Promise<void> {
|
|
111
|
+
await this._knex("video_batch")
|
|
112
|
+
.where({ id })
|
|
113
|
+
.update({
|
|
114
|
+
status: "FAILED",
|
|
115
|
+
updated_at: this._knex.fn.now()
|
|
116
|
+
});
|
|
117
|
+
}
|
|
121
118
|
}
|
|
@@ -4,134 +4,131 @@ import { ICamera } from "../../interfaces/camera/camera.interfaces";
|
|
|
4
4
|
import KnexManager from "../../KnexConnection";
|
|
5
5
|
|
|
6
6
|
export class CameraDAO implements IBaseDAO<ICamera> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async delete(id: number): Promise<boolean> {
|
|
35
|
-
const result = await this._knex("cameras").where({ id }).del();
|
|
36
|
-
return result > 0;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async getAll(page: number, limit: number): Promise<IDataPaginator<ICamera>> {
|
|
40
|
-
const offset = (page - 1) * limit;
|
|
41
|
-
|
|
42
|
-
const [countResult] = await this._knex("cameras").count("* as count");
|
|
43
|
-
const totalCount = +countResult.count;
|
|
44
|
-
const cameras = await this._knex("cameras").limit(limit).offset(offset);
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
success: true,
|
|
48
|
-
data: cameras,
|
|
49
|
-
page,
|
|
50
|
-
limit,
|
|
51
|
-
count: cameras.length,
|
|
52
|
-
totalCount,
|
|
53
|
-
totalPages: Math.ceil(totalCount / limit),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Get cameras for a specific location
|
|
59
|
-
* @param locationId - The location ID
|
|
60
|
-
* @param status - Optional filter by camera status
|
|
61
|
-
*/
|
|
62
|
-
async getCamerasByLocationId(
|
|
63
|
-
locationId: number,
|
|
64
|
-
status?: string,
|
|
65
|
-
): Promise<ICamera[]> {
|
|
66
|
-
let query = this._knex("cameras").where("locationId", locationId);
|
|
67
|
-
|
|
68
|
-
if (status) {
|
|
69
|
-
query = query.where("status", status);
|
|
7
|
+
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
8
|
+
|
|
9
|
+
async create(item: ICamera): Promise<ICamera> {
|
|
10
|
+
const [createdCamera] = await this._knex("cameras").insert(item).returning("*");
|
|
11
|
+
return createdCamera;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async getById(id: number): Promise<ICamera | null> {
|
|
15
|
+
const camera = await this._knex("cameras").where({ id }).first();
|
|
16
|
+
return camera || null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async getByUuid(uuid: string): Promise<ICamera | null> {
|
|
20
|
+
const camera = await this._knex("cameras").where({ uuid }).first();
|
|
21
|
+
return camera || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async update(id: number, item: Partial<ICamera>): Promise<ICamera | null> {
|
|
25
|
+
const [updatedCamera] = await this._knex("cameras").where({ id }).update(item).returning("*");
|
|
26
|
+
return updatedCamera || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async delete(id: number): Promise<boolean> {
|
|
30
|
+
const result = await this._knex("cameras").where({ id }).del();
|
|
31
|
+
return result > 0;
|
|
70
32
|
}
|
|
71
33
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const offset = (page - 1) * limit;
|
|
89
|
-
|
|
90
|
-
let query = this._knex("cameras").where("locationId", locationId);
|
|
91
|
-
|
|
92
|
-
if (status) {
|
|
93
|
-
query = query.where("status", status);
|
|
34
|
+
async getAll(page: number, limit: number): Promise<IDataPaginator<ICamera>> {
|
|
35
|
+
const offset = (page - 1) * limit;
|
|
36
|
+
|
|
37
|
+
const [countResult] = await this._knex("cameras").count("* as count");
|
|
38
|
+
const totalCount = +countResult.count;
|
|
39
|
+
const cameras = await this._knex("cameras").limit(limit).offset(offset);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
success: true,
|
|
43
|
+
data: cameras,
|
|
44
|
+
page,
|
|
45
|
+
limit,
|
|
46
|
+
count: cameras.length,
|
|
47
|
+
totalCount,
|
|
48
|
+
totalPages: Math.ceil(totalCount / limit),
|
|
49
|
+
};
|
|
94
50
|
}
|
|
95
51
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Check if a camera name already exists for a location
|
|
117
|
-
* @param name - Camera name
|
|
118
|
-
* @param locationId - Location ID
|
|
119
|
-
* @param excludeId - Optional camera ID to exclude (for update operations)
|
|
120
|
-
*/
|
|
121
|
-
async existsByNameAndLocation(
|
|
122
|
-
name: string,
|
|
123
|
-
locationId: number,
|
|
124
|
-
excludeId?: number,
|
|
125
|
-
): Promise<boolean> {
|
|
126
|
-
let query = this._knex("cameras")
|
|
127
|
-
.where("locationId", locationId)
|
|
128
|
-
.where("name", name);
|
|
129
|
-
|
|
130
|
-
if (excludeId !== undefined) {
|
|
131
|
-
query = query.whereNot("id", excludeId);
|
|
52
|
+
/**
|
|
53
|
+
* Get cameras for a specific location
|
|
54
|
+
* @param locationId - The location ID
|
|
55
|
+
* @param status - Optional filter by camera status
|
|
56
|
+
*/
|
|
57
|
+
async getCamerasByLocationId(
|
|
58
|
+
locationId: number,
|
|
59
|
+
status?: string
|
|
60
|
+
): Promise<ICamera[]> {
|
|
61
|
+
let query = this._knex("cameras")
|
|
62
|
+
.where("locationId", locationId);
|
|
63
|
+
|
|
64
|
+
if (status) {
|
|
65
|
+
query = query.where("status", status);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return query.orderBy("name", "asc");
|
|
132
69
|
}
|
|
133
70
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Get cameras for a specific location with pagination
|
|
73
|
+
* @param locationId - The location ID
|
|
74
|
+
* @param page - Page number
|
|
75
|
+
* @param limit - Items per page
|
|
76
|
+
* @param status - Optional filter by camera status
|
|
77
|
+
*/
|
|
78
|
+
async getCamerasByLocationIdPaginated(
|
|
79
|
+
locationId: number,
|
|
80
|
+
page: number,
|
|
81
|
+
limit: number,
|
|
82
|
+
status?: string
|
|
83
|
+
): Promise<IDataPaginator<ICamera>> {
|
|
84
|
+
const offset = (page - 1) * limit;
|
|
85
|
+
|
|
86
|
+
let query = this._knex("cameras")
|
|
87
|
+
.where("locationId", locationId);
|
|
88
|
+
|
|
89
|
+
if (status) {
|
|
90
|
+
query = query.where("status", status);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const [countResult] = await query.clone().count("* as count");
|
|
94
|
+
const totalCount = +countResult.count;
|
|
95
|
+
const cameras = await query
|
|
96
|
+
.clone()
|
|
97
|
+
.limit(limit)
|
|
98
|
+
.offset(offset)
|
|
99
|
+
.orderBy("name", "asc");
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
success: true,
|
|
103
|
+
data: cameras,
|
|
104
|
+
page,
|
|
105
|
+
limit,
|
|
106
|
+
count: cameras.length,
|
|
107
|
+
totalCount,
|
|
108
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Check if a camera name already exists for a location
|
|
114
|
+
* @param name - Camera name
|
|
115
|
+
* @param locationId - Location ID
|
|
116
|
+
* @param excludeId - Optional camera ID to exclude (for update operations)
|
|
117
|
+
*/
|
|
118
|
+
async existsByNameAndLocation(
|
|
119
|
+
name: string,
|
|
120
|
+
locationId: number,
|
|
121
|
+
excludeId?: number
|
|
122
|
+
): Promise<boolean> {
|
|
123
|
+
let query = this._knex("cameras")
|
|
124
|
+
.where("locationId", locationId)
|
|
125
|
+
.where("name", name);
|
|
126
|
+
|
|
127
|
+
if (excludeId !== undefined) {
|
|
128
|
+
query = query.whereNot("id", excludeId);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const result = await query.first();
|
|
132
|
+
return result !== null && result !== undefined;
|
|
133
|
+
}
|
|
137
134
|
}
|
package/src/dao/chat/chat.dao.ts
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import { Knex } from "knex";
|
|
2
2
|
import { IBaseDAO, IDataPaginator } from "../../d.types";
|
|
3
|
-
import {
|
|
4
|
-
IChat,
|
|
5
|
-
IChatCreate,
|
|
6
|
-
IChatUpdate,
|
|
7
|
-
} from "../../interfaces/chat/chat.interfaces";
|
|
3
|
+
import { IChat, IChatCreate, IChatUpdate } from '../../interfaces/chat/chat.interfaces';
|
|
8
4
|
import KnexManager from "../../KnexConnection";
|
|
9
5
|
|
|
6
|
+
export interface IChatPaginatorResponse extends IDataPaginator<IChat> {
|
|
7
|
+
hasMore: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
10
|
export class ChatDAO implements IBaseDAO<IChat> {
|
|
11
11
|
private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
|
|
12
12
|
|
|
13
13
|
async create(item: IChatCreate): Promise<IChat> {
|
|
14
|
-
const [result] = await this._knex(
|
|
14
|
+
const [result] = await this._knex('chat')
|
|
15
|
+
.insert(item)
|
|
16
|
+
.returning('*');
|
|
15
17
|
return result;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
async getById(id: number): Promise<IChat | null> {
|
|
19
|
-
const result = await this._knex(
|
|
21
|
+
const result = await this._knex('chat')
|
|
22
|
+
.where('id', id)
|
|
23
|
+
.first();
|
|
20
24
|
return result || null;
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
async getByUuid(uuid: string): Promise<IChat | null> {
|
|
24
|
-
const result = await this._knex(
|
|
28
|
+
const result = await this._knex('chat')
|
|
29
|
+
.where('uuid', uuid)
|
|
30
|
+
.first();
|
|
25
31
|
return result || null;
|
|
26
32
|
}
|
|
27
33
|
|
|
@@ -29,11 +35,11 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
29
35
|
const offset = (page - 1) * limit;
|
|
30
36
|
|
|
31
37
|
const [results, [{ count }]] = await Promise.all([
|
|
32
|
-
this._knex(
|
|
33
|
-
.orderBy(
|
|
38
|
+
this._knex('chat')
|
|
39
|
+
.orderBy('created_at', 'desc')
|
|
34
40
|
.limit(limit)
|
|
35
41
|
.offset(offset),
|
|
36
|
-
this._knex(
|
|
42
|
+
this._knex('chat').count('* as count')
|
|
37
43
|
]);
|
|
38
44
|
|
|
39
45
|
const totalCount = parseInt(count as string);
|
|
@@ -44,40 +50,40 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
44
50
|
limit,
|
|
45
51
|
count: results.length,
|
|
46
52
|
totalCount,
|
|
47
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
53
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
48
54
|
};
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
async update(id: number, item: IChatUpdate): Promise<IChat | null> {
|
|
52
|
-
const [result] = await this._knex(
|
|
53
|
-
.where(
|
|
58
|
+
const [result] = await this._knex('chat')
|
|
59
|
+
.where('id', id)
|
|
54
60
|
.update({
|
|
55
61
|
...item,
|
|
56
|
-
updated_at: new Date()
|
|
62
|
+
updated_at: new Date()
|
|
57
63
|
})
|
|
58
|
-
.returning(
|
|
64
|
+
.returning('*');
|
|
59
65
|
return result || null;
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
async delete(id: number): Promise<boolean> {
|
|
63
|
-
const result = await this._knex(
|
|
69
|
+
const result = await this._knex('chat')
|
|
70
|
+
.where('id', id)
|
|
71
|
+
.delete();
|
|
64
72
|
return result > 0;
|
|
65
73
|
}
|
|
66
74
|
|
|
67
|
-
async getByUserId(
|
|
68
|
-
userId: number,
|
|
69
|
-
page = 1,
|
|
70
|
-
limit = 10,
|
|
71
|
-
): Promise<IDataPaginator<IChat>> {
|
|
75
|
+
async getByUserId(userId: number, page = 1, limit = 10): Promise<IDataPaginator<IChat>> {
|
|
72
76
|
const offset = (page - 1) * limit;
|
|
73
77
|
|
|
74
78
|
const [results, [{ count }]] = await Promise.all([
|
|
75
|
-
this._knex(
|
|
76
|
-
.where(
|
|
77
|
-
.orderBy(
|
|
79
|
+
this._knex('chat')
|
|
80
|
+
.where('userId', userId)
|
|
81
|
+
.orderBy('created_at', 'desc')
|
|
78
82
|
.limit(limit)
|
|
79
83
|
.offset(offset),
|
|
80
|
-
this._knex(
|
|
84
|
+
this._knex('chat')
|
|
85
|
+
.where('userId', userId)
|
|
86
|
+
.count('* as count')
|
|
81
87
|
]);
|
|
82
88
|
|
|
83
89
|
const totalCount = parseInt(count as string);
|
|
@@ -88,31 +94,42 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
88
94
|
limit,
|
|
89
95
|
count: results.length,
|
|
90
96
|
totalCount,
|
|
91
|
-
totalPages: Math.ceil(totalCount / limit)
|
|
97
|
+
totalPages: Math.ceil(totalCount / limit)
|
|
92
98
|
};
|
|
93
99
|
}
|
|
94
100
|
|
|
95
|
-
async getActiveByUserId(
|
|
96
|
-
userId: number,
|
|
97
|
-
page = 1,
|
|
98
|
-
limit = 10,
|
|
99
|
-
): Promise<IDataPaginator<IChat>> {
|
|
101
|
+
async getActiveByUserId(userId: number, page = 1, limit = 10, query?: string): Promise<IChatPaginatorResponse> {
|
|
100
102
|
const offset = (page - 1) * limit;
|
|
101
103
|
|
|
104
|
+
// Build data query
|
|
105
|
+
let dataQuery = this._knex('chat')
|
|
106
|
+
.where('userId', userId)
|
|
107
|
+
.where('status', 'active');
|
|
108
|
+
|
|
109
|
+
// Build count query
|
|
110
|
+
let countQuery = this._knex('chat')
|
|
111
|
+
.where('userId', userId)
|
|
112
|
+
.where('status', 'active');
|
|
113
|
+
|
|
114
|
+
// Apply search filter if query is provided
|
|
115
|
+
if (query && query.trim()) {
|
|
116
|
+
const searchTerm = `%${query.trim()}%`;
|
|
117
|
+
dataQuery = dataQuery.where('title', 'ILIKE', searchTerm);
|
|
118
|
+
countQuery = countQuery.where('title', 'ILIKE', searchTerm);
|
|
119
|
+
}
|
|
120
|
+
|
|
102
121
|
const [results, [{ count }]] = await Promise.all([
|
|
103
|
-
|
|
104
|
-
.
|
|
105
|
-
.where("status", "active")
|
|
106
|
-
.orderBy("created_at", "desc")
|
|
122
|
+
dataQuery
|
|
123
|
+
.orderBy('created_at', 'desc')
|
|
107
124
|
.limit(limit)
|
|
108
125
|
.offset(offset),
|
|
109
|
-
|
|
110
|
-
.where("userId", userId)
|
|
111
|
-
.where("status", "active")
|
|
112
|
-
.count("* as count"),
|
|
126
|
+
countQuery.count('* as count')
|
|
113
127
|
]);
|
|
114
128
|
|
|
115
129
|
const totalCount = parseInt(count as string);
|
|
130
|
+
const totalPages = Math.ceil(totalCount / limit);
|
|
131
|
+
const hasMore = page < totalPages;
|
|
132
|
+
|
|
116
133
|
return {
|
|
117
134
|
success: true,
|
|
118
135
|
data: results,
|
|
@@ -120,7 +137,8 @@ export class ChatDAO implements IBaseDAO<IChat> {
|
|
|
120
137
|
limit,
|
|
121
138
|
count: results.length,
|
|
122
139
|
totalCount,
|
|
123
|
-
totalPages
|
|
140
|
+
totalPages,
|
|
141
|
+
hasMore
|
|
124
142
|
};
|
|
125
143
|
}
|
|
126
|
-
}
|
|
144
|
+
}
|