vr-models 1.0.37 → 1.0.39

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.
@@ -49,5 +49,13 @@ export declare class Device extends Model<InferAttributes<Device>, InferCreation
49
49
  canBeActivated(): boolean;
50
50
  isAssigned(): boolean;
51
51
  getPaymentPlan(): Promise<DevicePaymentPlan | null>;
52
+ /**
53
+ * Check if device can be unlocked by user
54
+ */
55
+ canBeUnlocked(userId: string): Promise<{
56
+ canUnlock: boolean;
57
+ reason?: string;
58
+ details?: any;
59
+ }>;
52
60
  }
53
61
  export type DeviceModel = typeof Device;
@@ -117,5 +117,69 @@ class Device extends vr_migrations_1.Model {
117
117
  .DevicePaymentPlan;
118
118
  return await DevicePaymentPlan.findByPk(this.devicePaymentPlanId);
119
119
  }
120
+ // Add to device.models.ts
121
+ /**
122
+ * Check if device can be unlocked by user
123
+ */
124
+ async canBeUnlocked(userId) {
125
+ // Check if permanently unlocked
126
+ if (this.isPermanentlyUnlocked) {
127
+ return { canUnlock: false, reason: "Device is permanently unlocked" };
128
+ }
129
+ // Check if disabled
130
+ if (this.status === "disabled") {
131
+ return { canUnlock: false, reason: "Device is disabled" };
132
+ }
133
+ // Check if already unlocked
134
+ if (this.status === "unlocked") {
135
+ return { canUnlock: false, reason: "Device is already unlocked" };
136
+ }
137
+ // Check if has payment plan
138
+ if (!this.devicePaymentPlanId) {
139
+ return {
140
+ canUnlock: false,
141
+ reason: "Device is not associated with a payment plan",
142
+ };
143
+ }
144
+ // Get payment plan
145
+ const paymentPlan = await this.getPaymentPlan();
146
+ if (!paymentPlan) {
147
+ return { canUnlock: false, reason: "Payment plan not found" };
148
+ }
149
+ // Check ownership
150
+ if (paymentPlan.userId !== userId) {
151
+ return { canUnlock: false, reason: "Device does not belong to you" };
152
+ }
153
+ // Check payment plan status
154
+ if (paymentPlan.status !== "ACTIVE") {
155
+ let reason = "Payment plan is not active";
156
+ if (paymentPlan.status === "DEFAULTED")
157
+ reason = "Payment plan is defaulted";
158
+ if (paymentPlan.status === "CANCELLED")
159
+ reason = "Payment plan is cancelled";
160
+ return { canUnlock: false, reason };
161
+ }
162
+ // Check for overdue installments
163
+ const Installment = this.constructor.sequelize.models.Installment;
164
+ const overdueInstallments = await Installment.findAll({
165
+ where: {
166
+ devicePaymentPlanId: this.devicePaymentPlanId,
167
+ status: "OVERDUE",
168
+ },
169
+ });
170
+ if (overdueInstallments.length > 0) {
171
+ const totalOverdue = overdueInstallments.reduce((sum, inst) => sum + inst.amount, 0);
172
+ return {
173
+ canUnlock: false,
174
+ reason: `You have ${overdueInstallments.length} overdue installment(s)`,
175
+ details: {
176
+ overdueCount: overdueInstallments.length,
177
+ totalOverdueAmount: totalOverdue,
178
+ overdueInstallments,
179
+ },
180
+ };
181
+ }
182
+ return { canUnlock: true };
183
+ }
120
184
  }
121
185
  exports.Device = Device;
@@ -3,6 +3,7 @@ import type { Device } from "./device.models";
3
3
  import type { User } from "./user.models";
4
4
  import type { Installment } from "./installment.models";
5
5
  export type PaymentPlanStatus = "ACTIVE" | "COMPLETED" | "DEFAULTED" | "CANCELLED";
6
+ export declare const PAYMENT_STATUS: readonly ["ACTIVE", "COMPLETED", "DEFAULTED", "CANCELLED"];
6
7
  export interface DevicePaymentPlanAttributes {
7
8
  id: string;
8
9
  userId: string;
@@ -1,8 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DevicePaymentPlan = void 0;
3
+ exports.DevicePaymentPlan = exports.PAYMENT_STATUS = void 0;
4
4
  // src/models/devicePaymentPlan.models.ts
5
5
  const vr_migrations_1 = require("vr-migrations");
6
+ exports.PAYMENT_STATUS = [
7
+ "ACTIVE",
8
+ "COMPLETED",
9
+ "DEFAULTED",
10
+ "CANCELLED",
11
+ ];
6
12
  class DevicePaymentPlan extends vr_migrations_1.Model {
7
13
  // Static initialization method
8
14
  static initialize(sequelize) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-models",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
4
4
  "description": "Shared database models package for VR applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",