vr-commons 1.0.151 → 1.0.152

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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-commons",
3
- "version": "1.0.151",
3
+ "version": "1.0.152",
4
4
  "description": "Shared functions package",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",