vr-commons 1.0.150 → 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.
@@ -85,7 +85,7 @@ export declare const stockUtil: {
85
85
  /**
86
86
  * Check if pair exists and is not soft deleted
87
87
  */
88
- checkPairExists(pairId: string, productId: string, transaction?: any): Promise<{
88
+ checkPairExists(pairId: string, transaction?: any): Promise<{
89
89
  exists: boolean;
90
90
  devices?: Device[];
91
91
  missing?: string[];
@@ -93,10 +93,31 @@ export declare const stockUtil: {
93
93
  /**
94
94
  * Check if pair IDs exist and are not soft deleted (for bulk operations)
95
95
  */
96
- checkMultiplePairsExist(pairIds: string[], productId: string, transaction?: any): Promise<{
96
+ checkMultiplePairsExist(pairIds: string[], transaction?: any): Promise<{
97
97
  valid: boolean;
98
98
  existingPairs: string[];
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
@@ -292,11 +302,10 @@ exports.stockUtil = {
292
302
  /**
293
303
  * Check if pair exists and is not soft deleted
294
304
  */
295
- async checkPairExists(pairId, productId, transaction) {
305
+ async checkPairExists(pairId, transaction) {
296
306
  const devices = await vr_models_1.Device.findAll({
297
307
  where: {
298
308
  pairId: pairId,
299
- productId: productId,
300
309
  deletedAt: null, // ✅ Exclude soft deleted
301
310
  },
302
311
  transaction,
@@ -320,12 +329,12 @@ exports.stockUtil = {
320
329
  /**
321
330
  * Check if pair IDs exist and are not soft deleted (for bulk operations)
322
331
  */
323
- async checkMultiplePairsExist(pairIds, productId, transaction) {
332
+ async checkMultiplePairsExist(pairIds, transaction) {
324
333
  const existingPairs = [];
325
334
  const missingPairs = [];
326
335
  const incompletePairs = [];
327
336
  for (const pairId of pairIds) {
328
- const result = await this.checkPairExists(pairId, productId, transaction);
337
+ const result = await this.checkPairExists(pairId, transaction);
329
338
  if (!result.exists) {
330
339
  if (result.devices && result.devices.length === 1) {
331
340
  incompletePairs.push(pairId);
@@ -345,4 +354,101 @@ exports.stockUtil = {
345
354
  incompletePairs,
346
355
  };
347
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
+ },
348
454
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-commons",
3
- "version": "1.0.150",
3
+ "version": "1.0.152",
4
4
  "description": "Shared functions package",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",