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