<commit_before>module INAB
  module Entities
    class EmbeddedSubtransactionRepresenter < Grape::Roar::Decorator
      include ::Roar::JSON
      property :id
      property :description
      property :amount
      property :category_id
    end

    class TransactionRepresenter < Grape::Roar::Decorator
      include ::Roar::JSON
      property :id
      property :date
      property :payee
      property :description
      property :amount
      property :category_id
      property :account_id
      property :transfer_account_id
      property :type
      collection :subtransactions, extend: EmbeddedSubtransactionRepresenter
    end

    class Transactions < Grape::API
      helpers do
        params :transaction_params do
          requires :date, type: Date, desc: "The date at which the Transaction took place"
          optional :payee, type: String, desc: "The payee of the Transaction"
          optional :description, type: String, desc: "The description of the Transaction"
          requires :amount, type: Integer, desc: "The amount of the Transaction in cents"
          optional :category_id, type: Integer, desc: "The id of the Category"
          requires :account_id, type: Integer, desc: "The id of the Account"
          optional :transfer_account_id, type: Integer, desc: "The id of the Account to which the transfer occured"
          requires :type, type: Symbol, desc: "The type of Transaction"
          optional :subtransactions, type: Array do
            requires :description, type: String, desc: "The description of the Subtransaction"
            requires :amount, type: Integer, desc: "The amount of the Subtransaction in cents"
            requires :category_id, type: Integer, desc: "The id of the Category"
          end
        end
      end

      resource plural_of(Transaction) do
        get_one(Transaction, TransactionRepresenter)
        get_all(Transaction, TransactionRepresenter)
        # TODO the returned transactions does not contain its subtransactions
        delete_one(Transaction, TransactionRepresenter)

        desc "Creates a new Transaction"
        params do
          use :transaction_params
        end
        post do
          creation_keys = declared(params).dup
          subtransactions_provided = creation_keys.delete(:subtransactions)

          Transaction.db.transaction do
            tr = Transaction.create(creation_keys)

            if subtransactions_provided
              subtransactions_provided.each do |subtr|
                Subtransaction.create(subtr.merge({transaction_id: tr.id}))
              end
            end

            present find_instance(Transaction, tr.id), with: TransactionRepresenter
          end
        end

        desc "Updates an existing Transaction"
        params do
          requires :id, type: String, desc: "The id of the Transaction"
          use :transaction_params
        end
        patch ':id' do
          update_keys = declared(params).dup
          update_keys.delete(:id)
          subtransactions_provided = update_keys.delete(:subtransactions)

          entry = find_instance(Transaction, params[:id])

          Transaction.db.transaction do
            entry.update(update_keys)

            # Delete existing subtransactions
            entry.subtransactions.each { |st| st.destroy }

            # Create new subtransactions
            if subtransactions_provided
              subtransactions_provided.each do |subtr|
                Subtransaction.create(subtr.merge({transaction_id: entry.id}))
              end
            end
          end

          present find_instance(Transaction, entry.id), with: TransactionRepresenter
        end
      end
    end
  end
end
<commit_msg>Server: API update, made some subtransaction fields optional<commit_after>module INAB
  module Entities
    class EmbeddedSubtransactionRepresenter < Grape::Roar::Decorator
      include ::Roar::JSON
      property :id
      property :description
      property :amount
      property :category_id
    end

    class TransactionRepresenter < Grape::Roar::Decorator
      include ::Roar::JSON
      property :id
      property :date
      property :payee
      property :description
      property :amount
      property :category_id
      property :account_id
      property :transfer_account_id
      property :type
      collection :subtransactions, extend: EmbeddedSubtransactionRepresenter
    end

    class Transactions < Grape::API
      helpers do
        params :transaction_params do
          requires :date, type: Date, desc: "The date at which the Transaction took place"
          optional :payee, type: String, desc: "The payee of the Transaction"
          optional :description, type: String, desc: "The description of the Transaction"
          requires :amount, type: Integer, desc: "The amount of the Transaction in cents"
          optional :category_id, type: Integer, desc: "The id of the Category"
          requires :account_id, type: Integer, desc: "The id of the Account"
          optional :transfer_account_id, type: Integer, desc: "The id of the Account to which the transfer occured"
          requires :type, type: Symbol, desc: "The type of Transaction"
          optional :subtransactions, type: Array do
            optional :description, type: String, desc: "The description of the Subtransaction"
            requires :amount, type: Integer, desc: "The amount of the Subtransaction in cents"
            optional :category_id, type: Integer, desc: "The id of the Category"
          end
        end
      end

      resource plural_of(Transaction) do
        get_one(Transaction, TransactionRepresenter)
        get_all(Transaction, TransactionRepresenter)
        # TODO the returned transactions does not contain its subtransactions
        delete_one(Transaction, TransactionRepresenter)

        desc "Creates a new Transaction"
        params do
          use :transaction_params
        end
        post do
          creation_keys = declared(params).dup
          subtransactions_provided = creation_keys.delete(:subtransactions)

          Transaction.db.transaction do
            tr = Transaction.create(creation_keys)

            if subtransactions_provided
              subtransactions_provided.each do |subtr|
                Subtransaction.create(subtr.merge({transaction_id: tr.id}))
              end
            end

            present find_instance(Transaction, tr.id), with: TransactionRepresenter
          end
        end

        desc "Updates an existing Transaction"
        params do
          requires :id, type: String, desc: "The id of the Transaction"
          use :transaction_params
        end
        patch ':id' do
          update_keys = declared(params).dup
          update_keys.delete(:id)
          subtransactions_provided = update_keys.delete(:subtransactions)

          entry = find_instance(Transaction, params[:id])

          Transaction.db.transaction do
            entry.update(update_keys)

            # Delete existing subtransactions
            entry.subtransactions.each { |st| st.destroy }

            # Create new subtransactions
            if subtransactions_provided
              subtransactions_provided.each do |subtr|
                Subtransaction.create(subtr.merge({transaction_id: entry.id}))
              end
            end
          end

          present find_instance(Transaction, entry.id), with: TransactionRepresenter
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.06315789473684211, 'fraction_numerical': 0.0, 'mean_word_length': 2.3659192825112108, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2746247', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1798, 'n_words': 607}