Skip to content

Commit b5cfaf7

Browse files
Merge pull request #2020 from OneCommunityGlobal/Akshith-create-suppliers-orders-models
Akshith - Kitchen Inventory Management - Build database models for orders and suppliers
2 parents d3e487e + 872f720 commit b5cfaf7

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const mongoose = require('mongoose');
2+
3+
const { Schema } = mongoose;
4+
5+
const Order = new Schema({
6+
supplierId: {
7+
type: Schema.Types.ObjectId,
8+
ref: 'supplier',
9+
required: true,
10+
},
11+
status: {
12+
type: String,
13+
required: true,
14+
enum: ['Pending', 'Ordered', 'Shipped', 'Delivered', 'Cancelled'],
15+
default: 'Pending',
16+
},
17+
orderDate: {
18+
type: Date,
19+
default: Date.now,
20+
},
21+
expectedDeliveryDate: {
22+
type: Date,
23+
},
24+
actualDeliveryDate: {
25+
type: Date,
26+
},
27+
items: [
28+
{
29+
itemName: {
30+
type: String,
31+
required: true,
32+
trim: true,
33+
},
34+
quantity: {
35+
type: Number,
36+
required: true,
37+
min: 1,
38+
},
39+
pricePerItem: {
40+
type: Number,
41+
required: true,
42+
min: 0,
43+
},
44+
},
45+
],
46+
totalAmount: {
47+
type: Number,
48+
default: 0,
49+
},
50+
created: {
51+
type: Date,
52+
default: Date.now,
53+
},
54+
});
55+
56+
Order.pre('save', function (next) {
57+
if (this.items && this.items.length > 0) {
58+
this.totalAmount = this.items.reduce((sum, item) => sum + item.quantity * item.pricePerItem, 0);
59+
}
60+
next();
61+
});
62+
63+
module.exports = mongoose.model('order', Order, 'orders');
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const mongoose = require('mongoose');
2+
3+
const { Schema } = mongoose;
4+
5+
const Supplier = new Schema({
6+
name: {
7+
type: String,
8+
required: true,
9+
trim: true,
10+
},
11+
contact: {
12+
type: String,
13+
trim: true,
14+
},
15+
email: {
16+
type: String,
17+
required: true,
18+
lowercase: true,
19+
match: [/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/, 'Provide Valid email address'],
20+
},
21+
phone: {
22+
type: String,
23+
required: true,
24+
trim: true,
25+
},
26+
specialities: [
27+
{
28+
type: String,
29+
trim: true,
30+
},
31+
],
32+
website: {
33+
type: String,
34+
trim: true,
35+
match: [
36+
/^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/,
37+
'Provide valid website url',
38+
],
39+
},
40+
isActive: {
41+
type: Boolean,
42+
default: true,
43+
},
44+
created: {
45+
type: Date,
46+
required: true,
47+
default: Date.now,
48+
},
49+
updated: {
50+
type: Date,
51+
default: Date.now,
52+
},
53+
});
54+
55+
module.exports = mongoose.model('supplier', Supplier, 'suppliers');

0 commit comments

Comments
 (0)