Skip to content

Commit 3a24180

Browse files
Refactors FogBugz case creation for support conversations
Moves FogBugz case creation logic into a dedicated service function to improve modularity and simplify the controller. Enhances maintainability by concentrating related logic and reduces duplication in handling support conversation integration with FogBugz.
1 parent c2daa7f commit 3a24180

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

services/web/server/src/simcore_service_webserver/conversations/_controller/_conversations_messages_rest.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import functools
2-
import json
32
import logging
43
from typing import Any
5-
from urllib.parse import urljoin
64

75
from aiohttp import web
86
from common_library.json_serialization import json_dumps
@@ -14,7 +12,6 @@
1412
ConversationMessageID,
1513
ConversationMessagePatchDB,
1614
ConversationMessageType,
17-
ConversationPatchDB,
1815
ConversationType,
1916
)
2017
from models_library.rest_pagination import (
@@ -36,8 +33,6 @@
3633

3734
from ..._meta import API_VTAG as VTAG
3835
from ...email import email_service
39-
from ...fogbugz import get_fogbugz_rest_client
40-
from ...fogbugz._client import FogbugzCaseCreate
4136
from ...fogbugz.settings import FogbugzSettings
4237
from ...login.decorators import login_required
4338
from ...models import AuthenticatedRequestContext
@@ -129,44 +124,18 @@ async def create_conversation_message(request: web.Request):
129124
assert product.support_assigned_fogbugz_project_id # nosec
130125

131126
try:
132-
user = await users_service.get_user(request.app, req_ctx.user_id)
133127
_url = request.url
134128
_conversation_url = f"{_url.scheme}://{_url.host}/#/conversation/{path_params.conversation_id}"
135129

136-
_description = f"""
137-
Dear Support Team,
138-
139-
We have received a support request from {user["first_name"]} {user["last_name"]} ({user["email"]}) on {request.host}.
140-
141-
All communication should take place in the Platform Support Center at the following link: {_conversation_url}
142-
143-
First message content: {message.content}
144-
145-
Extra content: {json.dumps(_conversation.extra_context)}
146-
"""
147-
148-
_fogbugz_client = get_fogbugz_rest_client(request.app)
149-
_fogbugz_case_data = FogbugzCaseCreate(
150-
fogbugz_project_id=product.support_assigned_fogbugz_project_id,
151-
title=f"Request for Support on {request.host}",
152-
description=_description,
153-
)
154-
_case_id = await _fogbugz_client.create_case(_fogbugz_case_data)
155-
156-
await _conversation_service.update_conversation(
130+
await _conversation_service.create_fogbugz_case_for_support_conversation(
157131
request.app,
158-
project_id=None,
159-
conversation_id=_conversation.conversation_id,
160-
updates=ConversationPatchDB(
161-
name=None,
162-
extra_context=_conversation.extra_context
163-
| {
164-
"fogbugz_case_url": urljoin(
165-
f"{fogbugz_settings_or_none.FOGBUGZ_URL}",
166-
f"f/cases/{_case_id}",
167-
)
168-
},
169-
),
132+
conversation=_conversation,
133+
user_id=req_ctx.user_id,
134+
message_content=message.content,
135+
conversation_url=_conversation_url,
136+
host=request.host,
137+
product_support_assigned_fogbugz_project_id=product.support_assigned_fogbugz_project_id,
138+
fogbugz_url=str(fogbugz_settings_or_none.FOGBUGZ_URL),
170139
)
171140
except Exception: # pylint: disable=broad-except
172141
_logger.exception(

services/web/server/src/simcore_service_webserver/conversations/_conversation_service.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# pylint: disable=unused-argument
22

3+
import json
34
import logging
45
from typing import Any
6+
from urllib.parse import urljoin
57

68
from aiohttp import web
79
from models_library.basic_types import IDStr
@@ -22,6 +24,7 @@
2224
notify_conversation_deleted,
2325
notify_conversation_updated,
2426
)
27+
from ..fogbugz import FogbugzCaseCreate, get_fogbugz_rest_client
2528
from ..groups.api import list_user_groups_ids_with_read_access
2629
from ..products import products_service
2730
from ..projects._groups_repository import list_project_groups
@@ -240,3 +243,55 @@ async def list_support_conversations_for_user(
240243
limit=limit,
241244
order_by=OrderBy(field=IDStr("conversation_id"), direction=OrderDirection.DESC),
242245
)
246+
247+
248+
async def create_fogbugz_case_for_support_conversation(
249+
app: web.Application,
250+
*,
251+
conversation: ConversationGetDB,
252+
user_id: UserID,
253+
message_content: str,
254+
conversation_url: str,
255+
host: str,
256+
product_support_assigned_fogbugz_project_id: int,
257+
fogbugz_url: str,
258+
) -> None:
259+
"""Creates a FogBugz case for a support conversation and updates the conversation with the case URL."""
260+
user = await users_service.get_user(app, user_id)
261+
262+
description = f"""
263+
Dear Support Team,
264+
265+
We have received a support request from {user["first_name"]} {user["last_name"]} ({user["email"]}) on {host}.
266+
267+
All communication should take place in the Platform Support Center at the following link: {conversation_url}
268+
269+
First message content: {message_content}
270+
271+
Extra content: {json.dumps(conversation.extra_context)}
272+
"""
273+
274+
fogbugz_client = get_fogbugz_rest_client(app)
275+
fogbugz_case_data = FogbugzCaseCreate(
276+
fogbugz_project_id=product_support_assigned_fogbugz_project_id,
277+
title=f"Request for Support on {host}",
278+
description=description,
279+
)
280+
case_id = await fogbugz_client.create_case(fogbugz_case_data)
281+
282+
# Update conversation with FogBugz case URL
283+
await update_conversation(
284+
app,
285+
project_id=None,
286+
conversation_id=conversation.conversation_id,
287+
updates=ConversationPatchDB(
288+
name=None,
289+
extra_context=conversation.extra_context
290+
| {
291+
"fogbugz_case_url": urljoin(
292+
f"{fogbugz_url}",
293+
f"f/cases/{case_id}",
294+
)
295+
},
296+
),
297+
)

0 commit comments

Comments
 (0)