Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.79 KB | None | 0 0
  1. class Api::IntegrationsController < ApplicationController
  2.   skip_before_filter :authenticate_user!
  3.   skip_before_filter :verify_authenticity_token
  4.  
  5.   def show
  6.     if request.query_parameters && request.query_parameters[:token] == 'MTc4OTY6MjUwOTE6OGYzNWYyMzBlZDE5NzYyMGZmM2VjMjZjYzMwYWNjZmQ='
  7.       case request.query_parameters[:action]
  8.       when 'export'
  9.         requests = Request.joins(:project)
  10.           .where('requests.created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime)
  11.           .limit(request.query_parameters[:limit].to_i)
  12.           .offset(request.query_parameters[:offset].to_i)
  13.         data = {
  14.           'pagination': {
  15.             'total_count': Request.where('created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime).size,
  16.             'limit': requests.size
  17.           },
  18.           'fields': [],
  19.           'statuses': [
  20.             {'id': 0, 'name': 'Первичный интерес', 'type': 'progress'},
  21.             {'id': 1, 'name': 'В проект', 'type': 'progress'},
  22.             {'id': 2, 'name': 'Реализация', 'type': 'progress'},
  23.             {'id': 3, 'name': 'Завершен', 'type': 'progress'},
  24.             {'id': 4, 'name': 'Завершен и оплачен', 'type': 'paid'},
  25.             {'id': 5, 'name': 'Отказ', 'type': 'canceled'},
  26.             {'id': 6, 'name': 'Тендер', 'type': 'progress'}
  27.           ],
  28.           'orders': []
  29.         }
  30.         requests.each do |request|
  31.           data[:orders].push({
  32.             'id': request.id,
  33.             'date_create': request.created_at.to_time.to_i,
  34.             'status': Request.statuses.fetch(request.status, nil),
  35.             'price': request.project.estimate.outer_cost,
  36.             'cost': request.project.estimate.outer_cost,
  37.             'roistat': request.roistat,
  38.             'client_id': request.project.client_id
  39.           })
  40.         end
  41.       when 'export_clients'
  42.         data = {
  43.           clients: []
  44.         }
  45.         contacts = Contact.includes(:client, :telephones, :emails)
  46.           .where('created_at >= ?', Time.at(request.query_parameters[:date].to_i).to_datetime)
  47.           .limit(request.query_parameters[:limit])
  48.           .offset(request.query_parameters[:offset])
  49.         contacts.each do |contact|
  50.           data[:clients].push({
  51.             'id': contact.id,
  52.             'name': contact.to_s,
  53.             'phone': contact.telephones.map(&:value).join(', '),
  54.             'email': contact.emails.map(&:value).join(', '),
  55.             'company': contact.client.try(&:to_s),
  56.             'birth_date': contact.birthday_on,
  57.           })
  58.         end
  59.       else
  60.         data = {status: 'error', message: 'Invalid token'}
  61.       end
  62.       render json: data
  63.     end
  64.   rescue
  65.     render json:{status: 'error'}
  66.   end
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement