Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Surveys Controller
- class Api::V1::SurveysController < Api::V1::BaseController
- before_action :set_survey, only: [:show, :questions]
- # Survey List
- def index
- @schedules = @account.schedules.includes(:survey, survey_version: :departments).joins(:survey)
- .where(surveys: { status: 1 }, completed_at: nil).where(ignore: false)
- .where('schedules.start_time <= :time AND schedules.buffer_time >= :time', time: Time.current)
- .order('schedules.start_time asc')
- end
- # Surveys for schedule reminder
- def reminder
- @schedules = @account.schedules.includes(:survey, :survey_version).joins(:survey)
- .where(surveys: { status: 1 }, completed_at: nil).where(ignore: false).where.not(survey_versions: { notify_time: 0 })
- .where(schedules: { notify_time: Time.current.beginning_of_day..Time.current.end_of_day })
- .order('schedules.notify_time asc')
- end
- # History of Survey for current Day
- def history
- @schedules = @account.schedules.includes(:survey, :survey_version).where(ignore: false)
- .where(completed_at: Time.current.beginning_of_day..Time.current.end_of_day)
- end
- # Survey Show on History list
- def history_show
- schedule = Schedule.find(params[:id])
- @survey = schedule.survey
- if !schedule.completed_at.nil?
- @answers = schedule.answers.group_by(&:question_id)
- else
- render json: { code: 404, status: 'not_found', payload: {}, errors: ['Survey not found'] }
- end
- end
- # Details of Surveys
- def survey_details
- @schedule = Schedule.includes(:survey_version).find(params[:id])
- @questions = (@schedule.survey_version.questions.order('created_at ASC') + Incident.default_questions) unless @schedule.survey_version.disable_default_questions
- @questions = @schedule.survey_version.questions.order('created_at ASC') if @schedule.survey_version.disable_default_questions
- @staffs = @account.organization.accounts
- @departments = @account.organization.departments
- end
- def departments
- render json: { departments: @account.organization.departments.pluck(:id, :name) }
- end
- private
- def set_survey
- @survey = Survey.find(params[:id])
- end
- end
- # Survey Model
- class Survey < ActiveRecord::Base
- has_many :email_notifications, as: :item
- belongs_to :version, class_name: 'SurveyVersion', foreign_key: 'version_id'
- belongs_to :department
- belongs_to :account
- belongs_to :organization
- has_many :survey_versions, dependent: :destroy
- has_many :completed_surveys, dependent: :destroy
- has_many :schedules, dependent: :destroy
- enum status: %w(draft published suspended archived)
- accepts_nested_attributes_for :survey_versions
- def publish!
- update_attributes(status: 1, published_on: Time.current, suspended_on: nil, archived_on: nil)
- version.update_attribute(:unpublished_at, nil)
- version.schedule!
- end
- def suspend!
- update_attributes(status: 2, suspended_on: Time.current)
- version.update_attribute(:unpublished_at, Time.current)
- version.remove_future_schedules!
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement