payservedb 4.5.9 → 4.6.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/package.json +1 -1
- package/src/models/water_meters.js +106 -43
package/package.json
CHANGED
|
@@ -1,47 +1,110 @@
|
|
|
1
|
-
const
|
|
2
|
-
const { getModel } = require('../../../../utils/getModel');
|
|
1
|
+
const mongoose = require('mongoose');
|
|
3
2
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const waterMeterSchema = 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
|
+
});
|
|
7
105
|
|
|
8
|
-
|
|
9
|
-
const unitModel = await getModel('Unit', payservedb.Unit.schema, facilityId);
|
|
106
|
+
waterMeterSchema.index({ meterNumber: 1, status: 1 });
|
|
10
107
|
|
|
11
|
-
|
|
12
|
-
const unit = await unitModel.findById(unitId);
|
|
13
|
-
if (!unit) {
|
|
14
|
-
return reply.code(404).send({
|
|
15
|
-
success: false,
|
|
16
|
-
error: 'Unit not found'
|
|
17
|
-
});
|
|
18
|
-
}
|
|
108
|
+
const WaterMeter = mongoose.model('WaterMeter', waterMeterSchema);
|
|
19
109
|
|
|
20
|
-
|
|
21
|
-
const meter = await analogMeterModel.findOne({ unitId: unitId });
|
|
22
|
-
if (!meter) {
|
|
23
|
-
return reply.code(404).send({
|
|
24
|
-
success: false,
|
|
25
|
-
error: 'No meter found for this unit'
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Return only the simplified meter information
|
|
30
|
-
return reply.code(200).send({
|
|
31
|
-
status: meter.status,
|
|
32
|
-
meterNumber: meter.meterNumber,
|
|
33
|
-
currentReading: meter.currentReading,
|
|
34
|
-
previousReading: meter.previousReading,
|
|
35
|
-
lastReadingDate: meter.lastReadingDate,
|
|
36
|
-
lastUpdated: meter.updatedAt
|
|
37
|
-
});
|
|
38
|
-
} catch (err) {
|
|
39
|
-
console.error('Error in retrieving analog meter details by unit:', err);
|
|
40
|
-
return reply.code(400).send({
|
|
41
|
-
success: false,
|
|
42
|
-
error: err.message
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
module.exports = getMeterDetailsByUnit;
|
|
110
|
+
module.exports = WaterMeter;
|