-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-ex.js
More file actions
240 lines (225 loc) · 6.7 KB
/
seed-ex.js
File metadata and controls
240 lines (225 loc) · 6.7 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const connectDB = require("./config/db");
const Role = require("./models/Role");
const User = require("./models/User");
const Permission = require("./models/Permission");
const Restaurant = require("./models/Restaurant");
const Item = require("./models/Item");
const Order = require("./models/Order");
const seedDatabase = async () => {
await connectDB();
// Clear existing Data
await Role.deleteMany();
await User.deleteMany();
await Permission.deleteMany();
await Restaurant.deleteMany();
await Item.deleteMany();
await Order.deleteMany();
// Define initial permissions
const permissions = [
{ name: "CREATE_USER", description: "Create a new user" },
{ name: "UPDATE_USER", description: "Update an existing user" },
{ name: "DELETE_USER", description: "Delete an existing user" },
{ name: "VIEW_USER", description: "View an existing user" },
{ name: "CREATE_RESTAURANT", description: "Create a new restaurant" },
{ name: "UPDATE_RESTAURANT", description: "Update an existing restaurant" },
{ name: "DELETE_RESTAURANT", description: "Delete an existing restaurant" },
{ name: "CREATE_REVIEW", description: "Create a new review" },
{ name: "UPDATE_REVIEW", description: "Update an existing review" },
{ name: "DELETE_REVIEW", description: "Delete an existing review" },
{ name: "CREATE_LIKE", description: "Create a new like" },
{ name: "DELETE_LIKE", description: "Delete an existing like" },
{ name: "CREATE_FAVORITE", description: "Create a new favorite" },
{ name: "DELETE_FAVORITE", description: "Delete an existing favorite" },
{ name: "TAKE_ORDER", description: "Take an order" },
];
// Create permissions
const initialPermissions = await Permission.insertMany(permissions);
console.log("Permissions created: ", initialPermissions);
// Define initial roles
const roles = [
{
name: "Admin",
permissions: [...initialPermissions.map((permission) => permission._id)],
},
{
name: "Client",
permissions: [
initialPermissions[4]._id,
initialPermissions[10]._id,
initialPermissions[11]._id,
initialPermissions[12]._id,
initialPermissions[13]._id,
],
},
{
name: "Manager",
permissions: [
initialPermissions[4]._id,
initialPermissions[5]._id,
initialPermissions[6]._id,
initialPermissions[9]._id,
],
},
{
name: "Delivery",
permissions: [initialPermissions[14]._id],
},
];
// Create roles
const initialRoles = await Role.insertMany(roles);
console.log("Roles created: ", initialRoles);
// Define initial users
const users = [
{
fullname: { fname: "Admin", lname: "Test" },
username: "Admin",
email: "admin@email.com",
phoneNumber: "1234567890",
password: "password",
CIN: "AA123456",
roles: [initialRoles[0]._id],
isVerified: true,
},
{
fullname: { fname: "Client", lname: "Test" },
username: "Client",
email: "client@email.com",
phoneNumber: "1234522310",
password: "password",
CIN: "CC123456",
roles: [initialRoles[1]._id],
isVerified: true,
},
{
fullname: { fname: "Manager", lname: "Test" },
username: "Manager",
email: "manager@email.com",
phoneNumber: "1234542410",
password: "password",
CIN: "MM123456",
roles: [initialRoles[2]._id],
isVerified: true,
},
{
fullname: { fname: "Delivery", lname: "Test" },
username: "Delivery",
email: "delivery@email.com",
phoneNumber: "1234562290",
password: "password",
CIN: "DD123456",
roles: [initialRoles[3]._id],
isVerified: true,
},
{
fullname: { fname: "Client 2", lname: "Test" },
username: "Client2",
email: "client2@email.com",
phoneNumber: "0987654321",
password: "password",
CIN: "CC654321",
roles: [initialRoles[1]._id],
isVerified: true,
},
{
fullname: { fname: "Manager 2", lname: "Test" },
username: "Manager2",
email: "manager2@email.com",
phoneNumber: "1122334455",
password: "password",
CIN: "MM654321",
roles: [initialRoles[2]._id],
isVerified: true,
},
];
// Create users
const initialUsers = await User.insertMany(users);
console.log("Users created: ", initialUsers);
// Create restaurants
const initialRestaurants = await Restaurant.insertMany([
{
name: "Restaurant 1",
address: "Address 1",
phoneNumber: "1122334455",
website: "",
description: "Description 1",
owner: initialUsers[2]._id,
openAt: "08:00",
closeAt: "22:00",
category: {
name: "Fast Food",
description: "Fast Food",
},
},
{
name: "Restaurant 2",
address: "Address 2",
phoneNumber: "2233445566",
website: "http://restaurant2.com",
description: "Description 2",
owner: initialUsers[2]._id,
openAt: "10:00",
closeAt: "23:00",
category: {
name: "Italian",
description: "Italian Cuisine",
},
},
]);
console.log("Restaurants created: ", initialRestaurants);
// Create items
const initialItems = await Item.insertMany([
{
name: "Item 1",
description: "Description 1",
price: 10,
restaurant: initialRestaurants[0]._id,
category: "Category 1",
},
{
name: "Item 2",
description: "Description 2",
price: 15,
restaurant: initialRestaurants[1]._id,
category: "Category 2",
},
]);
console.log("Items created: ", initialItems);
// Create orders
const seedOrders = async () => {
const orders = [
{
user: initialUsers[1]._id, // Replace with a valid user ID
items: [
{
item: initialItems[0]._id, // Replace with a valid item ID
quantity: 2,
},
{
item: initialItems[1]._id, // Replace with a valid item ID
quantity: 1,
},
],
delivery: initialUsers[3]._id, // Replace with a valid user ID if necessary
status: "Pending",
totalAmount: 100.0,
paymentId: "payment12345", // Provide a valid payment ID
isDelivered: false,
},
// Add more orders as needed
];
try {
await Order.insertMany(orders);
console.log("Orders seeded successfully!");
} catch (error) {
console.error("Error seeding the orders: ", error);
}
};
// Call the seed function and wait for it to complete
await seedOrders();
// Close the connection
process.exit(0);
};
seedDatabase().catch((error) => {
console.error("Error seeding the database: ", error);
process.exit(1);
});