goldstars-services 1.0.45 → 1.0.49

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.
@@ -7,54 +7,67 @@ exports.default = void 0;
7
7
  var _reservationModel = _interopRequireDefault(require("../models/reservation.model.js"));
8
8
  var _bikeModel = _interopRequireDefault(require("../models/bike.model.js"));
9
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
11
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
12
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
14
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
10
15
  function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
11
16
  function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } // MicroService/src/services/reservation.service.js
12
17
  // Needed to check bike status/location
13
18
 
14
19
  /**
15
- * Checks if a bike is already reserved for a specific date and time slot.
20
+ * Checks if a bike is already reserved for a specific timestamp.
16
21
  * @param {string} bikeId - The ID of the bike.
17
- * @param {Date} date - The reservation date.
18
- * @param {string} timeSlot - The reservation time slot.
22
+ * @param {Date} timestamp - The reservation timestamp (UTC Date object).
19
23
  * @returns {Promise<boolean>} - True if the bike is already reserved, false otherwise.
20
24
  */
21
25
  var isBikeReserved = /*#__PURE__*/function () {
22
- var _ref = _asyncToGenerator(function* (bikeId, date, timeSlot) {
26
+ var _ref = _asyncToGenerator(function* (bikeId, timestamp) {
27
+ // Ensure timestamp is treated as a Date object if it comes as a string
28
+ var searchTimestamp = new Date(timestamp);
29
+ if (isNaN(searchTimestamp.getTime())) {
30
+ throw new Error('Timestamp inválido proporcionado a isBikeReserved.');
31
+ }
23
32
  var existingReservation = yield _reservationModel.default.findOne({
24
33
  bikeId,
25
- date,
26
- timeSlot
34
+ timestamp: searchTimestamp // Use the timestamp directly
27
35
  // Optional: Add status check if cancelled reservations should allow re-booking
28
36
  // status: 'confirmed',
29
37
  });
30
38
  return !!existingReservation;
31
39
  });
32
- return function isBikeReserved(_x, _x2, _x3) {
40
+ return function isBikeReserved(_x, _x2) {
33
41
  return _ref.apply(this, arguments);
34
42
  };
35
43
  }();
36
44
 
37
45
  /**
38
- * Creates a new reservation.
46
+ * Creates a new reservation using a timestamp.
39
47
  * @param {object} reservationData - Data for the new reservation.
40
48
  * @param {string} reservationData.userId - User ID.
41
49
  * @param {string} reservationData.bikeId - Bike ID.
42
- * @param {Date} reservationData.date - Reservation date.
43
- * @param {string} reservationData.timeSlot - Reservation time slot.
50
+ * @param {Date|string} reservationData.timestamp - Reservation timestamp (UTC Date object or valid date string).
44
51
  * @param {string} reservationData.location - Reservation location.
45
52
  * @returns {Promise<object>} - The created reservation document.
46
- * @throws {Error} - If bike not found, is instructor bike, or already reserved.
53
+ * @throws {Error} - If bike not found, is instructor bike, timestamp invalid, or already reserved.
47
54
  */
48
55
  var createReservation = /*#__PURE__*/function () {
49
56
  var _ref2 = _asyncToGenerator(function* (reservationData) {
57
+ // Destructure timestamp, remove date and timeSlot
50
58
  var {
51
59
  userId,
52
60
  bikeId,
53
- date,
54
- timeSlot,
61
+ timestamp,
55
62
  location
56
63
  } = reservationData;
57
64
 
65
+ // Validate timestamp
66
+ var reservationTimestamp = new Date(timestamp);
67
+ if (isNaN(reservationTimestamp.getTime())) {
68
+ throw new Error('Timestamp inválido proporcionado para la reserva.');
69
+ }
70
+
58
71
  // 1. Validate Bike
59
72
  var bike = yield _bikeModel.default.findById(bikeId);
60
73
  if (!bike) {
@@ -63,29 +76,28 @@ var createReservation = /*#__PURE__*/function () {
63
76
  if (bike.isInstructorBike) {
64
77
  throw new Error('La bicicleta del instructor no se puede reservar.');
65
78
  }
66
- // Ensure the bike belongs to the specified location (optional but good practice)
67
79
  if (bike.location !== location) {
68
80
  throw new Error("La bicicleta ".concat(bike.name, " no pertenece a la ubicaci\xF3n ").concat(location, "."));
69
81
  }
70
82
 
71
- // 2. Check if already reserved
72
- var alreadyReserved = yield isBikeReserved(bikeId, date, timeSlot);
83
+ // 2. Check if already reserved using timestamp
84
+ var alreadyReserved = yield isBikeReserved(bikeId, reservationTimestamp);
73
85
  if (alreadyReserved) {
74
86
  throw new Error('Esta bicicleta ya está reservada para esta fecha y hora.');
75
87
  }
76
88
 
77
- // 3. Create Reservation (Add userId validation if needed)
89
+ // 3. Create Reservation using timestamp
78
90
  var newReservation = new _reservationModel.default({
79
91
  userId,
80
92
  bikeId,
81
- date,
82
- timeSlot,
93
+ timestamp: reservationTimestamp,
94
+ // Use timestamp
83
95
  location
84
96
  });
85
97
  yield newReservation.save();
86
98
  return newReservation;
87
99
  });
88
- return function createReservation(_x4) {
100
+ return function createReservation(_x3) {
89
101
  return _ref2.apply(this, arguments);
90
102
  };
91
103
  }();
@@ -97,70 +109,76 @@ var createReservation = /*#__PURE__*/function () {
97
109
  * @returns {Promise<Array<object>>} - An array of bike objects, each with an added 'reservationStatus' field.
98
110
  */
99
111
  // Comentado el código existente para simplificar la función y probar la exportación
100
- /*
101
- const getBikeAvailabilityForSlot = async (location, date) => {
102
- // 1. Find all bikes for the given location
103
- const bikesInLocation = await Bike.find({ location }).lean(); // .lean() for plain JS objects
104
-
105
- // 2. Find all reservations for the specific date
106
- const reservations = await Reservation.find({
107
- date,
108
- location, // Filter by location as well for efficiency
109
- // Optional: Add status filter if needed
110
- // status: 'confirmed',
111
- })
112
- .populate('userId', 'name email') // Populate user name/email for admin view
113
- .lean();
114
-
115
- // 3. Create a map of reserved bike IDs for quick lookup
116
- const reservedBikeMap = new Map();
117
- reservations.forEach(res => {
118
- // Store the user info (or just true if user info isn't needed/allowed)
119
- reservedBikeMap.set(res.bikeId.toString(), res.userId); // Store populated user object
120
- });
121
-
122
- // 4. Combine bike data with reservation status
123
- const bikesWithStatus = bikesInLocation.map(bike => {
124
- const bikeIdStr = bike._id.toString();
125
- const reservedByUser = reservedBikeMap.get(bikeIdStr);
126
- let reservationStatus;
127
-
128
- if (bike.isInstructorBike) {
129
- reservationStatus = { status: 'instructor', reservedBy: null };
130
- } else if (reservedByUser) {
131
- // For admin: include user details. For regular user, this might be simplified later.
132
- reservationStatus = { status: 'agendada', reservedBy: reservedByUser };
133
- } else if (bike.status === 'Disponible') { // Check the bike's general status
134
- reservationStatus = { status: 'disponible', reservedBy: null };
135
- } else {
136
- // If bike's general status is 'No Disponible' or something else
137
- reservationStatus = { status: 'no_disponible_general', reservedBy: null };
138
- }
139
-
140
- return {
141
- ...bike, // Spread original bike data
142
- reservationStatus, // Add the specific status for this slot
143
- };
144
- });
145
-
146
- return bikesWithStatus;
147
- };
148
- */
149
112
 
150
- // Nueva implementación simplificada para probar la exportación
151
113
  var getBikeAvailabilityForSlot = /*#__PURE__*/function () {
152
114
  var _ref3 = _asyncToGenerator(function* (location, date) {
153
- return {
154
- message: "Function is being called successfully",
155
- location,
156
- date
157
- };
115
+ // 1. Find all bikes for the given location
116
+ var bikesInLocation = yield _bikeModel.default.find({
117
+ location
118
+ }).lean(); // .lean() for plain JS objects
119
+
120
+ // 2. Find all reservations for the specific date
121
+ var reservations = yield _reservationModel.default.find({
122
+ date,
123
+ location // Filter by location as well for efficiency
124
+ // Optional: Add status filter if needed
125
+ // status: 'confirmed',
126
+ }).populate('userId', 'name email') // Populate user name/email for admin view
127
+ .lean();
128
+
129
+ // 3. Create a map of reserved bike IDs for quick lookup
130
+ var reservedBikeMap = new Map();
131
+ reservations.forEach(res => {
132
+ // Store the user info (or just true if user info isn't needed/allowed)
133
+ reservedBikeMap.set(res.bikeId.toString(), res.userId); // Store populated user object
134
+ });
135
+
136
+ // 4. Combine bike data with reservation status
137
+ var bikesWithStatus = bikesInLocation.map(bike => {
138
+ var bikeIdStr = bike._id.toString();
139
+ var reservedByUser = reservedBikeMap.get(bikeIdStr);
140
+ var reservationStatus;
141
+ if (bike.isInstructorBike) {
142
+ reservationStatus = {
143
+ status: 'instructor',
144
+ reservedBy: null
145
+ };
146
+ } else if (reservedByUser) {
147
+ // For admin: include user details. For regular user, this might be simplified later.
148
+ reservationStatus = {
149
+ status: 'agendada',
150
+ reservedBy: reservedByUser
151
+ };
152
+ } else if (bike.status === 'Disponible') {
153
+ // Check the bike's general status
154
+ reservationStatus = {
155
+ status: 'disponible',
156
+ reservedBy: null
157
+ };
158
+ } else {
159
+ // If bike's general status is 'No Disponible' or something else
160
+ reservationStatus = {
161
+ status: 'no_disponible_general',
162
+ reservedBy: null
163
+ };
164
+ }
165
+ return _objectSpread(_objectSpread({}, bike), {}, {
166
+ // Spread original bike data
167
+ reservationStatus // Add the specific status for this slot
168
+ });
169
+ });
170
+ return bikesWithStatus;
158
171
  });
159
- return function getBikeAvailabilityForSlot(_x5, _x6) {
172
+ return function getBikeAvailabilityForSlot(_x4, _x5) {
160
173
  return _ref3.apply(this, arguments);
161
174
  };
162
175
  }();
163
176
 
177
+ // Nueva implementación simplificada para probar la exportación
178
+ /* const getBikeAvailabilityForSlot = async (location, date) => {
179
+ return { message: "Function is being called successfully", location, date };
180
+ };
181
+ */
164
182
  // Optional: Add functions for cancelling or fetching user-specific reservations later
165
183
  /**
166
184
  * Cancels a reservation.
@@ -191,7 +209,7 @@ var cancelReservation = /*#__PURE__*/function () {
191
209
  message: 'Reserva cancelada exitosamente.'
192
210
  };
193
211
  });
194
- return function cancelReservation(_x7, _x8, _x9) {
212
+ return function cancelReservation(_x6, _x7, _x8) {
195
213
  return _ref4.apply(this, arguments);
196
214
  };
197
215
  }();
@@ -199,5 +217,5 @@ var _default = exports.default = {
199
217
  getBikeAvailabilityForSlot,
200
218
  createReservation,
201
219
  cancelReservation,
202
- isBikeReserved
220
+ isBikeReserved // Keep exporting isBikeReserved if used elsewhere, otherwise could be removed if only internal
203
221
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goldstars-services",
3
- "version": "1.0.45",
3
+ "version": "1.0.49",
4
4
  "description": "This is the services layer for GoldStars",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {