File tree Expand file tree Collapse file tree
content-types/mentor-program
content-types/ngo-program
program/content-types/program
extensions/users-permissions/content-types/user Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const schema = require ( "./schema.json" ) ;
2+ const lifecycles = require ( "./lifecycles.js" ) ;
3+
4+ module . exports = {
5+ schema,
6+ lifecycles,
7+ } ;
Original file line number Diff line number Diff line change 1+ const { ValidationError } = require ( "@strapi/utils" ) . errors ;
2+
3+ module . exports = {
4+ async beforeCreate ( event ) {
5+ const ctx = strapi . requestContext . get ( ) ;
6+ const data = event . params . data || { } ;
7+
8+ const programId = data . program ;
9+ const mentorId = data . mentor ;
10+
11+ if ( ! programId || ! mentorId ) {
12+ ctx . badRequest ( `Program sau persoana resursa invalide` ) ;
13+ throw new ValidationError ( `Program sau persoana resursa invalide` ) ;
14+ }
15+
16+ // ✅ Check if a mentorship request between the same user and mentor already exists
17+ const existingMentorProgram = await strapi . db
18+ . query ( "api::mentor-program.mentor-program" )
19+ . findOne ( {
20+ where : {
21+ program : { id : programId } ,
22+ mentor : { id : mentorId } ,
23+ } ,
24+ } ) ;
25+
26+ if ( existingMentorProgram ) {
27+ ctx . badRequest (
28+ `Aceasta relatie intre program si persoana resursa exista deja`
29+ ) ;
30+ throw new ValidationError (
31+ `Aceasta relatie intre program si persoana resursa exista deja`
32+ ) ;
33+ }
34+
35+ const mentorData = await strapi . entityService . findOne (
36+ "plugin::users-permissions.user" ,
37+ mentorId ,
38+ { populate : "role" }
39+ ) ;
40+
41+ if ( mentorData ?. role ?. type !== "mentor" ) {
42+ ctx . badRequest ( `Persoana resursa nu este valida` ) ;
43+ throw new ValidationError ( `Persoana resursa nu este valida` ) ;
44+ }
45+ } ,
46+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "kind" : " collectionType" ,
3+ "collectionName" : " mentor_programs" ,
4+ "info" : {
5+ "singularName" : " mentor-program" ,
6+ "pluralName" : " mentor-programs" ,
7+ "displayName" : " Mentor Program"
8+ },
9+ "options" : {
10+ "draftAndPublish" : false
11+ },
12+ "pluginOptions" : {},
13+ "attributes" : {
14+ "mentor" : {
15+ "type" : " relation" ,
16+ "relation" : " manyToOne" ,
17+ "target" : " plugin::users-permissions.user" ,
18+ "inversedBy" : " mentorPrograms"
19+ },
20+ "program" : {
21+ "type" : " relation" ,
22+ "relation" : " manyToOne" ,
23+ "target" : " api::program.program" ,
24+ "inversedBy" : " programMentors"
25+ }
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ /**
4+ * mentor-program controller
5+ */
6+
7+ const { createCoreController } = require ( '@strapi/strapi' ) . factories ;
8+
9+ module . exports = createCoreController ( 'api::mentor-program.mentor-program' ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ /**
4+ * mentor-program router
5+ */
6+
7+ const { createCoreRouter } = require ( '@strapi/strapi' ) . factories ;
8+
9+ module . exports = createCoreRouter ( 'api::mentor-program.mentor-program' ) ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ /**
4+ * mentor-program service
5+ */
6+
7+ const { createCoreService } = require ( '@strapi/strapi' ) . factories ;
8+
9+ module . exports = createCoreService ( 'api::mentor-program.mentor-program' ) ;
Original file line number Diff line number Diff line change 1+ const schema = require ( "./schema.json" ) ;
2+ const lifecycles = require ( "./lifecycles.js" ) ;
3+
4+ module . exports = {
5+ schema,
6+ lifecycles,
7+ } ;
Original file line number Diff line number Diff line change 1+ const { ValidationError } = require ( "@strapi/utils" ) . errors ;
2+
3+ module . exports = {
4+ async beforeCreate ( event ) {
5+ const ctx = strapi . requestContext . get ( ) ;
6+ const data = event . params . data || { } ;
7+
8+ const ngoId = data . ngo ;
9+ const programId = data . program ;
10+
11+ if ( ! ngoId || ! programId ) {
12+ ctx . badRequest ( `Program sau organizatie invalide` ) ;
13+ throw new ValidationError ( `Program sau organizatie invalide` ) ;
14+ }
15+
16+ // ✅ Check if a mentorship request between the same user and mentor already exists
17+ const existingNgoProgram = await strapi . db
18+ . query ( "api::ngo-program.ngo-program" )
19+ . findOne ( {
20+ where : {
21+ program : { id : programId } ,
22+ ngo : { id : ngoId } ,
23+ } ,
24+ } ) ;
25+
26+ if ( existingNgoProgram ) {
27+ ctx . badRequest ( `Aceasta relatie exista deja` ) ;
28+ throw new ValidationError ( `Aceasta relatie exista deja` ) ;
29+ }
30+
31+ const ngoData = await strapi . entityService . findOne (
32+ "plugin::users-permissions.user" ,
33+ ngoId ,
34+ { populate : "role" }
35+ ) ;
36+
37+ if ( ngoData ?. role ?. type ?. toLowerCase ( ) !== "authenticated" ) {
38+ ctx . badRequest ( `Alege o organizatie valida` ) ;
39+ throw new ValidationError ( `Alege o organizatie valida` ) ;
40+ }
41+ } ,
42+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "kind" : " collectionType" ,
3+ "collectionName" : " ngo_programs" ,
4+ "info" : {
5+ "singularName" : " ngo-program" ,
6+ "pluralName" : " ngo-programs" ,
7+ "displayName" : " NGO Program" ,
8+ "description" : " "
9+ },
10+ "options" : {
11+ "draftAndPublish" : false
12+ },
13+ "pluginOptions" : {},
14+ "attributes" : {
15+ "ngo" : {
16+ "type" : " relation" ,
17+ "relation" : " manyToOne" ,
18+ "target" : " plugin::users-permissions.user" ,
19+ "inversedBy" : " ngoPrograms"
20+ },
21+ "program" : {
22+ "type" : " relation" ,
23+ "relation" : " manyToOne" ,
24+ "target" : " api::program.program" ,
25+ "inversedBy" : " programNgos"
26+ }
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ /**
4+ * ngo-program controller
5+ */
6+
7+ const { createCoreController } = require ( '@strapi/strapi' ) . factories ;
8+
9+ module . exports = createCoreController ( 'api::ngo-program.ngo-program' ) ;
You can’t perform that action at this time.
0 commit comments