-
Notifications
You must be signed in to change notification settings - Fork 163
feat(order-forms): add OrderForm REST API #5360
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
Open
toommz
wants to merge
13
commits into
main
Choose a base branch
from
feat/order-forms/read-order-form-rest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a6d34e4
feat(order-forms): add OrderForm REST API
toommz 11ac51a
feat: check for the order-forms feature flag in REST endpoints
toommz bea79ab
dry the feature flag check with a before_action
toommz 9abf219
adding missing migration and model after rebase
toommz d7c1651
link order forms to quote versions, not quotes
toommz 938979b
feat(order-forms): add signed_by_user association on OrderForm
toommz f1b6aba
feat(order-forms): add Customer#order_forms inverse association
toommz 85be944
chore(order-forms): seed OrderForms with varied statuses
toommz b3c74f6
feat(order-forms): sequence number and drop unused text fields
toommz aadf1da
feat(order-forms): use Sequenced concern for OrderForm number
toommz add1cb0
test(order-forms): cover Sequenced number generation
toommz 90a45c1
chore(order-forms): refresh annotation after migration
toommz e2f97e2
chore(order-forms): update structure.sql for OrderForm schema
toommz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
app/contracts/queries/order_forms_query_filters_contract.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Queries | ||
| class OrderFormsQueryFiltersContract < Dry::Validation::Contract | ||
| params do | ||
| optional(:status).maybe do | ||
| value(:string, included_in?: OrderForm::STATUSES.keys.map(&:to_s)) | | ||
| array(:string, included_in?: OrderForm::STATUSES.keys.map(&:to_s)) | ||
| end | ||
| optional(:customer_id).maybe { value(:string, format?: Regex::UUID) | array(:string, format?: Regex::UUID) } | ||
| optional(:number).maybe { value(:string) | array(:string) } | ||
| optional(:quote_number).maybe { value(:string) | array(:string) } | ||
| optional(:owner_id).maybe { value(:string, format?: Regex::UUID) | array(:string, format?: Regex::UUID) } | ||
| optional(:created_at_from).maybe(:time) | ||
| optional(:created_at_to).maybe(:time) | ||
| optional(:expires_at_from).maybe(:time) | ||
| optional(:expires_at_to).maybe(:time) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| module V1 | ||
| class OrderFormsController < Api::BaseController | ||
| before_action :ensure_feature_flag! | ||
|
|
||
| def index | ||
| result = OrderFormsQuery.call( | ||
| organization: current_organization, | ||
| pagination: { | ||
| page: params[:page], | ||
| limit: params[:per_page] || PER_PAGE | ||
| }, | ||
| filters: index_filters, | ||
| search_term: params[:search_term] | ||
| ) | ||
|
|
||
| if result.success? | ||
| render( | ||
| json: ::CollectionSerializer.new( | ||
| result.order_forms, | ||
| ::V1::OrderFormSerializer, | ||
| collection_name: "order_forms", | ||
| meta: pagination_metadata(result.order_forms) | ||
| ) | ||
| ) | ||
| else | ||
| render_error_response(result) | ||
| end | ||
| end | ||
|
|
||
| def show | ||
| order_form = current_organization.order_forms.find_by(id: params[:id]) | ||
| return not_found_error(resource: "order_form") unless order_form | ||
|
|
||
| render_order_form(order_form) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ensure_feature_flag! | ||
| forbidden_error(code: "feature_not_available") unless current_organization.feature_flag_enabled?(:order_forms) | ||
| end | ||
|
|
||
| def index_filters | ||
| { | ||
| status: params[:status], | ||
| customer_id: params[:customer_id], | ||
| number: params[:number], | ||
| quote_number: params[:quote_number], | ||
| owner_id: params[:owner_id], | ||
| created_at_from: params[:created_at_from], | ||
| created_at_to: params[:created_at_to], | ||
| expires_at_from: params[:expires_at_from], | ||
| expires_at_to: params[:expires_at_to] | ||
| } | ||
| end | ||
|
|
||
| def render_order_form(order_form) | ||
| render( | ||
| json: ::V1::OrderFormSerializer.new(order_form, root_name: "order_form") | ||
| ) | ||
| end | ||
|
|
||
| def resource_name | ||
| "order_form" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class OrderForm < ApplicationRecord | ||
| include Sequenced | ||
|
|
||
| STATUSES = { | ||
| generated: "generated", | ||
| signed: "signed", | ||
| expired: "expired", | ||
| voided: "voided" | ||
| }.freeze | ||
|
|
||
| VOID_REASONS = { | ||
| manual: "manual", | ||
| expired: "expired", | ||
| invalid: "invalid" | ||
| }.freeze | ||
|
|
||
| before_save :ensure_number | ||
|
|
||
| belongs_to :organization | ||
| belongs_to :customer | ||
| belongs_to :quote_version | ||
| belongs_to :signed_by_user, class_name: "User", optional: true | ||
| has_one :quote, through: :quote_version | ||
|
|
||
| enum :status, STATUSES, | ||
| default: :generated, | ||
| validate: true | ||
| enum :void_reason, VOID_REASONS, | ||
| instance_methods: false, | ||
| validate: {allow_nil: true} | ||
|
|
||
| validates :billing_snapshot, presence: true | ||
|
|
||
| sequenced( | ||
| scope: ->(order_form) { order_form.organization.order_forms }, | ||
| lock_key: ->(order_form) { order_form.organization_id } | ||
| ) | ||
|
|
||
| def self.ransackable_attributes(_ = nil) | ||
| %w[number] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ensure_number | ||
| return if number.present? | ||
| return if sequential_id.blank? | ||
|
|
||
| time = created_at || Time.current | ||
| formatted_sequential_id = format("%04d", sequential_id) | ||
| self.number = "OF-#{time.strftime("%Y")}-#{formatted_sequential_id}" | ||
| end | ||
| end | ||
|
|
||
| # == Schema Information | ||
| # | ||
| # Table name: order_forms | ||
| # Database name: primary | ||
| # | ||
| # id :uuid not null, primary key | ||
| # billing_snapshot :jsonb not null | ||
| # expires_at :datetime | ||
| # number :string not null | ||
| # signed_at :datetime | ||
| # status :enum default("generated"), not null | ||
| # void_reason :enum | ||
| # voided_at :datetime | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # customer_id :uuid not null | ||
| # organization_id :uuid not null | ||
| # quote_version_id :uuid not null | ||
| # sequential_id :integer not null | ||
| # signed_by_user_id :uuid | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_order_forms_on_customer_id (customer_id) | ||
| # index_order_forms_on_quote_version_id (quote_version_id) UNIQUE | ||
| # index_unique_order_forms_on_organization_number (organization_id,number) UNIQUE | ||
| # index_unique_order_forms_on_organization_sequential_id (organization_id,sequential_id) UNIQUE | ||
| # | ||
| # Foreign Keys | ||
| # | ||
| # fk_rails_... (customer_id => customers.id) | ||
| # fk_rails_... (organization_id => organizations.id) | ||
| # fk_rails_... (quote_version_id => quote_versions.id) | ||
| # fk_rails_... (signed_by_user_id => users.id) | ||
| # | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class OrderFormsQuery < BaseQuery | ||
| Result = BaseResult[:order_forms] | ||
| Filters = BaseFilters[ | ||
| :status, | ||
| :customer_id, | ||
| :number, | ||
| :quote_number, | ||
| :owner_id, | ||
| :created_at_from, | ||
| :created_at_to, | ||
| :expires_at_from, | ||
| :expires_at_to | ||
| ] | ||
|
|
||
| def call | ||
| return result unless validate_filters.success? | ||
|
|
||
| order_forms = base_scope.result | ||
| order_forms = with_status(order_forms) if filters.status.present? | ||
| order_forms = with_customer_id(order_forms) if filters.customer_id.present? | ||
| order_forms = with_number(order_forms) if filters.number.present? | ||
| order_forms = with_quote_number(order_forms) if filters.quote_number.present? | ||
| order_forms = with_owner_id(order_forms) if filters.owner_id.present? | ||
| order_forms = with_created_at_range(order_forms) if created_at_range? | ||
| order_forms = with_expires_at_range(order_forms) if expires_at_range? | ||
| order_forms = paginate(order_forms) | ||
| order_forms = apply_consistent_ordering(order_forms) | ||
|
|
||
| result.order_forms = order_forms | ||
| result | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def filters_contract | ||
| @filters_contract ||= Queries::OrderFormsQueryFiltersContract.new | ||
| end | ||
|
|
||
| def base_scope | ||
| organization.order_forms.includes(:customer, :quote_version).ransack(search_params) | ||
| end | ||
|
|
||
| def search_params | ||
| return if search_term.blank? | ||
|
|
||
| {number_cont: search_term} | ||
| end | ||
|
|
||
| def with_status(scope) | ||
| scope.where(status: filters.status) | ||
| end | ||
|
|
||
| def with_customer_id(scope) | ||
| scope.where(customer_id: filters.customer_id) | ||
| end | ||
|
|
||
| def with_number(scope) | ||
| scope.where(number: filters.number) | ||
| end | ||
|
|
||
| def with_quote_number(scope) | ||
| scope.joins(quote_version: :quote).where(quotes: {number: filters.quote_number}) | ||
| end | ||
|
|
||
| def with_owner_id(scope) | ||
| scope.where( | ||
| quote_version_id: QuoteVersion.where( | ||
| quote_id: QuoteOwner.where(user_id: filters.owner_id).select(:quote_id) | ||
| ).select(:id) | ||
| ) | ||
| end | ||
|
|
||
| def with_created_at_range(scope) | ||
| scope = scope.where(created_at: created_at_from..) if filters.created_at_from | ||
| scope = scope.where(created_at: ..created_at_to) if filters.created_at_to | ||
| scope | ||
| end | ||
|
|
||
| def with_expires_at_range(scope) | ||
| scope = scope.where(expires_at: expires_at_from..) if filters.expires_at_from | ||
| scope = scope.where(expires_at: ..expires_at_to) if filters.expires_at_to | ||
| scope | ||
| end | ||
|
|
||
| def created_at_range? | ||
| filters.created_at_from.present? || filters.created_at_to.present? | ||
| end | ||
|
|
||
| def expires_at_range? | ||
| filters.expires_at_from.present? || filters.expires_at_to.present? | ||
| end | ||
|
|
||
| def created_at_from | ||
| @created_at_from ||= parse_datetime_filter(:created_at_from) | ||
| end | ||
|
|
||
| def created_at_to | ||
| @created_at_to ||= parse_datetime_filter(:created_at_to) | ||
| end | ||
|
|
||
| def expires_at_from | ||
| @expires_at_from ||= parse_datetime_filter(:expires_at_from) | ||
| end | ||
|
|
||
| def expires_at_to | ||
| @expires_at_to ||= parse_datetime_filter(:expires_at_to) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module V1 | ||
| class OrderFormSerializer < ModelSerializer | ||
| def serialize | ||
| { | ||
| lago_id: model.id, | ||
| number: model.number, | ||
| status: model.status, | ||
| void_reason: model.void_reason, | ||
| billing_snapshot: model.billing_snapshot, | ||
| expires_at: model.expires_at&.iso8601, | ||
| signed_at: model.signed_at&.iso8601, | ||
| voided_at: model.voided_at&.iso8601, | ||
| lago_signed_by_user_id: model.signed_by_user_id, | ||
| lago_organization_id: model.organization_id, | ||
| lago_customer_id: model.customer_id, | ||
| lago_quote_id: model.quote_version.quote_id, | ||
| lago_quote_version_id: model.quote_version_id, | ||
| created_at: model.created_at.iso8601, | ||
| updated_at: model.updated_at.iso8601 | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.