mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
126 lines
3.4 KiB
Ruby
126 lines
3.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Api::V1::VisitsController < ApiController
|
|
def index
|
|
# If selection is true, filter by coordinates instead of time
|
|
if params[:selection] == 'true' && params[:sw_lat].present? && params[:sw_lng].present? && params[:ne_lat].present? && params[:ne_lng].present?
|
|
sw_lat = params[:sw_lat].to_f
|
|
sw_lng = params[:sw_lng].to_f
|
|
ne_lat = params[:ne_lat].to_f
|
|
ne_lng = params[:ne_lng].to_f
|
|
|
|
# Create the PostGIS bounding box polygon
|
|
bounding_box = "ST_MakeEnvelope(#{sw_lng}, #{sw_lat}, #{ne_lng}, #{ne_lat}, 4326)"
|
|
|
|
visits =
|
|
Visit
|
|
.includes(:place)
|
|
.where(user: current_api_user)
|
|
.joins(:place)
|
|
.where("ST_Contains(#{bounding_box}, ST_SetSRID(places.lonlat::geometry, 4326))")
|
|
.order(started_at: :desc)
|
|
else
|
|
# Regular time-based filtering
|
|
start_time = begin
|
|
Time.zone.parse(params[:start_at])
|
|
rescue StandardError
|
|
Time.zone.now.beginning_of_day
|
|
end
|
|
end_time = begin
|
|
Time.zone.parse(params[:end_at])
|
|
rescue StandardError
|
|
Time.zone.now.end_of_day
|
|
end
|
|
|
|
visits =
|
|
Visit
|
|
.includes(:place)
|
|
.where(user: current_api_user)
|
|
.where('started_at >= ? AND ended_at <= ?', start_time, end_time)
|
|
.order(started_at: :desc)
|
|
end
|
|
|
|
serialized_visits = visits.map do |visit|
|
|
Api::VisitSerializer.new(visit).call
|
|
end
|
|
|
|
render json: serialized_visits
|
|
end
|
|
|
|
def update
|
|
visit = current_api_user.visits.find(params[:id])
|
|
visit = update_visit(visit)
|
|
|
|
render json: Api::VisitSerializer.new(visit).call
|
|
end
|
|
|
|
def merge
|
|
# Validate that we have at least 2 visit IDs
|
|
visit_ids = params[:visit_ids]
|
|
if visit_ids.blank? || visit_ids.length < 2
|
|
return render json: { error: 'At least 2 visits must be selected for merging' }, status: :unprocessable_entity
|
|
end
|
|
|
|
# Find all visits that belong to the current user
|
|
visits = current_api_user.visits.where(id: visit_ids).order(started_at: :asc)
|
|
|
|
# Ensure we found all the visits
|
|
if visits.length != visit_ids.length
|
|
return render json: { error: 'One or more visits not found' }, status: :not_found
|
|
end
|
|
|
|
# Use the service to merge the visits
|
|
service = Visits::MergeService.new(visits)
|
|
merged_visit = service.call
|
|
|
|
if merged_visit&.persisted?
|
|
render json: Api::VisitSerializer.new(merged_visit).call, status: :ok
|
|
else
|
|
render json: { error: service.errors.join(', ') }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def bulk_update
|
|
service = Visits::BulkUpdateService.new(
|
|
current_api_user,
|
|
params[:visit_ids],
|
|
params[:status]
|
|
)
|
|
|
|
result = service.call
|
|
|
|
if result
|
|
render json: {
|
|
message: "#{result[:count]} visits updated successfully",
|
|
updated_count: result[:count]
|
|
}, status: :ok
|
|
else
|
|
render json: { error: service.errors.join(', ') }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def visit_params
|
|
params.require(:visit).permit(:name, :place_id, :status)
|
|
end
|
|
|
|
def merge_params
|
|
params.permit(visit_ids: [])
|
|
end
|
|
|
|
def bulk_update_params
|
|
params.permit(:status, visit_ids: [])
|
|
end
|
|
|
|
def update_visit(visit)
|
|
visit_params.each do |key, value|
|
|
visit[key] = value
|
|
visit.name = visit.place.name if visit_params[:place_id].present?
|
|
end
|
|
|
|
visit.save!
|
|
|
|
visit
|
|
end
|
|
end
|