Skip to content
Snippets Groups Projects
update-item.js 3.75 KiB
Newer Older
moon's avatar
moon committed
'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
moon's avatar
moon committed
  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 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;
    }

moon's avatar
moon committed
    return response;
  },
  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);