236 lines
6.2 KiB
Python
Executable File
236 lines
6.2 KiB
Python
Executable File
from flask import jsonify, request
|
|
from app.payment import payment
|
|
from app import db
|
|
from app.classes.customer import Customer_Customer
|
|
from app.classes.cards import Card_Card, Card_Card_schema
|
|
|
|
|
|
|
|
def set_card_main(user_id, card_id):
|
|
"""
|
|
updates a card of a user
|
|
"""
|
|
get_card_count = (
|
|
db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.count()
|
|
)
|
|
|
|
get_card = (
|
|
db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.filter(Card_Card.id == card_id)
|
|
.first()
|
|
)
|
|
|
|
if get_card_count > 0:
|
|
get_old_card = (
|
|
db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.main_card == True)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.first()
|
|
)
|
|
|
|
get_old_card.main_card = False
|
|
get_card.main_card = True
|
|
|
|
db.session.add(get_old_card)
|
|
db.session.commit()
|
|
else:
|
|
|
|
|
|
get_card.main_card = True
|
|
|
|
db.session.add(get_card)
|
|
db.session.commit()
|
|
|
|
|
|
@payment.route("/cards/<int:user_id>", methods=["GET"])
|
|
def get_user_cards(user_id):
|
|
"""
|
|
gets all cards of a user
|
|
"""
|
|
get_u_cards = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.all())
|
|
|
|
card_schema = Card_Card_schema(many=True)
|
|
return jsonify(card_schema.dump(get_u_cards))
|
|
|
|
|
|
@payment.route("/cards/onfile/<int:user_id>", methods=["GET"])
|
|
def get_user_cards_count(user_id):
|
|
"""
|
|
gets all cards of a user
|
|
"""
|
|
|
|
get_u_cards = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.count())
|
|
|
|
return jsonify({
|
|
"ok": True,
|
|
'cards': get_u_cards,
|
|
}), 200
|
|
|
|
|
|
@payment.route("/card/<int:card_id>", methods=["GET"])
|
|
def get_user_specific_card(card_id):
|
|
"""
|
|
gets a specific card of a user
|
|
"""
|
|
|
|
get_user_card = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.id == card_id)
|
|
.first())
|
|
|
|
card_schema = Card_Card_schema(many=False)
|
|
return jsonify(card_schema.dump(get_user_card))
|
|
|
|
|
|
|
|
@payment.route("/card/main/<int:card_id>/<int:user_id>", methods=["PUT"])
|
|
def set_main_card(user_id, card_id):
|
|
"""
|
|
updates a card of a user
|
|
"""
|
|
|
|
get_new_main_card = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.filter(Card_Card.id == card_id)
|
|
.first())
|
|
|
|
get_other_card = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.main_card == True)
|
|
.filter(Card_Card.user_id == user_id)
|
|
.first())
|
|
|
|
if get_other_card is not None:
|
|
get_other_card.main_card = False
|
|
db.session.add(get_other_card)
|
|
get_new_main_card.main_card = True
|
|
|
|
db.session.add(get_new_main_card)
|
|
db.session.commit()
|
|
|
|
return jsonify({"ok": True}), 200
|
|
|
|
|
|
|
|
|
|
@payment.route("/card/remove/<int:card_id>", methods=["DELETE"])
|
|
def remove_user_card(card_id):
|
|
"""
|
|
removes a card
|
|
"""
|
|
|
|
get_card = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.id == card_id)
|
|
.first())
|
|
|
|
db.session.delete(get_card)
|
|
db.session.commit()
|
|
|
|
return jsonify({"ok": True}), 200
|
|
|
|
|
|
@payment.route("/card/create/<int:user_id>", methods=["POST"])
|
|
def create_user_card(user_id):
|
|
"""
|
|
adds a card of a user
|
|
"""
|
|
get_customer = (db.session
|
|
.query(Customer_Customer)
|
|
.filter(Customer_Customer.id == user_id)
|
|
.first())
|
|
|
|
data = request.get_json()
|
|
# FIX: Use .get() for safety and get the correct key 'name_on_card'
|
|
name_on_card = data.get("name_on_card") # <-- This now matches the frontend
|
|
expiration_month = data.get("expiration_month")
|
|
expiration_year = data.get("expiration_year")
|
|
type_of_card = data.get("type_of_card")
|
|
security_number = data.get("security_number")
|
|
main_card = data.get("main_card", False)
|
|
zip_code = data.get("zip_code")
|
|
card_number = data.get("card_number")
|
|
|
|
# FIX: Correctly slice the last four digits
|
|
last_four = card_number[-4:] if card_number else ""
|
|
|
|
create_new_card = Card_Card(
|
|
user_id=get_customer.id,
|
|
card_number=card_number,
|
|
last_four_digits=last_four, # <-- Use the correctly sliced value
|
|
name_on_card=name_on_card,
|
|
expiration_month=expiration_month,
|
|
expiration_year=expiration_year,
|
|
type_of_card=type_of_card,
|
|
security_number=security_number,
|
|
accepted_or_declined=None,
|
|
main_card=main_card,
|
|
zip_code=zip_code
|
|
)
|
|
db.session.add(create_new_card)
|
|
db.session.flush()
|
|
|
|
if main_card:
|
|
set_card_main(user_id=get_customer.id, card_id=create_new_card.id)
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({"ok": True}), 200
|
|
|
|
|
|
@payment.route("/card/edit/<int:card_id>", methods=["PUT"])
|
|
def update_user_card(card_id):
|
|
"""
|
|
edits a card
|
|
"""
|
|
get_card = (db.session
|
|
.query(Card_Card)
|
|
.filter(Card_Card.id == card_id)
|
|
.first())
|
|
if not get_card:
|
|
return jsonify({"ok": False, "error": "Card not found"}), 404
|
|
|
|
data = request.get_json()
|
|
# FIX: Use .get() for safety and get the correct key 'name_on_card'
|
|
name_on_card = data.get("name_on_card") # <-- This now matches the frontend
|
|
expiration_month = data.get("expiration_month")
|
|
expiration_year = data.get("expiration_year")
|
|
type_of_card = data.get("type_of_card")
|
|
security_number = data.get("security_number")
|
|
card_number = data.get("card_number")
|
|
main_card = data.get("main_card", False)
|
|
zip_code = data.get("zip_code")
|
|
|
|
get_card.card_number = card_number
|
|
get_card.name_on_card = name_on_card
|
|
get_card.expiration_month = expiration_month
|
|
get_card.expiration_year = expiration_year
|
|
get_card.type_of_card = type_of_card
|
|
get_card.security_number = security_number
|
|
get_card.main_card = main_card
|
|
get_card.zip_code = zip_code
|
|
|
|
# FIX: Correctly slice the last four digits on edit
|
|
if card_number:
|
|
get_card.last_four_digits = card_number[-4:]
|
|
|
|
if main_card:
|
|
set_card_main(user_id=get_card.user_id, card_id=get_card.id)
|
|
|
|
db.session.add(get_card)
|
|
db.session.commit()
|
|
|
|
return jsonify({"ok": True}), 200 |