Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIs for invoice list page #208

Merged
merged 23 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,6 @@ gem "pundit", "~> 2.2"

# Data migration gem to migrate data alongside schema changes
gem "data_migrate", "~> 8.0.0.rc2"

# pagy for Pagination
gem "pagy", "~> 5.10"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ GEM
actionpack (>= 4.2)
omniauth (~> 2.0)
orm_adapter (0.5.0)
pagy (5.10.1)
activesupport
parallel (1.21.0)
parser (3.1.0.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -475,6 +477,7 @@ DEPENDENCIES
newrelic_rpm (~> 8.4)
omniauth-google-oauth2 (~> 1.0)
omniauth-rails_csrf_protection (~> 1.0)
pagy (~> 5.10)
pg
puma (~> 5.0)
pundit (~> 2.2)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base
include PunditHelper
include ErrorHandler
include CurrentCompanyConcern
include Pagy::Backend

before_action :authenticate_user!, :validate_company!

Expand Down
10 changes: 10 additions & 0 deletions app/controllers/concerns/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module ErrorHandler
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
rescue_from Discard::RecordNotDiscarded, with: :record_not_discarded
rescue_from ActiveRecord::RecordInvalid, with: :record_invalid
rescue_from Pagy::VariableError, with: :invalid_param_value
end

private
Expand Down Expand Up @@ -63,4 +64,13 @@ def record_invalid(exception)
format.html { render file: "public/422.html", status: :unprocessable_entity, layout: false, alert: message }
end
end

def invalid_param_value(exception)
message = exception.message

respond_to do |format|
format.json { render json: { errors: message }, status: :bad_request }
format.html { render file: "public/400.html", status: :bad_request, layout: false, alert: message }
end
end
apoorv-mishra marked this conversation as resolved.
Show resolved Hide resolved
end
1 change: 1 addition & 0 deletions app/controllers/internal_api/v1/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class InternalApi::V1::ApplicationController < ActionController::API
include ActionController::MimeResponds
include ErrorHandler
include CurrentCompanyConcern
include Pagy::Backend

before_action :authenticate_user!
end
19 changes: 17 additions & 2 deletions app/controllers/internal_api/v1/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

class InternalApi::V1::InvoicesController < InternalApi::V1::ApplicationController
def index
end
authorize Invoice
pagy, invoices = pagy(
current_company.invoices.includes(:client)
.from_date(params[:from])
.to_date(params[:to])
.for_clients(params[:client_ids])
.with_statuses(params[:statuses]),
items_param: :invoices_per_page)

def show
render :index, locals: {
invoices:,
pagy: pagy_metadata(pagy),
summary: {
overdue_amount: current_company.invoices.overdue.sum(:amount),
outstanding_amount: current_company.invoices.sum(:outstanding_amount),
draft_amount: current_company.invoices.draft.sum(:amount)
}
}
end
end
12 changes: 9 additions & 3 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
# amount_due :decimal(20, 2) default("0.0")
# discount :decimal(20, 2) default("0.0")
# status :integer default("0"), not null
# company_id :integer not null
# client_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_invoices_on_client_id (client_id)
# index_invoices_on_company_id (company_id)
# index_invoices_on_invoice_number (invoice_number) UNIQUE
# index_invoices_on_issue_date (issue_date)
# index_invoices_on_status (status)
#

# frozen_string_literal: true
Expand All @@ -40,7 +40,6 @@ class Invoice < ApplicationRecord
:overdue
]

belongs_to :company
belongs_to :client
has_many :invoice_line_items, dependent: :destroy

Expand All @@ -50,6 +49,13 @@ class Invoice < ApplicationRecord
:amount_paid, :amount_due, :discount, numericality: { greater_than_or_equal_to: 0 }
validates :invoice_number, uniqueness: true

scope :with_statuses, -> (statuses) { where(status: statuses) if statuses.present? }
scope :from_date, -> (from) { where("issue_date >= ?", from) if from.present? }
apoorv-mishra marked this conversation as resolved.
Show resolved Hide resolved
scope :to_date, -> (to) { where("issue_date <= ?", to) if to.present? }
scope :for_clients, -> (client_ids) { where(client_id: client_ids) if client_ids.present? }

delegate :name, to: :client, prefix: :client
apoorv-mishra marked this conversation as resolved.
Show resolved Hide resolved

def sub_total
@_sub_total ||= invoice_line_items.sum { |line_item| line_item[:rate] * line_item[:quantity] }
end
Expand Down
24 changes: 24 additions & 0 deletions app/views/internal_api/v1/invoices/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

json.key_format! camelize: :lower
json.deep_format_keys!
json.invoices do
json.array! invoices do |invoice|
json.id invoice.id
json.invoice_number invoice.invoice_number
json.issue_date invoice.issue_date
json.due_date invoice.due_date
json.amount invoice.amount
json.client do
json.name invoice.client_name
end
json.company do
json.name current_company.name
json.base_currency current_company.base_currency
json.date_format current_company.date_format || "YYYY-MM-DD"
end
json.status invoice.status
end
end
json.summary summary
json.pagy pagy
238 changes: 238 additions & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
# frozen_string_literal: true

require "pagy/extras/metadata"
Pagy::DEFAULT[:metadata] = %i[scaffold_url page count in items pages prev next last] # example

require "pagy/extras/items"
# Pagy initializer file (5.10.1)
# Customize only what you really need and notice that the core Pagy works also without any of the following lines.
# Should you just cherry pick part of this file, please maintain the require-order of the extras

# Pagy DEFAULT Variables
# See https://ddnexus.github.io/pagy/api/pagy#variables
# All the Pagy::DEFAULT are set for all the Pagy instances but can be overridden per instance by just passing them to
# Pagy.new|Pagy::Countless.new|Pagy::Calendar::*.new or any of the #pagy* controller methods

# Instance variables
# See https://ddnexus.github.io/pagy/api/pagy#instance-variables
# Pagy::DEFAULT[:page] = 1 # default
# Pagy::DEFAULT[:items] = 20 # default
# Pagy::DEFAULT[:outset] = 0 # default

# Other Variables
# See https://ddnexus.github.io/pagy/api/pagy#other-variables
# Pagy::DEFAULT[:size] = [1,4,4,1] # default
# Pagy::DEFAULT[:page_param] = :page # default
# The :params can be also set as a lambda e.g ->(params){ params.exclude('useless').merge!('custom' => 'useful') }
# Pagy::DEFAULT[:params] = {} # default
# Pagy::DEFAULT[:fragment] = '#fragment' # example
# Pagy::DEFAULT[:link_extra] = 'data-remote="true"' # example
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default
# Pagy::DEFAULT[:cycle] = true # example

# Extras
# See https://ddnexus.github.io/pagy/extras

# Backend Extras

# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding
# See https://ddnexus.github.io/pagy/extras/array
# require 'pagy/extras/array'

# Calendar extra: Add pagination filtering by calendar time unit (year, quarter, month, week, day)
# See https://ddnexus.github.io/pagy/extras/calendar
# require 'pagy/extras/calendar'
# Default for each unit
# Pagy::Calendar::Year::DEFAULT[:order] = :asc # Time direction of pagination
# Pagy::Calendar::Year::DEFAULT[:format] = '%Y' # strftime format
#
# Pagy::Calendar::Quarter::DEFAULT[:order] = :asc # Time direction of pagination
# Pagy::Calendar::Quarter::DEFAULT[:format] = '%Y-Q%q' # strftime format
#
# Pagy::Calendar::Month::DEFAULT[:order] = :asc # Time direction of pagination
# Pagy::Calendar::Month::DEFAULT[:format] = '%Y-%m' # strftime format
#
# Pagy::Calendar::Week::DEFAULT[:order] = :asc # Time direction of pagination
# Pagy::Calendar::Week::DEFAULT[:format] = '%Y-%W' # strftime format
#
# Pagy::Calendar::Day::DEFAULT[:order] = :asc # Time direction of pagination
# Pagy::Calendar::Day::DEFAULT[:format] = '%Y-%m-%d' # strftime format
#
# Uncomment the following lines, if you need calendar localization without using the I18n extra
# module LocalizePagyCalendar
# def localize(time, opts)
# ::I18n.l(time, **opts)
# end
# end
# Pagy::Calendar.prepend LocalizePagyCalendar

# Countless extra: Paginate without any count, saving one query per rendering
# See https://ddnexus.github.io/pagy/extras/countless
# require 'pagy/extras/countless'
# Pagy::DEFAULT[:countless_minimal] = false # default (eager loading)

# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
# Default :pagy_search method: change only if you use also
# the searchkick or meilisearch extra that defines the same
# Pagy::DEFAULT[:elasticsearch_rails_pagy_search] = :pagy_search
# Default original :search method called internally to do the actual search
# Pagy::DEFAULT[:elasticsearch_rails_search] = :search
# require 'pagy/extras/elasticsearch_rails'

# Headers extra: http response headers (and other helpers) useful for API pagination
# See http://ddnexus.github.io/pagy/extras/headers
# require 'pagy/extras/headers'
# Pagy::DEFAULT[:headers] = { page: 'Current-Page',
# items: 'Page-Items',
# count: 'Total-Count',
# pages: 'Total-Pages' } # default

# Meilisearch extra: Paginate `Meilisearch` result objects
# See https://ddnexus.github.io/pagy/extras/meilisearch
# Default :pagy_search method: change only if you use also
# the elasticsearch_rails or searchkick extra that define the same method
# Pagy::DEFAULT[:meilisearch_pagy_search] = :pagy_search
# Default original :search method called internally to do the actual search
# Pagy::DEFAULT[:meilisearch_search] = :ms_search
# require 'pagy/extras/meilisearch'

# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc.
# See https://ddnexus.github.io/pagy/extras/metadata
# you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
# require 'pagy/extras/shared'
# For performance reasons, you should explicitly set ONLY the metadata you use in the frontend
# Pagy::DEFAULT[:metadata] = %i[scaffold_url page count in items pages prev next last] # example

# Searchkick extra: Paginate `Searchkick::Results` objects
# See https://ddnexus.github.io/pagy/extras/searchkick
# Default :pagy_search method: change only if you use also
# the elasticsearch_rails or meilisearch extra that defines the same
# DEFAULT[:searchkick_pagy_search] = :pagy_search
# Default original :search method called internally to do the actual search
# Pagy::DEFAULT[:searchkick_search] = :search
# require 'pagy/extras/searchkick'
# uncomment if you are going to use Searchkick.pagy_search
# Searchkick.extend Pagy::Searchkick

# Frontend Extras

# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
# See https://ddnexus.github.io/pagy/extras/bootstrap
# require 'pagy/extras/bootstrap'

# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination
# See https://ddnexus.github.io/pagy/extras/bulma
# require 'pagy/extras/bulma'

# Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination
# See https://ddnexus.github.io/pagy/extras/foundation
# require 'pagy/extras/foundation'

# Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination
# See https://ddnexus.github.io/pagy/extras/materialize
# require 'pagy/extras/materialize'

# Navs extra: Add nav_js and combo_nav_js javascript helpers
# Notice: the other frontend extras add their own framework-styled versions,
# so require this extra only if you need the unstyled version
# See https://ddnexus.github.io/pagy/extras/navs
# require 'pagy/extras/navs'

# Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination
# See https://ddnexus.github.io/pagy/extras/semantic
# require 'pagy/extras/semantic'

# UIkit extra: Add nav helper and templates for UIkit pagination
# See https://ddnexus.github.io/pagy/extras/uikit
# require 'pagy/extras/uikit'

# Multi size var used by the *_nav_js helpers
# See https://ddnexus.github.io/pagy/extras/navs#steps
# Pagy::DEFAULT[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example

# Feature Extras

# Gearbox extra: Automatically change the number of items per page depending on the page number
# See https://ddnexus.github.io/pagy/extras/gearbox
# require 'pagy/extras/gearbox'
# set to false only if you want to make :gearbox_extra an opt-in variable
# Pagy::DEFAULT[:gearbox_extra] = false # default true
# Pagy::DEFAULT[:gearbox_items] = [15, 30, 60, 100] # default

# Items extra: Allow the client to request a custom number of items per page with an optional selector UI
# See https://ddnexus.github.io/pagy/extras/items
# require "pagy/extras/items"
# set to false only if you want to make :items_extra an opt-in variable
# Pagy::DEFAULT[:items_extra] = false # default true
# Pagy::DEFAULT[:items_param] = :items # default
# Pagy::DEFAULT[:max_items] = 100 # default

# Overflow extra: Allow for easy handling of overflowing pages
# See https://ddnexus.github.io/pagy/extras/overflow
# require 'pagy/extras/overflow'
# Pagy::DEFAULT[:overflow] = :empty_page # default (other options: :last_page and :exception)

# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination
# See https://ddnexus.github.io/pagy/extras/support
# require 'pagy/extras/support'

# Trim extra: Remove the page=1 param from links
# See https://ddnexus.github.io/pagy/extras/trim
# require 'pagy/extras/trim'
# set to false only if you want to make :trim_extra an opt-in variable
# Pagy::DEFAULT[:trim_extra] = false # default true

# Standalone extra: Use pagy in non Rack environment/gem
# See https://ddnexus.github.io/pagy/extras/standalone
# require 'pagy/extras/standalone'
# Pagy::DEFAULT[:url] = 'http://www.example.com/subdir' # optional default

# Rails
# Enable the .js file required by the helpers that use javascript
# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js)
# See https://ddnexus.github.io/pagy/extras#javascript

# With the asset pipeline
# Sprockets need to look into the pagy javascripts dir, so add it to the assets paths
# Rails.application.config.assets.paths << Pagy.root.join('javascripts')

# I18n

# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
# See https://ddnexus.github.io/pagy/api/frontend#i18n
# Notice: No need to configure anything in this section if your app uses only "en"
# or if you use the i18n extra below
#
# Examples:
# load the "de" built-in locale:
# Pagy::I18n.load(locale: 'de')
#
# load the "de" locale defined in the custom file at :filepath:
# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
#
# load the "de", "en" and "es" built-in locales:
# (the first passed :locale will be used also as the default_locale)
# Pagy::I18n.load({ locale: 'de' },
# { locale: 'en' },
# { locale: 'es' })
#
# load the "en" built-in locale, a custom "es" locale,
# and a totally custom locale complete with a custom :pluralize proc:
# (the first passed :locale will be used also as the default_locale)
# Pagy::I18n.load({ locale: 'en' },
# { locale: 'es', filepath: 'path/to/pagy-es.yml' },
# { locale: 'xyz', # not built-in
# filepath: 'path/to/pagy-xyz.yml',
# pluralize: lambda{ |count| ... } )

# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
# than the default pagy internal i18n (see above)
# See https://ddnexus.github.io/pagy/extras/i18n
# require 'pagy/extras/i18n'

# Default i18n key
# Pagy::DEFAULT[:i18n_key] = 'pagy.item_name' # default

# When you are done setting your own default freeze it, so it will not get changed accidentally
apoorv-mishra marked this conversation as resolved.
Show resolved Hide resolved
Pagy::DEFAULT.freeze
Loading