-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
145 lines (118 loc) · 3.19 KB
/
server.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
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
const dotenv = require('dotenv');
const sequelize = require('./config/db');
const schedule = require('node-schedule');
const Room = require('./models/roomModel');
const Employee = require('./models/employeeModel');
const Guest = require('./models/guestModel');
const RoomType = require('./models/roomTypeModel');
const Reservation = require('./models/reservationModel');
const Hotel = require('./models/hotelModel');
const RentalRoom = require('./models/rentalRoomModel');
const RentalRoomReservation = require('./models/rentalRoomReservationModel');
const RentalReservationReceipt = require('./models/rentalReservationReceiptModel');
const Resident = require('./models/residentModel');
const {
updateRentalRoomReserationStatus,
} = require('./controllers/rentalRoomReservationsController');
const {
checkoutExpiredReservation,
} = require('./controllers/reservationsController');
const {
unHandledRejection,
unCaughtException,
} = require('./controllers/errorController');
dotenv.config();
unCaughtException();
const app = require('./app');
// sequelize
Guest.hasMany(Reservation, {
constraints: true,
onDelete: 'CASCADE',
});
Reservation.belongsTo(Guest, {
constraints: true,
onDelete: 'CASCADE',
});
RoomType.hasMany(Room, {
constraints: true,
onDelete: 'CASCADE',
});
Room.belongsTo(RoomType, {
constraints: true,
onDelete: 'CASCADE',
});
Employee.hasMany(Reservation, {
constraints: true,
onDelete: 'CASCADE',
});
Reservation.belongsTo(Employee, {
constraints: true,
onDelete: 'CASCADE',
});
Hotel.hasMany(RoomType, {
constraints: true,
onDelete: 'CASCADE',
});
RoomType.belongsTo(Hotel, {
constraints: true,
onDelete: 'CASCADE',
});
Hotel.hasMany(RentalRoom, {
constraints: true,
onDelete: 'CASCADE',
});
RentalRoom.belongsTo(Hotel, {
constraints: true,
onDelete: 'CASCADE',
});
Resident.hasMany(RentalRoomReservation, {
constraints: true,
onDelete: 'CASCADE',
});
RentalRoomReservation.belongsTo(Resident, {
constraints: true,
onDelete: 'CASCADE',
});
RentalRoom.hasMany(RentalRoomReservation, {
constraints: true,
onDelete: 'CASCADE',
});
RentalRoomReservation.belongsTo(RentalRoom);
Employee.hasMany(RentalRoomReservation, {
constraints: true,
onDelete: 'CASCADE',
});
RentalRoomReservation.belongsTo(Employee);
RentalRoomReservation.hasMany(RentalReservationReceipt);
RentalReservationReceipt.belongsTo(RentalRoomReservation);
Resident.hasMany(RentalReservationReceipt);
RentalReservationReceipt.belongsTo(Resident);
sequelize
.sync({ force: true })
.then(() => {
// Employee.create({
// firstName: 'Admin',
// lastName: 'Admin',
// email: '[email protected]',
// password: '1234abcd',
// salary: 10000,
// dateOfBirth: '1998-01-01',
// phone: '0912345678',
// role: 'admin',
// hiredAt: '2021-01-01',
// });
console.log('DB connection successful!');
})
.catch((err) => {
console.log('DB connection failed!');
});
const port = process.env.PORT || 5000;
const server = app.listen(port, () => {
console.log(`App listening on port ${port}!`);
schedule.scheduleJob('*/1 * * * *', async () => {
await checkoutExpiredReservation();
await updateRentalRoomReserationStatus();
// console.log('running a task every minute');
});
});
unHandledRejection(server);