Skip to content

Commit

Permalink
Mise à jours des données enregistrées dans l'inbound parsing (#985)
Browse files Browse the repository at this point in the history
* update saved data items for inbound parsing

* add disclaimers for attachments

* move ATTRIBUTES_TO_SAVE_FOR_INBOUND to constants
  • Loading branch information
madjid-asa authored Nov 28, 2023
1 parent ab5fed8 commit 74e9418
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
19 changes: 18 additions & 1 deletion lemarche/api/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.views import APIView

from lemarche.api.emails.serializers import EmailsSerializer
from lemarche.conversations.constants import ATTRIBUTES_TO_SAVE_FOR_INBOUND
from lemarche.conversations.models import Conversation
from lemarche.conversations.utils import get_info_from_email_prefix
from lemarche.www.conversations.tasks import send_email_from_conversation
Expand All @@ -13,6 +14,21 @@
logger = logging.getLogger(__name__)


def clean_saved_data_of_inbound(data_inbound: dict):
"""We clean saved data to respect the law of RGPD
Args:
data_inbound (dict): all data that we receive from inbound api
Returns:
dict with only neccessary key to save
"""
clean_saved_data = {}
for key in ATTRIBUTES_TO_SAVE_FOR_INBOUND:
clean_saved_data[key] = data_inbound.get(key)
return clean_saved_data


class InboundParsingEmailView(APIView):
def post(self, request):
serializer = EmailsSerializer(data=request.data)
Expand All @@ -23,7 +39,8 @@ def post(self, request):
version, conv_uuid, user_kind = get_info_from_email_prefix(inbound_email_prefix)
conv: Conversation = Conversation.objects.get_conv_from_uuid(conv_uuid=conv_uuid, version=version)
# save the input data
conv.data.append(serializer.data)
data_inbound_clean = clean_saved_data_of_inbound(data_inbound=serializer.data.get("items")[0])
conv.data.append(data_inbound_clean)
conv.save()
# find user_kind
if version >= 1:
Expand Down
1 change: 1 addition & 0 deletions lemarche/conversations/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ATTRIBUTES_TO_SAVE_FOR_INBOUND = ["From", "To", "CC", "ReplyTo", "SentAtDate", "Attachments"]
23 changes: 15 additions & 8 deletions lemarche/static/js/filter_data_message.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function filterData(data, filterText) {
return data.filter(item => {
const from = `${item.items[0].From.Name} || ${item.items[0].From.Address}`;
const to = item.items[0].To[0].Address;
const RawHtmlBody = item.items[0].RawHtmlBody;
const from = `${item.From.Name} || ${item.From.Address}`;
const to = item.To[0].Address;
const RawHtmlBody = item.RawHtmlBody;
// filter fields
return from.includes(filterText) || to.includes(filterText) || RawHtmlBody.includes(filterText);
});
Expand All @@ -11,14 +11,14 @@ function filterData(data, filterText) {
// Fonction pour générer les lignes du tableau HTML
function generateTableRows(filteredData) {
const rows = filteredData.map(item => {
const sendAt = new Date(item.items[0].SentAtDate).strftime('%d/%m/%Y %Hh%M');
const from = `${item.items[0].From.Name} - ${item.items[0].From.Address}`;
const RawHtmlBody = item.items[0].RawHtmlBody ? item.items[0].RawHtmlBody : item.items[0].RawTextBody;
const sendAt = new Date(item.SentAtDate).strftime('%d/%m/%Y %Hh%M');
const from = `${item.From.Name} - ${item.From.Address}`;
const attachementsCount = item.Attachments ? item.Attachments.length : 0;

return `<tr>
<td>${sendAt}</td>
<td>${from}</td>
<td>${RawHtmlBody}</td>
<td>${attachementsCount}</td>
</tr>`;
});

Expand All @@ -35,7 +35,14 @@ function init() {
// const filteredData = filterData(rawData, filterText);

// generate rows
const tableRows = generateTableRows(rawData);
const headTable = `
<tr>
<th scope="col">Date d'envoi</th>
<th scope="col">Expéditeur</th>
<th scope="col">Nombre de PJ</th>
</tr>
`;
const tableRows = headTable + generateTableRows(rawData);

// insert rows
dataTableBody.innerHTML = tableRows;
Expand Down
12 changes: 9 additions & 3 deletions lemarche/www/conversations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from lemarche.utils.emails import send_mail_async, whitelist_recipient_list


DISCLAIMER_ATTACHMENTS = (
"\nVeuillez noter que cette conversation email ne prend pas en charge les pièces jointes.\n"
"Pour envoyer un document, un devis ou autre, demandez les coordonnées direct de votre interlocuteur"
)


def send_first_email_from_conversation(conv: Conversation):
siae: Siae = conv.siae
from_email = f"{conv.sender_first_name} {conv.sender_last_name} <{conv.sender_email_buyer_encoded}>"
Expand All @@ -16,7 +22,7 @@ def send_first_email_from_conversation(conv: Conversation):
f"{sender_company_name}"
f"Ce client vous a contacté via le Marché de l'inclusion. "
"Pour échanger avec lui, répondez simplement à cet e-mail.\n"
)
) + DISCLAIMER_ATTACHMENTS

send_mail_async(
email_subject=conv.title,
Expand All @@ -34,7 +40,7 @@ def send_email_from_conversation(
from_email = f"{conv.sender_first_name} {conv.sender_last_name} <{conv.sender_email_buyer_encoded}>"
send_mail_async(
email_subject=email_subject,
email_body=email_body,
email_body=email_body + DISCLAIMER_ATTACHMENTS,
recipient_list=whitelist_recipient_list([conv.sender_email_siae]),
from_email=from_email,
email_body_html=email_body_html,
Expand All @@ -45,7 +51,7 @@ def send_email_from_conversation(
from_email = f"{siae.contact_full_name} <{conv.sender_email_siae_encoded}>"
send_mail_async(
email_subject=email_subject,
email_body=email_body,
email_body=email_body + DISCLAIMER_ATTACHMENTS,
recipient_list=whitelist_recipient_list([conv.sender_email_buyer]),
from_email=from_email,
email_body_html=email_body_html,
Expand Down

0 comments on commit 74e9418

Please sign in to comment.