Skip to content

Commit

Permalink
add condition to avoid duplicate expiration calculations (#1519)
Browse files Browse the repository at this point in the history
* add condition to avoid duplicate expiration calculations by in payment processes

* update consent query

* update

* update namex-pay version
  • Loading branch information
eve-git authored Apr 11, 2024
1 parent df2e4ce commit b9a437b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
9 changes: 5 additions & 4 deletions api/namex/resources/payment/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from namex.models import State, User
from namex.resources.name_requests.abstract_nr_resource import AbstractNameRequestResource
from namex.services import EventRecorder
from namex.services.name_request.name_request_state import get_nr_state_actions, display_reapply_action
from namex.services.name_request.name_request_state import get_nr_state_actions, is_reapplication_eligible
from namex.services.name_request.utils import get_active_payment, has_active_payment
from namex.services.payment.exceptions import PaymentServiceError, SBCPaymentError, SBCPaymentException
from namex.services.payment.models import PaymentRequest
Expand Down Expand Up @@ -196,8 +196,9 @@ def handle_payment_response(payment_action, payment_response, payment, nr_id, nr
timedelta(hours=NAME_REQUEST_EXTENSION_PAD_HOURS) < datetime.utcnow():
msg = f'Extend NR for payment.id={payment.id} nr_model.state{nr_model.stateCd}, nr_model.expires:{nr_model.expirationDate}'
current_app.logger.debug(msg)
expiry_days = int(nr_svc.get_expiry_days(nr_model))
nr_model.expirationDate = nr_svc.create_expiry_date(nr_model.expirationDate, expiry_days)
if is_reapplication_eligible(nr_model.expirationDate):
expiry_days = int(nr_svc.get_expiry_days(nr_model))
nr_model.expirationDate = nr_svc.create_expiry_date(nr_model.expirationDate, expiry_days)
payment.payment_completion_date = datetime.utcnow()

nr_model.save_to_db()
Expand Down Expand Up @@ -406,7 +407,7 @@ def post(self, nr_id, payment_action=NameRequestActions.CREATE.value):
if existing_payment:
# if we already have a payment record, we can request existing payment status and return it
# get the payment status from Pay API
if payment_action == PaymentDAO.PaymentActions.REAPPLY.value and display_reapply_action(nr_model):
if payment_action == PaymentDAO.PaymentActions.REAPPLY.value and is_reapplication_eligible(nr_model.expirationDate):
# skip valid cases of REAPPLY, as these potentially can have more than a single instance
pass
else:
Expand Down
1 change: 1 addition & 0 deletions api/namex/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .exceptions import ServicesError
from .messages import MessageServices
from .audit_trail import EventRecorder
from .name_request.name_request_state import is_reapplication_eligible

22 changes: 14 additions & 8 deletions api/namex/services/name_request/name_request_state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from flask_restx.fields import Boolean

from namex.constants import \
Expand Down Expand Up @@ -76,18 +76,24 @@ def display_reapply_action(nr_model=None) -> Boolean:
"""Logic for displaying the renew button."""
try:
if nr_model and nr_model.stateCd in (State.CONDITIONAL, State.APPROVED):
if nr_model.expirationDate and not nr_model.is_expired and not nr_model.has_consumed_name:
todays_date = datetime.utcnow().date()
expiry_date = nr_model.expirationDate.date()

delta = expiry_date - todays_date
if delta.days <= 14:
return True
if nr_model.is_expired or nr_model.has_consumed_name:
return False
return is_reapplication_eligible(nr_model.expirationDate)
return False
except Exception as err:
raise NameRequestActionError(err)


def is_reapplication_eligible(expiration_date) -> Boolean:
if expiration_date:
todays_date = datetime.now(timezone.utc).date()
expiry_date = expiration_date.date()

delta = expiry_date - todays_date
return delta.days <= 14
return False


def display_resubmit_action(nr_model=None) -> Boolean:
"""Logic for displaying the resubmit button."""
try:
Expand Down
2 changes: 1 addition & 1 deletion services/namex-pay/src/namex_pay/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
Development release segment: .devN
"""

__version__ = '0.1.4' # pylint: disable=invalid-name
__version__ = '0.1.5' # pylint: disable=invalid-name
6 changes: 4 additions & 2 deletions services/namex-pay/src/namex_pay/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from flask import Flask
from namex import nro
from namex.models import db, Event, Payment, Request as RequestDAO, State, User # noqa:I001; import orders
from namex.services import EventRecorder, queue # noqa:I005;
from namex.services import EventRecorder, queue, is_reapplication_eligible # noqa:I005;
from queue_common.messages import create_cloud_event_msg # noqa:I005
from queue_common.service import QueueServiceManager
from queue_common.service_utils import QueueException, logger
Expand Down Expand Up @@ -131,7 +131,9 @@ async def update_payment_record(payment: Payment) -> Optional[Payment]:
logger.debug(msg)
capture_message(msg)
raise QueueException(msg)
nr.expirationDate = nr.expirationDate + timedelta(days=NAME_REQUEST_LIFESPAN_DAYS)
if is_reapplication_eligible(nr.expriationDate):
# to avoid duplicate expiration date calculated
nr.expirationDate = nr.expirationDate + timedelta(days=NAME_REQUEST_LIFESPAN_DAYS)
payment.payment_completion_date = datetime.utcnow()
payment.payment_status_code = State.COMPLETED

Expand Down

0 comments on commit b9a437b

Please sign in to comment.