def __init__(self): self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() self.__message_storage = MessageStorageImplementation() self.__customer_storage = CustomerStorageImplementation() self.__products_storage = ProductStorageImplementation()
def __init__(self, flow_id: str): if not isinstance(flow_id, str): raise ArgumentTypeException(self.__init__, 'flow_id', flow_id) elif not flow_id.strip(): raise ArgumentCannotBeEmptyException(self.__init__, 'flow_id') self.__flow_id = flow_id self.__logger = Logger()
def returns_send_item(): returns_storage = ReturnRequestStorageImplementation() sqs_sender = SqsSenderImplementation() logger = Logger() request_data = blueprint.current_request.json_body request_number = str(request_data.get('request_number') or '').strip() or None if not request_number: raise BadRequestError('"request_number" is required') return_request = returns_storage.load( ReturnRequest.Number(request_number)) if not return_request: raise NotFoundError( 'Return Request #{} does not exist!'.format(request_number)) if return_request.customer_id.value != __get_user().id: raise ForbiddenError('It is not your Return Request!') try: return_request.make_package_sent() except ApplicationLogicException as e: raise UnprocessableEntityError(str(e)) def __log_flow(text: str) -> None: logger.log_simple('Return Request #{} - Sending : {}'.format( return_request.number.value, text)) __log_flow('Start') # change status __log_flow('Saving - Start') try: __log_flow('Return Request Saving...') returns_storage.save(return_request) __log_flow('Return Request Saving - Done!') except BaseException as e: __log_flow( 'Return Request Saving - Not done because of Error: {}!'. format(str(e))) raise e __log_flow('Saving - End') # send sqs __log_flow('SQS Sending - Start') try: __log_flow('Return Request SQS Sending...') sqs_sender.send(ReturnRequestChangeSqsSenderEvent(return_request)) __log_flow('Return Request SQS Sending - Done!') except BaseException as e: logger.log_exception(e) __log_flow( 'Return Request SQS Sending - Not done because of Error: {}!'. format(str(e))) __log_flow('SQS Sending - End') __log_flow('End')
class OrderRefundSqsHandler(SqsHandlerInterface): def __init__(self) -> None: self.__messages_storage = MessageStorageImplementation() self.__order_storage = OrderStorageImplementation() self.__customer_storage = CustomerStorageImplementation() self.__product_storage = ProductStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() def handle(self, sqs_message: SqsMessage) -> None: def __log_flow(text: str) -> None: self.__logger.log_simple('{} : SQS Message #{} : {}'.format( self.__class__.__qualname__, sqs_message.id, text )) __log_flow('Start - {}'.format(sqs_message.message_data)) order_number = Order.Number(sqs_message.message_data['order_number']) simple_sku = SimpleSku(sqs_message.message_data['simple_sku']) qty = Qty(sqs_message.message_data['qty']) __log_flow('Order Updating...') order = self.__order_storage.load(order_number) order.refund(simple_sku, qty) __log_flow('Order Updated!') __log_flow('Order Saving...') self.__order_storage.save(order) __log_flow('Order Saved!') __log_flow('Order SQS Sending...') self.__sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order SQS Sent!') # add message (silently) try: __log_flow('Notification popup: Adding...') customer = self.__customer_storage.get_by_id(order.customer_id) product = self.__product_storage.load(simple_sku) message = Message( str(uuid.uuid4()), customer.email.value, 'Refund for Order #{}'.format(order.number.value), '"{}" has been Refunded in Qty {} for Order #{}'.format( product.name.value, qty.value, order.number.value ), ) self.__messages_storage.save(message) __log_flow('Notification popup: Added!') except BaseException as e: self.__logger.log_exception(e) __log_flow('Notification popup: Not Added because of Error : {}'.format(str(e))) __log_flow('End')
def getaddress(address_hash): logger = Logger() user = __get_current_user() try: ret = user.profile.get_address(address_hash) return {'status': True, 'data': ret} except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
def __init__(self): # Need to be imported here, because orders storage depends from payment method class in this file. from chalicelib.libs.purchase.order.storage import OrderStorageImplementation from chalicelib.libs.purchase.product.storage import ProductStorageImplementation self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() self.__message_storage = MessageStorageImplementation() self.__customer_storage = CustomerStorageImplementation() self.__products_storage = ProductStorageImplementation()
def credit_cards_list(): cards_storage = CreditCardsStorageImplementation() logger = Logger() try: user = __get_user() cards = cards_storage.get_all_by_customer(user.id) return [__get_card_response(card) for card in cards] except BaseException as e: logger.log_exception(e) return http_response_exception_or_throw(e)
def getInformation(): logger = Logger() try: email = str(__get_request().json_body.get('email') or '').strip() if not email: raise ValueError('email is empty') ret = InformationService().get(email).get_item() return {'status': True, 'data': ret} except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
def deleteaddress(address_hash): logger = Logger() user = __get_current_user() try: user.profile.delete_address(address_hash) event = InformationsSqsSenderEvent(user.profile.informations) SqsSenderImplementation().send(event) ret = user.profile.informations['addresses'] return {'status': True, 'data': ret} except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
def add_information(): logger = Logger() user = __get_current_user() request = __get_request() params = request.json_body try: user.profile.add_information(params) event = InformationsSqsSenderEvent(user.profile.informations) SqsSenderImplementation().send(event) return {'status': True} except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
class WebhooksFlowLog(object): def __init__(self, flow_id: str): if not isinstance(flow_id, str): raise ArgumentTypeException(self.__init__, 'flow_id', flow_id) elif not flow_id.strip(): raise ArgumentCannotBeEmptyException(self.__init__, 'flow_id') self.__flow_id = flow_id self.__logger = Logger() def write(self, message: str) -> None: self.__logger.log_simple('Peach Payment Webhooks Log #{}: {}'.format( self.__flow_id, message))
def __init__(self): self.__tiers_storage = CustomerTierStorageImplementation() self.__messages = MessageStorageImplementation() self.__logger = Logger() """""" # @todo : refactoring from chalicelib.libs.purchase.core import CustomerInterface from chalicelib.libs.purchase.customer.storage import CustomerStorageImplementation see = CustomerInterface.tier see = CustomerStorageImplementation.save """""" self.__elastic = Elastic( settings.AWS_ELASTICSEARCH_CUSTOMER_TIERS_CUSTOMER_TIERS, settings.AWS_ELASTICSEARCH_CUSTOMER_TIERS_CUSTOMER_TIERS)
def validate_user(self, email, password, check_dynamo: bool = False): created = False found = False msg = None if not email or not password: return found, created, 'email and password is required.' else: try: found_user = self.get(email, password, check_dynamo=check_dynamo) if found_user: found = True # TODO: Should create a new user in cognito created, msg = self.create_cognito_user(email, password) # request customer info try: # @todo : this is a crutch # This should be an api request, but it is impossible for now, # so we use sqs-communication. from chalicelib.libs.core.sqs_sender import SqsSenderImplementation from chalicelib.libs.purchase.customer.sqs import CrutchCustomerInfoRequestSqsSenderEvent event = CrutchCustomerInfoRequestSqsSenderEvent(user.email) SqsSenderImplementation().send(event) except BaseException as e: Logger().log_exception(e) else: msg = 'Incorrect username or password.' except Exception as e: msg = str(e) return found, created, msg
def addresses(): logger = Logger() user = __get_current_user() request = __get_request() if request.method == 'GET': return user.profile.informations['addresses'] elif request.method == 'POST': params = request.json_body try: ret = user.profile.add_addresses(params) event = InformationsSqsSenderEvent(user.profile.informations) SqsSenderImplementation().send(event) return {'status': True, 'data': ret["Attributes"]["addresses"]} except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
def get(self, email: str, password: str, check_dynamo: bool = False) -> bool: if check_dynamo: items = self.table.query( KeyConditionExpression="pk = :pk and sk = :sk", ExpressionAttributeValues={":pk": self.PARTITION_KEY, ":sk": email} )['Items'] if len(items) > 0: item = items[0] current_pwd_hash = item['hash'] salt = "" if current_pwd_hash.find(":") != -1: salt = current_pwd_hash[current_pwd_hash.find(":") + 1:] current_pwd_hash = current_pwd_hash[:current_pwd_hash.find(":")] hs = hashlib.md5((salt + password).encode()) coded_hash = hs.hexdigest() if coded_hash == current_pwd_hash: return True else: return False else: return False else: # TODO: This is the snippet to invoke Magento auth API. try: url = 'https://portal.runway.co.za/api/remoteAuth/customer' body = {'email': email, 'password': password} headers = {'Identification': 'RunwaySale::ReadAPI'} r = post(url, data=body, headers=headers) if r.status_code == 200: result = json.loads(r.text) if not result['status']: Logger().log_simple(', '.join([email, password, result['error']])) return result['status'] else: return False except BaseException as e: Logger().log_exception(e) return False
def regular_eft_payment_proof_send(): order_storage = OrderStorageImplementation() logger = Logger() file_storage = FileStorageImplementation() sqs_sender = SqsSenderImplementation() try: user_id = __get_user().id request_data = blueprint.current_request.json_body order_number_value = str(request_data.get('order_number', '')).strip() file_id = str(request_data.get('file_id', '')).strip() if not order_number_value: raise HttpIncorrectInputDataException('order_number is required!') elif not file_id: raise HttpIncorrectInputDataException('file_id is required!') order_number = Order.Number(order_number_value) order = order_storage.load(order_number) if not order: raise HttpNotFoundException('Order "{}" does not exist!'.format(order_number)) elif order.customer_id.value != user_id: raise HttpAccessDenyException('Access Denied!') elif not isinstance(order.payment_method, RegularEftOrderPaymentMethod): raise ApplicationLogicException('Order "{}" does not have Regular EFT payment!') file = file_storage.get(file_id) if not file: raise HttpNotFoundException('File does not exist!') def __log_flow(text: str) -> None: logger.log_simple('Regular EFT : Sending POP : {} : {}'.format(order.number.value, text)) __log_flow('Order Updating...') order.status = Order.Status(Order.Status.PAYMENT_SENT) order_storage.save(order) __log_flow('Order Updated!') try: __log_flow('Order SQS Sending...') sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order SQS Sent!') except BaseException as e: logger.log_exception(e) __log_flow('Order SQS NOT Sent because of Error: {}!'.format(str(e))) try: __log_flow('Regular EFT POP SQS Sending...') sqs_sender.send(RegularEftProofUploadedSqsSenderEvent(order, file)) __log_flow('Regular EFT POP SQS Sent!') except BaseException as e: logger.log_exception(e) __log_flow('Regular EFT POP SQS NOT Sent because of Error: {}!'.format(str(e))) return { 'Code': 'Success', 'Message': 'Success', } except BaseException as e: return http_response_exception_or_throw(e)
def informations(): logger = Logger() user = __get_current_user() request = __get_request() if request.method == 'GET': user_info = user.profile.informations if user_info['email'] == None or user_info[ 'email'] == '' or user_info['email'] == 'BLANK': user_info['email'] = user.email if user_info['first_name'] == None or user_info[ 'first_name'] == '' and user.first_name is not None: user_info['first_name'] = user.first_name if user_info['last_name'] == None or user_info[ 'last_name'] == '' and user.last_name is not None: user_info['last_name'] = user.last_name user.profile.informations = user_info return user_info elif request.method == 'POST': params = request.json_body try: form = AccountInformationForm() form.load(params) validation_errors = form.validate() if validation_errors: return { 'status': False, 'msg': tuple(validation_errors.values())[0][0] } user.profile.informations = params event = InformationsSqsSenderEvent(user.profile.informations) SqsSenderImplementation().send(event) return { 'status': True, 'msg': 'Information has been updated successfully' } except Exception as e: logger.log_exception(e) return {'status': False, 'msg': str(e)}
def possible_favorite_sizes(): user = __get_current_user() logger = Logger() if user.is_anyonimous: return __response_error('Authentication is required!') try: response = {} for category in user.profile.categories: gender_key = category.gender_name type_key = category.product_type_name response[gender_key] = response.get(gender_key) or {} response[gender_key][type_key] = response[gender_key].get( type_key) or [] response[gender_key][type_key].extend( [size.name for size in category.sizes]) response[gender_key][type_key] = list( set(response[gender_key][type_key])) return __response_success(response) except BaseException as e: logger.log_exception(e) return __response_error('Internal Server Error')
def __cancellation_create_submit_before_payment(order: Order, items): orders_storage = OrderStorageImplementation() products_storage = ProductStorageImplementation() sqs_sender = SqsSenderImplementation() logger = Logger() def __log_flow(text: str): logger.log_simple( 'Cancellation for Order #{} Before Payment: {}'.format( order.number.value, text)) __log_flow('Start - {}'.format([{ 'simple_sku': item['simple_sku'].value, 'qty': item['qty'].value, } for item in items])) # updates products_to_update = {} for item in items: products_to_update[ item['simple_sku'].value] = products_storage.load( item['simple_sku']) products_to_update[item['simple_sku'].value].restore_qty( item['qty']) order.cancel_before_payment(item['simple_sku'], item['qty']) order_change_event = OrderChangeSqsSenderEvent(order) # save order __log_flow('Order Saving...') orders_storage.save(order) __log_flow('Order Saved!') # saving products __log_flow('Products Saving... {}'.format( tuple(products_to_update.keys()))) for product in tuple(products_to_update.values()): __log_flow('Product {} Saving...'.format(product.simple_sku.value)) products_storage.update(product) __log_flow('Product {} Saved!'.format(product.simple_sku.value)) __log_flow('Products Saved!') # send order __log_flow('Order SQS Sending...') sqs_sender.send(order_change_event) __log_flow('Order SQS Sent!') __log_flow('End')
def __init__(self): from chalicelib.libs.purchase.product.storage import ProductStorageImplementation from chalicelib.libs.purchase.checkout.storage import CheckoutStorageImplementation from chalicelib.libs.purchase.customer.storage import CustomerStorageImplementation from chalicelib.libs.purchase.order.dtd_calculator import DtdCalculatorImplementation from chalicelib.libs.purchase.order.storage import OrderStorageImplementation from chalicelib.libs.core.sqs_sender import SqsSenderImplementation from chalicelib.libs.core.mailer import MailerImplementation from chalicelib.libs.core.logger import Logger super().__init__(ProductStorageImplementation(), CheckoutStorageImplementation(), CustomerStorageImplementation(), DtdCalculatorImplementation(), OrderStorageImplementation(), SqsSenderImplementation(), MailerImplementation(), Logger())
def on_signup(): logger = Logger() user = __get_current_user() # Attention! Please, do all actions silently (in try-except block) try: # Trigger to calculate product scores per the just registerred customer here. pass except Exception as e: pass # subscription try: __on_signup__subscription(user) except BaseException as e: logger.log_exception(e) # request customer info try: __on_signup_request_customer_info(user) except BaseException as e: logger.log_exception(e) # set information email and identification number try: email_value = str(__get_request().json_body.get('email') or '').strip() identification_number_value = str( __get_request().json_body.get('identification_number') or '').strip() # this is not usual usage, but it's simple to check identification number and email here in this way form = AccountInformationForm() form.load({ AccountInformationForm.ATTRIBUTE_EMAIL: email_value, AccountInformationForm.ATTRIBUTE_IDENTIFICATION_NUMBER: identification_number_value }) form_errors = form.validate() email_errors = form_errors.get( AccountInformationForm.ATTRIBUTE_EMAIL, []) identification_number_errors = form_errors.get( AccountInformationForm.ATTRIBUTE_IDENTIFICATION_NUMBER, []) if email_errors or identification_number_errors: return { 'status': False, 'message': (email_errors + identification_number_errors)[0] } # update only identification number and email information = user.profile.informations information['email'] = email_value information['identification_number'] = identification_number_value user.profile.informations = information event = InformationsSqsSenderEvent(user.profile.informations) SqsSenderImplementation().send(event) return {'status': True} except BaseException as e: logger.log_exception(e) return {'status': False, 'message': 'Internal Server Error'}
class OrderPaymentOhHoldHandler(SqsHandlerInterface): def __init__(self): self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() self.__message_storage = MessageStorageImplementation() self.__customer_storage = CustomerStorageImplementation() self.__products_storage = ProductStorageImplementation() def handle(self, sqs_message: SqsMessage) -> None: def __log_flow(text: str) -> None: self.__logger.log_simple('{} : SQS Message #{} : {}'.format( self.__class__.__qualname__, sqs_message.id, text )) __log_flow('Start : {}'.format(sqs_message.message_data)) if sqs_message.message_type != 'fixel_order_on_hold_by_portal': raise ValueError('{} does not know how to handle {} sqs message! Message data: {}'.format( self.__class__.__qualname__, sqs_message.message_type, sqs_message.message_data )) order_number_value = sqs_message.message_data.get('order_number') on_hold_status = sqs_message.message_data.get('status') order_number = Order.Number(order_number_value) order = self.__order_storage.load(order_number) if on_hold_status == Order.Status.CLOSED: self.__close_order_on_hold(order, __log_flow) else: self.__on_hold_not_closed_status(order, on_hold_status, __log_flow) self.__send_order_change_to_portal(order, __log_flow) self.__notify_about_order_status_change_silently(order, __log_flow) __log_flow('End') def __on_hold_not_closed_status(self, order: Order, on_hold_status: str, __log_flow) -> None: __log_flow('Order Updating...') order.status = Order.Status(on_hold_status) __log_flow('Order Updated!') __log_flow('Order Saving...') self.__order_storage.save(order) __log_flow('Order Saved!') def __close_order_on_hold(self, order: Order, __log_flow) -> None: __log_flow('Updating...') # close order __log_flow('Order Updating...') order.status = Order.Status(Order.Status.CLOSED) __log_flow('Order Updated!') # restore products qty __log_flow('Product Qty Updating - Start') products_to_save = [] for order_item in order.items: if order_item.qty_processable.value == 0: __log_flow('Product Qty Updating: {} skipped because of 0 qty'.format(order_item.simple_sku.value)) continue __log_flow('Product Qty Updating {} / {} ...'.format( order_item.simple_sku.value, order_item.qty_processable.value )) product = self.__products_storage.load(order_item.simple_sku) product.restore_qty(order_item.qty_processable) products_to_save.append(product) __log_flow('Product Qty Updated {} / {}!'.format( order_item.simple_sku.value, order_item.qty_processable.value )) __log_flow('Product Qty Updating - End') __log_flow('Updated!') __log_flow('Saving...') __log_flow('Order Saving...') self.__order_storage.save(order) __log_flow('Order Saved!') __log_flow('Products Saving...') for product in products_to_save: __log_flow('Product {} Saving...'.format(product.simple_sku.value)) self.__products_storage.update(product) __log_flow('Product {} Saved!'.format(product.simple_sku.value)) __log_flow('Products Saved!') __log_flow('Saved!') def __send_order_change_to_portal(self, order: Order, __log_flow) -> None: __log_flow('Order SQS: Sending...') self.__sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order SQS: Sent!') def __notify_about_order_status_change_silently(self, order: Order, __log_flow) -> None: try: __log_flow('Notification popup: Adding...') customer = self.__customer_storage.get_by_id(order.customer_id) self.__message_storage.save(Message( str(uuid.uuid4()), customer.email.value, 'Order #{} status is changed!', 'Order #{} has been turned to "{}" status!'.format(order.number.value, order.status.label) )) __log_flow('Notification popup: Added!') except BaseException as e: self.__logger.log_exception(e) __log_flow('Notification popup: Not Added because of Error : {}'.format(str(e)))
class OrderChangeSqsHandler(SqsHandlerInterface): def __init__(self): self.__messages_storage = MessageStorageImplementation() self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() def handle(self, sqs_message: SqsMessage) -> None: def __log_flow(text: str) -> None: self.__logger.log_simple('{} : SQS Message #{} : {}'.format( self.__class__.__qualname__, sqs_message.id, text )) __log_flow('Start - {}'.format(sqs_message.message_data)) data = { 'order_number': sqs_message.message_data.get('order_number', '') or '', 'order_status_mpc': sqs_message.message_data.get('order_status_mpc', '') or '', 'popup_message': { 'customer_email': sqs_message.message_data.get('popup_message').get('customer_email'), 'message_title': sqs_message.message_data.get('popup_message').get('message_title'), 'message_text': sqs_message.message_data.get('popup_message').get('message_text'), } if sqs_message.message_data.get('popup_message', None) or None else None, } __log_flow('Order: Updating...') order_number = Order.Number(data.get('order_number')) order = self.__order_storage.load(order_number) if not order: raise ValueError('Order "{}" does not exist in the MPC!') mpc_order_status = str(data.get('order_status_mpc')) order.status = Order.Status(mpc_order_status) __log_flow('Order: Updated!') __log_flow('Order: Saving...') self.__order_storage.save(order) __log_flow('Order: Saved!') # Attention! # We need to send-back order changes because of compatibility reason. __log_flow('Order: SQS Sending-Back...') self.__sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order: SQS Sent-Back!') # add message, if is needed (silently) try: message_data = data.get('popup_message') or None if message_data: __log_flow('Notification popup: Adding...') message = Message( str(uuid.uuid4()), message_data.get('customer_id'), message_data.get('message_title'), message_data.get('message_text'), ) self.__messages_storage.save(message) __log_flow('Notification popup: Added!') except BaseException as e: self.__logger.log_exception(e) __log_flow('Notification popup: Not Added because of Error : {}'.format(str(e))) __log_flow('End')
def customer_credits_checkout(): checkout_storage = CheckoutStorageImplementation() order_storage = OrderStorageImplementation() order_app_service = OrderAppService() cart_service = CartAppService() checkout_service = CheckoutAppService() sqs_sender = SqsSenderImplementation() logger = Logger() try: user = __get_user() # @todo : refactoring checkout = checkout_storage.load(Id(user.id)) if not checkout: raise ApplicationLogicException('Checkout does not exist!') elif checkout.total_due.value != 0: raise ApplicationLogicException('Unable to checkout not 0 amount with Customer Credits!') order = order_app_service.get_waiting_for_payment_by_checkout_or_checkout_new(user.id) def __log_flow(text: str) -> None: logger.log_simple('Customer Credits Payment Log for Order #{} : {}'.format( order.number.value, text )) __log_flow('Start') try: __log_flow('Credits Spending...') # Attention! # Currently we use f-bucks only! Other credits are not available for now! # @todo : other credit types # @todo : copy-paste code # @todo : when reservation of credits amount will be done, perhaps, use sqs to spend credits """""" from chalicelib.libs.purchase.core import Checkout see = Checkout.__init__ """""" # @TODO : refactoring : raw data usage import uuid import datetime from chalicelib.settings import settings from chalicelib.libs.core.elastic import Elastic fbucks_customer_amount_elastic = Elastic( settings.AWS_ELASTICSEARCH_FBUCKS_CUSTOMER_AMOUNT, settings.AWS_ELASTICSEARCH_FBUCKS_CUSTOMER_AMOUNT, ) fbucks_customer_amount_changes_elastic = Elastic( settings.AWS_ELASTICSEARCH_FBUCKS_CUSTOMER_AMOUNT_CHANGES, settings.AWS_ELASTICSEARCH_FBUCKS_CUSTOMER_AMOUNT_CHANGES, ) fbucks_customer_amount_elastic.update_data(order.customer_id.value, { 'script': 'ctx._source.amount -= ' + str(order.credit_spent_amount.value) }) fbucks_customer_amount_changes_elastic.create(str(uuid.uuid4()) + str(order.customer_id.value), { "customer_id": order.customer_id.value, "amount": -order.credit_spent_amount.value, "changed_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "order_number": order.number.value, }) __log_flow('Credits Spent!') __log_flow('Order Updating...') order.payment_method = CustomerCreditsOrderPaymentMethod() order.status = Order.Status(Order.Status.PAYMENT_SENT) order.status = Order.Status(Order.Status.PAYMENT_RECEIVED) order_storage.save(order) __log_flow('Order Updated!') except BaseException as e: __log_flow('Not done because of Error : {}'.format(str(e))) raise e # send order update to sqs try: __log_flow('Order Change SQS Sending...') sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order Change SQS Sent!') except BaseException as e: __log_flow('Order Change SQS NOT Sent because of Error: {}!'.format(str(e))) logger.log_exception(e) # flush cart try: __log_flow('Cart Flushing...') cart_service.clear_cart(user.session_id) __log_flow('Cart Flushed!') except BaseException as e: __log_flow('Cart NOT Flushed because of Error: {}'.format(str(e))) logger.log_exception(e) # flush checkout try: __log_flow('Checkout Flushing...') checkout_service.remove(user.id) __log_flow('Checkout Flushed!') except BaseException as e: __log_flow('Checkout NOT Flushed because of Error: {}'.format(str(e))) logger.log_exception(e) result = { 'order_number': order.number.value } __log_flow('End') return result except BaseException as e: logger.log_exception(e) return http_response_exception_or_throw(e)
def __init__(self): self.__sqs_client = boto3.client('sqs') self.__logger = Logger()
class _SqsSenderSqs(SqsSenderInterface): def __init__(self): self.__sqs_client = boto3.client('sqs') self.__logger = Logger() def send_batch(self, events: List[SqsSenderEventInterface]) -> None: # Group by event_type grouped = dict() for event in events: if not grouped.get(event.event_type): grouped[event.event_type] = list() grouped[event.event_type].append(event.event_data) CHUNK_SIZE = settings.CALCULATE_SCORE_CHUNK_SIZE for event_type, event_data in grouped.items(): events_map = settings.SQS_SENDER_CONFIG.get('params').get('events') queue_data = events_map.get(event_type) or None if not queue_data: raise ArgumentValueException( '{} does not know, how to send event!'.format( self.send_batch.__qualname__)) queue_url = queue_data.get('queue_url') # queue = boto3.resource('sqs').get_queue_by_name(QueueName=queue_url.split('/')[-1]) for idx in range(0, len(event_data), CHUNK_SIZE): batch = event_data[idx:idx + CHUNK_SIZE] def __log_flow(msg: str, event_type: str = event_type, data: list = batch): self.__logger.log_simple( '{} : Sending SQS "{}" : {} : {}'.format( self.__class__.__qualname__, event_type, data, msg)) __log_flow('Start') __log_flow('SQS Point: {} -> {}'.format(event_type, queue_url)) send_method = self.__send_fifo if str( queue_url)[-5:] == '.fifo' else self.__send_standard send_method(queue_url, event_type, batch, __log_flow) __log_flow('End') def send(self, event: SqsSenderEventInterface) -> None: def __log_flow(text: str) -> None: self.__logger.log_simple('{} : Sending SQS "{}" : {} : {}'.format( self.__class__.__qualname__, event.event_type, event.event_data, text)) __log_flow('Start') events_map = settings.SQS_SENDER_CONFIG.get('params').get('events') queue_data = events_map.get(event.event_type) or None if not queue_data: raise ArgumentValueException( '{} does not know, how to send "{}" event!'.format( self.send.__qualname__, event.event_type)) queue_url = queue_data.get('queue_url') object_type = queue_data.get('object_type') send_method = self.__send_fifo if str( queue_url)[-5:] == '.fifo' else self.__send_standard __log_flow('SQS Point: {} -> {}'.format(object_type, queue_url)) send_method(queue_url, object_type, event.event_data, __log_flow) __log_flow('End') def __send_standard(self, queue_url: str, object_type: str, data: dict, __log_flow): __log_flow('Standard - Start') response = self.__sqs_client.send_message(QueueUrl=queue_url, MessageBody=json.dumps(data), DelaySeconds=45, MessageAttributes={ 'object_type': { 'StringValue': object_type, 'DataType': 'String', } }) __log_flow('Standard - End: {}'.format(response)) def __send_fifo(self, queue_url: str, object_type: str, data: dict, __log_flow): __log_flow('Fifo - Start') response = self.__sqs_client.send_message( QueueUrl=queue_url, MessageGroupId=object_type, MessageDeduplicationId=hashlib.md5( (object_type + json.dumps(data) + datetime.datetime.now().strftime('%Y%m%d%H%M%S') ).encode('utf-8')).hexdigest(), MessageBody=json.dumps(data), MessageAttributes={ 'object_type': { 'StringValue': object_type, 'DataType': 'String', } }) __log_flow('Fifo - End: {}'.format(response))
def __init__(self): self.__messages_storage = MessageStorageImplementation() self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger()
def returns_reject(): returns_storage = ReturnRequestStorageImplementation() orders_storage = OrderStorageImplementation() sqs_sender = SqsSenderImplementation() logger = Logger() request_data = blueprint.current_request.json_body request_number = str(request_data.get('request_number') or '').strip() or None if not request_number: raise BadRequestError('"request_number" is required') order_number_value = str(request_data.get('order_number') or '').strip() or None if not order_number_value: raise BadRequestError('"order_number" is required') simple_sku_value = str(request_data.get('simple_sku') or '').strip() or None if not simple_sku_value: raise BadRequestError('"simple_sku" is required') return_request = returns_storage.load( ReturnRequest.Number(request_number)) if not return_request: raise NotFoundError( 'Return Request #{} does not exist!'.format(request_number)) if return_request.customer_id.value != __get_user().id: raise ForbiddenError('It is not your Return Request!') order_number = OrderNumber(order_number_value) simple_sku = SimpleSku(simple_sku_value) order = orders_storage.load(order_number) if (not order or not len([ item for item in return_request.items if item.order_number == order_number ]) or not len( [item for item in order.items if item.simple_sku == simple_sku])): raise NotFoundError( 'Product "{}" is not requested in Return Request #{} for Order #{}!' .format( simple_sku.value, return_request.number.value, order_number.value, )) # update values try: qty = return_request.get_item_qty(order_number, simple_sku) return_request.make_item_rejected(order_number, simple_sku) order.reject_return(simple_sku, qty) except ApplicationLogicException as e: raise UnprocessableEntityError(str(e)) # save updates def __log_flow(text: str) -> None: logger.log_simple('Return Request #{} - Rejecting : {}'.format( return_request.number.value, text)) __log_flow('Start') # saving __log_flow('Saving - Start') try: __log_flow('Order #{} Saving...'.format(order_number.value)) orders_storage.save(order) __log_flow('Order #{} Saving - Done!'.format(order_number.value)) __log_flow('Return Request Saving...') returns_storage.save(return_request) __log_flow('Return Request Saving - Done!') except ApplicationLogicException as e: __log_flow('Not Saved because of Error : {}'.format(str(e))) raise UnprocessableEntityError(str(e)) __log_flow('Saving - End') # send sqs __log_flow('SQS Sending - Start') try: __log_flow('Return Request SQS Sending...') sqs_sender.send(ReturnRequestChangeSqsSenderEvent(return_request)) __log_flow('Return Request SQS Sending - Done!') except BaseException as e: __log_flow( 'Return Request SQS Sending - Not done because of Error : {}'. format(str(e))) logger.log_exception(e) __log_flow('SQS Sending - End') __log_flow('End')
def returns_create_submit(): """ POST : { items: [ { order_number: str, simple_sku: str, qty: int, reason: str, file_ids: [str, ...], additional_comment: str|null, }, ... ], delivery_method: str, refund_method: { type: str, params: { # credit_card_eft account_holder_name: str, account_number: str, branch_code: str } } } """ user_id = __get_user().id now = datetime.datetime.now() returns_storage = ReturnRequestStorageImplementation() orders_storage = OrderStorageImplementation() file_storage = FileStorageImplementation() sqs_sender = SqsSenderImplementation() logger = Logger() # 1. Check input # ------------------------------- request_data = blueprint.current_request.json_body or {} input_items = request_data.get('items') if not input_items or not isinstance(input_items, (list, tuple, set)): raise BadRequestError( 'Incorrect Input Data! Parameter "items" is required!') elif sum([ not isinstance(item, dict) or not isinstance(item.get('order_number'), str) or not isinstance(item.get('simple_sku'), str) or not isinstance(item.get('qty'), int) or not isinstance(item.get('reason'), str) or not isinstance(item.get('file_ids'), (list, tuple, set)) or sum([ not isinstance(file_id, str) for file_id in item['file_ids'] ]) > 0 or not (item.get('additional_comment') is None or isinstance(item.get('additional_comment'), str)) for item in input_items ]) > 0: raise BadRequestError( 'Incorrect Input Data! Incorrect "items" structure!') delivery_method_input_descriptor = request_data.get('delivery_method') if not delivery_method_input_descriptor or not isinstance( delivery_method_input_descriptor, str): raise BadRequestError( 'Incorrect Input Data! Parameter "delivery_method" is required!' ) refund_method_input_data = request_data.get('refund_method') if not refund_method_input_data: raise BadRequestError( 'Incorrect Input Data! Parameter "refund_method" is required!') elif (not isinstance(refund_method_input_data, dict) or not isinstance(refund_method_input_data.get('type'), str) or not isinstance(refund_method_input_data.get('params'), dict)): raise BadRequestError( 'Incorrect Input Data! Parameter "refund_method" is incorrect!' ) # collect control data initial_data = __get_initial_data() control_data = { 'reasons': [reason['key'] for reason in initial_data['reasons']], 'delivery_methods': [ _delivery_method['key'] for _delivery_method in initial_data['delivery_methods'] ], 'orders': {}, } for order_data in initial_data['orders']: order_number = order_data['order_number'] for order_data_item in order_data['items']: simple_sku = order_data_item['simple_sku'] qty = order_data_item['qty_can_return'] control_data['orders'][order_number] = control_data[ 'orders'].get(order_number) or {} control_data['orders'][order_number][simple_sku] = qty # validate input data if ( # items sum([ item['order_number'] not in control_data['orders'].keys() or item['simple_sku'] not in control_data['orders'][item['order_number']].keys() or item['qty'] not in range( 1, control_data['orders'][item['order_number']][ item['simple_sku']] + 1) or item['reason'] not in control_data['reasons'] or sum([ not file_id.strip() or not file_storage.get(file_id) for file_id in item['file_ids'] ]) > 0 or (item['additional_comment'] is not None and len(item['additional_comment']) > 255) for item in input_items ]) > 0 # delivery method or delivery_method_input_descriptor not in control_data['delivery_methods'] # refund method (method structure/data check) or refund_method_input_data['type'] not in [ EftRefundMethod('test', 'test', 'test').descriptor, StoreCreditRefundMethod().descriptor, MobicredRefundMethod().descriptor, CreditCardRefundMethod().descriptor, ] or (refund_method_input_data['type'] == EftRefundMethod( 'test', 'test', 'test').descriptor and (not isinstance( refund_method_input_data.get( 'params', {}).get('account_holder_name'), str) or not refund_method_input_data['params'].get('account_holder_name') or not isinstance( refund_method_input_data.get('params', {}).get('account_number'), str) or not refund_method_input_data['params'].get('account_number') or not isinstance( refund_method_input_data.get('params', {}).get('branch_code'), str) or not refund_method_input_data['params'].get('branch_code'))) or (refund_method_input_data['type'] in ( StoreCreditRefundMethod().descriptor, MobicredRefundMethod().descriptor, CreditCardRefundMethod().descriptor, ) and len(refund_method_input_data['params']) > 0)): raise BadRequestError('Incorrect Input Data! Incorrect values!') # check duplicates in order if len( set([ str(item['order_number']) + str(item['simple_sku']) for item in input_items ])) != len(input_items): raise BadRequestError( 'Incorrect Input Data! Input items has duplicates!') # check refund methods # "...credit-card should be allowed only when one of selected orders was paid by credit card, # but eft and credits should be available for all return-requests..." _allowed_refund_methods_keys = [] for item in input_items: _order_refund_method_keys = [ _order_refund_method['key'] for _order in initial_data['orders'] if _order['order_number'] == item['order_number'] for _order_refund_method in _order['refund_methods'] ] # intersection of all input orders if len(_allowed_refund_methods_keys) == 0: _allowed_refund_methods_keys = _order_refund_method_keys else: _allowed_refund_methods_keys = [ key for key in _allowed_refund_methods_keys if key in _order_refund_method_keys ] if refund_method_input_data[ 'type'] not in _allowed_refund_methods_keys: raise BadRequestError( 'Incorrect Input Data! Refund method {} is not allowed for selected orders!' .format(refund_method_input_data['type'])) # 2. Create Return Request entity # ------------------------------- return_request_items = [] for item in input_items: order_number = item['order_number'] simple_sku = item['simple_sku'] qty = item['qty'] cost = None for initial_order in initial_data['orders']: if initial_order['order_number'] == order_number: for initial_item in initial_order['items']: if initial_item['simple_sku'] == simple_sku: cost = tuple( filter(lambda x: x.get('qty') == qty, initial_item['costs']))[0].get('cost') break reason = ReturnRequest.Item.Reason(item['reason']) attached_files = tuple([ ReturnRequest.Item.AttachedFile(file_storage.get(file_id).url) for file_id in item['file_ids'] ]) additional_comment = item.get('additional_comment') if item.get( 'additional_comment') else None additional_comment = ReturnRequest.Item.AdditionalComment( additional_comment) if additional_comment else None return_request_items.append( ReturnRequest.Item(OrderNumber(order_number), SimpleSku(simple_sku), Qty(qty), Cost(cost), reason, attached_files, additional_comment)) delivery_method_instance = None for _delivery_method_instance in [ HandDeliveryMethod(), CourierOrPostOffice(), RunwaysaleToCollect() ]: if _delivery_method_instance.descriptor == delivery_method_input_descriptor: delivery_method_instance = _delivery_method_instance break refund_method_instance = None for _refund_method_instance in [ StoreCreditRefundMethod(), EftRefundMethod('test', 'test', 'test'), MobicredRefundMethod(), CreditCardRefundMethod() ]: if _refund_method_instance.descriptor == refund_method_input_data[ 'type']: refund_method_instance = _refund_method_instance.__class__( **refund_method_input_data['params']) break return_request = ReturnRequest( Id(user_id), ReturnRequest.Number(now.strftime('%y%j03%f')), tuple(return_request_items), delivery_method_instance, refund_method_instance) # 3. Modify orders qty # ------------------------------- modified_orders = {} for return_item in return_request.items: order = modified_orders.get( return_item.order_number.value) or orders_storage.load( return_item.order_number) order.request_return(return_item.simple_sku, return_item.qty) modified_orders[order.number.value] = order # 4. Save changes # ------------------------------- def __log_flow(text: str) -> None: logger.log_simple('Return Request #{} - Creation : {}'.format( return_request.number.value, text)) __log_flow('Start') __log_flow('Saving Return Request...') returns_storage.save(return_request) __log_flow('Saving Return Request - Done!') __log_flow('Saving Orders...') for order in tuple(modified_orders.values()): __log_flow('Saving Order #{}...'.format(order.number.value)) orders_storage.save(order) __log_flow('Saving Order #{} - Done!'.format(order.number.value)) __log_flow('Saving Orders - Done!') # 5. Send SQS # ------------------------------- __log_flow('SQS Sending Return Request...') sqs_sender.send(ReturnRequestChangeSqsSenderEvent(return_request)) __log_flow('SQS Sending Return Request - Done!') __log_flow('End') return {'request_number': return_request.number.value}
class RegularEftPaymentSqsHandler(SqsHandlerInterface): def __init__(self): # Need to be imported here, because orders storage depends from payment method class in this file. from chalicelib.libs.purchase.order.storage import OrderStorageImplementation from chalicelib.libs.purchase.product.storage import ProductStorageImplementation self.__order_storage = OrderStorageImplementation() self.__sqs_sender = SqsSenderImplementation() self.__logger = Logger() self.__message_storage = MessageStorageImplementation() self.__customer_storage = CustomerStorageImplementation() self.__products_storage = ProductStorageImplementation() def handle(self, sqs_message: SqsMessage) -> None: def __log_flow(text: str) -> None: self.__logger.log_simple('{} : SQS Message #{} : {}'.format( self.__class__.__qualname__, sqs_message.id, text)) __log_flow('Start') if sqs_message.message_type != 'regular_eft_proof_check_result': raise ValueError( '{} does not know how to handle {} sqs message! Message data: {}' .format(self.__class__.__qualname__, sqs_message.message_type, sqs_message.message_data)) order_number_value = sqs_message.message_data.get('order_number') is_proof_accepted = sqs_message.message_data.get('is_proof_accepted') __log_flow('Order #{} - Payment - {}'.format( order_number_value, 'Accepted' if is_proof_accepted else 'Declined')) __log_flow('Updating...') order_number = Order.Number(order_number_value) order = self.__order_storage.load(order_number) if not order: raise ValueError( 'Unable to handle {} sqs-message #{}: order does not exist. Message data: {}' .format(sqs_message.message_type, sqs_message.id, sqs_message.message_data)) if not isinstance(order.payment_method, RegularEftOrderPaymentMethod): raise ValueError( 'Order #{} is not a Regular EFT payment order!'.format( order.number.value)) if is_proof_accepted: # accept order payment __log_flow('Order Updating...') order.status = Order.Status(Order.Status.PAYMENT_RECEIVED) self.__order_storage.save(order) __log_flow('Order Updated!') else: # Attention! # Order must be closed first to avoid multiple "restore-qty" actions! # @todo : refactoring ? # close order __log_flow('Order Closing...') order.status = Order.Status(Order.Status.CLOSED) self.__order_storage.save(order) __log_flow('Order Closed!') # restore products qty __log_flow('Product Qty Restoring - Start') for order_item in order.items: if order_item.qty_processable.value == 0: __log_flow( 'Product Qty Restoring: {} skipped because of 0 qty'. format(order_item.simple_sku.value)) continue try: __log_flow('Product Qty Restoring {} / {} ...'.format( order_item.simple_sku.value, order_item.qty_processable.value)) product = self.__products_storage.load( order_item.simple_sku) product.restore_qty(order_item.qty_processable) self.__products_storage.update(product) __log_flow('Product Qty Restored {} / {}!'.format( order_item.simple_sku.value, order_item.qty_processable.value)) except BaseException as e: self.__logger.log_exception(e) __log_flow( 'Product Qty NOT Restored {} / {} because of Error: '. format(order_item.simple_sku.value, order_item.qty_processable.value, str(e))) __log_flow('Product Qty Restoring - End') __log_flow('Updated!') # send to portal __log_flow('Order SQS: Sending...') self.__sqs_sender.send(OrderChangeSqsSenderEvent(order)) __log_flow('Order SQS: Sent!') # silently add notification (silently) try: __log_flow('Notification popup: Adding...') customer = self.__customer_storage.get_by_id(order.customer_id) if not customer: raise ValueError( '{} cant notify customer #{} about Regular-EFT payment updates for Order #{}' .format(self.handle.__qualname__, order.customer_id.value, order.number.value)) self.__message_storage.save( Message( str(uuid.uuid4()), customer.email.value, 'Regular EFT Payment has been checked!', 'Regular EFT Payment for Order #{} has been checked and {}!' .format(order.number.value, 'Accepted' if is_proof_accepted else 'Declined'))) __log_flow('Notification popup: Added!') except BaseException as e: self.__logger.log_exception(e) __log_flow( 'Notification popup: Not Added because of Error : {}'.format( str(e))) __log_flow('End')