diff --git a/pay-api/migrations/versions/2024_01_22_fccdab259e05_.py b/pay-api/migrations/versions/2024_01_22_fccdab259e05_.py new file mode 100644 index 000000000..6d67b9eb2 --- /dev/null +++ b/pay-api/migrations/versions/2024_01_22_fccdab259e05_.py @@ -0,0 +1,27 @@ +"""Adding invoice_number column to non_sufficient_funds table + +Revision ID: fccdab259e05 +Revises: b65365f7852b +Create Date: 2024-01-22 17:13:44.797905 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = 'fccdab259e05' +down_revision = 'b65365f7852b' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('non_sufficient_funds', sa.Column( + 'invoice_number', sa.String(length=50), comment='CFS Invoice number')) + op.create_index(op.f('ix_non_sufficient_funds_invoice_number'), 'non_sufficient_funds', ['invoice_number'], unique=False) + + +def downgrade(): + op.drop_index(op.f('ix_non_sufficient_funds_invoice_number'), table_name='non_sufficient_funds') + op.drop_column('non_sufficient_funds', 'invoice_number') diff --git a/pay-api/src/pay_api/models/non_sufficient_funds.py b/pay-api/src/pay_api/models/non_sufficient_funds.py index d4a71abfc..16bfed190 100644 --- a/pay-api/src/pay_api/models/non_sufficient_funds.py +++ b/pay-api/src/pay_api/models/non_sufficient_funds.py @@ -40,12 +40,14 @@ class NonSufficientFundsModel(BaseModel): # pylint: disable=too-many-instance-a 'id', 'description', 'invoice_id', + 'invoice_number' ] } id = db.Column(db.Integer, primary_key=True, autoincrement=True) description = db.Column(db.String(50), nullable=True) invoice_id = db.Column(db.Integer, ForeignKey('invoices.id'), nullable=False) + invoice_number = db.Column(db.String(50), nullable=False, index=True, comment='CFS Invoice number') @define @@ -53,8 +55,9 @@ class NonSufficientFundsSchema: # pylint: disable=too-few-public-methods """Used to search for NSF records.""" id: int - invoice_id: int description: str + invoice_id: int + invoice_number: str @classmethod def from_row(cls, row: NonSufficientFundsModel): @@ -62,4 +65,5 @@ def from_row(cls, row: NonSufficientFundsModel): https://www.attrs.org/en/stable/init.html """ - return cls(id=row.id, invoice_id=row.invoice_id, description=row.description) + return cls(id=row.id, invoice_id=row.invoice_id, invoice_number=row.invoice_number, + description=row.description) diff --git a/pay-api/src/pay_api/services/non_sufficient_funds.py b/pay-api/src/pay_api/services/non_sufficient_funds.py index 719598fbd..9d3a3981f 100644 --- a/pay-api/src/pay_api/services/non_sufficient_funds.py +++ b/pay-api/src/pay_api/services/non_sufficient_funds.py @@ -53,13 +53,14 @@ def populate(value: NonSufficientFundsModel): return non_sufficient_funds_service @staticmethod - def save_non_sufficient_funds(invoice_id: int, description: str) -> NonSufficientFundsService: + def save_non_sufficient_funds(invoice_id: int, invoice_number: str, description: str) -> NonSufficientFundsService: """Create Non-Sufficient Funds record.""" current_app.logger.debug(' 0: + logger.info('Ignoring duplicate NSF event for account: %s ', payment_account.auth_account_id) return False # Set CFS Account Status. - payment_account: PaymentAccountModel = _get_payment_account(row) cfs_account: CfsAccountModel = CfsAccountModel.find_effective_by_account_id(payment_account.id) is_already_frozen = cfs_account.status == CfsAccountStatus.FREEZE.value logger.info('setting payment account id : %s status as FREEZE', payment_account.id) @@ -734,6 +736,10 @@ def _create_nsf_invoice(cfs_account: CfsAccountModel, inv_number: str, created_by='SYSTEM' ) invoice = invoice.save() + + NonSufficientFundsService.save_non_sufficient_funds(invoice_id=invoice.id, invoice_number=inv_number, + description='NSF') + distribution: DistributionCodeModel = DistributionCodeModel.find_by_active_for_fee_schedule( fee_schedule.fee_schedule_id)