module Logistics module Core class OperationDocumentsController < ApplicationController before_action :set_operation_document, only: [:update, :destroy] def index operation_documents = OperationDocument.where(:operation_id => params[:id]) data = ApplicationRecord.as_json(operation_documents) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end def create @operation_document = OperationDocument.new(operation_document_params) if @operation_document.save response = Mks::Common::MethodResponse.new(true, 'Operation document saved successfully!', @operation_document, nil, nil) else errors = Mks::Common::Util.error_messages @operation_document, 'Operation document' response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end def update if @operation_document.update(operation_document_params) response = Mks::Common::MethodResponse.new(true, "Operation document updated successfully!", @operation_document, nil, nil) else errors = Mks::Common::Util.error_messages @operation_document, "Operation document" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end def accept document = OperationDocument.find(params[:id]) document.is_accepted = true document.user_id = params[:user_id] document.date_accepted = DateTime.now.to_date document.save response = Mks::Common::MethodResponse.new(true, "Operation document accepted!", document, nil, nil) render json: response end def download document = OperationDocument.find(params[:id]) render json: Base64.encode64(document.file.read) end def destroy @operation_document.destroy response = Mks::Common::MethodResponse.new(true, "Operation document deleted!", nil, nil, nil) render json: response end private def set_operation_document @operation_document = OperationDocument.find(params[:id]) end def operation_document_params params.require(:operation_document).permit(:operation_id, :document, :applicant, :original, :copy, :certified, :source, :is_mandatory, :is_accepted, :file, :user_id, :date_accepted) end end end end