dawarich/app/controllers/api/v1/visits_controller.rb
Evgenii Burmakin 8934c29fce
0.36.2 (#2007)
* fix: move foreman to global gems to fix startup crash (#1971)

* Update exporting code to stream points data to file in batches to red… (#1980)

* Update exporting code to stream points data to file in batches to reduce memory usage

* Update changelog

* Update changelog

* Feature/maplibre frontend (#1953)

* Add a plan to use MapLibre GL JS for the frontend map rendering, replacing Leaflet

* Implement phase 1

* Phases 1-3 + part of 4

* Fix e2e tests

* Phase 6

* Implement fog of war

* Phase 7

* Next step: fix specs, phase 7 done

* Use our own map tiles

* Extract v2 map logic to separate manager classes

* Update settings panel on v2 map

* Update v2 e2e tests structure

* Reimplement location search in maps v2

* Update speed routes

* Implement visits and places creation in v2

* Fix last failing test

* Implement visits merging

* Fix a routes e2e test and simplify the routes layer styling.

* Extract js to modules from maps_v2_controller.js

* Implement area creation

* Fix spec problem

* Fix some e2e tests

* Implement live mode in v2 map

* Update icons and panel

* Extract some styles

* Remove unused file

* Start adding dark theme to popups on MapLibre maps

* Make popups respect dark theme

* Move v2 maps to maplibre namespace

* Update v2 references to maplibre

* Put place, area and visit info into side panel

* Update API to use safe settings config method

* Fix specs

* Fix method name to config in SafeSettings and update usages accordingly

* Add missing public files

* Add handling for real time points

* Fix remembering enabled/disabled layers of the v2 map

* Fix lots of e2e tests

* Add settings to select map version

* Use maps/v2 as main path for MapLibre maps

* Update routing

* Update live mode

* Update maplibre controller

* Update changelog

* Remove some console.log statements

---------

Co-authored-by: Robin Tuszik <mail@robin.gg>
2025-12-06 20:54:49 +01:00

124 lines
3.1 KiB
Ruby

# frozen_string_literal: true
class Api::V1::VisitsController < ApiController
def index
visits = Visits::Finder.new(current_api_user, params).call
serialized_visits = visits.map do |visit|
Api::VisitSerializer.new(visit).call
end
render json: serialized_visits
end
def show
visit = current_api_user.visits.find(params[:id])
render json: Api::VisitSerializer.new(visit).call
end
def create
service = Visits::Create.new(current_api_user, visit_params)
result = service.call
if result
render json: Api::VisitSerializer.new(service.visit).call
else
error_message = service.errors || 'Failed to create visit'
render json: { error: error_message }, status: :unprocessable_content
end
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_content
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_content
end
end
def bulk_update
service = Visits::BulkUpdate.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_content
end
end
def destroy
visit = current_api_user.visits.find(params[:id])
if visit.destroy
head :no_content
else
render json: {
error: 'Failed to delete visit',
errors: visit.errors.full_messages
}, status: :unprocessable_content
end
rescue ActiveRecord::RecordNotFound
render json: { error: 'Visit not found' }, status: :not_found
end
private
def visit_params
params.require(:visit).permit(:name, :place_id, :status, :latitude, :longitude, :started_at, :ended_at)
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|
next if %w[latitude longitude].include?(key.to_s)
visit[key] = value
visit.name = visit.place.name if visit_params[:place_id].present?
end
visit.save!
visit
end
end