From 46d2aae216c448ab4eec621439e068728d5e0d01 Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Mon, 30 May 2022 11:31:24 +0200 Subject: [PATCH] Mirko Test API --- app/controllers/api/v1/mirko_controller.rb | 28 +++++++++++++++++ app/serializers/mirko_serializer.rb | 36 ++++++++++++++++++++++ app/serializers/user_serializer.rb | 2 +- config/routes.rb | 1 + 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/mirko_controller.rb create mode 100644 app/serializers/mirko_serializer.rb diff --git a/app/controllers/api/v1/mirko_controller.rb b/app/controllers/api/v1/mirko_controller.rb new file mode 100644 index 000000000..f8ed71687 --- /dev/null +++ b/app/controllers/api/v1/mirko_controller.rb @@ -0,0 +1,28 @@ +class Api::V1::MirkoController < Api::V1::BaseController + before_action -> { doorkeeper_authorize! 'orders:read' } + + def index + time_now = Time.now + gos = GroupOrder + .includes(group_order_articles: { order_article: :article }, + order: :supplier) + .references(:orders) + .where(ordergroup_id: current_user.ordergroup.id) + .limit(200) + .where(orders: { + ends: (time_now.weeks_ago(1)..time_now.next_year), state: "open", pickup: nil + }) + .or( + GroupOrder + .includes(group_order_articles: { order_article: :article }, + order: :supplier) + .references(:orders) + .where(ordergroup_id: current_user.ordergroup.id) + .limit(200) + .where(orders: { + pickup: ((time_now.weeks_ago(5)..(time_now.weeks_ago(-1)))), state: %w[finished received closed] + }) + ) + render json: gos, each_serializer: MirkoSerializer + end +end diff --git a/app/serializers/mirko_serializer.rb b/app/serializers/mirko_serializer.rb new file mode 100644 index 000000000..0a94df33f --- /dev/null +++ b/app/serializers/mirko_serializer.rb @@ -0,0 +1,36 @@ +class MirkoSerializer < ActiveModel::Serializer + attributes :id + # has_many :group_order_articles, serializer: MirkoSubSerializer + attributes :enddate + attributes :pickupdate + attributes :order_state + attributes :suppliername + attributes :article_details + attributes :transport + + def article_details + object.group_order_articles.map do |goa| + { id: goa.id, + name: goa.order_article.article.name, + unit: goa.order_article.article.unit, + price: goa.order_article.article.price, + ordered: goa.quantity, received: goa.result } + end + end + + def suppliername + object.order.name + end + + def enddate + object.order.ends + end + + def pickupdate + object.order.pickup + end + + def order_state + object.order.state + end +end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 5510b747f..9179f5451 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -1,3 +1,3 @@ class UserSerializer < ActiveModel::Serializer - attributes :id, :name, :email, :locale + attributes :id, :name, :email, :locale, :ordergroup_name end diff --git a/config/routes.rb b/config/routes.rb index 9cef0c771..4f7f34c49 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -293,6 +293,7 @@ resources :order_articles, only: [:index, :show] resources :group_order_articles resources :article_categories, only: [:index, :show] + resources :mirko end end