Skip to content

Commit

Permalink
Merge pull request #12 from CampusTecNode/feature/bookings
Browse files Browse the repository at this point in the history
Feature/bookings
  • Loading branch information
David-PX authored Oct 13, 2024
2 parents b8c8319 + 089192a commit fb1a84b
Show file tree
Hide file tree
Showing 16 changed files with 1,090 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\server.js"
}
]
}
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const likesRoutes = require('./src/likes/likesRoutes');
const cartStatusRoutes = require('./src/cartStatus/cartStatusRoutes');
const shoppingCart = require('./src/shoppingCart/shoppingCartRoutes');
const notifications = require('./src/notifications/notificationsRoutes')
const spacesRoutes = require('./src/spaces/spacesRoutes');
const reservationsRoutes = require('./src/reservations/reservationsRoutes');
// TODO: Use verifyRole middleware
const { verifyToken } = require('./src/auth/authMiddleware');

Expand Down Expand Up @@ -94,6 +96,8 @@ app.use('/likes', verifyToken, likesRoutes);
app.use('/cartStatus', verifyToken, cartStatusRoutes);
app.use('/shoppingCart', verifyToken, shoppingCart);
app.use('/notifications', verifyToken, notifications);
app.use('/spaces', verifyToken, spacesRoutes);
app.use('/reservations', verifyToken, reservationsRoutes);



Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"express": "^4.21.0",
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.15",
"pg": "^8.13.0",
"pg-hstore": "^2.3.4",
Expand Down
13 changes: 12 additions & 1 deletion src/data/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const ShoppingCart = require('../../shoppingCart/shoppingCart');
const CartDetails = require('../../cartDetails/cartDetail');
const OrderDetails = require('./orderDetails');
const Notifications = require('../../notifications/notifications');
const Reservations = require('../../reservations/reservations');
const Spaces = require('../../spaces/spaces');

require('../middlewares/updateMiddleware')(sequelize);

Expand Down Expand Up @@ -63,6 +65,13 @@ OrderDetails.belongsTo(Orders, { foreignKey: 'OrderID' });
Users.hasMany(Notifications, { foreignKey: 'UserID' });
Notifications.belongsTo(Users, { foreignKey: 'UserID' });

// Relación entre Usuarios y Reservas
Users.hasMany(Reservations, { foreignKey: 'UserID' });
Reservations.belongsTo(Users, { foreignKey: 'UserID' });

// Relación entre Espacios y Reservas
Spaces.hasMany(Reservations, { foreignKey: 'SpaceID' });
Reservations.belongsTo(Spaces, { foreignKey: 'SpaceID' });

module.exports = {
sequelize,
Expand All @@ -79,5 +88,7 @@ module.exports = {
ShoppingCart,
CartDetails,
OrderDetails,
Notifications
Notifications,
Reservations,
Spaces,
};
107 changes: 106 additions & 1 deletion src/data/seeder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// src/data/seeder.js
const { Users, Roles, UserRoles, Products, Categories, OrderStatus, PaymentMethods, UserLikedProducts, ShoppingCart, CartDetails, Orders, OrderDetails, CartStatus } = require('../../src/data/models/index');
const { Users, Roles, UserRoles, Products, Categories, OrderStatus, PaymentMethods, UserLikedProducts, ShoppingCart, CartDetails, Orders, OrderDetails, CartStatus, Spaces } = require('../../src/data/models/index');

async function seedDatabase() {
try {
Expand Down Expand Up @@ -596,6 +596,111 @@ async function seedDatabase() {
{ OrderID: 4, ProductID: 8, Count: 4, UnitPrice: 6.00 }, // Cuatro marcadores en la orden 4
]);

await Spaces.bulkCreate([
{
Name: 'Mesa de PingPong 1',
Description: 'Mesa de ping pong cerca de la cafetería',
Capacity: 2,
Type: 'PingPong',
Location: 'Edificio A, Planta Baja',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Mesa de PingPong 2',
Description: 'Mesa de ping pong cerca de la cancha deportiva',
Capacity: 2,
Type: 'PingPong',
Location: 'Edificio Deportivo',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Mesa de PingPong 3',
Description: 'Mesa de ping pong cerca de la cancha deportiva',
Capacity: 2,
Type: 'PingPong',
Location: 'Edificio Deportivo',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Mesa de PingPong 4',
Description: 'Mesa de ping pong cerca de la cancha deportiva',
Capacity: 2,
Type: 'PingPong',
Location: 'Edificio Deportivo',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Cubículo de Estudio 1',
Description: 'Cubículo con capacidad para 4 personas, incluye proyector.',
Capacity: 4,
Type: 'Cubiculo',
Location: 'Biblioteca, Piso 1',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Cubículo de Estudio 2',
Description: 'Cubículo con capacidad para 6 personas.',
Capacity: 6,
Type: 'Cubiculo',
Location: 'Biblioteca, Piso 1',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Cubículo de Estudio 3',
Description: 'Cubículo con capacidad para 6 personas.',
Capacity: 6,
Type: 'Cubiculo',
Location: 'Biblioteca, Piso 1',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Cubículo de Estudio 4',
Description: 'Cubículo con capacidad para 6 personas.',
Capacity: 6,
Type: 'Cubiculo',
Location: 'Biblioteca, Piso 2',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Parcela de Emprendimiento 1',
Description: 'Espacio designado para mostrar y promocionar productos de emprendimiento estudiantil.',
Capacity: 10,
Type: 'Parcela',
Location: 'Plaza Central',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},
{
Name: 'Parcela de Emprendimiento 2',
Description: 'Espacio en la entrada principal para emprendimientos estudiantiles',
Capacity: 15,
Type: 'Parcela',
Location: 'Entrada Principal',
Available: true,
CreatedBy: 'System',
UpdatedBy: 'System'
},


]);

console.log('Seeding completed successfully');
} catch (error) {
console.error('Error during seeding:', error);
Expand Down
24 changes: 24 additions & 0 deletions src/reservations/activeSpacesCron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const cron = require('node-cron');
const { Spaces, Reservations } = require('../data/models');
const { Op } = require('sequelize');

cron.schedule('*/2 * * * *', async () => { // Se ejecuta cada 5 minutos
const now = new Date();

// Buscar las reservas que hayan terminado pero que no se hayan marcado como canceladas
const finishedReservations = await Reservations.findAll({
where: {
EndTime: { [Op.lt]: now },
DeletedAt: null,
},
});

for (const reservation of finishedReservations) {
// Cambiar el estado del espacio a disponible
const space = await Spaces.findByPk(reservation.SpaceID);
space.Available = true;
await space.save();
}

console.log('Checked reservations and updated space availability.');
});
67 changes: 67 additions & 0 deletions src/reservations/reservations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../data/connection');

const Reservations = sequelize.define('Reservations', {
ID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
UserID: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Users',
key: 'id',
}
},
SpaceID: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Spaces',
key: 'ID',
}
},
StartDate: {
type: DataTypes.DATE,
allowNull: false,
},
EndDate: {
type: DataTypes.DATE,
allowNull: false,
},
CreatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
CreatedBy: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'System',
},
UpdatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
UpdatedBy: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'System',
},
DeletedAt: {
type: DataTypes.DATE,
allowNull: true,
},
DeletedBy: {
type: DataTypes.STRING,
allowNull: true,
}
}, {
tableName: 'Reservations',
timestamps: false,
});

module.exports = Reservations;
Loading

0 comments on commit fb1a84b

Please sign in to comment.