Skip to content

Commit

Permalink
Fix #1415 As shop owner I can change the email address of a subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Oct 7, 2024
1 parent 9bc6bae commit 39fe010
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
71 changes: 70 additions & 1 deletion subscribie/blueprints/admin/subscriber.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from . import admin
from subscribie.auth import login_required
from subscribie.models import Person
from flask import render_template
from flask import render_template, url_for, request
from subscribie.utils import get_stripe_secret_key, get_stripe_connect_account_id
from subscribie.database import database
import stripe
import json
import logging

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -54,6 +56,73 @@ def show_subscriber(subscriber_id):
)


def show_subscriber_email(subscriber_id):
person = Person.query.execution_options(include_archived=True).get(subscriber_id)

return f"""
<span hx-target="this" hx-swap="outerHTML">
<li class="list-group-item">📧 {person.email}
<button hx-get="{url_for('admin.edit_subscriber', subscriber_id=person.id)}">
Edit
</button>
</li>
</span>
"""


@admin.route("/subscriber/<subscriber_id>/email", methods=["GET"])
@login_required
def get_subscriber_email_address(subscriber_id):
return show_subscriber_email(subscriber_id)


@admin.route("/subscriber/<subscriber_id>/edit", methods=["GET", "PUT"])
@login_required
def edit_subscriber(subscriber_id):
person = Person.query.execution_options(include_archived=True).get(subscriber_id)

if request.method == "PUT":
old_email = person.email
new_email = request.form.get("email")
person.email = new_email
database.session.commit()

# Also update Stripe customer email record
try:
if len(person.subscriptions) > 0:
stripe_customer_id = json.loads(
person.subscriptions[0].stripe_invoices[0].stripe_invoice_raw_json
)["customer"]
stripe.api_key = get_stripe_secret_key()
stripe_connect_account_id = get_stripe_connect_account_id()
stripe.Customer.modify(
id=stripe_customer_id,
stripe_account=stripe_connect_account_id,
email=new_email,
)
except Exception as e:
log.error(
f"Failure updating person '{person.id}' email to {new_email}. {e}"
)
log.debug("Switching person email address back to old email")
person.email = old_email
database.session.commit()

return show_subscriber_email(subscriber_id)

return f"""
<form hx-put="{url_for('admin.edit_subscriber', subscriber_id=subscriber_id)}" hx-target="this" hx-swap="outerHTML">
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="{person.email}">
</div>
<button class="btn btn-primary">Submit</button>
<button class="btn btn-secondary"
hx-get="{url_for('admin.get_subscriber_email_address', subscriber_id=subscriber_id)}">Cancel</button>
</form>
"""


@admin.route("/charge/subscriber/<person_id>", methods=["GET"])
@login_required
def charge_subscriber(person_id):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ <h2 class="text-center text-dark mb-3">Subscriber: {{ person.full_name | title }
<h3 class="card-title">👤 {{ person.full_name | title }}</h3>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">📧 {{ person.email }}</li>
<span hx-target="this" hx-swap="outerHTML">
<li class="list-group-item">📧 {{ person.email }}
<button hx-get="{{ url_for('admin.edit_subscriber', subscriber_id = person.id) }}">
Edit
</button>
</li>
</span>
<li class="list-group-item">📱 {{ person.mobile }}</li>
<li class="list-group-item">🏠 <strong>Address</strong>
<address>
Expand Down

0 comments on commit 39fe010

Please sign in to comment.