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 9c99c0307..ab262ddd9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -301,6 +301,7 @@ resources :order_articles, only: %i[index show] resources :group_order_articles resources :article_categories, only: %i[index show] + resources :mirko end end