@tmscloud/tbt-knex 0.0.1

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 (58) hide show
  1. package/dist/KnexConnection.d.ts +17 -0
  2. package/dist/KnexConnection.js +81 -0
  3. package/dist/KnexConnection.js.map +1 -0
  4. package/dist/d.types.d.ts +16 -0
  5. package/dist/d.types.js +3 -0
  6. package/dist/d.types.js.map +1 -0
  7. package/dist/dao/driver-tracking/driver-tracking.dao.d.ts +42 -0
  8. package/dist/dao/driver-tracking/driver-tracking.dao.js +329 -0
  9. package/dist/dao/driver-tracking/driver-tracking.dao.js.map +1 -0
  10. package/dist/dao/gps-location/gps-location.dao.d.ts +76 -0
  11. package/dist/dao/gps-location/gps-location.dao.js +364 -0
  12. package/dist/dao/gps-location/gps-location.dao.js.map +1 -0
  13. package/dist/dao/role/role.dao.d.ts +38 -0
  14. package/dist/dao/role/role.dao.js +146 -0
  15. package/dist/dao/role/role.dao.js.map +1 -0
  16. package/dist/dao/route/route.dao.d.ts +38 -0
  17. package/dist/dao/route/route.dao.js +167 -0
  18. package/dist/dao/route/route.dao.js.map +1 -0
  19. package/dist/dao/route-assignment/route-assignment.dao.d.ts +58 -0
  20. package/dist/dao/route-assignment/route-assignment.dao.js +330 -0
  21. package/dist/dao/route-assignment/route-assignment.dao.js.map +1 -0
  22. package/dist/dao/sundays-package-version/sundays-package-version.dao.d.ts +11 -0
  23. package/dist/dao/sundays-package-version/sundays-package-version.dao.js +88 -0
  24. package/dist/dao/sundays-package-version/sundays-package-version.dao.js.map +1 -0
  25. package/dist/dao/trip/trip.dao.d.ts +102 -0
  26. package/dist/dao/trip/trip.dao.js +502 -0
  27. package/dist/dao/trip/trip.dao.js.map +1 -0
  28. package/dist/dao/user/user.dao.d.ts +74 -0
  29. package/dist/dao/user/user.dao.js +287 -0
  30. package/dist/dao/user/user.dao.js.map +1 -0
  31. package/dist/index.d.ts +19 -0
  32. package/dist/index.js +26 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/interfaces/driver-tracking/driver-tracking.interfaces.d.ts +102 -0
  35. package/dist/interfaces/driver-tracking/driver-tracking.interfaces.js +3 -0
  36. package/dist/interfaces/driver-tracking/driver-tracking.interfaces.js.map +1 -0
  37. package/dist/interfaces/gps-location/gps-location.interfaces.d.ts +30 -0
  38. package/dist/interfaces/gps-location/gps-location.interfaces.js +3 -0
  39. package/dist/interfaces/gps-location/gps-location.interfaces.js.map +1 -0
  40. package/dist/interfaces/role/role.interfaces.d.ts +8 -0
  41. package/dist/interfaces/role/role.interfaces.js +3 -0
  42. package/dist/interfaces/role/role.interfaces.js.map +1 -0
  43. package/dist/interfaces/route/route.interfaces.d.ts +23 -0
  44. package/dist/interfaces/route/route.interfaces.js +3 -0
  45. package/dist/interfaces/route/route.interfaces.js.map +1 -0
  46. package/dist/interfaces/route-assignment/route-assignment.interfaces.d.ts +18 -0
  47. package/dist/interfaces/route-assignment/route-assignment.interfaces.js +3 -0
  48. package/dist/interfaces/route-assignment/route-assignment.interfaces.js.map +1 -0
  49. package/dist/interfaces/sundays-package-version/sundays-package-version.interfaces.d.ts +6 -0
  50. package/dist/interfaces/sundays-package-version/sundays-package-version.interfaces.js +3 -0
  51. package/dist/interfaces/sundays-package-version/sundays-package-version.interfaces.js.map +1 -0
  52. package/dist/interfaces/trip/trip.interfaces.d.ts +46 -0
  53. package/dist/interfaces/trip/trip.interfaces.js +3 -0
  54. package/dist/interfaces/trip/trip.interfaces.js.map +1 -0
  55. package/dist/interfaces/user/user.interfaces.d.ts +25 -0
  56. package/dist/interfaces/user/user.interfaces.js +3 -0
  57. package/dist/interfaces/user/user.interfaces.js.map +1 -0
  58. package/package.json +52 -0
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SundaysPackageVersionDAO = void 0;
16
+ const KnexConnection_1 = __importDefault(require("../../KnexConnection"));
17
+ class SundaysPackageVersionDAO {
18
+ constructor() {
19
+ this._knex = KnexConnection_1.default.getConnection();
20
+ }
21
+ create(item) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const [created] = yield this._knex("sundays_package_version")
24
+ .insert(item)
25
+ .returning("*");
26
+ return created;
27
+ });
28
+ }
29
+ getById(id) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const result = yield this._knex("sundays_package_version")
32
+ .select("*")
33
+ .where("id", id)
34
+ .first();
35
+ return result || null;
36
+ });
37
+ }
38
+ update(id, item) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const [updated] = yield this._knex("sundays_package_version")
41
+ .where({ id })
42
+ .update(item)
43
+ .returning("*");
44
+ return updated || null;
45
+ });
46
+ }
47
+ delete(id) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const result = yield this._knex("sundays_package_version")
50
+ .where({ id })
51
+ .del();
52
+ return result > 0;
53
+ });
54
+ }
55
+ getAll(page, limit) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const offset = (page - 1) * limit;
58
+ const query = this._knex("sundays_package_version").select("*");
59
+ const [countResult] = yield query.clone().clearSelect().count("* as count");
60
+ const totalCount = +countResult.count;
61
+ const data = yield query
62
+ .clone()
63
+ .limit(limit)
64
+ .offset(offset)
65
+ .orderBy("id", "desc");
66
+ return {
67
+ success: true,
68
+ data,
69
+ page,
70
+ limit,
71
+ count: data.length,
72
+ totalCount,
73
+ totalPages: Math.ceil(totalCount / limit),
74
+ };
75
+ });
76
+ }
77
+ getLatestVersion() {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const result = yield this._knex("sundays_package_version")
80
+ .select("*")
81
+ .orderBy("id", "desc")
82
+ .first();
83
+ return result || null;
84
+ });
85
+ }
86
+ }
87
+ exports.SundaysPackageVersionDAO = SundaysPackageVersionDAO;
88
+ //# sourceMappingURL=sundays-package-version.dao.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sundays-package-version.dao.js","sourceRoot":"","sources":["../../../src/dao/sundays-package-version/sundays-package-version.dao.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,0EAA+C;AAE/C,MAAa,wBAAwB;IAArC;QAGU,UAAK,GAAyB,wBAAW,CAAC,aAAa,EAAE,CAAC;IAqEpE,CAAC;IAnEO,MAAM,CAAC,IAA4B;;YACvC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC;iBAC1D,MAAM,CAAC,IAAI,CAAC;iBACZ,SAAS,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,OAAO,CAAC;QACjB,CAAC;KAAA;IAEK,OAAO,CAAC,EAAU;;YACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC;iBACvD,MAAM,CAAC,GAAG,CAAC;iBACX,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;iBACf,KAAK,EAAE,CAAC;YACX,OAAO,MAAM,IAAI,IAAI,CAAC;QACxB,CAAC;KAAA;IAEK,MAAM,CACV,EAAU,EACV,IAAqC;;YAErC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC;iBAC1D,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;iBACb,MAAM,CAAC,IAAI,CAAC;iBACZ,SAAS,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,OAAO,IAAI,IAAI,CAAC;QACzB,CAAC;KAAA;IAEK,MAAM,CAAC,EAAU;;YACrB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC;iBACvD,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;iBACb,GAAG,EAAE,CAAC;YACT,OAAO,MAAM,GAAG,CAAC,CAAC;QACpB,CAAC;KAAA;IAEK,MAAM,CACV,IAAY,EACZ,KAAa;;YAEb,MAAM,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;YAElC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAEhE,MAAM,CAAC,WAAW,CAAC,GAAG,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YAC5E,MAAM,UAAU,GAAG,CAAC,WAAW,CAAC,KAAK,CAAC;YACtC,MAAM,IAAI,GAAG,MAAM,KAAK;iBACrB,KAAK,EAAE;iBACP,KAAK,CAAC,KAAK,CAAC;iBACZ,MAAM,CAAC,MAAM,CAAC;iBACd,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YAEzB,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,IAAI;gBACJ,IAAI;gBACJ,KAAK;gBACL,KAAK,EAAE,IAAI,CAAC,MAAM;gBAClB,UAAU;gBACV,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;aAC1C,CAAC;QACJ,CAAC;KAAA;IAEK,gBAAgB;;YACpB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC;iBACvD,MAAM,CAAC,GAAG,CAAC;iBACX,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;iBACrB,KAAK,EAAE,CAAC;YACX,OAAO,MAAM,IAAI,IAAI,CAAC;QACxB,CAAC;KAAA;CACF;AAxED,4DAwEC"}
@@ -0,0 +1,102 @@
1
+ import { ITrip, ITripWithStops, ITripStop } from "../../interfaces/trip/trip.interfaces";
2
+ import { IBaseDAO, IDataPaginator } from "../../d.types";
3
+ export declare class TripDAO implements IBaseDAO<ITrip> {
4
+ private _knex?;
5
+ private get knex();
6
+ /**
7
+ * Parse JSON fields from database
8
+ */
9
+ private parseTripJson;
10
+ /**
11
+ * Base query with all joins (without stops)
12
+ */
13
+ private baseQuery;
14
+ /**
15
+ * Get all trips with pagination
16
+ */
17
+ getAll(page?: number, limit?: number): Promise<IDataPaginator<ITrip>>;
18
+ /**
19
+ * Get trip by ID
20
+ */
21
+ getById(id: number): Promise<ITrip | null>;
22
+ /**
23
+ * Get trip by UUID
24
+ */
25
+ getByUuid(uuid: string): Promise<ITrip | null>;
26
+ /**
27
+ * Get trip by UUID with stops
28
+ */
29
+ getByUuidWithStops(uuid: string): Promise<ITripWithStops | null>;
30
+ /**
31
+ * Get trips by user ID with pagination
32
+ */
33
+ getByUserId(userId: number, page?: number, limit?: number): Promise<IDataPaginator<ITrip>>;
34
+ /**
35
+ * Get trips by assignment ID
36
+ */
37
+ getByAssignmentId(assignmentId: number, page?: number, limit?: number): Promise<IDataPaginator<ITrip>>;
38
+ /**
39
+ * Get today's trips for a user
40
+ */
41
+ getTodaysTrips(userId: number): Promise<ITripWithStops[]>;
42
+ /**
43
+ * Get trips by status
44
+ */
45
+ getByStatus(status: string, page?: number, limit?: number): Promise<IDataPaginator<ITrip>>;
46
+ /**
47
+ * Create a new trip
48
+ */
49
+ create(data: ITrip): Promise<ITrip>;
50
+ /**
51
+ * Update a trip by ID
52
+ */
53
+ update(id: number, data: Partial<ITrip>): Promise<ITrip | null>;
54
+ /**
55
+ * Delete a trip by ID
56
+ */
57
+ delete(id: number): Promise<boolean>;
58
+ /**
59
+ * Start a trip
60
+ */
61
+ startTrip(id: number): Promise<ITrip | null>;
62
+ /**
63
+ * Complete a trip
64
+ */
65
+ completeTrip(id: number): Promise<ITrip | null>;
66
+ /**
67
+ * Cancel a trip
68
+ */
69
+ cancelTrip(id: number): Promise<ITrip | null>;
70
+ /**
71
+ * Get all stops for a trip
72
+ */
73
+ getTripStops(tripId: number): Promise<ITripStop[]>;
74
+ /**
75
+ * Get stop by ID
76
+ */
77
+ getStopById(stopId: number): Promise<ITripStop | null>;
78
+ /**
79
+ * Create a trip stop
80
+ */
81
+ createStop(data: ITripStop): Promise<ITripStop>;
82
+ /**
83
+ * Update a trip stop
84
+ */
85
+ updateStop(stopId: number, data: Partial<ITripStop>): Promise<ITripStop | null>;
86
+ /**
87
+ * Delete a trip stop
88
+ */
89
+ deleteStop(stopId: number): Promise<boolean>;
90
+ /**
91
+ * Mark stop as arrived
92
+ */
93
+ arriveAtStop(stopId: number): Promise<ITripStop | null>;
94
+ /**
95
+ * Mark stop as completed
96
+ */
97
+ completeStop(stopId: number): Promise<ITripStop | null>;
98
+ /**
99
+ * Mark stop as skipped
100
+ */
101
+ skipStop(stopId: number): Promise<ITripStop | null>;
102
+ }
@@ -0,0 +1,502 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.TripDAO = void 0;
16
+ const KnexConnection_1 = __importDefault(require("../../KnexConnection"));
17
+ class TripDAO {
18
+ get knex() {
19
+ if (!this._knex) {
20
+ this._knex = KnexConnection_1.default.getConnection();
21
+ }
22
+ return this._knex;
23
+ }
24
+ /**
25
+ * Parse JSON fields from database
26
+ */
27
+ parseTripJson(trip) {
28
+ var _a, _b, _c;
29
+ // Parse route JSON if present
30
+ if (trip.route && typeof trip.route === "string") {
31
+ trip.route = JSON.parse(trip.route);
32
+ }
33
+ if (((_a = trip.route) === null || _a === void 0 ? void 0 : _a.geojson) && typeof trip.route.geojson === "string") {
34
+ trip.route.geojson = JSON.parse(trip.route.geojson);
35
+ }
36
+ if (((_b = trip.route) === null || _b === void 0 ? void 0 : _b.custom_instructions) &&
37
+ typeof trip.route.custom_instructions === "string") {
38
+ trip.route.custom_instructions = JSON.parse(trip.route.custom_instructions);
39
+ }
40
+ // Parse user JSON if present
41
+ if (trip.user && typeof trip.user === "string") {
42
+ trip.user = JSON.parse(trip.user);
43
+ }
44
+ if (((_c = trip.user) === null || _c === void 0 ? void 0 : _c.role) && typeof trip.user.role === "string") {
45
+ trip.user.role = JSON.parse(trip.user.role);
46
+ }
47
+ // Parse assignment JSON if present
48
+ if (trip.assignment && typeof trip.assignment === "string") {
49
+ trip.assignment = JSON.parse(trip.assignment);
50
+ }
51
+ // Parse manifest_data if present
52
+ if (trip.manifest_data && typeof trip.manifest_data === "string") {
53
+ trip.manifest_data = JSON.parse(trip.manifest_data);
54
+ }
55
+ // Parse stops array if present
56
+ if (trip.stops && typeof trip.stops === "string") {
57
+ trip.stops = JSON.parse(trip.stops);
58
+ }
59
+ return trip;
60
+ }
61
+ /**
62
+ * Base query with all joins (without stops)
63
+ */
64
+ baseQuery() {
65
+ return this.knex("trips as t")
66
+ .leftJoin("routes as r", "t.route_id", "r.id")
67
+ .leftJoin("users as u", "t.user_id", "u.id")
68
+ .leftJoin("roles as ur", "u.role_id", "ur.id")
69
+ .leftJoin("route_assignments as ra", "t.assignment_id", "ra.id")
70
+ .select("t.*", this.knex.raw("to_jsonb(r.*) as route"), this.knex.raw(`jsonb_build_object(
71
+ 'id', u.id,
72
+ 'uuid', u.uuid,
73
+ 'email', u.email,
74
+ 'username', u.username,
75
+ 'first_name', u.first_name,
76
+ 'last_name', u.last_name,
77
+ 'role_id', u.role_id,
78
+ 'is_active', u.is_active,
79
+ 'role', to_jsonb(ur.*)
80
+ ) as user`), this.knex.raw("to_jsonb(ra.*) as assignment"));
81
+ }
82
+ /**
83
+ * Get all trips with pagination
84
+ */
85
+ getAll() {
86
+ return __awaiter(this, arguments, void 0, function* (page = 1, limit = 10) {
87
+ const offset = (page - 1) * limit;
88
+ // Get paginated data
89
+ const rawData = yield this.baseQuery()
90
+ .limit(limit)
91
+ .offset(offset)
92
+ .orderBy("t.scheduled_start", "desc");
93
+ // Parse JSON fields
94
+ const data = rawData.map((trip) => this.parseTripJson(trip));
95
+ // Get total count
96
+ const totalCountResult = yield this.knex("trips")
97
+ .count("* as count")
98
+ .first();
99
+ const totalCount = Number((totalCountResult === null || totalCountResult === void 0 ? void 0 : totalCountResult.count) || 0);
100
+ const count = data.length;
101
+ const totalPages = Math.ceil(totalCount / limit);
102
+ return {
103
+ success: true,
104
+ data,
105
+ page,
106
+ limit,
107
+ count,
108
+ totalCount,
109
+ totalPages,
110
+ };
111
+ });
112
+ }
113
+ /**
114
+ * Get trip by ID
115
+ */
116
+ getById(id) {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ const result = yield this.baseQuery().where("t.id", id).first();
119
+ return result ? this.parseTripJson(result) : null;
120
+ });
121
+ }
122
+ /**
123
+ * Get trip by UUID
124
+ */
125
+ getByUuid(uuid) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const result = yield this.baseQuery().where("t.uuid", uuid).first();
128
+ return result ? this.parseTripJson(result) : null;
129
+ });
130
+ }
131
+ /**
132
+ * Get trip by UUID with stops
133
+ */
134
+ getByUuidWithStops(uuid) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ const trip = yield this.getByUuid(uuid);
137
+ if (!trip)
138
+ return null;
139
+ // Get stops for this trip
140
+ const stops = yield this.knex("trip_stops")
141
+ .where("trip_id", trip.id)
142
+ .orderBy("stop_order", "asc");
143
+ return Object.assign(Object.assign({}, trip), { stops });
144
+ });
145
+ }
146
+ /**
147
+ * Get trips by user ID with pagination
148
+ */
149
+ getByUserId(userId_1) {
150
+ return __awaiter(this, arguments, void 0, function* (userId, page = 1, limit = 10) {
151
+ const offset = (page - 1) * limit;
152
+ // Get paginated data
153
+ const rawData = yield this.baseQuery()
154
+ .where("t.user_id", userId)
155
+ .limit(limit)
156
+ .offset(offset)
157
+ .orderBy("t.scheduled_start", "desc");
158
+ // Parse JSON fields
159
+ const data = rawData.map((trip) => this.parseTripJson(trip));
160
+ // Get total count
161
+ const totalCountResult = yield this.knex("trips")
162
+ .where("user_id", userId)
163
+ .count("* as count")
164
+ .first();
165
+ const totalCount = Number((totalCountResult === null || totalCountResult === void 0 ? void 0 : totalCountResult.count) || 0);
166
+ const count = data.length;
167
+ const totalPages = Math.ceil(totalCount / limit);
168
+ return {
169
+ success: true,
170
+ data,
171
+ page,
172
+ limit,
173
+ count,
174
+ totalCount,
175
+ totalPages,
176
+ };
177
+ });
178
+ }
179
+ /**
180
+ * Get trips by assignment ID
181
+ */
182
+ getByAssignmentId(assignmentId_1) {
183
+ return __awaiter(this, arguments, void 0, function* (assignmentId, page = 1, limit = 10) {
184
+ const offset = (page - 1) * limit;
185
+ // Get paginated data
186
+ const rawData = yield this.baseQuery()
187
+ .where("t.assignment_id", assignmentId)
188
+ .limit(limit)
189
+ .offset(offset)
190
+ .orderBy("t.scheduled_start", "asc");
191
+ // Parse JSON fields
192
+ const data = rawData.map((trip) => this.parseTripJson(trip));
193
+ // Get total count
194
+ const totalCountResult = yield this.knex("trips")
195
+ .where("assignment_id", assignmentId)
196
+ .count("* as count")
197
+ .first();
198
+ const totalCount = Number((totalCountResult === null || totalCountResult === void 0 ? void 0 : totalCountResult.count) || 0);
199
+ const count = data.length;
200
+ const totalPages = Math.ceil(totalCount / limit);
201
+ return {
202
+ success: true,
203
+ data,
204
+ page,
205
+ limit,
206
+ count,
207
+ totalCount,
208
+ totalPages,
209
+ };
210
+ });
211
+ }
212
+ /**
213
+ * Get today's trips for a user
214
+ */
215
+ getTodaysTrips(userId) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ const today = new Date();
218
+ today.setHours(0, 0, 0, 0);
219
+ const tomorrow = new Date(today);
220
+ tomorrow.setDate(tomorrow.getDate() + 1);
221
+ const rawData = yield this.baseQuery()
222
+ .where("t.user_id", userId)
223
+ .whereBetween("t.scheduled_start", [today, tomorrow])
224
+ .orderBy("t.scheduled_start", "asc");
225
+ const trips = rawData.map((trip) => this.parseTripJson(trip));
226
+ // Get stops for all trips
227
+ const tripsWithStops = yield Promise.all(trips.map((trip) => __awaiter(this, void 0, void 0, function* () {
228
+ const stops = yield this.knex("trip_stops")
229
+ .where("trip_id", trip.id)
230
+ .orderBy("stop_order", "asc");
231
+ return Object.assign(Object.assign({}, trip), { stops });
232
+ })));
233
+ return tripsWithStops;
234
+ });
235
+ }
236
+ /**
237
+ * Get trips by status
238
+ */
239
+ getByStatus(status_1) {
240
+ return __awaiter(this, arguments, void 0, function* (status, page = 1, limit = 10) {
241
+ const offset = (page - 1) * limit;
242
+ // Get paginated data
243
+ const rawData = yield this.baseQuery()
244
+ .where("t.status", status)
245
+ .limit(limit)
246
+ .offset(offset)
247
+ .orderBy("t.scheduled_start", "desc");
248
+ // Parse JSON fields
249
+ const data = rawData.map((trip) => this.parseTripJson(trip));
250
+ // Get total count
251
+ const totalCountResult = yield this.knex("trips")
252
+ .where("status", status)
253
+ .count("* as count")
254
+ .first();
255
+ const totalCount = Number((totalCountResult === null || totalCountResult === void 0 ? void 0 : totalCountResult.count) || 0);
256
+ const count = data.length;
257
+ const totalPages = Math.ceil(totalCount / limit);
258
+ return {
259
+ success: true,
260
+ data,
261
+ page,
262
+ limit,
263
+ count,
264
+ totalCount,
265
+ totalPages,
266
+ };
267
+ });
268
+ }
269
+ /**
270
+ * Create a new trip
271
+ */
272
+ create(data) {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ const [result] = yield this.knex("trips")
275
+ .insert({
276
+ uuid: data.uuid,
277
+ assignment_id: data.assignment_id,
278
+ user_id: data.user_id,
279
+ route_id: data.route_id,
280
+ scheduled_start: data.scheduled_start,
281
+ scheduled_end: data.scheduled_end,
282
+ status: data.status || "pending",
283
+ manifest_data: data.manifest_data
284
+ ? JSON.stringify(data.manifest_data)
285
+ : null,
286
+ notes: data.notes,
287
+ })
288
+ .returning("*");
289
+ // Fetch with joins
290
+ return this.getById(result.id);
291
+ });
292
+ }
293
+ /**
294
+ * Update a trip by ID
295
+ */
296
+ update(id, data) {
297
+ return __awaiter(this, void 0, void 0, function* () {
298
+ const updateData = {};
299
+ if (data.scheduled_start !== undefined)
300
+ updateData.scheduled_start = data.scheduled_start;
301
+ if (data.scheduled_end !== undefined)
302
+ updateData.scheduled_end = data.scheduled_end;
303
+ if (data.actual_start !== undefined)
304
+ updateData.actual_start = data.actual_start;
305
+ if (data.actual_end !== undefined)
306
+ updateData.actual_end = data.actual_end;
307
+ if (data.status !== undefined)
308
+ updateData.status = data.status;
309
+ if (data.manifest_data !== undefined)
310
+ updateData.manifest_data = JSON.stringify(data.manifest_data);
311
+ if (data.distance_traveled !== undefined)
312
+ updateData.distance_traveled = data.distance_traveled;
313
+ if (data.duration_minutes !== undefined)
314
+ updateData.duration_minutes = data.duration_minutes;
315
+ if (data.notes !== undefined)
316
+ updateData.notes = data.notes;
317
+ updateData.updated_at = new Date();
318
+ const [result] = yield this.knex("trips")
319
+ .where("id", id)
320
+ .update(updateData)
321
+ .returning("*");
322
+ return result ? this.getById(result.id) : null;
323
+ });
324
+ }
325
+ /**
326
+ * Delete a trip by ID
327
+ */
328
+ delete(id) {
329
+ return __awaiter(this, void 0, void 0, function* () {
330
+ const result = yield this.knex("trips").where("id", id).delete();
331
+ return result > 0;
332
+ });
333
+ }
334
+ /**
335
+ * Start a trip
336
+ */
337
+ startTrip(id) {
338
+ return __awaiter(this, void 0, void 0, function* () {
339
+ return this.update(id, {
340
+ status: "in_progress",
341
+ actual_start: new Date(),
342
+ });
343
+ });
344
+ }
345
+ /**
346
+ * Complete a trip
347
+ */
348
+ completeTrip(id) {
349
+ return __awaiter(this, void 0, void 0, function* () {
350
+ const trip = yield this.getById(id);
351
+ if (!trip)
352
+ return null;
353
+ // Calculate duration if actual_start exists
354
+ let duration_minutes;
355
+ if (trip.actual_start) {
356
+ const start = new Date(trip.actual_start);
357
+ const end = new Date();
358
+ duration_minutes = Math.round((end.getTime() - start.getTime()) / 1000 / 60);
359
+ }
360
+ return this.update(id, {
361
+ status: "completed",
362
+ actual_end: new Date(),
363
+ duration_minutes,
364
+ });
365
+ });
366
+ }
367
+ /**
368
+ * Cancel a trip
369
+ */
370
+ cancelTrip(id) {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ return this.update(id, {
373
+ status: "cancelled",
374
+ });
375
+ });
376
+ }
377
+ // ========== TRIP STOPS METHODS ==========
378
+ /**
379
+ * Get all stops for a trip
380
+ */
381
+ getTripStops(tripId) {
382
+ return __awaiter(this, void 0, void 0, function* () {
383
+ return this.knex("trip_stops")
384
+ .where("trip_id", tripId)
385
+ .orderBy("stop_order", "asc");
386
+ });
387
+ }
388
+ /**
389
+ * Get stop by ID
390
+ */
391
+ getStopById(stopId) {
392
+ return __awaiter(this, void 0, void 0, function* () {
393
+ const result = yield this.knex("trip_stops").where("id", stopId).first();
394
+ return result || null;
395
+ });
396
+ }
397
+ /**
398
+ * Create a trip stop
399
+ */
400
+ createStop(data) {
401
+ return __awaiter(this, void 0, void 0, function* () {
402
+ const [result] = yield this.knex("trip_stops")
403
+ .insert({
404
+ uuid: data.uuid,
405
+ trip_id: data.trip_id,
406
+ stop_order: data.stop_order,
407
+ latitude: data.latitude,
408
+ longitude: data.longitude,
409
+ address: data.address,
410
+ name: data.name,
411
+ scheduled_time: data.scheduled_time,
412
+ status: data.status || "pending",
413
+ instructions: data.instructions,
414
+ metadata: data.metadata ? JSON.stringify(data.metadata) : null,
415
+ notes: data.notes,
416
+ })
417
+ .returning("*");
418
+ return result;
419
+ });
420
+ }
421
+ /**
422
+ * Update a trip stop
423
+ */
424
+ updateStop(stopId, data) {
425
+ return __awaiter(this, void 0, void 0, function* () {
426
+ const updateData = {};
427
+ if (data.stop_order !== undefined)
428
+ updateData.stop_order = data.stop_order;
429
+ if (data.latitude !== undefined)
430
+ updateData.latitude = data.latitude;
431
+ if (data.longitude !== undefined)
432
+ updateData.longitude = data.longitude;
433
+ if (data.address !== undefined)
434
+ updateData.address = data.address;
435
+ if (data.name !== undefined)
436
+ updateData.name = data.name;
437
+ if (data.scheduled_time !== undefined)
438
+ updateData.scheduled_time = data.scheduled_time;
439
+ if (data.actual_arrival_time !== undefined)
440
+ updateData.actual_arrival_time = data.actual_arrival_time;
441
+ if (data.actual_departure_time !== undefined)
442
+ updateData.actual_departure_time = data.actual_departure_time;
443
+ if (data.status !== undefined)
444
+ updateData.status = data.status;
445
+ if (data.instructions !== undefined)
446
+ updateData.instructions = data.instructions;
447
+ if (data.metadata !== undefined)
448
+ updateData.metadata = JSON.stringify(data.metadata);
449
+ if (data.notes !== undefined)
450
+ updateData.notes = data.notes;
451
+ updateData.updated_at = new Date();
452
+ const [result] = yield this.knex("trip_stops")
453
+ .where("id", stopId)
454
+ .update(updateData)
455
+ .returning("*");
456
+ return result || null;
457
+ });
458
+ }
459
+ /**
460
+ * Delete a trip stop
461
+ */
462
+ deleteStop(stopId) {
463
+ return __awaiter(this, void 0, void 0, function* () {
464
+ const result = yield this.knex("trip_stops").where("id", stopId).delete();
465
+ return result > 0;
466
+ });
467
+ }
468
+ /**
469
+ * Mark stop as arrived
470
+ */
471
+ arriveAtStop(stopId) {
472
+ return __awaiter(this, void 0, void 0, function* () {
473
+ return this.updateStop(stopId, {
474
+ status: "arrived",
475
+ actual_arrival_time: new Date(),
476
+ });
477
+ });
478
+ }
479
+ /**
480
+ * Mark stop as completed
481
+ */
482
+ completeStop(stopId) {
483
+ return __awaiter(this, void 0, void 0, function* () {
484
+ return this.updateStop(stopId, {
485
+ status: "completed",
486
+ actual_departure_time: new Date(),
487
+ });
488
+ });
489
+ }
490
+ /**
491
+ * Mark stop as skipped
492
+ */
493
+ skipStop(stopId) {
494
+ return __awaiter(this, void 0, void 0, function* () {
495
+ return this.updateStop(stopId, {
496
+ status: "skipped",
497
+ });
498
+ });
499
+ }
500
+ }
501
+ exports.TripDAO = TripDAO;
502
+ //# sourceMappingURL=trip.dao.js.map