Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aadil #3

Merged
merged 7 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# To up the server using docker-compose
```
docker-compose up --build
```
### Note : It will not restart on code change. You have to run this command again.


# Org

<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="45"></a>
Expand Down
233 changes: 156 additions & 77 deletions agent/src/controllers/agentController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


// import { Agent, Client, ServiceProvider } from '@org/db';
import { Booking, ServiceItem } from '@org/db';
import { Booking } from '@org/db';



Expand All @@ -10,20 +10,18 @@ import { Booking, ServiceItem } from '@org/db';
// Show all pending, in-progress, and completed bookings
export const getAgentDashboard = async (req, res) => {
try {
const agentId = req.user.id;
const agentId = req.user.id;

console.log("AAAAAAAAAAAAA",agentId);
// Find all bookings assigned to this agent
const bookings = await Booking.find({ agentId })
.populate('serviceItems') // Populate service items in each booking
.populate('customerId', 'name') // Populate customer name for each booking
.populate('serviceProviderId', 'name'); // Populate service provider name

// Filter bookings based on status
const bookings = await Booking.find({ agent: agentId })
// .populate('customer', 'name') // Populate customer name for each booking
// .populate('serviceProvider', 'name'); // Populate service provider name
// Filter bookings based on status
const pendingBookings = bookings.filter(b => b.status === 'Pending');
const inProgressBookings = bookings.filter(b => b.status === 'In Progress');
const completedBookings = bookings.filter(b => b.status === 'Completed');


// Send the data for the agent's dashboard
res.status(200).json({
totalBookings: bookings.length,
Expand All @@ -36,11 +34,11 @@ export const getAgentDashboard = async (req, res) => {
}
};

// 1. Add a Service to a Booking (as described before)
export const addServiceToBooking = async (req, res) => {
// 1. Add an Extra Task to a Booking
export const addExtraTaskToBooking = async (req, res) => {
try {
const { bookingId } = req.params;
const { description, cost } = req.body; // Description and cost of the service
const { description, extraPrice } = req.body;

// Find the booking
const booking = await Booking.findById(bookingId);
Expand All @@ -53,113 +51,194 @@ export const addServiceToBooking = async (req, res) => {
return res.status(403).json({ error: 'Not authorized to modify this booking' });
}

// Create a new service item (problem identified by agent)
const serviceItem = new ServiceItem({
bookingId,
description,
cost,
imageUrl: req.file ? req.file.path : null
});


await serviceItem.save();

// Add the service item to the booking
booking.serviceItems.push(serviceItem._id);
booking.updatedAt = new Date();
// Add the extra task to the booking
booking.extraTasks.push({ description, extraPrice });
booking.updatedAt = new Date();
await booking.save();

res.status(201).json({
message: 'Service added successfully',
serviceItem
message: 'Extra task added successfully',
extraTasks: booking.extraTasks,
});
} catch (error) {
res.status(500).json({ error: 'Failed to add service to booking' });
res.status(500).json({ error: 'Failed to add extra task to booking' });
}
};

// 2. Update a Service for a Booking
export const updateServiceInBooking = async (req, res) => {
// 2. Update an Extra Task in a Booking
export const updateExtraTaskInBooking = async (req, res) => {
try {
const { serviceId } = req.params;
const { description, cost } = req.body;
// Find the service item
const serviceItem = await ServiceItem.findById(serviceId);
if (!serviceItem) {
return res.status(404).json({ error: 'Service item not found' });
const { bookingId, taskIndex } = req.params;
const { description, extraPrice } = req.body;

// Find the booking
const booking = await Booking.findById(bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}

// Check if the agent is allowed to modify this service
const booking = await Booking.findById(serviceItem.bookingId);
if (booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to modify this service' });
// Check if the agent is allowed to modify this booking
if (!booking.agent || booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to modify this booking' });
}

// Update service details
if (description) serviceItem.description = description;
if (cost) serviceItem.cost = cost;
serviceItem.updatedAt = new Date();
await serviceItem.save();
// Check if the task index is valid
if (taskIndex >= booking.extraTasks.length || taskIndex < 0) {
return res.status(404).json({ error: 'Task not found' });
}

// Update the specific extra task
if (description) booking.extraTasks[taskIndex].description = description;
if (extraPrice) booking.extraTasks[taskIndex].extraPrice = extraPrice;
booking.updatedAt = new Date();
await booking.save();

res.status(200).json({
message: 'Service updated successfully',
serviceItem
message: 'Extra task updated successfully',
extraTasks: booking.extraTasks,
});
} catch (error) {
res.status(500).json({ error: 'Failed to update service' });
res.status(500).json({ error: 'Failed to update extra task' });
}
};

// 3. Delete a Service from a Booking
export const deleteServiceFromBooking = async (req, res) => {
// 3. Delete an Extra Task from a Booking
export const deleteExtraTaskFromBooking = async (req, res) => {
try {
const { serviceId } = req.params;
const { bookingId, taskIndex } = req.params;


const serviceItem = await ServiceItem.findById(serviceId);
if (!serviceItem) {
return res.status(404).json({ error: 'Service item not found' });
// Find the booking
const booking = await Booking.findById(bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}

// Check if the agent is allowed to delete this service
const booking = await Booking.findById(serviceItem.bookingId);
if (booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to delete this service' });
// Check if the agent is allowed to modify this booking
if (!booking.agent || booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to delete this task' });
}

// Remove the service item from the booking's serviceItems array
booking.serviceItems = booking.serviceItems.filter(
item => item.toString() !== serviceItem._id.toString()
);
await booking.save();
// Check if the task index is valid
if (taskIndex >= booking.extraTasks.length || taskIndex < 0) {
return res.status(404).json({ error: 'Task not found' });
}

// Delete the service item
await ServiceItem.deleteOne({ _id: serviceItem._id });
// Remove the task at the specified index
booking.extraTasks.splice(taskIndex, 1);
await booking.save();

res.status(200).json({ message: 'Service deleted successfully' });
res.status(200).json({ message: 'Extra task deleted successfully' });
} catch (error) {
res.status(500).json({ error: 'Failed to delete service' });
res.status(500).json({ error: 'Failed to delete extra task' });
}
};

// 4. Get All Services of a Booking (for agent's dashboard)
export const getServicesForBooking = async (req, res) => {
// 4. Get All Extra Tasks of a Booking (for agent's dashboard)
export const getExtraTasksForBooking = async (req, res) => {
try {
const { bookingId } = req.params;

// Find the booking
const booking = await Booking.findById(bookingId).populate('serviceItems');
const booking = await Booking.findById(bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}

// Check if the agent is allowed to view services for this booking
if (booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to view services' });
// Check if the agent is allowed to view tasks for this booking
if (!booking.agent || booking.agent.toString() !== req.user.id) {
return res.status(403).json({ error: 'Not authorized to view extra tasks' });
}

res.status(200).json({ services: booking.serviceItems });
res.status(200).json({ extraTasks: booking.extraTasks });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch services' });
res.status(500).json({ error: 'Failed to fetch extra tasks' });
}
};





export const updateBookingToInProgress = async (req, res) => {
try {
const { bookingId } = req.body;
const agentId = req.user.id;

// Find the booking and ensure it belongs to the agent
const booking = await Booking.findOne({
_id: bookingId,
agent: agentId
});

if (!booking) {
return res.status(404).json({
error: 'Booking not found or not assigned to this agent'
});
}

// Validate current status
if (booking.status !== 'Pending') {
return res.status(400).json({
error: 'Booking must be in Pending status to move to In Progress'
});
}

// Update the status
booking.status = 'In Progress';
// booking.startTime = new Date(); // Optional: track when work started
await booking.save();

res.status(200).json({
message: 'Booking status updated to In Progress',
booking
});

} catch (error) {
res.status(500).json({
error: 'Failed to update booking status',
message: error.message
});
}
};

export const updateBookingToCompleted = async (req, res) => {
try {
const { bookingId } = req.body;
const agentId = req.user.id;

// Find the booking and ensure it belongs to the agent
const booking = await Booking.findOne({
_id: bookingId,
agent: agentId
});

if (!booking) {
return res.status(404).json({
error: 'Booking not found or not assigned to this agent'
});
}

// Validate current status
if (booking.status !== 'In Progress') {
return res.status(400).json({
error: 'Booking must be in In Progress status to move to Completed'
});
}

// Update the status
booking.status = 'Completed';
// booking.completionTime = new Date(); // Optional: track when work was completed
await booking.save();

res.status(200).json({
message: 'Booking status updated to Completed',
booking
});

} catch (error) {
res.status(500).json({
error: 'Failed to update booking status',
message: error.message
});
}
};
23 changes: 15 additions & 8 deletions agent/src/routes/agentRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ import express from 'express'
const router = express.Router();


import {verifyJWT} from '@org/utils'
import {isAuthorized, verifyJWT} from '@org/utils'
import multer from 'multer'
import {getAgentDashboard,updateServiceInBooking,deleteServiceFromBooking,getServicesForBooking,addServiceToBooking} from '../controllers/agentController'
import {getAgentDashboard,updateExtraTaskInBooking,deleteExtraTaskFromBooking,getExtraTasksForBooking,addExtraTaskToBooking,updateBookingToInProgress,updateBookingToCompleted} from '../controllers/agentController'
// Setup Multer for image uploads
const upload = multer({ dest: 'uploads/' });

// Agent dashboard: View all bookings
router.get('/dashboard',verifyJWT,getAgentDashboard);
router.get('/dashboard',verifyJWT, isAuthorized(['AGENT']),getAgentDashboard);

// Add service to a booking (with image upload)
router.post('/booking/:bookingId/service',verifyJWT, upload.single('image'),addServiceToBooking);
router.post('/booking/:bookingId/service',verifyJWT,addExtraTaskToBooking);

// Update a service in a booking
router.put('/service/:serviceId',verifyJWT, updateServiceInBooking);
router.put('/bookings/:bookingId/extra-task/:taskIndex', verifyJWT, updateExtraTaskInBooking);

// Delete a service from a booking
router.delete('/service/:serviceId',verifyJWT, deleteServiceFromBooking);

// Delete a service from a booking
router.delete('/bookings/:bookingId/extra-task/:taskIndex', verifyJWT, deleteExtraTaskFromBooking);
// Get all services for a specific booking
router.get('/booking/:bookingId/services',verifyJWT,getServicesForBooking);
router.get('/bookings/:bookingId/extra-tasks',verifyJWT , getExtraTasksForBooking);

// Route to update booking status to "In Progress"
router.put('/bookings/in-progress', verifyJWT, updateBookingToInProgress);

// Route to update booking status to "Completed"
router.put('/bookings/completed', verifyJWT, updateBookingToCompleted);


export default router;
Loading
Loading