Skip to content

Commit

Permalink
fix: removed all garbage UI and starting from scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
AdibSadman192 committed Nov 27, 2024
1 parent 1ab9c6c commit 898e3da
Show file tree
Hide file tree
Showing 125 changed files with 1,323 additions and 22,208 deletions.
38 changes: 23 additions & 15 deletions backend/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ exports.register = async (req, res) => {
}
});
} catch (error) {
console.error('Registration error:', error);
console.error('Error during registration:', error);
res.status(500).json({
message: 'Registration failed. Please try again.'
message: 'An error occurred during registration. Please try again later.',
error: error.message
});
}
};
Expand Down Expand Up @@ -98,7 +99,8 @@ exports.login = async (req, res) => {
} catch (error) {
console.error('Login error:', error);
res.status(500).json({
message: 'Login failed. Please try again.'
message: 'An error occurred during login. Please try again later.',
error: error.message
});
}
};
Expand All @@ -114,9 +116,11 @@ exports.getMe = async (req, res) => {
data: user,
});
} catch (error) {
res.status(400).json({
console.error('Error getting current user:', error);
res.status(500).json({
success: false,
message: error.message,
message: 'An error occurred while getting current user.',
error: error.message
});
}
};
Expand Down Expand Up @@ -172,9 +176,11 @@ exports.createAdmin = async (req, res) => {
},
});
} catch (error) {
res.status(400).json({
console.error('Error creating admin user:', error);
res.status(500).json({
success: false,
message: error.message,
message: 'An error occurred while creating admin user.',
error: error.message
});
}
};
Expand Down Expand Up @@ -222,9 +228,11 @@ exports.updateUserRole = async (req, res) => {
data: user,
});
} catch (error) {
res.status(400).json({
console.error('Error updating user role:', error);
res.status(500).json({
success: false,
message: error.message,
message: 'An error occurred while updating user role.',
error: error.message
});
}
};
Expand Down Expand Up @@ -262,11 +270,11 @@ exports.changePassword = async (req, res) => {
message: 'Password updated successfully',
});
} catch (error) {
console.error('Change password error:', error);
console.error('Error changing password:', error);
res.status(500).json({
success: false,
message: 'Error changing password',
error: error.message,
message: 'An error occurred while changing password.',
error: error.message
});
}
};
Expand Down Expand Up @@ -322,11 +330,11 @@ exports.deleteAccount = async (req, res) => {
message: 'Account deleted successfully',
});
} catch (error) {
console.error('Account deletion error:', error);
console.error('Error deleting account:', error);
res.status(500).json({
success: false,
message: 'Error deleting account',
error: error.message,
message: 'An error occurred while deleting account.',
error: error.message
});
}
};
77 changes: 44 additions & 33 deletions backend/controllers/bookingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,55 @@ const { catchAsync, NotFoundError, ValidationError } = require('../utils/errorHa
// @route POST /api/bookings
// @access Private
exports.createBooking = catchAsync(async (req, res) => {
// Get property and check if it exists
const property = await Property.findById(req.body.property);
if (!property) {
throw new NotFoundError('Property not found');
}

// Check if property is available for the requested dates
const isAvailable = await Booking.checkAvailability(
property._id,
new Date(req.body.startDate),
new Date(req.body.endDate)
);
try {
const { property, startDate, endDate, totalAmount } = req.body;
const tenantId = req.user.id;

// Get property and check if it exists
const propertyDoc = await Property.findById(property);
if (!propertyDoc) {
throw new NotFoundError('Property not found');
}

if (!isAvailable) {
throw new ValidationError('Property is not available for these dates');
}
// Check if property is available for the requested dates
const isAvailable = await Booking.checkAvailability(
propertyDoc._id,
new Date(startDate),
new Date(endDate)
);

// Create booking
const booking = new Booking({
property: property._id,
tenant: req.user.id,
owner: property.owner,
startDate: req.body.startDate,
endDate: req.body.endDate,
totalAmount: req.body.totalAmount || 0
});
if (!isAvailable) {
throw new ValidationError('Property is not available for these dates');
}

// Calculate total amount if not provided
if (!req.body.totalAmount) {
await booking.calculateTotalAmount();
}
// Create booking
const booking = await Booking.create({
property: propertyDoc._id,
tenant: tenantId,
owner: propertyDoc.owner,
startDate,
endDate,
totalAmount: totalAmount || 0,
status: 'pending'
});

await booking.save();
// Calculate total amount if not provided
if (!totalAmount) {
await booking.calculateTotalAmount();
}

res.status(201).json({
success: true,
data: booking
});
res.status(201).json({
success: true,
data: booking
});
} catch (error) {
console.error('Error creating booking:', error);
res.status(500).json({
success: false,
message: 'An error occurred while creating the booking. Please try again later.',
error: error.message
});
}
});

// @desc Get all bookings
Expand Down
101 changes: 54 additions & 47 deletions backend/controllers/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,56 +127,63 @@ exports.getChatMessages = asyncHandler(async (req, res, next) => {
// @route POST /api/chats/:chatId/messages
// @access Private
exports.sendMessage = asyncHandler(async (req, res, next) => {
const { chatId } = req.params;
const { content, type = 'text', attachments = [] } = req.body;

const chat = await Chat.findById(chatId);
if (!chat) {
return next(new ErrorResponse(`Chat not found with id of ${chatId}`, 404));
}

// Make sure user is part of the chat
if (!chat.participants.includes(req.user._id)) {
return next(new ErrorResponse(`Not authorized to send message in this chat`, 401));
}

// Create message
const message = await Message.create({
chat: chatId,
sender: req.user._id,
content,
type,
attachments
});

// Update chat's last message
chat.lastMessage = message._id;
chat.updatedAt = Date.now();
await chat.save();
try {
const { chatId } = req.params;
const { content, type = 'text', attachments = [] } = req.body;
const senderId = req.user._id;

// Validate chat existence
const chat = await Chat.findById(chatId);
if (!chat) {
return next(new ErrorResponse(`Chat not found with id of ${chatId}`, 404));
}

// Populate sender info
await message.populate('sender', 'name avatar');
// Make sure user is part of the chat
if (!chat.participants.includes(senderId)) {
return next(new ErrorResponse(`Not authorized to send message in this chat`, 401));
}

// Create notification for other participants
const otherParticipants = chat.participants.filter(
p => p.toString() !== req.user._id.toString()
);
// Create message
const message = await Message.create({
chat: chatId,
sender: senderId,
content,
type,
attachments
});

await Notification.create({
recipients: otherParticipants,
type: 'message',
title: `New message from ${req.user.name}`,
message: content.substring(0, 100),
data: {
chatId,
messageId: message._id
}
});
// Update chat's last message
chat.lastMessage = message._id;
chat.updatedAt = Date.now();
await chat.save();

// Populate sender info
await message.populate('sender', 'name avatar');

// Create notification for other participants
const otherParticipants = chat.participants.filter(
p => p.toString() !== senderId.toString()
);

await Notification.create({
recipients: otherParticipants,
type: 'message',
title: `New message from ${req.user.name}`,
message: content.substring(0, 100),
data: {
chatId,
messageId: message._id
}
});

res.status(201).json({
success: true,
data: message
});
res.status(201).json({
success: true,
data: message
});
} catch (error) {
console.error('Error sending message:', error);
return next(new ErrorResponse(`An error occurred while sending the message. Please try again later.`, 500));
}
});

// @desc Delete message
Expand Down Expand Up @@ -271,7 +278,7 @@ exports.markChatAsRead = asyncHandler(async (req, res, next) => {
read: false
},
{
$set: { read: true, readAt: Date.now() }
$set: { read: true, readAt: new Date() }
}
);

Expand Down
36 changes: 36 additions & 0 deletions backend/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,39 @@ exports.deleteNotification = asyncHandler(async (req, res) => {
data: {}
});
});

// @desc Create a notification
// @route POST /api/notifications
// @access Private
exports.createNotification = async (req, res) => {
try {
const { recipients, type, title, message, data } = req.body;

// Validate recipients
if (!recipients || recipients.length === 0) {
return res.status(400).json({
message: 'Recipients are required'
});
}

// Create notification
const notification = await Notification.create({
recipients,
type,
title,
message,
data
});

res.status(201).json({
message: 'Notification created successfully',
notification
});
} catch (error) {
console.error('Error creating notification:', error);
res.status(500).json({
message: 'An error occurred while creating the notification. Please try again later.',
error: error.message
});
}
};
Loading

0 comments on commit 898e3da

Please sign in to comment.