-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
76 lines (62 loc) · 2.5 KB
/
database.js
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
const express = require("express");
const mongoose = require("mongoose");
const app = express();
require('dotenv').config();
MONGO_URL = process.env.MONGO_URL;
// Connect to MongoDB
mongoose.connect(
MONGO_URL,
{ useNewUrlParser: true, useUnifiedTopology: true }
).then(() => {
console.log("Connected to MongoDB MowManDB!");
}).catch((err) => {
console.log("Error connecting to MongoDB MowManDB! in database.js");
console.error(err);
});
// Define the user schema
const userSchema = new mongoose.Schema({
google_id: { type: String, unique: true },
profile_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile' },
created_at: { type: Date, default: Date.now },
});
// Define the profile schema
const profileSchema = new mongoose.Schema({
user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, // Reference to the user model
name: String,
email: String,
customers: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Customer' }], // Reference to the customer model
appointments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Appointment' }], // Reference to the appointment model
});
// Define the customer schema
const customerSchema = new mongoose.Schema({
name: String,
email: String,
phone: String,
associated_profile_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile' }, // Reference to the profile model
});
// appointment schema
const appointmentSchema = new mongoose.Schema({
associated_customer_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer' },
associated_profile_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Profile' }, // Reference to the user model
name: String,
email: String,
phone: String,
date: Date,
time: String,
cost: String,
notes: String,
status: { type: Boolean, default: true }
});
// Define the settings schema
const settingSchema = new mongoose.Schema({
associated_profile_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Profile' }, // Reference to the profile model
sendCustomerEmailNotifications: { type: Boolean, default: true }
});
// Create the models
const User = mongoose.model("User", userSchema);
const Profile = mongoose.model("Profile", profileSchema);
const Customer = mongoose.model("Customer", customerSchema);
const Appointment = mongoose.model("Appointment", appointmentSchema);
const Setting = mongoose.model("Setting", settingSchema);
// Export the models
module.exports = { User, Profile, Customer, Appointment, Setting};