Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #83 from igrowker/franco-back
Browse files Browse the repository at this point in the history
volunteerModel modificado
  • Loading branch information
Davpad authored Dec 4, 2024
2 parents a9df5c3 + 942c37a commit 2364412
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/constants/publicRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const allPublicRoutes = [
{ path: "/animals/:id", method: "GET" },
{ path: "/admin/dashboard", method: "GET" },
{ path: "/volunteer", method: "GET"},
{ path: "/volunteer/:id", method: "GET"},
{ path: "/files/upload", method: "POST"}
];

Expand Down
27 changes: 20 additions & 7 deletions src/controllers/volunteersController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import * as volunteerService from '../services/volunteersServices';
import mongoose from 'mongoose';
import Refugee from '../models/refugeeModel';

const handleResponse = (res: Response, data: any, success: boolean, message: string, statusCode: number) => {
res.status(statusCode).json({
Expand Down Expand Up @@ -29,16 +31,17 @@ export const getVolunteerOpportunities = async (req: Request, res: Response): Pr

export const getVolunteerOpportunitiesByRefugeeId = async (req: Request, res: Response): Promise<void> => {
try {
const refugeId = req.params.refugeeId;
const volunteerOpportunities = await volunteerService.getOpportunitiesByRefugeeId(refugeId);
const refugeeId = req.params.refugeeId;

const volunteerOpportunities = await volunteerService.getOpportunitiesByRefugeeId(refugeeId);
handleSuccess(res, volunteerOpportunities);
} catch (error) {
handleError(res, error, 'Error al obtener las oportunidades de voluntariado');
}
};

export const createVolunteerController = async (req: Request, res: Response) => {
const { refugeeID } = req.params;
const { id } = req.params;
const { description, requirements, availability } = req.body;

const token = req.headers.authorization?.split(' ')[1];
Expand All @@ -49,20 +52,30 @@ export const createVolunteerController = async (req: Request, res: Response) =>

try {
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'defaultSecret');
const userID = (decoded as { sub: string }).sub;
const userID = (decoded as { userId: string }).userId;

if (![refugeeID, userID].every(Boolean)) {
if (![id, userID].every(Boolean)) {
return res.status(400).json({ error: 'Faltan los parámetros necesarios' });
}

const newOpportunity = await volunteerService.createVolunteerOpportunity({
refugee_id: refugeeID,
refugee_id: id,
user_id: userID,
description,
requirements,
availability,
availability
});

const updatedRefugee = await Refugee.findByIdAndUpdate(
id,
{ $push: { opportunities: newOpportunity._id } },
{ new: true }
);

if (!updatedRefugee) {
return res.status(404).json({ error: 'Refugio no encontrado' });
}

res.status(201).json(newOpportunity);
} catch (error) {
res.status(400).json({
Expand Down
6 changes: 6 additions & 0 deletions src/models/refugeeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IRefugee extends Document {
description: string;
img: string;
pets: Types.ObjectId[];
opportunities: Types.ObjectId[]
}

export interface RefugeeInput {
Expand Down Expand Up @@ -37,6 +38,11 @@ const refugeeSchema = new Schema<IRefugee>({
type: [Schema.Types.ObjectId],
ref: 'Animal',
default: [],
},
opportunities: {
type: [Schema.Types.ObjectId],
ref: "Volunteer",
default: []
}
});

Expand Down
5 changes: 0 additions & 5 deletions src/models/volunteersModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ interface IVolunteerOpportunity extends Document {
}

const VolunteerOpportunitySchema: Schema = new Schema({
opportunity_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Opportunity",
required: true,
},
refugee_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Refugee",
Expand Down
5 changes: 2 additions & 3 deletions src/routes/volunteerRouts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import express from 'express';
import { createVolunteerController, getVolunteerOpportunities, getVolunteerOpportunitiesByRefugeeId, deleteVolunteerOpportunity, updateVolunteerOpportunity } from '../controllers/volunteersController';
import { checkRole } from '../middlewares/roleMiddleware';
import { authenticateToken } from '../middlewares/authMiddleware';

const volunteerRoutes = express.Router();

volunteerRoutes.get('/', authenticateToken,getVolunteerOpportunities);
volunteerRoutes.get('/:id', getVolunteerOpportunitiesByRefugeeId);
volunteerRoutes.get('/',getVolunteerOpportunities);
volunteerRoutes.get('/:refugeeId', getVolunteerOpportunitiesByRefugeeId);
volunteerRoutes.post('/:id', checkRole('refugee'), createVolunteerController);
volunteerRoutes.delete('/:id', checkRole('refugee'), deleteVolunteerOpportunity);
volunteerRoutes.put('/:opportunityId', updateVolunteerOpportunity);
Expand Down
9 changes: 6 additions & 3 deletions src/services/volunteersServices.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import mongoose from 'mongoose';
import VolunteersModel from '../models/volunteersModel';

export const getVolunteerOpportunities = async () => {
return VolunteersModel.find().populate('refugee_id', 'name');
};

export const getOpportunitiesByRefugeeId = async (refugee_id: string) => {
return VolunteersModel.find({ refugee_id }).populate('refugee_id', 'name');
};

const objectIdRefugeeId = new mongoose.Types.ObjectId(refugee_id)
return VolunteersModel.find({ refugee_id: objectIdRefugeeId}).populate('refugee_id', 'name');
};

export const createVolunteerOpportunity = async ({
refugee_id,
Expand All @@ -30,7 +33,7 @@ export const createVolunteerOpportunity = async ({
user_id,
description,
requirements,
availability,
availability
});

return opportunity.save();
Expand Down

0 comments on commit 2364412

Please sign in to comment.