Skip to content

Commit c237e66

Browse files
jzbahraiwhabanks
andauthored
Task/add freshdesk other (#2220)
* Add an api for to send other category to freshdesk * fix * fix --------- Co-authored-by: William B <[email protected]>
1 parent bab5f15 commit c237e66

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

app/clients/freshdesk.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,20 @@ def _generate_description(self):
7777
f"- Texte alternatif français : {self.contact.alt_text_fr}",
7878
]
7979
)
80-
80+
elif self.contact.is_new_template_category_request():
81+
message = "<br>".join(
82+
[
83+
f"New template category request from {self.contact.name} ({self.contact.email_address}):",
84+
f"- Service id: {self.contact.service_id}",
85+
f"- New Template Category Request name: {self.contact.template_category_name_en}",
86+
f"- Template id request: {self.contact.template_id_link}",
87+
"<hr>",
88+
f"Demande de nouvelle catégorie de modèle de {self.contact.name} ({self.contact.email_address}):",
89+
f"- Identifiant du service: {self.contact.service_id}",
90+
f"- Nom de la nouvelle catégorie de modèle demandée: {self.contact.template_category_name_fr}",
91+
f"- Demande d'identifiant de modèle: {self.contact.template_id_link}",
92+
]
93+
)
8194
if len(self.contact.user_profile):
8295
message += f"<br><br>---<br><br> {self.contact.user_profile}"
8396

app/user/contact_request.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class ContactRequest:
3434
branding_logo_name: str = field(default="")
3535
alt_text_en: str = field(default="")
3636
alt_text_fr: str = field(default="")
37+
template_category_name_en: str = field(default="")
38+
template_category_name_fr: str = field(default="")
39+
template_id_link: str = field(default="")
3740

3841
def __post_init__(self):
3942
# email address is mandatory for us
@@ -56,3 +59,6 @@ def is_go_live_request(self):
5659

5760
def is_branding_request(self):
5861
return "branding_request" in self.support_type.lower()
62+
63+
def is_new_template_category_request(self):
64+
return "new_template_category_request" in self.support_type.lower()

app/user/rest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,36 @@ def send_branding_request(user_id):
518518
return jsonify({"status_code": status_code}), 204
519519

520520

521+
@user_blueprint.route("/<uuid:user_id>/new-template-category-request", methods=["POST"])
522+
def send_new_template_category_request(user_id):
523+
contact = None
524+
data = request.json
525+
try:
526+
user = get_user_by_id(user_id=user_id)
527+
contact = ContactRequest(
528+
support_type="new_template_category_request",
529+
friendly_support_type="New template category request",
530+
name=user.name,
531+
email_address=user.email_address,
532+
service_id=data["service_id"],
533+
template_category_name_en=data["template_category_name_en"],
534+
template_category_name_fr=data["template_category_name_fr"],
535+
template_id_link=f"https://{current_app.config['ADMIN_BASE_URL']}/services/{data['service_id']}/templates/{data['template_id']}",
536+
)
537+
contact.tags = ["z_skip_opsgenie", "z_skip_urgent_escalation"]
538+
539+
except TypeError as e:
540+
current_app.logger.error(e)
541+
return jsonify({}), 400
542+
except NoResultFound as e:
543+
# This means that get_user_by_id couldn't find a user
544+
current_app.logger.error(e)
545+
return jsonify({}), 400
546+
547+
status_code = Freshdesk(contact).send_ticket()
548+
return jsonify({"status_code": status_code}), 204
549+
550+
521551
@user_blueprint.route("/<uuid:user_id>", methods=["GET"])
522552
@user_blueprint.route("", methods=["GET"])
523553
def get_user(user_id=None):

tests/app/clients/test_freshdesk.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,54 @@ def match_json(request):
181181
assert response == 201
182182
assert email_freshdesk_ticket_mock.not_called()
183183

184+
def test_send_ticket_other_category(self, email_freshdesk_ticket_mock, notify_api: Flask):
185+
def match_json(request):
186+
expected = {
187+
"product_id": 42,
188+
"subject": "New template category request",
189+
"description": "New template category request from name ([email protected]):<br>"
190+
"- Service id: 8624bd36-b70b-4d4b-a459-13e1f4770b92<br>"
191+
"- New Template Category Request name: test category name<br>"
192+
"- Template id request: http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103<br>"
193+
"<hr><br>"
194+
"Demande de nouvelle catégorie de modèle de name ([email protected]):<br>"
195+
"- Identifiant du service: 8624bd36-b70b-4d4b-a459-13e1f4770b92<br>"
196+
"- Nom de la nouvelle catégorie de modèle demandée: test category name<br>"
197+
"- Demande d'identifiant de modèle: http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103",
198+
"email": "[email protected]",
199+
"priority": 1,
200+
"status": 2,
201+
"tags": [],
202+
}
203+
204+
encoded_auth = base64.b64encode(b"freshdesk-api-key:x").decode("ascii")
205+
json_matches = request.json() == expected
206+
basic_auth_header = request.headers.get("Authorization") == f"Basic {encoded_auth}"
207+
208+
return json_matches and basic_auth_header
209+
210+
with requests_mock.mock() as rmock:
211+
rmock.request(
212+
"POST",
213+
"https://freshdesk-test.com/api/v2/tickets",
214+
additional_matcher=match_json,
215+
status_code=201,
216+
)
217+
data: Dict[str, Any] = {
218+
"email_address": "[email protected]",
219+
"name": "name",
220+
"friendly_support_type": "New template category request",
221+
"support_type": "new_template_category_request",
222+
"service_id": "8624bd36-b70b-4d4b-a459-13e1f4770b92",
223+
"template_category_name_en": "test category name",
224+
"template_category_name_fr": "test category name",
225+
"template_id_link": "http://localhost:6012/services/8624bd36-b70b-4d4b-a459-13e1f4770b92/templates/3ed1f07a-1b20-4f83-9a3e-158ab9b00103",
226+
}
227+
with notify_api.app_context():
228+
response = freshdesk.Freshdesk(ContactRequest(**data)).send_ticket()
229+
assert response == 201
230+
assert email_freshdesk_ticket_mock.not_called()
231+
184232
def test_send_ticket_other(self, email_freshdesk_ticket_mock, notify_api: Flask):
185233
def match_json(request):
186234
expected = {

tests/app/user/test_rest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,31 @@ def test_send_branding_request(client, sample_service, sample_organisation, mock
980980
mocked_salesforce_client.engagement_update.assert_not_called()
981981

982982

983+
class TestFreshDeskRequestTickets:
984+
def test_send_request_for_new_category(self, client, sample_service, sample_organisation, mocker):
985+
sample_user = sample_service.users[0]
986+
sample_service.organisation = sample_organisation
987+
post_data = {
988+
"service_name": sample_service.name,
989+
"email_address": sample_user.email_address,
990+
"service_id": str(sample_service.id),
991+
"template_category_name_en": "test",
992+
"template_category_name_fr": "test",
993+
"template_id": "1234",
994+
}
995+
mocked_freshdesk = mocker.patch("app.user.rest.Freshdesk.send_ticket", return_value=201)
996+
mocked_salesforce_client = mocker.patch("app.user.rest.salesforce_client")
997+
998+
resp = client.post(
999+
url_for("user.send_new_template_category_request", user_id=str(sample_user.id)),
1000+
data=json.dumps(post_data),
1001+
headers=[("Content-Type", "application/json"), create_authorization_header()],
1002+
)
1003+
assert resp.status_code == 204
1004+
mocked_freshdesk.assert_called_once_with()
1005+
mocked_salesforce_client.engagement_update.assert_not_called()
1006+
1007+
9831008
def test_send_user_confirm_new_email_returns_204(client, sample_user, change_email_confirmation_template, mocker):
9841009
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
9851010
new_email = "[email protected]"

0 commit comments

Comments
 (0)