Skip to content
Snippets Groups Projects
Commit 8aa4ffa6 authored by moon's avatar moon
Browse files

Added controller for NovelCondensate

parent d17e13f9
No related branches found
No related tags found
No related merge requests found
......@@ -54,9 +54,9 @@
},
"reviewedBy": {
"type": "relation",
"relation": "oneToMany",
"relation": "manyToOne",
"target": "plugin::users-permissions.user",
"mappedBy": "ReviewedNovelCondensates"
"inversedBy": "ReviewedNovelCondensates"
},
"ReviewedAt": {
"type": "datetime"
......
......@@ -6,4 +6,94 @@
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::novel-condensate.novel-condensate');
module.exports = createCoreController('api::novel-condensate.novel-condensate', ({ strapi }) => ({
async create(ctx) {
const now = new Date();
ctx.request.body.data.submittedBy = ctx.state.user;
ctx.request.body.data.SubmittedAt = now;
switch (ctx.state.user.role.name) {
case 'Maintainer':
case 'Administrator':
ctx.request.body.data.ReviewedAt = now;
ctx.request.body.data.reviewedBy = ctx.state.user;
break;
}
return super.create(ctx);
},
// This updateReview function is called by only either Maintainer or Administrator
async updateReview(ctx) {
// console.log('update info')
let response;
switch (ctx.state.user.role.name) {
case 'Public':
case 'Authenticated':
case 'Contributor':
// Remove operation should be intact
const sanitizedEntity = await this.sanitizeOutput(null, ctx);
response = this.transformResponse(sanitizedEntity);
break;
case 'Maintainer':
case 'Administrator':
// Handle the review comment and
// If ReviewAt field is null, should be set. Otherwise it continues;
// Apply review operation
const now = new Date();
if (ctx.request.body.data.ReviewedAt === null) {
ctx.request.body.data.ReviewedAt = now;
}
ctx.request.body.data.reviewedBy = ctx.state.user.id;
response = await super.update(ctx);
break;
}
return response;
},
async find(ctx) {
ctx.query['populate'] = ['submittedBy', 'reviewedBy'];
switch (ctx.state.user.role.name) {
case 'Public':
case 'Authenticated':
case 'Contributor':
// Remove operation should be intact
ctx.query['filters'] = { submittedBy: ctx.state.user.id };
break;
case 'Maintainer':
case 'Administrator':
break;
}
return super.find(ctx)
},
async findOne(ctx) {
const { id } = ctx.params;
const { query } = ctx;
query['populate'] = ['submittedBy', 'reviewedBy'];
let entity = await strapi.service("api::update-item.update-item").findOne(id, query);
switch (ctx.state.user.role.name) {
case 'Public':
case 'Authenticated':
case 'Contributor':
if (entity.submittedBy === null || entity.submittedBy.id !== ctx.state.user.id) {
entity = null;
}
break;
case 'Maintainer':
case 'Administrator':
break;
}
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
return this.transformResponse(sanitizedEntity);
}
}));
module.exports = {
routes: [
{
method: 'PUT',
path: '/novel-condensate/review/:id',
handler: 'api::novel-condensate.novel-condensate.updateReview',
},
]
}
......@@ -100,17 +100,17 @@
"scientific_discipline": {
"type": "json"
},
"ReviewedNovelCondensates": {
"SubmittedNovelCondensates": {
"type": "relation",
"relation": "manyToOne",
"relation": "oneToMany",
"target": "api::novel-condensate.novel-condensate",
"inversedBy": "reviewedBy"
"mappedBy": "submittedBy"
},
"SubmittedNovelCondensates": {
"ReviewedNovelCondensates": {
"type": "relation",
"relation": "oneToMany",
"target": "api::novel-condensate.novel-condensate",
"mappedBy": "submittedBy"
"mappedBy": "reviewedBy"
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment