Assign user_id to points on parsing

This commit is contained in:
Eugene Burmakin 2025-01-20 20:07:46 +01:00
parent 6644fc9a13
commit 983768a572
3 changed files with 33 additions and 5 deletions

View file

@ -22,6 +22,9 @@ class Api::V1::PointsController < ApiController
end
def create
Points::CreateJob.perform_later(batch_params, current_api_user.id)
render json: { message: 'Points are being processed' }
end
def update
@ -45,6 +48,10 @@ class Api::V1::PointsController < ApiController
params.require(:point).permit(:latitude, :longitude)
end
def batch_params
params.permit(locations: [:type, { geometry: {}, properties: {} }], batch: {})
end
def point_serializer
params[:slim] == 'true' ? Api::SlimPointSerializer : Api::PointSerializer
end

View file

@ -1,16 +1,17 @@
# frozen_string_literal: true
class Points::Params
attr_reader :data, :points
attr_reader :data, :points, :user_id
def initialize(json)
def initialize(json, user_id)
@data = json.with_indifferent_access
@points = @data[:locations]
@user_id = user_id
end
def call
points.map do |point|
next if point[:geometry].nil? || point.dig(:properties, :timestamp).nil?
next unless params_valid?(point)
{
latitude: point[:geometry][:coordinates][1],
@ -26,7 +27,8 @@ class Points::Params
vertical_accuracy: point[:properties][:vertical_accuracy],
course_accuracy: point[:properties][:course_accuracy],
course: point[:properties][:course],
raw_data: point
raw_data: point,
user_id: user_id
}
end.compact
end
@ -38,4 +40,10 @@ class Points::Params
value.positive? ? value : nil
end
def params_valid?(point)
point[:geometry].present? &&
point[:geometry][:coordinates].present? &&
point.dig(:properties, :timestamp).present?
end
end

View file

@ -1,5 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Points::CreateJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
describe '#perform' do
subject(:perform) { described_class.new.perform(json, user.id) }
let(:file_path) { 'spec/fixtures/files/points/geojson_example.json' }
let(:file) { File.open(file_path) }
let(:json) { JSON.parse(file.read) }
let(:user) { create(:user) }
it 'creates a point' do
expect { perform }.to change { Point.count }.by(6)
end
end
end