77} from "../utils/errors" ;
88
99import { BCRYPT_WORK_FACTOR } from "../config" ;
10-
10+ /**
11+ * @todo : change get org by email function or refactor
12+ */
1113export 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!!
0 commit comments