Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inbound parsing : améliorer le matching #910

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lemarche/api/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from lemarche.api.emails.serializers import EmailsSerializer
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 @@ -16,22 +17,24 @@ class InboundParsingEmailView(APIView):
def post(self, request):
serializer = EmailsSerializer(data=request.data)
if serializer.is_valid():
inboundEmail = serializer.validated_data.get("items")[0]
address_mail_label = inboundEmail["To"][0]["Address"].split("@")[0]
inbound_email = serializer.validated_data.get("items")[0]
inbound_email_prefix = inbound_email["To"][0]["Address"].split("@")[0]
# get conversation object
version, conv_uuid, user_kind = Conversation.get_email_info_from_address(address_mail_label)
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)
conv.save()
user_kind = user_kind if version == 0 else conv.get_user_kind(conv_uuid)
# make the transfert of emails
# find user_kind
if version >= 1:
user_kind = conv.get_user_kind(conv_uuid)
# make the transfer of emails
send_email_from_conversation(
conv=conv,
user_kind=user_kind,
email_subject=inboundEmail.get("Subject", conv.title),
email_body=inboundEmail.get("RawTextBody"),
email_body_html=inboundEmail.get("RawHtmlBody"),
email_subject=inbound_email.get("Subject", conv.title),
email_body=inbound_email.get("RawTextBody"),
email_body_html=inbound_email.get("RawHtmlBody"),
)
return Response(conv.uuid, status=status.HTTP_201_CREATED)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.2 on 2023-09-05 18:03
from django.db import migrations


def set_uuids_again(apps, schema_editor):
Conversation = apps.get_model("conversations", "Conversation")
for conversation in Conversation.objects.filter(version=0):
conversation.sender_encoded = ""
conversation.set_sender_encoded()
conversation.siae_encoded = ""
conversation.set_siae_encoded()
conversation.save()


class Migration(migrations.Migration):
dependencies = [
("conversations", "0011_conversation_sender_siae_encoded_unique"),
]

operations = [
migrations.RunPython(set_uuids_again, migrations.RunPython.noop),
]
26 changes: 4 additions & 22 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.db import IntegrityError, models
from django.db.models import Func, IntegerField
from django.db.models import Func, IntegerField, Q
from django.utils import timezone
from django.utils.text import slugify
from django_extensions.db.fields import ShortUUIDField
Expand All @@ -27,7 +27,7 @@ def get_conv_from_uuid(self, conv_uuid: str, version=1):
if version == 0:
return self.get(uuid=conv_uuid)
else:
return self.get(models.Q(sender_encoded=conv_uuid) | models.Q(siae_encoded=conv_uuid))
return self.get(Q(sender_encoded__endswith=conv_uuid) | Q(siae_encoded__endswith=conv_uuid))


class Conversation(models.Model):
Expand Down Expand Up @@ -128,9 +128,9 @@ def save(self, *args, **kwargs):

def get_user_kind(self, conv_uuid):
# method only available in version >= 1
if conv_uuid == self.sender_encoded:
if self.sender_encoded.endswith(conv_uuid):
return self.USER_KIND_SENDER_TO_BUYER
elif conv_uuid == self.siae_encoded:
elif self.siae_encoded.endswith(conv_uuid):
return self.USER_KIND_SENDER_TO_SIAE

@property
Expand Down Expand Up @@ -170,24 +170,6 @@ def nb_messages(self):
"""
return len(self.data) + 1

@staticmethod
def get_email_info_from_address(address_mail_label: str) -> list:
"""Extract info from address mail managed by this class
Args:
address_mail_label (str): _description_

Returns:
[VERSION, UUID, KIND_SENDER]
"""
email_infos = address_mail_label.split("_")
# version is 0 email is like "uuid_kind"
# version is 1 email is like "full_name_can_be_long_short_uuid"
version = 0 if len(email_infos) == 2 else 1
# in version 1 kind sender is not usefull
uuid = email_infos[0] if version == 0 else "_".join(email_infos)
kind_sender = email_infos[1] if version == 0 else None
return version, uuid, kind_sender

@property
def is_validated(self) -> bool:
return self.validated_at is not None
Expand Down
23 changes: 23 additions & 0 deletions lemarche/conversations/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def get_info_from_email_prefix(email_prefix: str) -> list:
"""
Extract info from email_prefix
version 0 format: uuid_b, uuid_s (long uuid)
vesion 1 format: prenom_nom_uuid (short uuid)

Args:
email_prefix (str): _description_

Returns:
[VERSION, UUID, KIND_SENDER]
"""
email_prefix_infos = email_prefix.split("_")
# specificity of version 0: only 1 char at the end
if len(email_prefix_infos[-1]) == 1:
version = 0
uuid = email_prefix_infos[0]
kind_sender = email_prefix_infos[1]
else: # version 1
version = 1
uuid = email_prefix[-1]
kind_sender = None # not useful
return version, uuid, kind_sender