support: Update remote server customer default discount.

Adds ability to update the default discount for a customer attached
to a remote server via the remote server support view.
This commit is contained in:
Lauryn Menard 2023-12-02 18:13:39 +01:00 committed by Tim Abbott
parent 38eaa4b958
commit 4250e9c1c9
3 changed files with 50 additions and 1 deletions

View File

@ -383,7 +383,10 @@ def get_remote_servers_for_support(
@require_server_admin
@has_request_variables
def remote_servers_support(
request: HttpRequest, query: Optional[str] = REQ("q", default=None)
request: HttpRequest,
query: Optional[str] = REQ("q", default=None),
remote_server_id: Optional[int] = REQ(default=None, converter=to_non_negative_int),
discount: Optional[Decimal] = REQ(default=None, converter=to_decimal),
) -> HttpResponse:
context: Dict[str, Any] = {}
@ -391,6 +394,34 @@ def remote_servers_support(
context["success_message"] = request.session["success_message"]
del request.session["success_message"]
acting_user = request.user
assert isinstance(acting_user, UserProfile)
if settings.BILLING_ENABLED and request.method == "POST":
# We check that request.POST only has two keys in it: The
# remote_server_id and a field to change.
keys = set(request.POST.keys())
if "csrfmiddlewaretoken" in keys:
keys.remove("csrfmiddlewaretoken")
if len(keys) != 2:
raise JsonableError(_("Invalid parameters"))
assert remote_server_id is not None
remote_server = RemoteZulipServer.objects.get(id=remote_server_id)
support_view_request = None
if discount is not None:
support_view_request = SupportViewRequest(
support_type=SupportType.attach_discount,
discount=discount,
)
if support_view_request is not None:
billing_session = RemoteServerBillingSession(
support_staff=acting_user, remote_server=remote_server
)
success_message = billing_session.process_support_view_request(support_view_request)
context["success_message"] = success_message
email_to_search = None
hostname_to_search = None
if query:
@ -418,6 +449,7 @@ def remote_servers_support(
context["remote_servers"] = remote_servers
context["remote_server_to_max_monthly_messages"] = remote_server_to_max_monthly_messages
context["plan_data"] = plan_data
context["get_discount"] = get_customer_discount_for_support_view
return render(
request,

View File

@ -46,6 +46,19 @@
<b>Last updated</b>: {{ remote_server.last_updated|timesince }} ago<br />
<b>Max monthly messages</b>: {{ remote_server_to_max_monthly_messages[remote_server.id] }}<br />
</div>
<form method="POST" class="remote-server-form">
<b>Discount</b>:<br />
{{ csrf_input }}
<input type="hidden" name="remote_server_id" value="{{ remote_server.id }}" />
{% if plan_data[remote_server.id].current_plan and plan_data[remote_server.id].current_plan.fixed_price %}
<input type="number" name="discount" value="{{ get_discount(plan_data[remote_server.id].customer) }}" disabled />
<button type="submit" class="btn btn-default support-submit-button" disabled>Update</button>
{% else %}
<input type="number" name="discount" value="{{ get_discount(plan_data[remote_server.id].customer) }}" required />
<button type="submit" class="btn btn-default support-submit-button">Update</button>
{% endif %}
</form>
</div>
{% endfor %}
</div>

View File

@ -155,6 +155,10 @@ tr.admin td:first-child {
padding-bottom: 15px;
}
.remote-server-form {
margin-bottom: 10px;
}
.support-realm-icon {
max-width: 25px;
position: relative;