-
Notifications
You must be signed in to change notification settings - Fork 34
refactor(imports): back PDF imports with statements #1786
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
JSONbored
wants to merge
8
commits into
we-promise:main
Choose a base branch
from
JSONbored:codex/refactor-pdf-import-account-statements
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
8 commits
Select commit
Hold shift + click to select a range
fdba387
refactor(imports): back PDF imports with statements
JSONbored f2a9c40
fix(imports): address statement reuse review
JSONbored b923034
fix(statements): tighten statement review access
JSONbored 0068c15
fix(imports): harden statement-backed PDF imports
JSONbored b436ac0
fix(imports): recover PDF processing claims
JSONbored 2895b6e
fix(imports): avoid stale PDF claim resets
JSONbored 9955e77
fix(statements): address vault review follow-ups
JSONbored 5061811
Merge branch 'main' into codex/refactor-pdf-import-account-statements
JSONbored 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
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 |
|---|---|---|
|
|
@@ -2,9 +2,9 @@ class ImportsController < ApplicationController | |
| include SettingsHelper | ||
|
|
||
| before_action :set_import, only: %i[show update publish destroy revert apply_template] | ||
| before_action :require_statement_import_permission!, only: %i[update publish destroy revert apply_template] | ||
|
|
||
| def update | ||
| # Handle both pdf_import[account_id] and import[account_id] param formats | ||
| account_id = params.dig(:pdf_import, :account_id) || params.dig(:import, :account_id) | ||
|
|
||
| if account_id.present? | ||
|
|
@@ -13,7 +13,9 @@ def update | |
| redirect_back_or_to import_path(@import), alert: t("imports.update.invalid_account", default: "Account not found.") | ||
| return | ||
| end | ||
| @import.update!(account: account) | ||
| return if @import.account_statement.present? && !require_account_permission!(account) | ||
|
|
||
| @import.assign_account!(account) | ||
| end | ||
|
|
||
| redirect_to import_path(@import), notice: t("imports.update.account_saved", default: "Account saved.") | ||
|
|
@@ -54,12 +56,7 @@ def create | |
| return | ||
| end | ||
|
|
||
| # Handle PDF file uploads - process with AI | ||
| if file.present? && Import::ALLOWED_PDF_MIME_TYPES.include?(file.content_type) | ||
| unless valid_pdf_file?(file) | ||
| redirect_to new_import_path, alert: t("imports.create.invalid_pdf") | ||
| return | ||
| end | ||
| create_pdf_import(file) | ||
| return | ||
| end | ||
|
|
@@ -87,8 +84,6 @@ def create | |
| return | ||
| end | ||
|
|
||
| # Stream reading is not fully applicable here as we store the raw string in the DB, | ||
| # but we have validated size beforehand to prevent memory exhaustion from massive files. | ||
| import.update!(raw_file_str: file.read) | ||
|
|
||
| redirect_to import_configuration_path(import), notice: t("imports.create.csv_uploaded") | ||
|
|
@@ -132,24 +127,41 @@ def destroy | |
|
|
||
| private | ||
| def set_import | ||
| @import = Current.family.imports.includes(:account).find(params[:id]) | ||
| @import = Current.family.imports.includes(:account, :account_statement).find(params[:id]) | ||
| raise ActiveRecord::RecordNotFound if @import.account_statement.present? && [email protected]_statement.viewable_by?(Current.user) | ||
| end | ||
|
|
||
| def import_params | ||
| params.require(:import).permit(:import_file) | ||
| end | ||
|
|
||
| def require_statement_import_permission! | ||
| return unless @import.account_statement.present? | ||
| return if @import.account_statement.manageable_by?(Current.user) | ||
|
|
||
| redirect_target = @import.account || @import.account_statement | ||
| redirect_back_or_to redirect_target, alert: t("accounts.not_authorized") | ||
| end | ||
|
|
||
| def create_pdf_import(file) | ||
| unless AccountStatement.statement_manager?(Current.user) | ||
| redirect_to new_import_path, alert: t("accounts.not_authorized") | ||
| return | ||
| end | ||
|
|
||
| if file.size > Import::MAX_PDF_SIZE | ||
| redirect_to new_import_path, alert: t("imports.create.pdf_too_large", max_size: Import::MAX_PDF_SIZE / 1.megabyte) | ||
| return | ||
| end | ||
|
|
||
| pdf_import = Current.family.imports.create!(type: "PdfImport") | ||
| pdf_import.pdf_file.attach(file) | ||
| pdf_import = PdfImport.create_from_upload!(family: Current.family, file: file, user: Current.user) | ||
| pdf_import.process_with_ai_later | ||
|
|
||
| redirect_to import_path(pdf_import), notice: t("imports.create.pdf_processing") | ||
| rescue AccountStatement::DuplicateUploadError | ||
| redirect_to new_import_path, alert: t("imports.create.duplicate_pdf_unavailable") | ||
| rescue AccountStatement::InvalidUploadError | ||
| redirect_to new_import_path, alert: t("imports.create.invalid_pdf") | ||
| end | ||
|
|
||
| def create_document_import(file) | ||
|
|
@@ -174,11 +186,6 @@ def create_document_import(file) | |
| end | ||
|
|
||
| if ext == ".pdf" | ||
| unless valid_pdf_file?(file) | ||
| redirect_to new_import_path, alert: t("imports.create.invalid_pdf") | ||
| return | ||
| end | ||
|
|
||
| create_pdf_import(file) | ||
| return | ||
| end | ||
|
|
@@ -239,10 +246,4 @@ def create_sure_import(file) | |
|
|
||
| redirect_to import_path(import), notice: t("imports.create.ndjson_uploaded") | ||
| end | ||
|
|
||
| def valid_pdf_file?(file) | ||
| header = file.read(5) | ||
| file.rewind | ||
| header&.start_with?("%PDF-") | ||
| 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.