Skip to content

Commit

Permalink
cocontracting btn for anonymous user with siae_id
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Sep 19, 2023
1 parent d79eb77 commit a786717
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="alert alert-danger mt-3 mt-lg-0" role="alert">
<p class="mb-0 fs-sm">
N'ayant pu identifier votre structure, nous n'avons pas pu prendre en compte votre demande de mise en relation.
</p>
</div>
8 changes: 3 additions & 5 deletions lemarche/templates/tenders/_detail_cta_cocontracting.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
<div class="card c-card rounded-lg shadow-lg">
<div class="card-body">
<h3>Répondre en co-traitance ?</h3>

<p>Notre équipe vous met en relation avec d’autres structures désireuses de répondre en co-traitance.</p>

{% if user.is_authenticated %}
<button type="button" class="btn btn-primary btn-block" title="Être mis en relation" hx-post="{% url 'tenders:detail-cocontracting-click' tender.slug %}" hx-target="#detail_cocontracting_container">
Être mis en relation
</button>
{% else %}
<button type="button" class="btn btn-primary btn-block" title="Être mis en relation">
Être mis en relation pour {{ siae_id }}
{% elif siae_id %}
<button type="button" class="btn btn-primary btn-block" title="Être mis en relation" hx-post="{% url 'tenders:detail-cocontracting-click' tender.slug %}?siae_id={{siae_id}}" hx-target="#detail_cocontracting_container">
Être mis en relation
</button>
{% endif %}
</div>
Expand Down
2 changes: 2 additions & 0 deletions lemarche/templates/tenders/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@
</div>
{% elif not user_siae_has_detail_contact_click_date %}
{% include "tenders/_detail_cta.html" with tender=tender user_can_click=True %}
{% include "tenders/_detail_cta_cocontracting.html" with tender=tender %}
{% endif %}
{% endif %}
{% elif siae_id %}
{% if not siae_has_detail_contact_click_date %}
{% include "tenders/_detail_cta.html" with tender=tender user_can_click=True siae_id=siae_id %}
{% include "tenders/_detail_cta_cocontracting.html" with tender=tender siae_id=siae_id %}
{% else %}
{% include "tenders/_detail_contact.html" with tender=tender %}
{% endif %}
Expand Down
3 changes: 2 additions & 1 deletion lemarche/www/tenders/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,5 @@ def notify_admin_siae_wants_cocontracting(tender: Tender, siae: Siae):
recipient_list=[settings.NOTIFY_EMAIL],
)

# api_slack.send_message_to_channel(text=email_body, service_id=settings.SLACK_WEBHOOK_C4_SUPPORT_CHANNEL)
if settings.BITOUBI_ENV == "prod":
api_slack.send_message_to_channel(text=email_body, service_id=settings.SLACK_WEBHOOK_C4_SUPPORT_CHANNEL)
58 changes: 45 additions & 13 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import timedelta
from unittest import mock

from django.conf import settings
from django.contrib.gis.geos import Point
Expand Down Expand Up @@ -854,7 +855,10 @@ def test_tender_cocontracting_display(self):
url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug})
response = self.client.get(url)
self.assertNotContains(response, "Répondre en co-traitance ?")

# anonymous but with siae_id in url
url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug})
response = self.client.get(f"{url}?siae_id=15")
self.assertContains(response, "Répondre en co-traitance ?")
# siae user interested but has a detail_contact_click_date
self.client.force_login(self.siae_user_1)
url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug})
Expand All @@ -866,7 +870,7 @@ def test_tender_cocontracting_display(self):
response = self.client.get(url)
self.assertContains(response, "Répondre en co-traitance ?")
# siae user interested
cls.tendersiae_1_2 = TenderSiae.objects.create(
tendersiae_1_2 = TenderSiae.objects.create(
tender=self.tender_1, siae=self.siae_2, email_send_date=timezone.now()
)
self.client.force_login(self.siae_user_2)
Expand Down Expand Up @@ -999,33 +1003,61 @@ def test_update_tendersiae_stats_on_tender_contact_click_with_siae_id_param(self

class TenderDetailCocontractingClickView(TestCase):
@classmethod
def setUpTestData(cls):
cls.siae = SiaeFactory(name="ZZ ESI")
cls.siae_user = UserFactory(kind=User.KIND_SIAE, siaes=[cls.siae])
cls.user_buyer = UserFactory(kind=User.KIND_BUYER, company_name="Entreprise Buyer")
cls.tender = TenderFactory(
def setUpTestData(self):
self.siae = SiaeFactory(name="ZZ ESI")
self.siae_user = UserFactory(kind=User.KIND_SIAE, siaes=[self.siae])
self.user_buyer = UserFactory(kind=User.KIND_BUYER, company_name="Entreprise Buyer")
self.tender = TenderFactory(
kind=tender_constants.KIND_TENDER,
author=cls.user_buyer,
author=self.user_buyer,
amount=tender_constants.AMOUNT_RANGE_100_150,
accept_share_amount=True,
response_kind=[Tender.RESPONSE_KIND_EMAIL],
)
cls.tendersiae = TenderSiae.objects.create(
tender=cls.tender,
siae=cls.siae,
self.tendersiae = TenderSiae.objects.create(
tender=self.tender,
siae=self.siae,
source="EMAIL",
email_send_date=timezone.now(),
email_link_click_date=timezone.now(),
detail_display_date=timezone.now(),
detail_contact_click_date=timezone.now(),
)
TenderQuestionFactory(tender=cls.tender)
TenderQuestionFactory(tender=self.tender)

def test_user_can_notify_cocontracting_wish(self):
url = reverse("tenders:detail-cocontracting-click", kwargs={"slug": self.tender.slug})
with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async:
response = self.client.post(url, data={})
self.assertEqual(response.status_code, 403)
mock_send_mail_async.assert_not_called()

with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async:
response = self.client.post(f"{url}?siae_id=999999", data={})
self.assertContains(response, "nous n'avons pas pu prendre en compte votre demande de mise en relation")
mock_send_mail_async.assert_not_called()

with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async:
response = self.client.post(f"{url}?siae_id={self.siae.id}", data={})
self.assertContains(response, "Nous avons bien pris en compte votre demande de mise en relation")
mock_send_mail_async.assert_called_once()
email_body = mock_send_mail_async.call_args[1]["email_body"]
self.assertTrue(f"La structure {self.siae.name } souhaite répondre en co-traitance" in email_body)

self.client.force_login(self.siae_user)
response = self.client.post(url, data={})
with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async:
response = self.client.post(url, data={})
self.assertContains(response, "Nous avons bien pris en compte votre demande de mise en relation")
mock_send_mail_async.assert_called_once()
email_body = mock_send_mail_async.call_args[1]["email_body"]
self.assertTrue(f"La structure {self.siae.name } souhaite répondre en co-traitance" in email_body)

user_without_siae = UserFactory(kind=User.KIND_SIAE)
self.client.force_login(user_without_siae)
with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async:
response = self.client.post(url, data={})
self.assertContains(response, "nous n'avons pas pu prendre en compte votre demande de mise en relation")
mock_send_mail_async.assert_not_called()


# TODO: this test doesn't work anymore. find a way to test logging post-email in non-prod environments?
Expand Down
13 changes: 10 additions & 3 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def get_success_message(self, detail_contact_click_confirm):
return f"<strong>{self.object.cta_card_button_text}</strong><br />Pour {self.object.cta_card_button_text.lower()}, vous devez accepter d'être mis en relation avec l'acheteur." # noqa


class TenderDetailCocontractingClickView(LoginRequiredMixin, DetailView):
class TenderDetailCocontractingClickView(SiaeUserRequiredOrSiaeIdParamMixin, DetailView):
"""
Endpoint to handle cocontracting button click
"""
Expand All @@ -419,8 +419,15 @@ def post(self, request, *args, **kwargs):
self.object = self.get_object()
user = self.request.user

if settings.BITOUBI_ENV == "prod":
notify_admin_siae_wants_cocontracting(self.object, user.siaes.first())
if self.request.user.is_authenticated:
siae = user.siaes.first()
else:
siae = Siae.objects.filter(pk=self.request.GET.get("siae_id", None)).first()

if siae:
notify_admin_siae_wants_cocontracting(self.object, siae)
else:
self.template_name = "tenders/_detail_cocontracting_click_error.html"

return self.get(request)

Expand Down

0 comments on commit a786717

Please sign in to comment.