payservedb 5.6.2 → 5.6.3

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
@@ -172,7 +172,8 @@ const models = {
172
172
  PendingCredential: require('./src/models/pendingCredentials'),
173
173
  UnitManagementTemplate: require('./src/models/unitManagementTemplate'),
174
174
  PropertyManagerRevenue: require('./src/models/propertyManagerRevenue'),
175
- PropertyManagerContract: require('./src/models/propertyManagerContract')
175
+ PropertyManagerContract: require('./src/models/propertyManagerContract'),
176
+ BillerAddress: require('./src/models/billerAddress')
176
177
  };
177
178
 
178
179
  // Function to get models dynamically from a specific database connection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "5.6.2",
3
+ "version": "5.6.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -0,0 +1,117 @@
1
+ const mongoose = require("mongoose");
2
+
3
+ // Define the schema for Biller Address
4
+ const billerAddressSchema = new mongoose.Schema(
5
+ {
6
+ name: {
7
+ type: String,
8
+ required: [true, "Address name is required"],
9
+ trim: true,
10
+ minlength: [1, "Address name must be at least 1 character long"],
11
+ },
12
+ companyName: {
13
+ type: String,
14
+ required: [true, "Company name is required"],
15
+ trim: true,
16
+ },
17
+ website: {
18
+ type: String,
19
+ trim: true,
20
+ validate: {
21
+ validator: function (v) {
22
+ if (!v) return true; // Optional field
23
+ return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-])\/?$/.test(v);
24
+ },
25
+ message: "Please enter a valid website URL"
26
+ }
27
+ },
28
+ email: {
29
+ type: String,
30
+ required: [true, "Email is required"],
31
+ trim: true,
32
+ lowercase: true,
33
+ validate: {
34
+ validator: function (v) {
35
+ return /^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/.test(v);
36
+ },
37
+ message: "Please enter a valid email address"
38
+ }
39
+ },
40
+ phone: {
41
+ type: String,
42
+ trim: true,
43
+ },
44
+ address: {
45
+ type: String,
46
+ required: [true, "Address is required"],
47
+ trim: true,
48
+ },
49
+ city: {
50
+ type: String,
51
+ required: [true, "City is required"],
52
+ trim: true,
53
+ },
54
+ state: {
55
+ type: String,
56
+ trim: true,
57
+ },
58
+ country: {
59
+ type: String,
60
+ required: [true, "Country is required"],
61
+ trim: true,
62
+ default: "Kenya"
63
+ },
64
+ postalCode: {
65
+ type: String,
66
+ trim: true,
67
+ },
68
+ logo: {
69
+ type: String, // Will store the file path/URL of the uploaded logo
70
+ required: false,
71
+ },
72
+ isDefault: {
73
+ type: Boolean,
74
+ default: false,
75
+ },
76
+ facilityId: {
77
+ type: mongoose.Schema.Types.ObjectId,
78
+ ref: "Facility",
79
+ required: true,
80
+ },
81
+ createdBy: {
82
+ type: mongoose.Schema.Types.ObjectId,
83
+ ref: "User",
84
+ },
85
+ updatedBy: {
86
+ type: mongoose.Schema.Types.ObjectId,
87
+ ref: "User",
88
+ }
89
+ },
90
+ {
91
+ timestamps: true,
92
+ }
93
+ );
94
+
95
+ // Index for efficient queries
96
+ billerAddressSchema.index({ facilityId: 1 });
97
+ billerAddressSchema.index({ facilityId: 1, isDefault: 1 });
98
+
99
+ // Pre-save middleware to ensure only one default address per facility
100
+ billerAddressSchema.pre('save', async function (next) {
101
+ if (this.isDefault && this.isModified('isDefault')) {
102
+ // If this address is being set as default, unset all other defaults for this facility
103
+ await this.constructor.updateMany(
104
+ {
105
+ facilityId: this.facilityId,
106
+ _id: { $ne: this._id }
107
+ },
108
+ { $set: { isDefault: false } }
109
+ );
110
+ }
111
+ next();
112
+ });
113
+
114
+ // Compile the model from the schema
115
+ const BillerAddress = mongoose.model("BillerAddress", billerAddressSchema);
116
+
117
+ module.exports = BillerAddress;