@trafficgroup/knex-rel 0.1.21 → 0.1.23-rc.0

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.
Files changed (121) hide show
  1. package/.claude/settings.local.json +5 -2
  2. package/CLAUDE.md +2 -11
  3. package/dist/constants/folder.constants.d.ts +2 -2
  4. package/dist/constants/folder.constants.js +6 -15
  5. package/dist/constants/folder.constants.js.map +1 -1
  6. package/dist/constants/study.constants.d.ts +1 -1
  7. package/dist/constants/study.constants.js +1 -1
  8. package/dist/constants/video.constants.d.ts +2 -2
  9. package/dist/constants/video.constants.js +5 -9
  10. package/dist/constants/video.constants.js.map +1 -1
  11. package/dist/dao/VideoMinuteResultDAO.d.ts +19 -2
  12. package/dist/dao/VideoMinuteResultDAO.js +75 -29
  13. package/dist/dao/VideoMinuteResultDAO.js.map +1 -1
  14. package/dist/dao/auth/auth.dao.js +1 -4
  15. package/dist/dao/auth/auth.dao.js.map +1 -1
  16. package/dist/dao/batch/batch.dao.d.ts +0 -10
  17. package/dist/dao/batch/batch.dao.js +14 -51
  18. package/dist/dao/batch/batch.dao.js.map +1 -1
  19. package/dist/dao/camera/camera.dao.js +7 -10
  20. package/dist/dao/camera/camera.dao.js.map +1 -1
  21. package/dist/dao/chat/chat.dao.d.ts +1 -1
  22. package/dist/dao/chat/chat.dao.js +40 -27
  23. package/dist/dao/chat/chat.dao.js.map +1 -1
  24. package/dist/dao/folder/folder.dao.js +7 -18
  25. package/dist/dao/folder/folder.dao.js.map +1 -1
  26. package/dist/dao/location/location.dao.js +9 -16
  27. package/dist/dao/location/location.dao.js.map +1 -1
  28. package/dist/dao/message/message.dao.d.ts +1 -1
  29. package/dist/dao/message/message.dao.js +26 -18
  30. package/dist/dao/message/message.dao.js.map +1 -1
  31. package/dist/dao/report-configuration/report-configuration.dao.js +68 -39
  32. package/dist/dao/report-configuration/report-configuration.dao.js.map +1 -1
  33. package/dist/dao/study/study.dao.d.ts +8 -1
  34. package/dist/dao/study/study.dao.js +20 -15
  35. package/dist/dao/study/study.dao.js.map +1 -1
  36. package/dist/dao/systemConfiguration/SystemConfigurationDAO.d.ts +2 -2
  37. package/dist/dao/systemConfiguration/SystemConfigurationDAO.js +26 -26
  38. package/dist/dao/systemConfiguration/SystemConfigurationDAO.js.map +1 -1
  39. package/dist/dao/user/user.dao.js +1 -4
  40. package/dist/dao/user/user.dao.js.map +1 -1
  41. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js +8 -26
  42. package/dist/dao/user-push-notification-token/user-push-notification-token.dao.js.map +1 -1
  43. package/dist/dao/video/video.dao.js +28 -30
  44. package/dist/dao/video/video.dao.js.map +1 -1
  45. package/dist/index.d.ts +10 -9
  46. package/dist/index.js +4 -4
  47. package/dist/index.js.map +1 -1
  48. package/dist/interfaces/batch/batch.interfaces.d.ts +1 -1
  49. package/dist/interfaces/camera/camera.interfaces.d.ts +1 -1
  50. package/dist/interfaces/chat/chat.interfaces.d.ts +3 -3
  51. package/dist/interfaces/folder/folder.interfaces.d.ts +1 -1
  52. package/dist/interfaces/message/message.interfaces.d.ts +2 -2
  53. package/dist/interfaces/report-configuration/report-configuration.interfaces.d.ts +1 -0
  54. package/dist/interfaces/study/study.interfaces.d.ts +2 -8
  55. package/dist/interfaces/user/user.interfaces.d.ts +1 -1
  56. package/dist/interfaces/user-push-notification-token/user-push-notification-token.interfaces.d.ts +1 -1
  57. package/dist/interfaces/video/video.interfaces.d.ts +2 -2
  58. package/migrations/20250717160737_migration.ts +1 -1
  59. package/migrations/20250717160908_migration.ts +2 -5
  60. package/migrations/20250717161310_migration.ts +1 -1
  61. package/migrations/20250717161406_migration.ts +3 -3
  62. package/migrations/20250717162431_migration.ts +1 -1
  63. package/migrations/20250717173228_migration.ts +2 -2
  64. package/migrations/20250717204731_migration.ts +1 -1
  65. package/migrations/20250722210109_migration.ts +4 -8
  66. package/migrations/20250722211019_migration.ts +1 -1
  67. package/migrations/20250723153852_migration.ts +10 -13
  68. package/migrations/20250723162257_migration.ts +7 -4
  69. package/migrations/20250723171109_migration.ts +7 -4
  70. package/migrations/20250723205331_migration.ts +9 -6
  71. package/migrations/20250724191345_migration.ts +11 -8
  72. package/migrations/20250730180932_migration.ts +13 -14
  73. package/migrations/20250730213625_migration.ts +11 -8
  74. package/migrations/20250804124509_migration.ts +21 -26
  75. package/migrations/20250804132053_migration.ts +8 -5
  76. package/migrations/20250804164518_migration.ts +7 -7
  77. package/migrations/20250823223016_migration.ts +21 -32
  78. package/migrations/20250910015452_migration.ts +6 -18
  79. package/migrations/20250911000000_migration.ts +4 -18
  80. package/migrations/20250917144153_migration.ts +7 -14
  81. package/migrations/20250930200521_migration.ts +4 -8
  82. package/migrations/20251010143500_migration.ts +6 -27
  83. package/migrations/20251020225758_migration.ts +15 -51
  84. package/migrations/20251112120000_migration.ts +2 -10
  85. package/migrations/20251112120200_migration.ts +7 -19
  86. package/migrations/20251112120300_migration.ts +2 -7
  87. package/migrations/20260109140000_migration.ts +2 -7
  88. package/package.json +3 -3
  89. package/src/constants/folder.constants.ts +8 -17
  90. package/src/constants/study.constants.ts +1 -1
  91. package/src/constants/video.constants.ts +7 -11
  92. package/src/d.types.ts +14 -18
  93. package/src/dao/VideoMinuteResultDAO.ts +127 -83
  94. package/src/dao/auth/auth.dao.ts +55 -58
  95. package/src/dao/batch/batch.dao.ts +100 -145
  96. package/src/dao/camera/camera.dao.ts +121 -124
  97. package/src/dao/chat/chat.dao.ts +45 -45
  98. package/src/dao/folder/folder.dao.ts +90 -105
  99. package/src/dao/location/location.dao.ts +87 -109
  100. package/src/dao/message/message.dao.ts +32 -32
  101. package/src/dao/reconciliation-log/reconciliation-log.dao.ts +1 -1
  102. package/src/dao/report-configuration/report-configuration.dao.ts +381 -370
  103. package/src/dao/study/study.dao.ts +83 -94
  104. package/src/dao/systemConfiguration/SystemConfigurationDAO.ts +35 -41
  105. package/src/dao/user/user.dao.ts +50 -52
  106. package/src/dao/user-push-notification-token/user-push-notification-token.dao.ts +48 -80
  107. package/src/dao/video/video.dao.ts +345 -396
  108. package/src/entities/BaseEntity.ts +1 -1
  109. package/src/index.ts +30 -43
  110. package/src/interfaces/auth/auth.interfaces.ts +10 -10
  111. package/src/interfaces/batch/batch.interfaces.ts +1 -1
  112. package/src/interfaces/camera/camera.interfaces.ts +9 -9
  113. package/src/interfaces/chat/chat.interfaces.ts +4 -4
  114. package/src/interfaces/folder/folder.interfaces.ts +2 -2
  115. package/src/interfaces/location/location.interfaces.ts +7 -7
  116. package/src/interfaces/message/message.interfaces.ts +3 -3
  117. package/src/interfaces/report-configuration/report-configuration.interfaces.ts +17 -16
  118. package/src/interfaces/study/study.interfaces.ts +3 -10
  119. package/src/interfaces/user/user.interfaces.ts +9 -9
  120. package/src/interfaces/user-push-notification-token/user-push-notification-token.interfaces.ts +9 -9
  121. package/src/interfaces/video/video.interfaces.ts +34 -34
@@ -4,160 +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
- private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
7
+ private _knex: Knex<any, unknown[]> = KnexManager.getConnection();
8
8
 
9
- async create(item: IBatch): Promise<IBatch> {
10
- const [createdBatch] = await this._knex("video_batch")
11
- .insert(item)
12
- .returning("*");
13
- return createdBatch;
14
- }
15
-
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
- }
24
-
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
- }
33
-
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
- }
41
-
42
- async delete(id: number): Promise<boolean> {
43
- const result = await this._knex("video_batch").where({ id }).del();
44
- return result > 0;
45
- }
46
-
47
- async getAll(
48
- page: number,
49
- limit: number,
50
- folderId?: number | null,
51
- ): Promise<IDataPaginator<IBatch>> {
52
- const offset = (page - 1) * limit;
53
-
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);
9
+ async create(item: IBatch): Promise<IBatch> {
10
+ const [createdBatch] = await this._knex("video_batch").insert(item).returning("*");
11
+ return createdBatch;
59
12
  }
60
13
 
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);
64
-
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
- }
75
-
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
- }
86
-
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
- }
101
-
102
- /**
103
- * Atomically increment completedVideos counter with row-level locking.
104
- * Returns the updated batch with fresh counters.
105
- */
106
- async incrementCompleted(id: number): Promise<IBatch> {
107
- return this._knex.transaction(async (trx) => {
108
- const batch = await trx("video_batch").where({ id }).forUpdate().first();
109
-
110
- if (!batch) throw new Error(`Batch ${id} not found`);
111
-
112
- const newCompleted = batch.completedVideos + 1;
113
-
114
- await trx("video_batch").where({ id }).update({
115
- completedVideos: newCompleted,
116
- updated_at: trx.fn.now(),
117
- });
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
+ }
118
22
 
119
- return { ...batch, completedVideos: newCompleted };
120
- });
121
- }
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
+ }
122
31
 
123
- /**
124
- * Atomically increment failedVideos counter with row-level locking.
125
- * Returns the updated batch with fresh counters.
126
- */
127
- async incrementFailed(id: number): Promise<IBatch> {
128
- return this._knex.transaction(async (trx) => {
129
- const batch = await trx("video_batch").where({ id }).forUpdate().first();
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
+ }
130
36
 
131
- if (!batch) throw new Error(`Batch ${id} not found`);
37
+ async delete(id: number): Promise<boolean> {
38
+ const result = await this._knex("video_batch").where({ id }).del();
39
+ return result > 0;
40
+ }
132
41
 
133
- const newFailed = batch.failedVideos + 1;
42
+ async getAll(page: number, limit: number, folderId?: number | null): Promise<IDataPaginator<IBatch>> {
43
+ const offset = (page - 1) * limit;
44
+
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
+ }
51
+
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);
55
+
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
+ }
134
66
 
135
- await trx("video_batch").where({ id }).update({
136
- failedVideos: newFailed,
137
- updated_at: trx.fn.now(),
138
- });
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
+ }
139
77
 
140
- return { ...batch, failedVideos: newFailed };
141
- });
142
- }
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
+ }
143
94
 
144
- /**
145
- * Mark batch as completed
146
- */
147
- async markCompleted(id: number): Promise<void> {
148
- await this._knex("video_batch").where({ id }).update({
149
- status: "COMPLETED",
150
- updated_at: this._knex.fn.now(),
151
- });
152
- }
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
+ }
153
106
 
154
- /**
155
- * Mark batch as failed
156
- */
157
- async markFailed(id: number): Promise<void> {
158
- await this._knex("video_batch").where({ id }).update({
159
- status: "FAILED",
160
- updated_at: this._knex.fn.now(),
161
- });
162
- }
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
+ }
163
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
- 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);
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
- 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);
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
- 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);
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
- const result = await query.first();
135
- return result !== null && result !== undefined;
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
  }