payservedb 4.2.5 → 4.2.7

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
@@ -9,22 +9,24 @@ const connections = {};
9
9
  // Utility function to connect to MongoDB
10
10
  async function connectToMongoDB(dbName, secured, username, password, url, port) {
11
11
  try {
12
- if (secured === true) {
13
- // This should be the secure connection with credentials
14
- const connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}?authSource=admin`;
12
+ if (secured === false) {
13
+ const url_ = url + ":" + port;
14
+ const connectionString = `mongodb://${url_}/${dbName}`;
15
15
  await mongoose.connect(connectionString, {
16
16
  useNewUrlParser: true,
17
17
  });
18
- console.log('Connected to MongoDB with security');
18
+ console.log('Connected to MongoDB');
19
19
  }
20
20
  else {
21
- // This is the unsecured connection
22
- const connectionString = `mongodb://${url}:${port}/${dbName}`;
21
+ const url_ = `${username}:${password}@${url}${port}`;
22
+ const source = '?authSource=admin'
23
+ const connectionString = `mongodb://${url_}/${dbName}${source}`;
23
24
  await mongoose.connect(connectionString, {
24
25
  useNewUrlParser: true,
25
26
  });
26
- console.log('Connected to MongoDB without security');
27
+ console.log('Connected to MongoDB');
27
28
  }
29
+
28
30
  } catch (err) {
29
31
  console.error('Error connecting to MongoDB:', err);
30
32
  throw err;
@@ -33,19 +35,13 @@ async function connectToMongoDB(dbName, secured, username, password, url, port)
33
35
 
34
36
 
35
37
  // Function to switch databases dynamically
36
- async function switchDB(dbName, secured, username, password, url, port) {
38
+ async function switchDB(dbName) {
37
39
  // Check if there's already a connection to the desired database
38
40
  if (connections[dbName]) {
39
41
  return connections[dbName]; // Return existing connection
40
42
  }
41
43
  try {
42
- let connectionString;
43
- if (secured === true) {
44
- connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}?authSource=admin`;
45
- } else {
46
- connectionString = `mongodb://${url}:${port}/${dbName}`;
47
- }
48
-
44
+ const connectionString = `mongodb://${url}/${dbName}${source}`;
49
45
  const dbConnection = await mongoose.createConnection(connectionString, {
50
46
  useNewUrlParser: true,
51
47
  });
@@ -131,7 +127,7 @@ const models = {
131
127
  FacilityPaymentDetails: require('./src/models/facility_payment_details'),
132
128
  DefaultPaymentDetails: require('./src/models/default_payment_details'),
133
129
  Currency: require('./src/models/currency_settings'),
134
- UserAccount: require('./src/models/user_account'),
130
+ UserAccount: require('./src/models/water_meter_account'),
135
131
  CashPayment: require('./src/models/cashpayment'),
136
132
  VasPayment: require('./src/models/vas_payments'),
137
133
  VasInvoicesQuickBooks: require('./src/models/vas_invoices_upload'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "4.2.5",
3
+ "version": "4.2.7",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -63,13 +63,7 @@ const userAccountSchema = new mongoose.Schema({
63
63
  },
64
64
  meter_id: {
65
65
  type: mongoose.Schema.Types.ObjectId,
66
- required: true,
67
- refPath: 'meterModel'
68
- },
69
- meterModel: {
70
- type: String,
71
- required: true,
72
- enum: ['Meter', 'AnalogMeter']
66
+ ref: 'Meter',
73
67
  }
74
68
  }, {
75
69
  timestamps: true
@@ -0,0 +1,110 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const meterSchema = new mongoose.Schema({
4
+ meterType: {
5
+ type: String,
6
+ required: true,
7
+ enum: ['analog', 'smart']
8
+ },
9
+ meterNumber: {
10
+ type: String,
11
+ required: true,
12
+ unique: true,
13
+ trim: true,
14
+ index: true
15
+ },
16
+ accountNumber: {
17
+ type: String,
18
+ required: true,
19
+ unique: true,
20
+ trim: true
21
+ },
22
+ facilityId: {
23
+ type: mongoose.Schema.Types.ObjectId,
24
+ ref: 'Facility',
25
+ },
26
+ unitId: {
27
+ type: mongoose.Schema.Types.ObjectId,
28
+ ref: 'Unit',
29
+ },
30
+ customerId: {
31
+ type: mongoose.Schema.Types.ObjectId,
32
+ ref: 'Customer',
33
+ },
34
+ manufacturer: {
35
+ type: mongoose.Schema.Types.ObjectId,
36
+ ref: 'MeterManufacturer',
37
+ },
38
+ protocol: {
39
+ type: mongoose.Schema.Types.ObjectId,
40
+ ref: 'MeterProtocol',
41
+ },
42
+ size: {
43
+ type: mongoose.Schema.Types.ObjectId,
44
+ ref: 'MeterSize',
45
+ },
46
+ initialReading: {
47
+ type: Number,
48
+ required: true,
49
+ min: 0,
50
+ default: 0
51
+ },
52
+ previousReading: {
53
+ type: Number,
54
+ min: 0,
55
+ },
56
+ currentReading: {
57
+ type: Number,
58
+ min: 0,
59
+ default: 0
60
+ },
61
+ lastReadingDate: {
62
+ type: Date,
63
+ default: Date.now
64
+ },
65
+ readingHistory: [{
66
+ previousReading: Number,
67
+ currentReading: Number,
68
+ readingDate: {
69
+ type: Date,
70
+ default: Date.now
71
+ },
72
+ readBy: String,
73
+ consumption: Number
74
+ }],
75
+ status: {
76
+ type: String,
77
+ required: true,
78
+ enum: ['open', 'closed', 'maintenance', 'faulty'],
79
+ default: 'open',
80
+ },
81
+ customerType: {
82
+ type: String,
83
+ enum: ['postpaid', 'prepaid'],
84
+ },
85
+ isInstalled: {
86
+ type: Boolean,
87
+ default: false
88
+ },
89
+ valveType: { // Specific to smart meters
90
+ type: String,
91
+ enum: ['automatic', 'manual'],
92
+ default: 'automatic'
93
+ },
94
+ accountBalance: { // Specific to smart meters
95
+ type: Number,
96
+ default: 0
97
+ },
98
+ negativeBalance: { // Specific to smart meters
99
+ type: Number,
100
+ default: 0
101
+ },
102
+ }, {
103
+ timestamps: true
104
+ });
105
+
106
+ meterSchema.index({ meterNumber: 1, status: 1 });
107
+
108
+ const Meter = mongoose.model('Meter', meterSchema);
109
+
110
+ module.exports = Meter;