Skip to content

Commit

Permalink
💚 Added few APIS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush-Vish committed Oct 28, 2024
1 parent 4be1da6 commit 4bdfd93
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
14 changes: 8 additions & 6 deletions db/src/lib/models/agent.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hash } from 'bcrypt';
import { sign } from 'jsonwebtoken';
import mongoose from 'mongoose'
import { pointSchema } from './booking.model';
// Define the schema for the Agent
const agentSchema = new mongoose.Schema({

Expand All @@ -13,6 +14,11 @@ const agentSchema = new mongoose.Schema({
required: true,
unique: true
},
status:{
type: String,
default: "FREE",
enum: ["BUSY", "FREE" , "OFFLINE"]
},
password: { type: String, required: true },
phoneNumber: {
type: String,
Expand All @@ -23,12 +29,8 @@ const agentSchema = new mongoose.Schema({
type: String,
},
location: {
latitude: {
type: Number,
},
longitude: {
type: Number,
}
type: pointSchema,
index: '2dsphere',
},
services: {
type: [String], // Array of services like coolerRepair, washingMachineRepair
Expand Down
69 changes: 56 additions & 13 deletions shopkeeper/src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,6 @@ interface SearchQuery{
limit?: number;
}

interface ServiceDocument {
category: string[];
basePrice: number;
finalPrice: number;
ratings: {
average: number;
};
serviceProvider: any;
serviceCompany: any;
}


const searchServices = async (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -328,9 +318,12 @@ const searchServices = async (req: Request, res: Response, next: NextFunction) =
return next(new ApiError("An error occurred: " + error.message, 500));
}
};
const getAllServices = async (req: Request, res: Response, next: NextFunction) => {
const getAllServices = async (req: RequestWithUser, res: Response, next: NextFunction) => {
try {
const services = await Service.find();
const ownerId = req.user.id;
const services = await Service.find({
serviceProvider: ownerId,
});
return res.status(200).json({
message: "Services retrieved successfully",
data: services,
Expand All @@ -354,6 +347,53 @@ const deleteService = async (req: RequestWithUser, res: Response, next: NextFunc
return next(new ApiError("An error occurred: " + error.message, 500));
}
}
const getAllAgents = async (req: RequestWithUser, res: Response, next: NextFunction) => {
try {
const ownerId = req.user.id;
const agents = await Agent.find({ serviceProviderId: ownerId });
return res.status(200).json({
message: "Agents retrieved successfully",
data: agents,
});
} catch (error) {
return next(new ApiError("An error occurred: " + error.message, 500));
}
}
const deleteAgent = async(req : Request , res : Response , next : NextFunction) => {
try {
const {agentId} = req.params;
const agent = await Agent.findByIdAndDelete(agentId);
if (!agent) {
return next(new ApiError("Agent not found", 404));
}
return res.status(200).json({ message: "Agent deleted successfully" });
} catch (error) {
return next(new ApiError("An error occurred: " + error.message, 500));
}
}

const getAgent = async (req: Request, res: Response, next: NextFunction) => {
try {
const {agentId} = req.params;
const agent = await Agent.findById(agentId);
if (!agent) {
return next(new ApiError("Agent not found", 404));
}
const agentBooking = await Booking.find({agent: agentId});
const agentServices = await Service.find({assignedAgents: agentId});

return res.status(200).json({
message: "Agent retrieved successfully",
agent,
agentBooking,
agentServices


});
} catch (error) {
return next(new ApiError("An error occurred: " + error.message, 500));
}
}
export {
getServiceProviderById,
registerOrg,
Expand All @@ -364,6 +404,9 @@ export {
availableAgents,
searchServices,
getAllServices,
deleteService
deleteService,
getAllAgents,
deleteAgent,
getAgent
};

9 changes: 8 additions & 1 deletion shopkeeper/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
assignAgent,
assignAgentForaBooking,
availableAgents,
deleteAgent,
deleteService,
getAgent,
getAllAgents,
getAllServices,
getOrgDetails,
registerOrg,
Expand Down Expand Up @@ -105,7 +108,7 @@ router.post(
* @access Private
*/
router.get(
'/check-availability',
'/agents',
verifyJWT,
isAuthorized(['SERVICE_PROVIDER']),
availableAgents
Expand Down Expand Up @@ -162,6 +165,10 @@ router.delete(
deleteService
);

router.get("/all-agents" , verifyJWT , isAuthorized(['SERVICE_PROVIDER']) , getAllAgents)
router.delete("/agent/:agentId" , verifyJWT , isAuthorized(['SERVICE_PROVIDER']) , deleteAgent)

router.get("/agent/:agentId" , verifyJWT , isAuthorized(['SERVICE_PROVIDER']) , getAgent);
/**
* TODO: Add a route to verify an Organization using legal documents with a machine learning model
*/
Expand Down

0 comments on commit 4bdfd93

Please sign in to comment.