-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseSchema
More file actions
219 lines (197 loc) · 10 KB
/
databaseSchema
File metadata and controls
219 lines (197 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// File: backend/models/User.js
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const userSchema = new mongoose.Schema({
name: { type: String, required: true, trim: true },
email: { type: String, required: true, unique: true, lowercase: true, match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address'] },
phone: { type: String, trim: true },
profilePhoto: { type: String },
passwordHash: { type: String, required: true, minlength: 6, select: false },
passwordResetToken: String,
tokenExpiry: Date,
roleId: { type: mongoose.Schema.Types.ObjectId, ref: 'Role', required: true },
lastLogin: { type: Date },
isActive: { type: Boolean, default: true },
}, { timestamps: true });
userSchema.pre('save', async function(next) {
if (!this.isModified('passwordHash')) return next();
const salt = await bcrypt.genSalt(12);
this.passwordHash = await bcrypt.hash(this.passwordHash, salt);
next();
});
userSchema.methods.comparePassword = async function(candidatePassword) {
return await bcrypt.compare(candidatePassword, this.passwordHash);
};
module.exports = mongoose.model('User', userSchema);
// ---
// File: backend/models/Role.js
const mongoose = require('mongoose');
const roleSchema = new mongoose.Schema({
name: { type: String, required: true, unique: true, trim: true },
description: { type: String, trim: true },
permissions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Permission' }],
isDefault: { type: Boolean, default: false },
}, { timestamps: true });
module.exports = mongoose.model('Role', roleSchema);
// ---
// File: backend/models/Permission.js
const mongoose = require('mongoose');
const permissionSchema = new mongoose.Schema({
module: { type: String, required: true, trim: true },
action: { type: String, required: true, trim: true },
scope: { type: String, trim: true, enum: ['own', 'all', 'department'] },
description: { type: String, trim: true },
}, { indexes: [{ unique: true, fields: ['module', 'action'] }] });
module.exports = mongoose.model('Permission', permissionSchema);
// ---
// File: backend/models/AuditLog.js
const mongoose = require('mongoose');
const auditLogSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
action: { type: String, required: true, trim: true },
actionType: { type: String, required: true, enum: ['CREATE', 'UPDATE', 'DELETE', 'LOGIN_SUCCESS', 'LOGIN_FAIL', 'PASSWORD_RESET', 'PERMISSION_CHANGE'] },
module: { type: String, required: true, trim: true },
recordId: { type: String, trim: true },
previousValues: { type: mongoose.Schema.Types.Mixed },
newValues: { type: mongoose.Schema.Types.Mixed },
ipAddress: { type: String },
metadata: { type: mongoose.Schema.Types.Mixed },
}, { timestamps: { createdAt: true, updatedAt: false } });
auditLogSchema.index({ userId: 1 });
auditLogSchema.index({ module: 1, actionType: 1 });
auditLogSchema.index({ createdAt: -1 });
module.exports = mongoose.model('AuditLog', auditLogSchema);
// ---
// File: backend/models/Product.js
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
sku: { type: String, required: true, unique: true, trim: true },
name: { type: String, required: true, trim: true },
description: { type: String, trim: true },
categoryId: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true },
productType: { type: String, enum: ['standard', 'bundle', 'service', 'subscription'], default: 'standard' },
brand: { type: String, trim: true },
supplierSku: { type: String, trim: true },
unit: { type: String, default: 'pcs' },
weight: { type: Number },
dimensions: { length: { type: Number }, width: { type: Number }, height: { type: Number } },
b2cPrice: { type: Number, required: true, default: 0 },
b2bPrice: { type: Number },
costPrice: { type: Number },
purchaseTaxRate: { type: Number, default: 0 },
salesTaxRate: { type: Number, default: 0 },
taxCategory: { type: String },
expiryDate: { type: Date },
manufactureDate: { type: Date },
shelfLifeDays: { type: Number },
storageConditions: { type: String },
lotNumber: { type: String },
serialNumbers: { type: [String], default: [] },
minOrderQty: { type: Number, default: 1 },
maxOrderQty: { type: Number },
reorderLevel: { type: Number },
attributes: [{ key: { type: String }, value: { type: String } }],
tags: { type: [String], default: [] },
images: { type: [String], default: [] },
videos: { type: [String], default: [] },
status: { type: String, enum: ['active', 'inactive', 'discontinued'], default: 'active' },
}, { timestamps: true });
module.exports = mongoose.model('Product', productSchema);
// ---
// File: backend/models/Category.js
const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({
name: { type: String, required: true, trim: true, unique: true },
parentId: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', default: null },
tags: { type: [String], default: [] }
}, { timestamps: true });
module.exports = mongoose.model('Category', categorySchema);
// ---
// File: backend/models/Warehouse.js
const mongoose = require('mongoose');
const warehouseSchema = new mongoose.Schema({
name: { type: String, required: true, trim: true, unique: true },
location: { type: String, required: true, trim: true },
managerId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
}, { timestamps: true });
module.exports = mongoose.model('Warehouse', warehouseSchema);
// ---
// File: backend/models/Supplier.js
const mongoose = require('mongoose');
const supplierSchema = new mongoose.Schema({
name: { type: String, required: true, trim: true, unique: true },
contactName: { type: String, trim: true },
phone: { type: String, trim: true },
email: { type: String, trim: true, lowercase: true, match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address'] },
address: { type: String, trim: true },
leadTimeDays: { type: Number, min: 0 },
rating: { type: Number, min: 1, max: 5 },
}, { timestamps: true });
module.exports = mongoose.model('Supplier', supplierSchema);
// ---
// File: backend/models/Customer.js
const mongoose = require('mongoose');
const customerSchema = new mongoose.Schema({
name: { type: String, required: true, trim: true, unique: true },
phone: { type: String, trim: true },
email: { type: String, trim: true, lowercase: true, match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address'] },
address: { type: String, trim: true },
loyaltyPoints: { type: Number, default: 0 },
taxExempt: { type: Boolean, default: false },
}, { timestamps: true });
module.exports = mongoose.model('Customer', customerSchema);
// ---
// File: backend/models/PurchaseOrder.js
const mongoose = require('mongoose');
const poItemSchema = new mongoose.Schema({
productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
quantity: { type: Number, required: true, min: 1 },
cost: { type: Number, required: true, min: 0 }
}, { _id: false });
const purchaseOrderSchema = new mongoose.Schema({
supplierId: { type: mongoose.Schema.Types.ObjectId, ref: 'Supplier', required: true },
orderDate: { type: Date, default: Date.now },
status: { type: String, required: true, enum: ['draft', 'pending', 'received', 'cancelled'], default: 'draft' },
items: [poItemSchema],
totalAmount: { type: Number, required: true },
approvedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
expectedDate: { type: Date },
receivedDate: { type: Date },
}, { timestamps: true });
module.exports = mongoose.model('PurchaseOrder', purchaseOrderSchema);
// ---
// File: backend/models/SalesOrder.js
const mongoose = require('mongoose');
const soItemSchema = new mongoose.Schema({
productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
quantity: { type: Number, required: true, min: 1 },
price: { type: Number, required: true, min: 0 }
}, { _id: false });
const salesOrderSchema = new mongoose.Schema({
customerId: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true },
salesPersonId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
orderDate: { type: Date, default: Date.now },
status: { type: String, required: true, enum: ['draft', 'paid', 'cancelled', 'returned'], default: 'draft' },
shipmentStatus: { type: String, required: true, enum: ['pending', 'shipped', 'delivered'], default: 'pending' },
items: [soItemSchema],
subtotal: { type: Number, required: true },
discounts: { type: Number, default: 0 },
taxAmount: { type: Number, default: 0 },
totalAmount: { type: Number, required: true },
paymentMethod: { type: String },
shippingAddress: { type: String, trim: true },
notes: { type: String, trim: true },
invoiced: { type: Boolean, default: false },
invoiceId: { type: String, trim: true },
}, { timestamps: true });
module.exports = mongoose.model('SalesOrder', salesOrderSchema);
// ---
// File: backend/models/ProductFieldConfig.js
const mongoose = require('mongoose');
const ALL_PRODUCT_FIELDS = [ 'id', 'sku', 'name', 'description', 'categoryId', 'productType', 'brand', 'supplierSku', 'unit', 'weight', 'dimensions', 'b2cPrice', 'b2bPrice', 'costPrice', 'purchaseTaxRate', 'salesTaxRate', 'taxCategory', 'expiryDate', 'manufactureDate', 'shelfLifeDays', 'storageConditions', 'lotNumber', 'serialNumbers', 'minOrderQty', 'maxOrderQty', 'reorderLevel', 'attributes', 'tags', 'images', 'videos', 'status', 'createdAt', 'updatedAt' ];
const DEFAULT_FIELDS = [ 'sku', 'name', 'description', 'b2cPrice', 'status' ];
const productFieldConfigSchema = new mongoose.Schema({
configKey: { type: String, default: 'main', unique: true },
activeFields: { type: [String], enum: ALL_PRODUCT_FIELDS, default: DEFAULT_FIELDS }
});
module.exports = mongoose.model('ProductFieldConfig', productFieldConfigSchema);