Skip to content

Commit 9906f77

Browse files
committed
had a "good" merge conflict but ended up resolving
1 parent 1408b81 commit 9906f77

File tree

8 files changed

+58
-42
lines changed

8 files changed

+58
-42
lines changed

volunteerverse-api/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { organizationRoutes } from './routes/organization';
1111
import { volunteerRoutes } from './routes/volunteer';
1212
import { projectRoutes } from './routes/projects';
1313
import { requireAuthenticatedUser } from './middleware/security';
14-
import { requireAuthenticatedUser } from './middleware/security';
14+
// import { requireAuthenticatedUser } from './middleware/security';
1515

1616

1717
export const app = express();
@@ -25,7 +25,7 @@ app.use("/auth", authRoutes)
2525
app.use("/volunteer", volunteerRoutes)
2626
app.use("/organization", requireAuthenticatedUser ,organizationRoutes)
2727

28-
app.use("/project", projectRoutes)
28+
app.use("/project", requireAuthenticatedUser , projectRoutes)
2929

3030

3131

volunteerverse-api/src/models/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export class Auth{
1111

1212
static async authenticate(creds: {email:string, password:string}){
1313
const requiredCreds = ["email", "password"]
14+
console.log("authenticate", requiredCreds)
1415
try{
1516
validateFields({required:requiredCreds, obj:creds, location:"user authentication"})
1617
} catch(error) {
1718
throw error
1819
}
1920

2021
const user = await this.fetchByEmail(creds.email)
22+
console.log(user.rows)
2123
const {user_type} = user
2224

2325
if (user){
@@ -41,7 +43,9 @@ export class Auth{
4143
const query = `SELECT * FROM authentication WHERE email=$1`
4244
const result = await db.query(query, [email])
4345
const user = result.rows[0]
46+
console.log(user)
4447
if (user){
48+
4549
return user
4650
}
4751
return null

volunteerverse-api/src/models/organization.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
} from "../utils/errors";
88

99
import { BCRYPT_WORK_FACTOR } from "../config";
10-
10+
/**
11+
* @todo: change get org by email function or refactor
12+
*/
1113
export class Organization {
1214
/**
1315
* Convert a user from the database into a user object that can be viewed publically.
@@ -35,12 +37,11 @@ userType: "organization";
3537
await Organization.fetchOrganizationByEmail(creds.email);
3638
if (existingOrganizationWithEmail) {
3739
throw new BadRequestError(`Duplicate email: ${creds.email}`);
38-
throw new BadRequestError(`Duplicate email: ${creds.email}`);
3940
}
4041

4142
const salt = await bcrypt.genSalt(BCRYPT_WORK_FACTOR);
4243
const hashedPassword = await bcrypt.hash(creds.password, salt);
43-
const normalizedOrgEmail = creds.email.toLowerCase();
44+
const normalizedOrgEmail = creds.email.toLowerCase().trim();
4445

4546
const orgResult = await db.query(
4647
`INSERT INTO organizations (
@@ -61,16 +62,12 @@ userType: "organization";
6162
website
6263
`,
6364
[
64-
creds.orgName,
65-
creds.orgDescription,
6665
creds.orgName,
6766
creds.orgDescription,
6867
normalizedOrgEmail,
6968
creds.logoUrl,
70-
creds.logoUrl,
7169
creds.founders,
7270
creds.orgWebsite,
73-
creds.orgWebsite,
7471
]
7572
);
7673

@@ -110,7 +107,7 @@ userType: "organization";
110107
logoUrl: logo_url,
111108
founders: founders,
112109
userType: user_type,
113-
orgWebsite: website
110+
orgWebsite: website,
114111
};
115112
}
116113
static async fetchInterestedVolunteersByEmail(email) {
@@ -136,9 +133,10 @@ userType: "organization";
136133
// item(that is access it) in the bracket(email in this case)
137134
const org_result = await db.query(
138135
`SELECT
136+
id,
139137
organization_name,
140138
organization_description,
141-
organization_email,
139+
organization_email AS "email",
142140
logo_url,
143141
founders
144142
FROM organizations
@@ -191,7 +189,14 @@ userType: "organization";
191189

192190
// return result.rows[0]
193191
// }
194-
static async toggleStateOfOrgProject(projectId, orgId, active) {
192+
static async toggleStateOfOrgProject(projectId, orgId) {
193+
194+
const activeResult = await db.query(`SELECT active FROM projects
195+
WHERE id = $1 `,
196+
[projectId])
197+
198+
console.log ("active result", activeResult.rows[0].active)
199+
195200
const orgResult = await db.query(
196201
`SELECT * FROM projects WHERE org_id = $1 AND id = $2`,
197202
[orgId, projectId]
@@ -201,7 +206,7 @@ userType: "organization";
201206
`UPDATE "projects" SET "active" = $1
202207
WHERE "id" = $2 AND "org_id" = $3
203208
RETURNING *`,
204-
[!active, projectId, orgId]
209+
[!activeResult.rows[0].active, projectId, orgId]
205210
);
206211
return result.rows;
207212
} else {
@@ -214,7 +219,6 @@ userType: "organization";
214219
`SELECT * FROM projects WHERE org_id = $1 AND id = $2`,
215220
[orgId, project_id]
216221
);
217-
218222
if (orgResult.rows.length !== 0) {
219223
const result = await db.query(
220224
`DELETE FROM "projects" WHERE "id" = $1`,
@@ -226,43 +230,44 @@ userType: "organization";
226230
}
227231

228232
static async updateApprovedVolunteers(approved, email, project_id, org_id) {
229-
//returns all the volunteers interested in the project
233+
// to check if the volunteer exist(if this does interestedVoluteersInfo will be populated)
230234
const interestedVolunteersInfo =
231235
await Organization.fetchInterestedVolunteersByEmail(email);
232236

237+
// to check if the project and( / for the )org exists if it does orgResult will be populated
233238
const orgResult = await db.query(
234239
`SELECT * FROM projects WHERE org_id = $1 AND id = $2`,
235240
[org_id, project_id]
236241
);
237-
console.log("the info under the projectid and orgid", orgResult.rows);
238-
242+
console.log("orgResult is here", orgResult);
243+
// to check if the volunteer exist
239244
if (interestedVolunteersInfo.length !== 0) {
240245
console.log(
241246
"the info of the volunteer if they exist",
242247
interestedVolunteersInfo
243248
);
244249

250+
// to check if the project and( / for the )org exists
245251
if (orgResult.rows.length !== 0) {
246252
const result = await db.query(
247253
`UPDATE "interested_volunteers" SET "approved" = $1
248254
WHERE "email" = $2 AND "project_id" = $3
249255
RETURNING *`,
250256
[!approved, email, project_id]
251257

252-
253258
);
254259
console.log("updating approved works!", result.rows)
255260

256261
return result.rows[0];
257-
258262

263+
} else {
264+
throw new UnauthorizedError("Organization/Project not found");
259265
}
260266
} else {
261267
throw new UnauthorizedError("Volunteer Email not found");
262268
}
263269

264270
}
265-
266271

267272
static async incrementAndDecrementApprovedVolunteers(email, projectId, orgId){
268273
const approvedResult = await db.query(`SELECT approved FROM interested_volunteers
@@ -274,12 +279,12 @@ userType: "organization";
274279
[projectId, orgId])
275280

276281

277-
console.log("approvedResult", approvedResult)
278-
console.log("approvedPeopleResult", approvedPeopleResult)
282+
console.log("approvedResult", approvedResult.rows[0])
283+
console.log("approvedPeopleResult", approvedPeopleResult.rows[0])
279284

280-
const approvedVolunteerState = await Organization.updateApprovedVolunteers(approvedResult.rows[0], email, projectId, orgId)
285+
const approvedVolunteerState = await Organization.updateApprovedVolunteers(approvedResult.rows[0].approved, email, projectId, orgId)
281286

282-
console.log("bringing volunteer state into incre/decre works!",approvedVolunteerState)
287+
console.log("bringing volunteer state into incre/decre works!",approvedVolunteerState.approved)
283288

284289
console.log("approvedPeopleohhhhhh", approvedPeopleResult.rows[0].approved_people )
285290
if (approvedVolunteerState.approved === true){
@@ -296,7 +301,7 @@ userType: "organization";
296301
return result.rows;
297302

298303
} else if (approvedVolunteerState.approved === false){
299-
console.log("oh wellllllll")
304+
console.log("approved is false", typeof approvedPeopleResult.rows[0].approved_people)
300305
const result = await db.query(
301306
`UPDATE "projects" SET "approved_people" = $1
302307
WHERE "org_id" = $2 AND "id" = $3
@@ -313,8 +318,6 @@ userType: "organization";
313318

314319

315320

316-
317-
318321
static async fetchAllOrganizationProjectsById(org_id) {
319322
const result = await db.query(
320323
// make sure this matches spelling in the table too!!

volunteerverse-api/src/models/volunteer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ export class Volunteer {
203203
return null;
204204
}
205205

206+
// add a check to make sure that the user type IS volunteer (middleware). do the same for organization
207+
206208
/**
207209
* When a volunteer expresses interest in a project, log it into database
208210
* @param projectId
@@ -213,7 +215,7 @@ export class Volunteer {
213215
if (volunteerCheck){
214216
throw new BadRequestError("Already expressed interest")
215217
}
216-
218+
217219
const query = `INSERT into interested_volunteers(email, project_id, approved) VALUES ($1,$2,$3) RETURNING email,project_id as "projectId",approved`;
218220
const result = await db.query(query, [email, projectId, false]);
219221
return result.rows[0];

volunteerverse-api/src/routes/organization.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ organizationRoutes.get("/projects", async function(req,res,next){
4949

5050
// })
5151

52+
organizationRoutes.put("/project/status/:projectId", async function(req,res,next){
53+
try{
54+
const { id } = res.locals.user
55+
const projectId = parseInt(req.params.projectId)
56+
//const {active} = req.body
57+
const result = await Organization.toggleStateOfOrgProject(projectId, id)
58+
res.json({projectState : result})
59+
}catch (error) {
60+
next(error);
61+
}
62+
})
5263
organizationRoutes.put("/project/:projectId", async function(req,res,next){
5364
console.log('whyy')
5465
try{
@@ -64,16 +75,7 @@ organizationRoutes.put("/project/:projectId", async function(req,res,next){
6475

6576
})
6677

67-
organizationRoutes.post("/project/status", async function(req,res,next){
68-
try{
69-
const {projectId, orgId, active} = req.body
70-
const result = await Organization.toggleStateOfOrgProject(projectId, orgId, active)
71-
res.json({projectState : result})
72-
}catch (error) {
73-
next(error);
74-
}
75-
76-
})
78+
7779

7880
organizationRoutes.delete("/project/:projectId", async function(req,res,next){
7981
const { id } = res.locals.user
@@ -84,7 +86,9 @@ organizationRoutes.delete("/project/:projectId", async function(req,res,next){
8486
})
8587

8688

87-
organizationRoutes.put("/project/interested/:projectId", async function (req, res, next) {
89+
90+
91+
organizationRoutes.get("/project/interested/:projectId", async function (req, res, next) {
8892
//req.body is what we put in insomnia when we test which to equate to what we put in the browser
8993
//that then goes into the function below
9094
const projectId = parseInt(req.params.projectId)

volunteerverse-api/src/routes/projects.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const projectRoutes = express.Router()
99
projectRoutes.post("/register", async function (req, res, next){
1010
try{
1111
const {id} = res.locals.user
12+
console.log(id)
13+
console.log("req.body", req.body)
14+
console.log("getting here")
1215
const project = await Projects.registerProject({...req.body, orgId: id})
1316
return res.status(201).json(project)
1417
} catch (error){

volunteerverse-api/src/routes/volunteer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ volunteerRoutes.post("/fetch", async function (req, res, next) {
2424
}
2525
});
2626

27-
volunteerRoutes.post("/interest/:projectId", async function (req, res, next) {
27+
volunteerRoutes.put("/interest/:projectId", async function (req, res, next) {
2828
try {
2929
const projectId = parseInt(req.params.projectId);
30-
const { email } = req.body;
30+
const { email } = res.locals.user;
3131
const result = await Volunteer.expressInterest(projectId, email);
3232
res.status(201).json(result);
3333
} catch (error) {

volunteerverse-api/volunteerverse-schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CREATE TABLE projects(
3434
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
3535
image_url TEXT,
3636
requested_people INTEGER NOT NULL,
37-
approved_people INTEGER NOT NULL,
37+
approved_people INTEGER NOT NULL DEFAULT 0,
3838
active BOOLEAN DEFAULT TRUE
3939
);
4040

0 commit comments

Comments
 (0)