Skip to content

Commit 9cbbeb0

Browse files
authored
Add mentor, ngo to program (#152)
1 parent fdab359 commit 9cbbeb0

16 files changed

Lines changed: 344 additions & 39 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const schema = require("./schema.json");
2+
const lifecycles = require("./lifecycles.js");
3+
4+
module.exports = {
5+
schema,
6+
lifecycles,
7+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const schema = require("./schema.json");
2+
const lifecycles = require("./lifecycles.js");
3+
4+
module.exports = {
5+
schema,
6+
lifecycles,
7+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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');

0 commit comments

Comments
 (0)