vr-commons 1.0.151 → 1.0.153

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.
@@ -99,4 +99,25 @@ export declare const stockUtil: {
99
99
  missingPairs: string[];
100
100
  incompletePairs: string[];
101
101
  }>;
102
+ /**
103
+ * Disable a device pair (both RIDER and PASSENGER)
104
+ * - Sets status to "disabled"
105
+ * - Logs the action
106
+ */
107
+ disableDevicePair(pairId: string, transaction?: any): Promise<{
108
+ success: boolean;
109
+ message: string;
110
+ devices: Device[];
111
+ }>;
112
+ /**
113
+ * Enable a device pair back (both RIDER and PASSENGER)
114
+ * - Sets status to "locked"
115
+ * - Restores dedicatedUser (RIDER/PASSENGER) from serial number
116
+ * - Logs the action
117
+ */
118
+ enableDevicePair(pairId: string, transaction?: any): Promise<{
119
+ success: boolean;
120
+ message: string;
121
+ devices: Device[];
122
+ }>;
102
123
  };
@@ -6,6 +6,16 @@ const vr_models_1 = require("vr-models");
6
6
  const sequelize_1 = require("sequelize");
7
7
  const eventLog_utils_1 = require("./eventLog.utils");
8
8
  const admin_devices_validations_1 = require("../validations/admin.devices.validations");
9
+ /**
10
+ * Extract dedication from serial number
11
+ */
12
+ const extractDedicationFromSerial = (serialNumber) => {
13
+ if (serialNumber.includes("RIDER"))
14
+ return "RIDER";
15
+ if (serialNumber.includes("PASSENGER"))
16
+ return "PASSENGER";
17
+ return "N/A";
18
+ };
9
19
  exports.stockUtil = {
10
20
  /**
11
21
  * Increment stock when devices are added/created
@@ -344,4 +354,101 @@ exports.stockUtil = {
344
354
  incompletePairs,
345
355
  };
346
356
  },
357
+ /**
358
+ * Disable a device pair (both RIDER and PASSENGER)
359
+ * - Sets status to "disabled"
360
+ * - Logs the action
361
+ */
362
+ async disableDevicePair(pairId, transaction) {
363
+ // 1. Find both devices in the pair (excluding soft deleted)
364
+ const devices = await vr_models_1.Device.findAll({
365
+ where: {
366
+ pairId: pairId,
367
+ deletedAt: null,
368
+ },
369
+ include: [{ model: vr_models_1.DevicePaymentPlan, as: "paymentPlan" }],
370
+ transaction,
371
+ });
372
+ if (devices.length === 0) {
373
+ throw new Error(`No active devices found for pair ID: ${pairId}`);
374
+ }
375
+ // 2. Verify the pair is complete (both RIDER and PASSENGER)
376
+ const hasRider = devices.some((d) => d.dedicatedUser === "RIDER");
377
+ const hasPassenger = devices.some((d) => d.dedicatedUser === "PASSENGER");
378
+ if (!hasRider || !hasPassenger) {
379
+ throw new Error(`Incomplete pair ${pairId}: missing ${!hasRider ? "RIDER" : ""} ${!hasPassenger ? "PASSENGER" : ""}`);
380
+ }
381
+ // 3. Check if any device has active payment plan
382
+ const devicesWithActivePlan = devices.filter((d) => d.paymentPlan && d.paymentPlan.status === "ACTIVE");
383
+ if (devicesWithActivePlan.length > 0) {
384
+ throw new Error(`Cannot disable pair ${pairId} with active payment plan(s). Cancel plan first.`);
385
+ }
386
+ // 4. Disable both devices
387
+ const deviceIds = devices.map((d) => d.id);
388
+ await vr_models_1.Device.update({
389
+ status: "disabled",
390
+ }, {
391
+ where: { id: { [sequelize_1.Op.in]: deviceIds } },
392
+ transaction,
393
+ });
394
+ // 6. Fetch updated devices
395
+ const updatedDevices = await vr_models_1.Device.findAll({
396
+ where: { id: { [sequelize_1.Op.in]: deviceIds } },
397
+ include: [{ model: vr_models_1.Product, as: "product" }],
398
+ transaction,
399
+ });
400
+ return {
401
+ success: true,
402
+ message: `Pair ${pairId} disabled successfully (${devices.length} devices)`,
403
+ devices: updatedDevices,
404
+ };
405
+ },
406
+ /**
407
+ * Enable a device pair back (both RIDER and PASSENGER)
408
+ * - Sets status to "locked"
409
+ * - Restores dedicatedUser (RIDER/PASSENGER) from serial number
410
+ * - Logs the action
411
+ */
412
+ async enableDevicePair(pairId, transaction) {
413
+ // 1. Find both devices in the pair
414
+ const devices = await vr_models_1.Device.findAll({
415
+ where: {
416
+ pairId: pairId,
417
+ deletedAt: null,
418
+ },
419
+ include: [{ model: vr_models_1.DevicePaymentPlan, as: "paymentPlan" }],
420
+ transaction,
421
+ });
422
+ if (devices.length === 0) {
423
+ throw new Error(`No active devices found for pair ID: ${pairId}`);
424
+ }
425
+ // 2. Verify the pair is complete (both RIDER and PASSENGER)
426
+ const hasRider = devices.some((d) => d.dedicatedUser === "RIDER");
427
+ const hasPassenger = devices.some((d) => d.dedicatedUser === "PASSENGER");
428
+ if (!hasRider || !hasPassenger) {
429
+ throw new Error(`Incomplete pair ${pairId}: missing ${!hasRider ? "RIDER" : ""} ${!hasPassenger ? "PASSENGER" : ""}`);
430
+ }
431
+ // 3. Enable both devices
432
+ const deviceIds = devices.map((d) => d.id);
433
+ // Update each device individually to restore dedication from serial number
434
+ for (const device of devices) {
435
+ const dedication = extractDedicationFromSerial(device.serialNumber);
436
+ await device.update({
437
+ status: "locked",
438
+ dedicatedUser: dedication,
439
+ devicePaymentPlanId: null,
440
+ }, { transaction });
441
+ }
442
+ // 5. Fetch updated devices
443
+ const updatedDevices = await vr_models_1.Device.findAll({
444
+ where: { id: { [sequelize_1.Op.in]: deviceIds } },
445
+ include: [{ model: vr_models_1.Product, as: "product" }],
446
+ transaction,
447
+ });
448
+ return {
449
+ success: true,
450
+ message: `Pair ${pairId} enabled successfully (${devices.length} devices)`,
451
+ devices: updatedDevices,
452
+ };
453
+ },
347
454
  };
@@ -211,38 +211,47 @@ export declare const unlockDevicesSchema: z.ZodObject<{
211
211
  };
212
212
  }>;
213
213
  export declare const disableDevicesSchema: z.ZodObject<{
214
- body: z.ZodEffects<z.ZodObject<{
215
- reason: z.ZodString;
216
- pairIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
217
- serialNumbers: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
214
+ body: z.ZodObject<{
215
+ pairIds: z.ZodArray<z.ZodString, "many">;
216
+ reason: z.ZodOptional<z.ZodString>;
218
217
  }, "strip", z.ZodTypeAny, {
219
- reason: string;
220
- pairIds?: string[] | undefined;
221
- serialNumbers?: string[] | undefined;
218
+ pairIds: string[];
219
+ reason?: string | undefined;
222
220
  }, {
223
- reason: string;
224
- pairIds?: string[] | undefined;
225
- serialNumbers?: string[] | undefined;
226
- }>, {
227
- reason: string;
228
- pairIds?: string[] | undefined;
229
- serialNumbers?: string[] | undefined;
221
+ pairIds: string[];
222
+ reason?: string | undefined;
223
+ }>;
224
+ }, "strip", z.ZodTypeAny, {
225
+ body: {
226
+ pairIds: string[];
227
+ reason?: string | undefined;
228
+ };
229
+ }, {
230
+ body: {
231
+ pairIds: string[];
232
+ reason?: string | undefined;
233
+ };
234
+ }>;
235
+ export declare const enableDevicesSchema: z.ZodObject<{
236
+ body: z.ZodObject<{
237
+ pairIds: z.ZodArray<z.ZodString, "many">;
238
+ reason: z.ZodOptional<z.ZodString>;
239
+ }, "strip", z.ZodTypeAny, {
240
+ pairIds: string[];
241
+ reason?: string | undefined;
230
242
  }, {
231
- reason: string;
232
- pairIds?: string[] | undefined;
233
- serialNumbers?: string[] | undefined;
243
+ pairIds: string[];
244
+ reason?: string | undefined;
234
245
  }>;
235
246
  }, "strip", z.ZodTypeAny, {
236
247
  body: {
237
- reason: string;
238
- pairIds?: string[] | undefined;
239
- serialNumbers?: string[] | undefined;
248
+ pairIds: string[];
249
+ reason?: string | undefined;
240
250
  };
241
251
  }, {
242
252
  body: {
243
- reason: string;
244
- pairIds?: string[] | undefined;
245
- serialNumbers?: string[] | undefined;
253
+ pairIds: string[];
254
+ reason?: string | undefined;
246
255
  };
247
256
  }>;
248
257
  export declare const makeDevicesPermanentSchema: z.ZodObject<{
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkAvailabilitySchema = exports.makeDevicesPermanentSchema = exports.disableDevicesSchema = exports.unlockDevicesSchema = exports.lockDevicesSchema = exports.permanentDeleteDevicesByPairsSchema = exports.deleteDevicesByPairsSchema = exports.getDevicesSchema = exports.getDeviceSchema = exports.createDevicePairsSchema = exports.deduplicatePairIds = exports.PAIR_ID_REGEX = void 0;
3
+ exports.checkAvailabilitySchema = exports.makeDevicesPermanentSchema = exports.enableDevicesSchema = exports.disableDevicesSchema = exports.unlockDevicesSchema = exports.lockDevicesSchema = exports.permanentDeleteDevicesByPairsSchema = exports.deleteDevicesByPairsSchema = exports.getDevicesSchema = exports.getDeviceSchema = exports.createDevicePairsSchema = exports.deduplicatePairIds = exports.PAIR_ID_REGEX = void 0;
4
4
  // src/validations/device.validations.ts
5
5
  const zod_1 = require("zod");
6
6
  const vr_models_1 = require("vr-models");
@@ -110,20 +110,22 @@ exports.unlockDevicesSchema = zod_1.z.object({
110
110
  path: ["pairIds"],
111
111
  }),
112
112
  });
113
- // Disable devices schema
113
+ // ✅ Disable devices by pair IDs only
114
114
  exports.disableDevicesSchema = zod_1.z.object({
115
- body: zod_1.z
116
- .object({
117
- ...flexibleIdentifiersSchema,
118
- reason: zod_1.z.string().min(1, "Reason is required"),
119
- })
120
- .refine((data) => {
121
- const hasPairIds = data.pairIds && data.pairIds.length > 0;
122
- const hasSerialNumbers = data.serialNumbers && data.serialNumbers.length > 0;
123
- return ((hasPairIds || hasSerialNumbers) && !(hasPairIds && hasSerialNumbers));
124
- }, {
125
- message: "Either pairIds or serialNumbers must be provided, but not both",
126
- path: ["pairIds"],
115
+ body: zod_1.z.object({
116
+ pairIds: zod_1.z
117
+ .array(zod_1.z.string().regex(exports.PAIR_ID_REGEX, PAIR_ID_ERROR))
118
+ .min(1, "At least one pair ID required"),
119
+ reason: zod_1.z.string().optional(),
120
+ }),
121
+ });
122
+ // ✅ Enable devices by pair IDs only
123
+ exports.enableDevicesSchema = zod_1.z.object({
124
+ body: zod_1.z.object({
125
+ pairIds: zod_1.z
126
+ .array(zod_1.z.string().regex(exports.PAIR_ID_REGEX, PAIR_ID_ERROR))
127
+ .min(1, "At least one pair ID required"),
128
+ reason: zod_1.z.string().optional(),
127
129
  }),
128
130
  });
129
131
  // Make permanent schema
@@ -10,6 +10,6 @@ export { updateProfileSchema, requestPhoneChangeSchema, verifyPhoneChangeSchema,
10
10
  export { updateAdminSchema, updateRiderSchema, deleteUserSchema, getUserSchema, getUsersSchema, hireAdminSchema, promoteAdminSchema, demoteAdminSchema, fireEmployeeSchema, upgradeToRiderSchema, resetPasswordSchema, } from "./users.admin.validations";
11
11
  export { createProductSchema, updateProductSchema, getProductSchema, getProductsSchema, deactivateProductSchema, activateProductSchema, deleteProductSchema, updateStockSchema, } from "./product.validations";
12
12
  export { createPricingSchema, updatePricingSchema, getPricingSchema, getPricingsSchema, deletePricingSchema, } from "./pricings.validations";
13
- export { checkAvailabilitySchema, createDevicePairsSchema, lockDevicesSchema, unlockDevicesSchema, deleteDevicesByPairsSchema, makeDevicesPermanentSchema, permanentDeleteDevicesByPairsSchema, disableDevicesSchema, getDeviceSchema, getDevicesSchema, } from "./admin.devices.validations";
13
+ export { checkAvailabilitySchema, createDevicePairsSchema, lockDevicesSchema, unlockDevicesSchema, deleteDevicesByPairsSchema, makeDevicesPermanentSchema, permanentDeleteDevicesByPairsSchema, disableDevicesSchema, enableDevicesSchema, getDeviceSchema, getDevicesSchema, } from "./admin.devices.validations";
14
14
  export { createPlanSchema, limitedUpdateSchema, getDevicePlanSchema, getPlanSchema, getUserPlansSchema, listPlansSchema, recordPaymentSchema, deletePlanSchema, } from "./admin.devicePayment.validations";
15
15
  export { listEventLogsSchema, getEventLogSchema, } from "./eventlogs.admin.validations";
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAppSpecsSchema = exports.activateAppSpecsSchema = exports.getActiveAppSpecsSchema = exports.listAppSpecsSchema = exports.updateAppSpecsSchema = exports.createAppSpecsSchema = exports.exportBansSchema = exports.createSuspensionSchema = exports.createBanSchema = exports.extendSuspensionSchema = exports.revokeSuspensionSchema = exports.revokeBanSchema = exports.reviewAppealSchema = exports.listSuspensionsSchema = exports.listPendingAppealsSchema = exports.listBansSchema = exports.getUserBansSchema = exports.getUserSuspensionsSchema = exports.getSuspensionSchema = exports.getUserRestrictionsSchema = exports.getBanSchema = exports.submitSuspensionAppealSchema = exports.submitBanAppealSchema = exports.deactivateAccountSchema = exports.createRiderSchema = exports.updateRiderProfileSchema = exports.updatePassengerProfileSchema = exports.passengerSignupSchema = exports.viewProfileSchema = exports.getAllUsersSchema = exports.updateUserProfileSchema = exports.getUserByIdSchema = exports.createUserSchema = exports.googleLoginSchema = exports.completePassengerProfileSchema = exports.checkRiderAccountSchema = exports.setRiderPasswordSchema = exports.riderPasswordRegex = exports.passwordRegex = exports.emailRegex = exports.phoneRegex = exports.requestOtpSchema = exports.resendOtpSchema = exports.verifyOtpSchema = exports.registerSchema = exports.refreshTokenSchema = exports.userLoginSchema = exports.forgotPasswordSchema = exports.riderLoginSchema = exports.validate = void 0;
4
- exports.getDevicesSchema = exports.getDeviceSchema = exports.disableDevicesSchema = exports.permanentDeleteDevicesByPairsSchema = exports.makeDevicesPermanentSchema = exports.deleteDevicesByPairsSchema = exports.unlockDevicesSchema = exports.lockDevicesSchema = exports.createDevicePairsSchema = exports.checkAvailabilitySchema = exports.deletePricingSchema = exports.getPricingsSchema = exports.getPricingSchema = exports.updatePricingSchema = exports.createPricingSchema = exports.updateStockSchema = exports.deleteProductSchema = exports.activateProductSchema = exports.deactivateProductSchema = exports.getProductsSchema = exports.getProductSchema = exports.updateProductSchema = exports.createProductSchema = exports.resetPasswordSchema = exports.upgradeToRiderSchema = exports.fireEmployeeSchema = exports.demoteAdminSchema = exports.promoteAdminSchema = exports.hireAdminSchema = exports.getUsersSchema = exports.getUserSchema = exports.deleteUserSchema = exports.updateRiderSchema = exports.updateAdminSchema = exports.registerRiderSchema = exports.riderPasswordSchema = exports.changePasswordSchema = exports.strongPasswordRegex = exports.deleteAccountSchema = exports.verifyPhoneChangeSchema = exports.requestPhoneChangeSchema = exports.updateProfileSchema = exports.getInstallmentsSchema = exports.getInstallmentByIdSchema = exports.payInstallmentSchema = exports.serialRegex = exports.extendSessionSchema = exports.unlockDeviceSchema = exports.getUserPaymentPlanSchema = exports.listUserPaymentPlansSchema = void 0;
5
- exports.getEventLogSchema = exports.listEventLogsSchema = exports.deletePlanSchema = exports.recordPaymentSchema = exports.listPlansSchema = exports.getUserPlansSchema = exports.getPlanSchema = exports.getDevicePlanSchema = exports.limitedUpdateSchema = exports.createPlanSchema = void 0;
4
+ exports.getDeviceSchema = exports.enableDevicesSchema = exports.disableDevicesSchema = exports.permanentDeleteDevicesByPairsSchema = exports.makeDevicesPermanentSchema = exports.deleteDevicesByPairsSchema = exports.unlockDevicesSchema = exports.lockDevicesSchema = exports.createDevicePairsSchema = exports.checkAvailabilitySchema = exports.deletePricingSchema = exports.getPricingsSchema = exports.getPricingSchema = exports.updatePricingSchema = exports.createPricingSchema = exports.updateStockSchema = exports.deleteProductSchema = exports.activateProductSchema = exports.deactivateProductSchema = exports.getProductsSchema = exports.getProductSchema = exports.updateProductSchema = exports.createProductSchema = exports.resetPasswordSchema = exports.upgradeToRiderSchema = exports.fireEmployeeSchema = exports.demoteAdminSchema = exports.promoteAdminSchema = exports.hireAdminSchema = exports.getUsersSchema = exports.getUserSchema = exports.deleteUserSchema = exports.updateRiderSchema = exports.updateAdminSchema = exports.registerRiderSchema = exports.riderPasswordSchema = exports.changePasswordSchema = exports.strongPasswordRegex = exports.deleteAccountSchema = exports.verifyPhoneChangeSchema = exports.requestPhoneChangeSchema = exports.updateProfileSchema = exports.getInstallmentsSchema = exports.getInstallmentByIdSchema = exports.payInstallmentSchema = exports.serialRegex = exports.extendSessionSchema = exports.unlockDeviceSchema = exports.getUserPaymentPlanSchema = exports.listUserPaymentPlansSchema = void 0;
5
+ exports.getEventLogSchema = exports.listEventLogsSchema = exports.deletePlanSchema = exports.recordPaymentSchema = exports.listPlansSchema = exports.getUserPlansSchema = exports.getPlanSchema = exports.getDevicePlanSchema = exports.limitedUpdateSchema = exports.createPlanSchema = exports.getDevicesSchema = void 0;
6
6
  var validate_validations_1 = require("./validate.validations");
7
7
  Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return validate_validations_1.validate; } });
8
8
  var auth_validations_1 = require("./auth.validations");
@@ -122,6 +122,7 @@ Object.defineProperty(exports, "deleteDevicesByPairsSchema", { enumerable: true,
122
122
  Object.defineProperty(exports, "makeDevicesPermanentSchema", { enumerable: true, get: function () { return admin_devices_validations_1.makeDevicesPermanentSchema; } });
123
123
  Object.defineProperty(exports, "permanentDeleteDevicesByPairsSchema", { enumerable: true, get: function () { return admin_devices_validations_1.permanentDeleteDevicesByPairsSchema; } });
124
124
  Object.defineProperty(exports, "disableDevicesSchema", { enumerable: true, get: function () { return admin_devices_validations_1.disableDevicesSchema; } });
125
+ Object.defineProperty(exports, "enableDevicesSchema", { enumerable: true, get: function () { return admin_devices_validations_1.enableDevicesSchema; } });
125
126
  Object.defineProperty(exports, "getDeviceSchema", { enumerable: true, get: function () { return admin_devices_validations_1.getDeviceSchema; } });
126
127
  Object.defineProperty(exports, "getDevicesSchema", { enumerable: true, get: function () { return admin_devices_validations_1.getDevicesSchema; } });
127
128
  var admin_devicePayment_validations_1 = require("./admin.devicePayment.validations");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-commons",
3
- "version": "1.0.151",
3
+ "version": "1.0.153",
4
4
  "description": "Shared functions package",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",