예제 #1
0
 def delete(self, db=None):
     if not db:
         db = CouponsAlchemyDB()
     db.delete_row_in_transaction("vouchers", **{'id': self.id_bin})
     if self.type is not VoucherType.regular_coupon.value:
         db.delete_row_in_transaction("auto_benefits",
                                      **{'voucher_id': self.id_bin})
예제 #2
0
def apply_benefits(args, order, benefits):
    order_id = args.get('order_id')
    user_id = args.get('customer_id')
    voucher_id_list = list()
    db = CouponsAlchemyDB()
    db.begin()
    try:
        rows_with_order_id = db.find("voucher_use_tracker",
                                     **{'order_id': order_id})
        if rows_with_order_id:
            db.delete_row_in_transaction("voucher_use_tracker",
                                         **{'order_id': order_id})
        for existing_voucher in order.existing_vouchers:
            voucher_id = existing_voucher['voucher'].id
            if voucher_id in voucher_id_list:
                continue
            voucher_id_list.append(voucher_id)
            rule = existing_voucher['voucher'].rules_list[0]
            if hasattr(order, 'validate') and not order.validate:
                pass
            else:
                success, error = rule.criteria_obj.check_usage(
                    order.customer_id, existing_voucher['voucher'].id_bin,
                    order_id, db)
                if not success:
                    db.rollback()
                    return False, 400, default_error_message

            if config.client == 'new_grocery':
                customer_id = getattr(request, "phone_no", "-")
            else:
                customer_id = user_id

            session_id = request.headers.get('X-ASKME-SESSIONID', None)
            user_uuid = request.headers.get('X-ASKME-USERID', None)

            transaction_log = VoucherTransactionLog(
                **{
                    'id': uuid.uuid1().hex,
                    'user_id': customer_id,
                    'session_id': session_id,
                    'user_uuid': user_uuid,
                    'voucher_id': voucher_id,
                    'order_id': order_id,
                    'status': VoucherTransactionStatus.in_progress.value,
                    'response': json.dumps(benefits)
                })
            transaction_log.save(db)
        db.commit()
    except Exception as e:
        logger.exception(e)
        db.rollback()
        return False, 500, u'Unknown Error. Please try after some time'
    # else:
    #     db.commit()
    return True, 200, None