Newer
Older
'use strict';
/**
* update-item controller
*/
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::update-item.update-item', ({ strapi }) => ({
async create(ctx) {
ctx.request.body.data.submittedBy = ctx.state.user;
ctx.request.body.data.SubmittedAt = new Date();
return super.create(ctx);
},
async update(ctx) {
// console.log(ctx.state.user.role.name);
switch (ctx.state.user.role.name) {
case 'Public':
case 'Authenticated':
case 'Contributor':
// Remove operation should be intact
if (ctx.request.body.data.submittedBy.id !== ctx.state.user.id) {
const sanitizedEntity = await this.sanitizeOutput(null, ctx);
response = this.transformResponse(sanitizedEntity);
} else {
ctx.request.body.data.updatedBy = ctx.state.user.id;
ctx.request.body.data.UpdateSubmissionTimestamp = new Date();
response = await super.update(ctx);
}
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 entity = await strapi.service('api::update-item.update-item').findOne(ctx.params.id);
// console.log(entity);
ctx.request.body.data.updatedBy = ctx.state.user.id;
ctx.request.body.data.UpdateSubmissionTimestamp = new Date();
response = await super.update(ctx);
return response;
// This updateReview function is called by only either Maintainer or Administrator
// 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 entity = await strapi.service('api::update-item.update-item').findOne(ctx.params.id);
// console.log(entity);
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;
ctx.request.body.data.UpdateReviewTimestamp = now;
response = await super.update(ctx);
break;
}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
},
async find(ctx) {
// console.log(ctx.state.user.role.name);
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':
// Handle the review comment and
// If ReviewAt field is null, should be set. Otherwise it continues;
// Apply review operation
// const entity = await strapi.service('api::update-item.update-item').findOne(ctx.params.id);
// console.log(entity);
break;
}
// console.log(ctx.query)
return super.find(ctx)
},
async findOne(ctx) {
// console.log(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);
// console.log(entity);
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);