|
1 | 1 | import functools |
| 2 | +import json |
2 | 3 | import logging |
3 | 4 | from typing import Any |
4 | 5 |
|
|
22 | 23 | from models_library.utils.fastapi_encoders import jsonable_encoder |
23 | 24 | from pydantic import BaseModel, ConfigDict |
24 | 25 | from servicelib.aiohttp import status |
| 26 | +from servicelib.aiohttp.application_keys import APP_SETTINGS_KEY |
25 | 27 | from servicelib.aiohttp.requests_validation import ( |
26 | 28 | parse_request_body_as, |
27 | 29 | parse_request_path_parameters_as, |
28 | 30 | parse_request_query_parameters_as, |
29 | 31 | ) |
30 | 32 | from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON |
31 | 33 | from servicelib.rest_constants import RESPONSE_MODEL_POLICY |
32 | | -from simcore_service_webserver.users import users_service |
33 | 34 |
|
34 | 35 | from ..._meta import API_VTAG as VTAG |
35 | 36 | from ...email import email_service |
| 37 | +from ...fogbugz import get_fogbugz_rest_client |
| 38 | +from ...fogbugz._client import FogbugzCaseCreate |
| 39 | +from ...fogbugz.settings import FogbugzSettings |
36 | 40 | from ...login.decorators import login_required |
37 | 41 | from ...models import AuthenticatedRequestContext |
38 | 42 | from ...products import products_web |
| 43 | +from ...users import users_service |
39 | 44 | from ...utils_aiohttp import envelope_json_response |
40 | 45 | from .. import _conversation_message_service, _conversation_service |
41 | 46 | from ._common import ConversationPathParams, raise_unsupported_type |
@@ -107,10 +112,60 @@ async def create_conversation_message(request: web.Request): |
107 | 112 | ) |
108 | 113 |
|
109 | 114 | # NOTE: This is done here in the Controller layer, as the interface around email currently needs request |
110 | | - if is_first_message: |
| 115 | + product = products_web.get_current_product(request) |
| 116 | + fogbugz_settings_or_none: FogbugzSettings | None = request.app[ |
| 117 | + APP_SETTINGS_KEY |
| 118 | + ].WEBSERVER_FOGBUGZ |
| 119 | + if ( |
| 120 | + product.support_standard_group_id |
| 121 | + and fogbugz_settings_or_none is not None |
| 122 | + and is_first_message |
| 123 | + ): |
| 124 | + _logger.debug( |
| 125 | + "Support settings available and FogBugz client configured, creating FogBugz case." |
| 126 | + ) |
| 127 | + assert product.support_assigned_fogbugz_project_id # nosec |
| 128 | + |
| 129 | + try: |
| 130 | + user = await users_service.get_user(request.app, req_ctx.user_id) |
| 131 | + _url = request.url |
| 132 | + _conversation_url = f"{_url.scheme}://{_url.host}/#/conversation/{path_params.conversation_id}" |
| 133 | + |
| 134 | + _description = f""" |
| 135 | + Dear Support Team, |
| 136 | +
|
| 137 | + We have received a support request from {user["first_name"]} {user["last_name"]} ({user["email"]}) on {request.host}. |
| 138 | +
|
| 139 | + All communication should take place in the Platform Support Center at the following link: {_conversation_url} |
| 140 | +
|
| 141 | + First message content: {message.content} |
| 142 | +
|
| 143 | + Extra content: {json.dumps(_conversation.extra_context)} |
| 144 | + """ |
| 145 | + |
| 146 | + _fogbugz_client = get_fogbugz_rest_client(request.app) |
| 147 | + _fogbugz_case_data = FogbugzCaseCreate( |
| 148 | + fogbugz_project_id=product.support_assigned_fogbugz_project_id, |
| 149 | + title=f"Request for Support on {request.host}", |
| 150 | + description=_description, |
| 151 | + ) |
| 152 | + await _fogbugz_client.create_case(_fogbugz_case_data) |
| 153 | + except Exception: # pylint: disable=broad-except |
| 154 | + _logger.exception( |
| 155 | + "Failed to create support request FogBugz case for conversation %s.", |
| 156 | + _conversation.conversation_id, |
| 157 | + ) |
| 158 | + |
| 159 | + elif ( |
| 160 | + product.support_standard_group_id |
| 161 | + and fogbugz_settings_or_none is None |
| 162 | + and is_first_message |
| 163 | + ): |
| 164 | + _logger.debug( |
| 165 | + "Support settings available, but no FogBugz client configured, sending email instead to create FogBugz case." |
| 166 | + ) |
111 | 167 | try: |
112 | 168 | user = await users_service.get_user(request.app, req_ctx.user_id) |
113 | | - product = products_web.get_current_product(request) |
114 | 169 | template_name = "request_support.jinja2" |
115 | 170 | destination_email = product.support_email |
116 | 171 | email_template_path = await products_web.get_product_template_path( |
@@ -141,6 +196,8 @@ async def create_conversation_message(request: web.Request): |
141 | 196 | template_name, |
142 | 197 | destination_email, |
143 | 198 | ) |
| 199 | + else: |
| 200 | + _logger.debug("No support settings available, skipping FogBugz case creation.") |
144 | 201 |
|
145 | 202 | data = ConversationMessageRestGet.from_domain_model(message) |
146 | 203 | return envelope_json_response(data, web.HTTPCreated) |
|
0 commit comments