payservedb 9.9.5 → 9.11.0

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.
package/index.js CHANGED
@@ -5,6 +5,18 @@ require("dotenv").config();
5
5
  // Maintain a record of open connections for each database
6
6
  const connections = {};
7
7
 
8
+ // Consuming services (e.g. backend_main) typically run one connection per
9
+ // cluster worker process. Without an explicit cap here, the MongoDB Node
10
+ // driver defaults maxPoolSize to 100 *per connection* — so N worker
11
+ // processes each calling connectToMongoDB() can reserve up to N*100
12
+ // sockets on the server even under light load. Capping it keeps a single
13
+ // process's footprint bounded and predictable.
14
+ const defaultMongoOptions = {
15
+ useNewUrlParser: true,
16
+ maxPoolSize: 20,
17
+ minPoolSize: 2,
18
+ };
19
+
8
20
  // Utility function to connect to MongoDB
9
21
  async function connectToMongoDB(
10
22
  dbName,
@@ -18,17 +30,13 @@ async function connectToMongoDB(
18
30
  if (secured === false) {
19
31
  const url_ = url + ":" + port;
20
32
  const connectionString = `mongodb://${url_}/${dbName}`;
21
- await mongoose.connect(connectionString, {
22
- useNewUrlParser: true,
23
- });
33
+ await mongoose.connect(connectionString, defaultMongoOptions);
24
34
  console.log("Connected to MongoDB");
25
35
  } else {
26
36
  const url_ = `${username}:${password}@${url}:${port}`;
27
37
  const source = "?authSource=admin";
28
38
  const connectionString = `mongodb://${url_}/${dbName}${source}`;
29
- await mongoose.connect(connectionString, {
30
- useNewUrlParser: true,
31
- });
39
+ await mongoose.connect(connectionString, defaultMongoOptions);
32
40
  console.log("Connected to MongoDB");
33
41
  }
34
42
  } catch (err) {
@@ -53,9 +61,7 @@ async function switchDB(dbName) {
53
61
  const source = "?authSource=admin";
54
62
 
55
63
  const connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}${source}`;
56
- const dbConnection = await mongoose.createConnection(connectionString, {
57
- useNewUrlParser: true,
58
- });
64
+ const dbConnection = await mongoose.createConnection(connectionString, defaultMongoOptions);
59
65
 
60
66
  connections[dbName] = dbConnection; // Store the connection for reuse
61
67
  console.log(`Switched to database: ${dbName}`);
@@ -257,6 +263,20 @@ const models = {
257
263
  ZohoItem: require("./src/models/zohoItem"),
258
264
  ZohoAccount: require("./src/models/zohoAccount"),
259
265
  QuickBooksConfig: require("./src/models/quickbooks_config"),
266
+ QuickBooksOAuthState: require("./src/models/quickbooks_oauth_state"),
267
+ QuickBooksAccountMapping: require("./src/models/quickbooks_account_mapping"),
268
+ QuickBooksItemMapping: require("./src/models/quickbooks_item_mapping"),
269
+ QuickBooksPaymentChannelMapping: require("./src/models/quickbooks_payment_channel_mapping"),
270
+ QuickBooksCustomerMapping: require("./src/models/quickbooks_customer_mapping"),
271
+ QuickBooksVendorMapping: require("./src/models/quickbooks_vendor_mapping"),
272
+ QuickBooksInvoiceMapping: require("./src/models/quickbooks_invoice_mapping"),
273
+ QuickBooksPaymentMapping: require("./src/models/quickbooks_payment_mapping"),
274
+ QuickBooksInvoicePaymentMapping: require("./src/models/quickbooks_invoice_payment_mapping"),
275
+ QuickBooksDepositMapping: require("./src/models/quickbooks_deposit_mapping"),
276
+ QuickBooksOutboundSyncState: require("./src/models/quickbooks_outbound_sync_state"),
277
+ QuickBooksCdcSweepState: require("./src/models/quickbooks_cdc_sweep_state"),
278
+ QuickBooksBackfillState: require("./src/models/quickbooks_backfill_state"),
279
+ QuickBooksInvoiceRouting: require("./src/models/quickbooks_invoice_routing"),
260
280
  PrivacyPolicy: require("./src/models/privacy_policy"),
261
281
  TermsAndConditions: require("./src/models/terms_and_conditions"),
262
282
  CommunityGuidelines: require("./src/models/community_guidelines"),
@@ -387,9 +407,7 @@ async function connectToMoveInDB(dbName, secured, username, password, url, port)
387
407
  connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}${source}`;
388
408
  }
389
409
 
390
- const conn = await mongoose.createConnection(connectionString, {
391
- useNewUrlParser: true,
392
- });
410
+ const conn = await mongoose.createConnection(connectionString, defaultMongoOptions);
393
411
 
394
412
  console.log(`Connected to Move-In DB: ${dbName}`);
395
413
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "9.9.5",
3
+ "version": "9.11.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -64,11 +64,11 @@ const powerMeterSchema = new mongoose.Schema({
64
64
  type: String,
65
65
  required: true
66
66
  },
67
-
67
+
68
68
  meterCategory: {
69
69
  type: String,
70
70
  required: true,
71
- enum: ['unit', 'common_area', 'bulk'],
71
+ enum: ['unit', 'common_area', 'commercial', 'bulk'],
72
72
  default: 'unit'
73
73
  },
74
74
 
@@ -0,0 +1,63 @@
1
+ // Admin-picked GL account mapping; not synced/derived.
2
+ const mongoose = require("mongoose");
3
+
4
+ const quickBooksAccountMappingSchema = new mongoose.Schema(
5
+ {
6
+ facilityId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: "Facility",
9
+ required: true,
10
+ index: true,
11
+ },
12
+ qbConfigId: {
13
+ type: mongoose.Schema.Types.ObjectId,
14
+ ref: "QuickBooksConfig",
15
+ required: true,
16
+ index: true,
17
+ },
18
+ payserveAccountId: {
19
+ type: mongoose.Schema.Types.ObjectId,
20
+ ref: "GLAccount",
21
+ required: true,
22
+ },
23
+ // Which side of a Dr/Cr posting this account plays for its category, e.g. rent_lease's
24
+ // receivable vs. its revenue/liability counterpart. Lets sync logic pull both legs of a posting.
25
+ accountRole: {
26
+ type: String,
27
+ enum: ["receivable", "liability_deposit", "liability_deferred_revenue", "revenue", "cash"],
28
+ required: true,
29
+ },
30
+ // Canonical invoice category this account applies to (see invoice_categories.js).
31
+ category: {
32
+ type: String,
33
+ default: null,
34
+ },
35
+ qbAccountId: {
36
+ type: String,
37
+ trim: true,
38
+ default: null,
39
+ },
40
+ qbAccountName: {
41
+ type: String,
42
+ trim: true,
43
+ default: null,
44
+ },
45
+ qbAccountType: {
46
+ type: String,
47
+ trim: true,
48
+ default: null,
49
+ },
50
+ },
51
+ { timestamps: true },
52
+ );
53
+
54
+ quickBooksAccountMappingSchema.index(
55
+ { facilityId: 1, qbConfigId: 1, payserveAccountId: 1 },
56
+ { unique: true },
57
+ );
58
+ quickBooksAccountMappingSchema.index(
59
+ { facilityId: 1, qbConfigId: 1, category: 1, accountRole: 1 },
60
+ { unique: true, partialFilterExpression: { category: { $type: "string" } } },
61
+ );
62
+
63
+ module.exports = { schema: quickBooksAccountMappingSchema };
@@ -0,0 +1,47 @@
1
+ const mongoose = require("mongoose");
2
+
3
+ const quickBooksBackfillStateSchema = new mongoose.Schema(
4
+ {
5
+ facilityId: {
6
+ type: mongoose.Schema.Types.ObjectId,
7
+ ref: "Facility",
8
+ required: true,
9
+ index: true,
10
+ },
11
+ qbConfigId: {
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: "QuickBooksConfig",
14
+ required: true,
15
+ index: true,
16
+ },
17
+ status: {
18
+ type: String,
19
+ enum: ["not_started", "running", "completed", "error"],
20
+ default: "not_started",
21
+ },
22
+ startedAt: { type: Date, default: null },
23
+ finishedAt: { type: Date, default: null },
24
+ summary: {
25
+ customersProcessed: { type: Number, default: 0 },
26
+ invoicesProcessed: { type: Number, default: 0 },
27
+ paymentsProcessed: { type: Number, default: 0 },
28
+ vendorsProcessed: { type: Number, default: 0 },
29
+ errors: { type: Number, default: 0 },
30
+ // Capped list; recent failures for review.
31
+ errorDetails: [
32
+ {
33
+ _id: false,
34
+ entity: String,
35
+ id: String,
36
+ message: String,
37
+ },
38
+ ],
39
+ },
40
+ lastError: { type: String, default: null },
41
+ },
42
+ { timestamps: true },
43
+ );
44
+
45
+ quickBooksBackfillStateSchema.index({ facilityId: 1, qbConfigId: 1 }, { unique: true });
46
+
47
+ module.exports = { schema: quickBooksBackfillStateSchema };
@@ -0,0 +1,56 @@
1
+ // Per-(facility, realm) row: only for "last sweep" display.
2
+ const mongoose = require("mongoose");
3
+
4
+ const cdcSweepSummarySchema = new mongoose.Schema(
5
+ {
6
+ customersProcessed: { type: Number, default: 0 },
7
+ invoicesProcessed: { type: Number, default: 0 },
8
+ paymentsProcessed: { type: Number, default: 0 },
9
+ vendorsProcessed: { type: Number, default: 0 },
10
+ errors: { type: Number, default: 0 },
11
+ },
12
+ { _id: false },
13
+ );
14
+
15
+ const quickBooksCdcSweepStateSchema = new mongoose.Schema(
16
+ {
17
+ facilityId: {
18
+ type: mongoose.Schema.Types.ObjectId,
19
+ ref: "Facility",
20
+ required: true,
21
+ index: true,
22
+ },
23
+ qbConfigId: {
24
+ type: mongoose.Schema.Types.ObjectId,
25
+ ref: "QuickBooksConfig",
26
+ required: true,
27
+ index: true,
28
+ },
29
+ startedAt: {
30
+ type: Date,
31
+ default: null,
32
+ },
33
+ lastSweepAt: {
34
+ type: Date,
35
+ default: null,
36
+ },
37
+ lastSweepStatus: {
38
+ type: String,
39
+ enum: ["running", "success", "error"],
40
+ default: null,
41
+ },
42
+ lastSweepError: {
43
+ type: String,
44
+ default: null,
45
+ },
46
+ lastSweepSummary: {
47
+ type: cdcSweepSummarySchema,
48
+ default: () => ({}),
49
+ },
50
+ },
51
+ { timestamps: true },
52
+ );
53
+
54
+ quickBooksCdcSweepStateSchema.index({ facilityId: 1, qbConfigId: 1 }, { unique: true });
55
+
56
+ module.exports = { schema: quickBooksCdcSweepStateSchema };
@@ -5,9 +5,14 @@ const quickBooksConfigSchema = new mongoose.Schema({
5
5
  type: mongoose.Schema.Types.ObjectId,
6
6
  ref: 'Facility',
7
7
  required: true,
8
- unique: true,
9
8
  index: true
10
9
  },
10
+ // Admin-facing name distinguishing this realm from others on the same facility, e.g. "Nauras Exclusive".
11
+ label: {
12
+ type: String,
13
+ required: true,
14
+ trim: true
15
+ },
11
16
  clientId: {
12
17
  type: String,
13
18
  required: true,
@@ -21,7 +26,8 @@ const quickBooksConfigSchema = new mongoose.Schema({
21
26
  realmId: {
22
27
  type: String,
23
28
  required: true,
24
- trim: true
29
+ trim: true,
30
+ unique: true
25
31
  },
26
32
  environment: {
27
33
  type: String,
@@ -43,11 +49,18 @@ const quickBooksConfigSchema = new mongoose.Schema({
43
49
  isActive: {
44
50
  type: Boolean,
45
51
  default: true
52
+ },
53
+ // Used to resolve a realm when no explicit invoice-category routing exists for the facility.
54
+ isDefault: {
55
+ type: Boolean,
56
+ default: false
46
57
  }
47
58
  }, {
48
59
  timestamps: true
49
60
  });
50
61
 
62
+ quickBooksConfigSchema.index({ facilityId: 1, realmId: 1 }, { unique: true });
63
+
51
64
  const QuickBooksConfig = mongoose.model('QuickBooksConfig', quickBooksConfigSchema);
52
65
 
53
- module.exports = QuickBooksConfig;
66
+ module.exports = QuickBooksConfig;
@@ -0,0 +1,99 @@
1
+ // Nullable: QBO-only customers staged as needs_review.
2
+ const mongoose = require("mongoose");
3
+
4
+ const linkedInvoiceForReviewSchema = new mongoose.Schema(
5
+ {
6
+ qbDocNumber: String,
7
+ qbTotalAmt: Number,
8
+ },
9
+ { _id: false },
10
+ );
11
+
12
+ const quickBooksCustomerMappingSchema = new mongoose.Schema(
13
+ {
14
+ facilityId: {
15
+ type: mongoose.Schema.Types.ObjectId,
16
+ ref: "Facility",
17
+ required: true,
18
+ index: true,
19
+ },
20
+ qbConfigId: {
21
+ type: mongoose.Schema.Types.ObjectId,
22
+ ref: "QuickBooksConfig",
23
+ required: true,
24
+ index: true,
25
+ },
26
+ payserveCustomerId: {
27
+ type: mongoose.Schema.Types.ObjectId,
28
+ ref: "Customer",
29
+ // default:null defeats sparse index
30
+ },
31
+ qbCustomerId: {
32
+ type: String,
33
+ trim: true,
34
+ },
35
+ qbSyncToken: {
36
+ type: String,
37
+ trim: true,
38
+ },
39
+ qbDisplayName: {
40
+ type: String,
41
+ trim: true,
42
+ },
43
+ syncStatus: {
44
+ type: String,
45
+ enum: ["synced", "pending", "error", "needs_review"],
46
+ default: "pending",
47
+ index: true,
48
+ },
49
+ syncError: {
50
+ type: String,
51
+ default: null,
52
+ },
53
+ lastSyncDirection: {
54
+ type: String,
55
+ enum: ["inbound", "outbound"],
56
+ default: null,
57
+ },
58
+ lastSyncedAt: {
59
+ type: Date,
60
+ default: null,
61
+ },
62
+ // Raw QBO snapshot for admin review. Union of backend_main's (admin
63
+ // review panel) and invoice_service's (parsed-from-invoice) fields —
64
+ // both processes write to this same shared collection.
65
+ pendingData: {
66
+ displayName: String,
67
+ email: String,
68
+ phone: String,
69
+ notes: String,
70
+ linkedInvoices: [linkedInvoiceForReviewSchema],
71
+ givenName: String,
72
+ familyName: String,
73
+ phoneNumber: String,
74
+ title: String,
75
+ active: Boolean,
76
+ parsedCustomerType: String,
77
+ parsedResidentType: String,
78
+ parsedIdNumber: String,
79
+ parsedKraPin: String,
80
+ parsedCustomerNumber: String,
81
+ rawNotes: String,
82
+ },
83
+ },
84
+ { timestamps: true },
85
+ );
86
+
87
+ // sparse ignores docs missing ALL indexed fields;
88
+ // facilityId/qbConfigId are always set, so sparse never applied.
89
+ // partialFilterExpression is the correct null-safe fix.
90
+ quickBooksCustomerMappingSchema.index(
91
+ { facilityId: 1, qbConfigId: 1, payserveCustomerId: 1 },
92
+ { unique: true, partialFilterExpression: { payserveCustomerId: { $exists: true } } },
93
+ );
94
+ quickBooksCustomerMappingSchema.index(
95
+ { facilityId: 1, qbConfigId: 1, qbCustomerId: 1 },
96
+ { unique: true, partialFilterExpression: { qbCustomerId: { $exists: true } } },
97
+ );
98
+
99
+ module.exports = { schema: quickBooksCustomerMappingSchema };
@@ -0,0 +1,120 @@
1
+ // Deposit <-> QB Sales/Refund Receipt.
2
+ // One row per Deposit; syncs once collected, gated on the deposit's status
3
+ // reaching "Held" before the payment leg is pushed (see deposit.js Held-gate).
4
+ const mongoose = require("mongoose");
5
+
6
+ const refundSchema = new mongoose.Schema(
7
+ {
8
+ qbRefundReceiptId: String,
9
+ amount: Number,
10
+ refundedAt: { type: Date, default: Date.now },
11
+ },
12
+ { _id: false },
13
+ );
14
+
15
+ const quickBooksDepositMappingSchema = new mongoose.Schema(
16
+ {
17
+ facilityId: {
18
+ type: mongoose.Schema.Types.ObjectId,
19
+ ref: "Facility",
20
+ required: true,
21
+ index: true,
22
+ },
23
+ qbConfigId: {
24
+ type: mongoose.Schema.Types.ObjectId,
25
+ ref: "QuickBooksConfig",
26
+ required: true,
27
+ index: true,
28
+ },
29
+ depositId: {
30
+ type: mongoose.Schema.Types.ObjectId,
31
+ ref: "Deposit",
32
+ required: true,
33
+ },
34
+ contractId: {
35
+ type: mongoose.Schema.Types.ObjectId,
36
+ ref: "LevyContract",
37
+ default: null,
38
+ },
39
+ customerId: {
40
+ type: mongoose.Schema.Types.ObjectId,
41
+ ref: "Customer",
42
+ default: null,
43
+ },
44
+ // Applied amount, posted via Sales Receipt.
45
+ depositAmount: {
46
+ type: Number,
47
+ default: null,
48
+ },
49
+ qbSalesReceiptId: {
50
+ type: String,
51
+ trim: true,
52
+ default: null,
53
+ },
54
+ qbSalesReceiptSyncToken: {
55
+ type: String,
56
+ trim: true,
57
+ default: null,
58
+ },
59
+ // Overpayment pushed as unapplied-credit Payment.
60
+ overpaymentAmount: {
61
+ type: Number,
62
+ default: 0,
63
+ },
64
+ qbOverpaymentPaymentId: {
65
+ type: String,
66
+ trim: true,
67
+ default: null,
68
+ },
69
+ qbOverpaymentPaymentSyncToken: {
70
+ type: String,
71
+ trim: true,
72
+ default: null,
73
+ },
74
+ qbCustomerId: {
75
+ type: String,
76
+ trim: true,
77
+ default: null,
78
+ },
79
+ qbDepositAccountId: {
80
+ type: String,
81
+ trim: true,
82
+ default: null,
83
+ },
84
+ refundedAmount: {
85
+ type: Number,
86
+ default: 0,
87
+ },
88
+ refunds: [refundSchema],
89
+ // Deposit's held-status at the time of the last sync attempt; the sync
90
+ // layer only pushes the payment leg once the source deposit's status is "Held".
91
+ depositStatusAtSync: {
92
+ type: String,
93
+ default: null,
94
+ },
95
+ syncStatus: {
96
+ type: String,
97
+ enum: ["synced", "pending", "pending_hold", "error"],
98
+ default: "pending",
99
+ index: true,
100
+ },
101
+ syncError: {
102
+ type: String,
103
+ default: null,
104
+ },
105
+ lastSyncDirection: {
106
+ type: String,
107
+ enum: ["inbound", "outbound"],
108
+ default: null,
109
+ },
110
+ lastSyncedAt: {
111
+ type: Date,
112
+ default: null,
113
+ },
114
+ },
115
+ { timestamps: true },
116
+ );
117
+
118
+ quickBooksDepositMappingSchema.index({ facilityId: 1, qbConfigId: 1, depositId: 1 }, { unique: true });
119
+
120
+ module.exports = { schema: quickBooksDepositMappingSchema };
@@ -0,0 +1,94 @@
1
+ // Invoice <-> QB mapping. Written by invoice_service; read/updated by
2
+ // backend_main on void and pulls. Single shared collection, single source of truth.
3
+ const mongoose = require("mongoose");
4
+
5
+ const quickBooksInvoiceMappingSchema = new mongoose.Schema(
6
+ {
7
+ facilityId: {
8
+ type: mongoose.Schema.Types.ObjectId,
9
+ ref: "Facility",
10
+ required: true,
11
+ index: true,
12
+ },
13
+ qbConfigId: {
14
+ type: mongoose.Schema.Types.ObjectId,
15
+ ref: "QuickBooksConfig",
16
+ required: true,
17
+ index: true,
18
+ },
19
+ // Source collection varies; ref left generic on purpose.
20
+ sourceType: {
21
+ type: String,
22
+ enum: ["Invoice", "VasInvoice", "WaterInvoice", "PowerInvoice", "BookingInvoice"],
23
+ required: true,
24
+ default: "Invoice",
25
+ },
26
+ // Canonical category used for realm routing and account/item mapping (see invoice_categories.js).
27
+ invoiceCategory: {
28
+ type: String,
29
+ enum: [
30
+ "rent_lease",
31
+ "security_deposit",
32
+ "service_charge",
33
+ "utility_power",
34
+ "utility_water",
35
+ "levy_contract",
36
+ "property_management",
37
+ ],
38
+ required: true,
39
+ },
40
+ payserveInvoiceId: {
41
+ type: mongoose.Schema.Types.ObjectId,
42
+ required: true,
43
+ },
44
+ qbInvoiceId: {
45
+ type: String,
46
+ trim: true,
47
+ // default:null defeats sparse index
48
+ },
49
+ qbSyncToken: {
50
+ type: String,
51
+ trim: true,
52
+ },
53
+ qbDocNumber: {
54
+ type: String,
55
+ trim: true,
56
+ },
57
+ // Voided-in-QuickBooks flag, tracked separately from PayServe.
58
+ voidedInQuickBooks: {
59
+ type: Boolean,
60
+ default: false,
61
+ },
62
+ syncStatus: {
63
+ type: String,
64
+ enum: ["synced", "pending", "error", "needs_review", "pending_hold"],
65
+ default: "pending",
66
+ index: true,
67
+ },
68
+ syncError: {
69
+ type: String,
70
+ default: null,
71
+ },
72
+ lastSyncDirection: {
73
+ type: String,
74
+ enum: ["inbound", "outbound"],
75
+ default: null,
76
+ },
77
+ lastSyncedAt: {
78
+ type: Date,
79
+ default: null,
80
+ },
81
+ },
82
+ { timestamps: true },
83
+ );
84
+
85
+ quickBooksInvoiceMappingSchema.index(
86
+ { facilityId: 1, qbConfigId: 1, sourceType: 1, payserveInvoiceId: 1 },
87
+ { unique: true },
88
+ );
89
+ quickBooksInvoiceMappingSchema.index(
90
+ { facilityId: 1, qbConfigId: 1, qbInvoiceId: 1 },
91
+ { unique: true, sparse: true },
92
+ );
93
+
94
+ module.exports = { schema: quickBooksInvoiceMappingSchema };
@@ -0,0 +1,60 @@
1
+ // Idempotency record for invoice_service's auto-Payment-on-Paid-invoice flow.
2
+ // Distinct from QuickBooksPaymentMapping (backend_main's cash-payment-driven
3
+ // mapping) — this one guards against double-posting a QB Payment when an
4
+ // invoice_service-side invoice status flips to Paid.
5
+ const mongoose = require("mongoose");
6
+
7
+ const quickBooksInvoicePaymentMappingSchema = new mongoose.Schema(
8
+ {
9
+ facilityId: {
10
+ type: mongoose.Schema.Types.ObjectId,
11
+ ref: "Facility",
12
+ required: true,
13
+ index: true,
14
+ },
15
+ qbConfigId: {
16
+ type: mongoose.Schema.Types.ObjectId,
17
+ ref: "QuickBooksConfig",
18
+ required: true,
19
+ index: true,
20
+ },
21
+ payserveInvoiceId: {
22
+ type: mongoose.Schema.Types.ObjectId,
23
+ ref: "Invoice",
24
+ required: true,
25
+ },
26
+ qbPaymentId: {
27
+ type: String,
28
+ default: null,
29
+ },
30
+ qbSyncToken: {
31
+ type: String,
32
+ default: null,
33
+ },
34
+ amount: {
35
+ type: Number,
36
+ default: null,
37
+ },
38
+ syncStatus: {
39
+ type: String,
40
+ enum: ["synced", "pending", "error"],
41
+ default: "pending",
42
+ },
43
+ syncError: {
44
+ type: String,
45
+ default: null,
46
+ },
47
+ lastSyncedAt: {
48
+ type: Date,
49
+ default: null,
50
+ },
51
+ },
52
+ { timestamps: true },
53
+ );
54
+
55
+ quickBooksInvoicePaymentMappingSchema.index(
56
+ { facilityId: 1, qbConfigId: 1, payserveInvoiceId: 1 },
57
+ { unique: true },
58
+ );
59
+
60
+ module.exports = { schema: quickBooksInvoicePaymentMappingSchema };
@@ -0,0 +1,38 @@
1
+ // Resolves which connected QuickBooks realm (qbConfigId) an invoice category
2
+ // should post to for a given facility. Facilities with a single active realm
3
+ // don't need a row here — sync falls back to that facility's isDefault QuickBooksConfig.
4
+ const mongoose = require("mongoose");
5
+
6
+ const quickBooksInvoiceRoutingSchema = new mongoose.Schema(
7
+ {
8
+ facilityId: {
9
+ type: mongoose.Schema.Types.ObjectId,
10
+ ref: "Facility",
11
+ required: true,
12
+ index: true,
13
+ },
14
+ invoiceCategory: {
15
+ type: String,
16
+ enum: [
17
+ "rent_lease",
18
+ "security_deposit",
19
+ "service_charge",
20
+ "utility_power",
21
+ "utility_water",
22
+ "levy_contract",
23
+ "property_management",
24
+ ],
25
+ required: true,
26
+ },
27
+ qbConfigId: {
28
+ type: mongoose.Schema.Types.ObjectId,
29
+ ref: "QuickBooksConfig",
30
+ required: true,
31
+ },
32
+ },
33
+ { timestamps: true },
34
+ );
35
+
36
+ quickBooksInvoiceRoutingSchema.index({ facilityId: 1, invoiceCategory: 1 }, { unique: true });
37
+
38
+ module.exports = { schema: quickBooksInvoiceRoutingSchema };
@@ -0,0 +1,87 @@
1
+ // Invoice lines reference Items, not Accounts directly.
2
+ const mongoose = require("mongoose");
3
+
4
+ const quickBooksItemMappingSchema = new mongoose.Schema(
5
+ {
6
+ facilityId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: "Facility",
9
+ required: true,
10
+ index: true,
11
+ },
12
+ qbConfigId: {
13
+ type: mongoose.Schema.Types.ObjectId,
14
+ ref: "QuickBooksConfig",
15
+ required: true,
16
+ index: true,
17
+ },
18
+ category: {
19
+ type: String,
20
+ required: true,
21
+ trim: true,
22
+ },
23
+ // Admin-facing label for the category (invoice_service's matching UI).
24
+ categoryLabel: {
25
+ type: String,
26
+ trim: true,
27
+ default: null,
28
+ },
29
+ requiredClassification: {
30
+ type: String,
31
+ enum: ["Asset", "Liability", "Equity", "Revenue", "Expense"],
32
+ default: null,
33
+ },
34
+ qbItemId: {
35
+ type: String,
36
+ trim: true,
37
+ default: null,
38
+ },
39
+ qbItemName: {
40
+ type: String,
41
+ trim: true,
42
+ default: null,
43
+ },
44
+ qbItemType: {
45
+ type: String,
46
+ trim: true,
47
+ default: "Service",
48
+ },
49
+ qbIncomeAccountId: {
50
+ type: String,
51
+ trim: true,
52
+ default: null,
53
+ },
54
+ qbIncomeAccountName: {
55
+ type: String,
56
+ trim: true,
57
+ default: null,
58
+ },
59
+ // How this row was populated — invoice_service's auto-matching vs. manual admin pick.
60
+ matchType: {
61
+ type: String,
62
+ enum: ["auto", "manual", "created"],
63
+ default: "manual",
64
+ },
65
+ matchConfidence: {
66
+ type: Number,
67
+ default: null,
68
+ },
69
+ isActive: {
70
+ type: Boolean,
71
+ default: true,
72
+ },
73
+ mappedBy: {
74
+ type: mongoose.Schema.Types.ObjectId,
75
+ default: null,
76
+ },
77
+ lastSyncedAt: {
78
+ type: Date,
79
+ default: null,
80
+ },
81
+ },
82
+ { timestamps: true },
83
+ );
84
+
85
+ quickBooksItemMappingSchema.index({ facilityId: 1, qbConfigId: 1, category: 1 }, { unique: true });
86
+
87
+ module.exports = { schema: quickBooksItemMappingSchema };
@@ -0,0 +1,24 @@
1
+ // CSRF state; new connect for the same qbConfigId invalidates the prior one.
2
+ const mongoose = require("mongoose");
3
+
4
+ const quickBooksOAuthStateSchema = new mongoose.Schema(
5
+ {
6
+ facilityId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: "Facility",
9
+ required: true,
10
+ },
11
+ qbConfigId: {
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: "QuickBooksConfig",
14
+ required: true,
15
+ },
16
+ state: { type: String, required: true },
17
+ createdAt: { type: Date, default: Date.now },
18
+ },
19
+ { timestamps: false },
20
+ );
21
+
22
+ quickBooksOAuthStateSchema.index({ qbConfigId: 1 }, { unique: true });
23
+
24
+ module.exports = { schema: quickBooksOAuthStateSchema };
@@ -0,0 +1,40 @@
1
+ // Shared collection; invoice_service and backend_main both write.
2
+ const mongoose = require("mongoose");
3
+
4
+ const quickBooksOutboundSyncStateSchema = new mongoose.Schema(
5
+ {
6
+ facilityId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: "Facility",
9
+ required: true,
10
+ },
11
+ qbConfigId: {
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: "QuickBooksConfig",
14
+ required: true,
15
+ },
16
+ entity: {
17
+ type: String,
18
+ enum: ["Customer", "Invoice", "Payment", "InvoiceVoid", "Vendor"],
19
+ required: true,
20
+ },
21
+ status: {
22
+ type: String,
23
+ enum: ["not_started", "running", "completed", "error"],
24
+ default: "not_started",
25
+ },
26
+ startedAt: { type: Date, default: null },
27
+ finishedAt: { type: Date, default: null },
28
+ summary: {
29
+ processed: { type: Number, default: 0 },
30
+ skipped: { type: Number, default: 0 },
31
+ errors: { type: Number, default: 0 },
32
+ },
33
+ lastError: { type: String, default: null },
34
+ },
35
+ { timestamps: true },
36
+ );
37
+
38
+ quickBooksOutboundSyncStateSchema.index({ facilityId: 1, qbConfigId: 1, entity: 1 }, { unique: true });
39
+
40
+ module.exports = { schema: quickBooksOutboundSyncStateSchema };
@@ -0,0 +1,60 @@
1
+ // Missing mapping falls back to Undeposited Funds.
2
+ const mongoose = require("mongoose");
3
+
4
+ const quickBooksPaymentChannelMappingSchema = new mongoose.Schema(
5
+ {
6
+ facilityId: {
7
+ type: mongoose.Schema.Types.ObjectId,
8
+ ref: "Facility",
9
+ required: true,
10
+ index: true,
11
+ },
12
+ qbConfigId: {
13
+ type: mongoose.Schema.Types.ObjectId,
14
+ ref: "QuickBooksConfig",
15
+ required: true,
16
+ index: true,
17
+ },
18
+ channel: {
19
+ type: String,
20
+ required: true,
21
+ trim: true,
22
+ },
23
+ channelLabel: {
24
+ type: String,
25
+ trim: true,
26
+ default: null,
27
+ },
28
+ // Null means post to Undeposited Funds.
29
+ qbAccountId: {
30
+ type: String,
31
+ trim: true,
32
+ default: null,
33
+ },
34
+ qbAccountName: {
35
+ type: String,
36
+ trim: true,
37
+ default: null,
38
+ },
39
+ isActive: {
40
+ type: Boolean,
41
+ default: true,
42
+ },
43
+ mappedBy: {
44
+ type: mongoose.Schema.Types.ObjectId,
45
+ default: null,
46
+ },
47
+ lastSyncedAt: {
48
+ type: Date,
49
+ default: null,
50
+ },
51
+ },
52
+ { timestamps: true },
53
+ );
54
+
55
+ quickBooksPaymentChannelMappingSchema.index(
56
+ { facilityId: 1, qbConfigId: 1, channel: 1 },
57
+ { unique: true },
58
+ );
59
+
60
+ module.exports = { schema: quickBooksPaymentChannelMappingSchema };
@@ -0,0 +1,122 @@
1
+ // Unmatched QBO payments staged as needs_review.
2
+ const mongoose = require("mongoose");
3
+
4
+ const appliedInvoiceSchema = new mongoose.Schema(
5
+ {
6
+ invoiceNumber: String,
7
+ payserveInvoiceId: mongoose.Schema.Types.ObjectId,
8
+ qbInvoiceId: String,
9
+ amount: Number,
10
+ },
11
+ { _id: false },
12
+ );
13
+
14
+ const linkedInvoiceSchema = new mongoose.Schema(
15
+ {
16
+ qbInvoiceId: String,
17
+ amount: Number,
18
+ },
19
+ { _id: false },
20
+ );
21
+
22
+ // Tracks invoices this QB payment applied to.
23
+ const appliedPaymentSchema = new mongoose.Schema(
24
+ {
25
+ qbInvoiceId: String,
26
+ cashPaymentId: { type: mongoose.Schema.Types.ObjectId, ref: "CashPayment" },
27
+ payserveInvoiceId: mongoose.Schema.Types.ObjectId,
28
+ invoiceNumber: String,
29
+ amount: Number,
30
+ appliedAt: { type: Date, default: Date.now },
31
+ },
32
+ { _id: false },
33
+ );
34
+
35
+ const quickBooksPaymentMappingSchema = new mongoose.Schema(
36
+ {
37
+ facilityId: {
38
+ type: mongoose.Schema.Types.ObjectId,
39
+ ref: "Facility",
40
+ required: true,
41
+ index: true,
42
+ },
43
+ qbConfigId: {
44
+ type: mongoose.Schema.Types.ObjectId,
45
+ ref: "QuickBooksConfig",
46
+ required: true,
47
+ index: true,
48
+ },
49
+ cashPaymentId: {
50
+ type: mongoose.Schema.Types.ObjectId,
51
+ ref: "CashPayment",
52
+ default: null,
53
+ },
54
+ sourceInvoiceNumber: {
55
+ type: String,
56
+ trim: true,
57
+ default: null,
58
+ },
59
+ channel: {
60
+ type: String,
61
+ default: null,
62
+ },
63
+ totalAmount: {
64
+ type: Number,
65
+ default: null,
66
+ },
67
+ appliedInvoiceNumbers: [appliedInvoiceSchema],
68
+ qbPaymentId: {
69
+ type: String,
70
+ trim: true,
71
+ },
72
+ qbSyncToken: {
73
+ type: String,
74
+ trim: true,
75
+ },
76
+ qbCustomerId: {
77
+ type: String,
78
+ trim: true,
79
+ },
80
+ qbDepositAccountId: {
81
+ type: String,
82
+ trim: true,
83
+ default: null,
84
+ },
85
+ syncStatus: {
86
+ type: String,
87
+ enum: ["synced", "pending", "error", "needs_review", "applied", "pending_hold"],
88
+ default: "pending",
89
+ index: true,
90
+ },
91
+ syncError: {
92
+ type: String,
93
+ default: null,
94
+ },
95
+ lastSyncDirection: {
96
+ type: String,
97
+ enum: ["inbound", "outbound"],
98
+ default: null,
99
+ },
100
+ lastSyncedAt: {
101
+ type: Date,
102
+ default: null,
103
+ },
104
+ // Only set for needs_review rows.
105
+ pendingData: {
106
+ qbCustomerId: String,
107
+ totalAmount: Number,
108
+ deleted: { type: Boolean, default: false },
109
+ linkedInvoices: [linkedInvoiceSchema],
110
+ },
111
+ // PayServe payments created from this QB payment.
112
+ appliedPayments: [appliedPaymentSchema],
113
+ },
114
+ { timestamps: true },
115
+ );
116
+
117
+ quickBooksPaymentMappingSchema.index(
118
+ { facilityId: 1, qbConfigId: 1, qbPaymentId: 1 },
119
+ { unique: true, sparse: true },
120
+ );
121
+
122
+ module.exports = { schema: quickBooksPaymentMappingSchema };
@@ -0,0 +1,79 @@
1
+ // Vendor auto-create forbidden: creates User w/ password.
2
+ // Directory sync only; Bill/Expense out of scope.
3
+ const mongoose = require("mongoose");
4
+
5
+ const quickBooksVendorMappingSchema = new mongoose.Schema(
6
+ {
7
+ facilityId: {
8
+ type: mongoose.Schema.Types.ObjectId,
9
+ ref: "Facility",
10
+ required: true,
11
+ index: true,
12
+ },
13
+ qbConfigId: {
14
+ type: mongoose.Schema.Types.ObjectId,
15
+ ref: "QuickBooksConfig",
16
+ required: true,
17
+ index: true,
18
+ },
19
+ payserveSupplierId: {
20
+ type: mongoose.Schema.Types.ObjectId,
21
+ ref: "Supplier",
22
+ // default:null defeats sparse index
23
+ },
24
+ qbVendorId: {
25
+ type: String,
26
+ trim: true,
27
+ },
28
+ qbSyncToken: {
29
+ type: String,
30
+ trim: true,
31
+ },
32
+ qbDisplayName: {
33
+ type: String,
34
+ trim: true,
35
+ },
36
+ syncStatus: {
37
+ type: String,
38
+ enum: ["synced", "pending", "error", "needs_review"],
39
+ default: "pending",
40
+ index: true,
41
+ },
42
+ syncError: {
43
+ type: String,
44
+ default: null,
45
+ },
46
+ lastSyncDirection: {
47
+ type: String,
48
+ enum: ["inbound", "outbound"],
49
+ default: null,
50
+ },
51
+ lastSyncedAt: {
52
+ type: Date,
53
+ default: null,
54
+ },
55
+ // Raw QBO snapshot for admin review.
56
+ pendingData: {
57
+ displayName: String,
58
+ email: String,
59
+ phone: String,
60
+ taxIdentificationNumber: String,
61
+ notes: String,
62
+ },
63
+ },
64
+ { timestamps: true },
65
+ );
66
+
67
+ // sparse ignores docs missing ALL indexed fields;
68
+ // facilityId/qbConfigId are always set, so sparse never applied.
69
+ // partialFilterExpression is the correct null-safe fix.
70
+ quickBooksVendorMappingSchema.index(
71
+ { facilityId: 1, qbConfigId: 1, payserveSupplierId: 1 },
72
+ { unique: true, partialFilterExpression: { payserveSupplierId: { $exists: true } } },
73
+ );
74
+ quickBooksVendorMappingSchema.index(
75
+ { facilityId: 1, qbConfigId: 1, qbVendorId: 1 },
76
+ { unique: true, sparse: true },
77
+ );
78
+
79
+ module.exports = { schema: quickBooksVendorMappingSchema };