back-end/app/controllers/complaint_controller.rb
#assign complaint to superviser if ward is present in complaint
# else call district office assignment if district office is present
# else return no data present for district
assign complaint to superviser if ward is present in complaint
else call district office assignment if district office is present
else return no data present for district
assign complaint to superviser if ward is present in complaint else call district office assignment if district office is present else return no data present for district
def auto_assign_complaint(complaint_id, state, district, subject, ward, sub_category)
    district_office = DistrictOffice.where(state: state,
                                           district: district).first
    if district_office
      ward_office = WardOffice.where(district_office_id: district_office.id,
                                     ward: ward).first
      if ward_office
        all_ward_supervisers = AdminUser.where(designation: "superviser",
                                               municipal_id: ward_office.id,
                                               department: subject)
        if all_ward_supervisers
          least_complaints = 9999
          least_complaints_user_id = 13
          all_ward_supervisers.each do |superviser|
            total_complaints = ComplaintStatus.where(admin_user_id: superviser.id,
                                                     status: "active")
            if total_complaints < least_complaints
              least_complaints = total_complaints
              least_complaints_user_id = superviser.id
            end
          end
          final_superviser = AdminUser.find(least_complaints_user_id)
          complaint_update = ComplaintUpdate.new(complaint_id: complaint_id,
                                                 assigned_to: "superviser: " + final_superviser.name,
                                                 notes: "Auto Assignment by System to concerned office")
          complaint_status = ComplaintStatus.new(complaint_id: complaint_id,
                                                 admin_user_id: final_superviser.id,
                                                 district_office_id: district_office.id,
                                                 ward_office_id: ward_office.id,
                                                 department: subject,
                                                 sub_category: sub_category,
                                                 status: "pending")
          if complaint_status.save && complaint_update.save
            return "Auto Assignment Complete!"
          else
            register_new_complaint(complaint_id, state, district, subject)
            return "Databse couldn't be updated"
          end
        else
          register_new_complaint(complaint_id, state, district, subject)
          return "No employees registered for the ward"
        end
      else
        register_new_complaint(complaint_id, state, district, subject)
      end
    else
      return "No data for district office complaint can't be forwarded at the moment"
    end
  end
def auto_assign_complaint ( complaint_id , state , district , subject , ward , sub_category ) district_office = DistrictOffice . where ( state : state , district : district ) . first if district_office ward_office = WardOffice . where ( district_office_id : district_office . id , ward : ward ) . first if ward_office all_ward_supervisers = AdminUser . where ( designation : "superviser" , municipal_id : ward_office . id , department : subject ) if all_ward_supervisers least_complaints = 9999 least_complaints_user_id = 13 all_ward_supervisers . each do | superviser | total_complaints = ComplaintStatus . where ( admin_user_id : superviser . id , status : "active" ) if total_complaints < least_complaints least_complaints = total_complaints least_complaints_user_id = superviser . id end end final_superviser = AdminUser . find ( least_complaints_user_id ) complaint_update = ComplaintUpdate . new ( complaint_id : complaint_id , assigned_to : "superviser: " + final_superviser . name , notes : "Auto Assignment by System to concerned office" ) complaint_status = ComplaintStatus . new ( complaint_id : complaint_id , admin_user_id : final_superviser . id , district_office_id : district_office . id , ward_office_id : ward_office . id , department : subject , sub_category : sub_category , status : "pending" ) if complaint_status . save && complaint_update . save return "Auto Assignment Complete!" else register_new_complaint ( complaint_id , state , district , subject ) return "Databse couldn't be updated" end else register_new_complaint ( complaint_id , state , district , subject ) return "No employees registered for the ward" end else register_new_complaint ( complaint_id , state , district , subject ) end else return "No data for district office complaint can't be forwarded at the moment" end end
assign complaint to superviser if ward is present in complaint
else call district office assignment if district office is present
else return no data present for district
assign complaint to superviser if ward is present in complaint else call district office assignment if district office is present else return no data present for district
# finding superviser with least active complaints
complaint_id
<not_specific>
state
<not_specific>
district
<not_specific>
subject
<not_specific>
ward
<not_specific>
sub_category
<not_specific>