Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'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
ctx.request.body.data.updatedBy = ctx.state.user.id;
ctx.request.body.data.UpdateSubmissionTimestamp = new Date();
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.reviewedBy = ctx.state.user.id;
ctx.request.body.data.UpdateReviewTimestamp = new Date();
break;
}
return super.update(ctx);
},
// 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);
ctx.request.body.data.reviewedBy = ctx.state.user.id;
ctx.request.body.data.UpdateReviewTimestamp = new Date();
response = await super.update(ctx);
break;
}
69
70
71
72
73
74
75
76
77
78
79
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
},
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);