Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Api::IntegrationsController < ApplicationController
- skip_before_filter :authenticate_user!
- skip_before_filter :verify_authenticity_token
- def show
- if request.query_parameters && request.query_parameters[:token] == 'MTc4OTY6MjUwOTE6OGYzNWYyMzBlZDE5NzYyMGZmM2VjMjZjYzMwYWNjZmQ='
- case request.query_parameters[:action]
- when 'export'
- requests = Request.joins(:project)
- .where('requests.created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime)
- .limit(request.query_parameters[:limit].to_i)
- .offset(request.query_parameters[:offset].to_i)
- data = {
- 'pagination': {
- 'total_count': Request.where('created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime).size,
- 'limit': requests.size
- },
- 'fields': [],
- 'statuses': [
- {'id': 0, 'name': 'Первичный интерес', 'type': 'progress'},
- {'id': 1, 'name': 'В проект', 'type': 'progress'},
- {'id': 2, 'name': 'Реализация', 'type': 'progress'},
- {'id': 3, 'name': 'Завершен', 'type': 'progress'},
- {'id': 4, 'name': 'Завершен и оплачен', 'type': 'paid'},
- {'id': 5, 'name': 'Отказ', 'type': 'canceled'},
- {'id': 6, 'name': 'Тендер', 'type': 'progress'}
- ],
- 'orders': []
- }
- requests.each do |request|
- data[:orders].push({
- 'id': request.id,
- 'date_create': request.created_at.to_time.to_i,
- 'status': Request.statuses.fetch(request.status, nil),
- 'price': request.project.estimate.outer_cost,
- 'cost': request.project.estimate.outer_cost,
- 'roistat': request.roistat,
- 'client_id': request.project.client_id
- })
- end
- when 'export_clients'
- data = {
- clients: []
- }
- contacts = Contact.includes(:client, :telephones, :emails)
- .where('created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime)
- .limit(request.query_parameters[:limit])
- .offset(request.query_parameters[:offset])
- contacts.each do |contact|
- data[:clients].push({
- 'id': contact.id,
- 'name': contact.to_s,
- 'phone': contact.telephones.map(&:value).join(', '),
- 'email': contact.emails.map(&:value).join(', '),
- 'company': contact.client.try(&:to_s),
- 'birth_date': contact.birthday_on,
- })
- end
- else
- data = {status: 'error', message: 'Invalid token'}
- end
- render json: data
- end
- rescue
- render json:{status: 'error'}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement