Skip to content

Commit

Permalink
fixup! ✨(dimail) synchronize mailboxes from dimail to our db
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeammet committed Oct 30, 2024
1 parent ac53473 commit 4357e59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/backend/mailbox_manager/tests/test_utils_dimail_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def test_dimail_synchronization__already_sync():
"""
Nothing should be created when everything is already synced.
No mailbox should be created when everything is already synced.
"""
domain = factories.MailDomainFactory(status=enums.MailDomainStatusChoices.ENABLED)
factories.MailboxFactory.create_batch(3, domain=domain)
Expand Down Expand Up @@ -53,8 +53,10 @@ def test_dimail_synchronization__already_sync():
status=status.HTTP_200_OK,
content_type="application/json",
)
dimail_client.synchronize_dimail_mailboxes(domain.name)
dimail_client.synchronize_mailboxes_from_dimail(domain.name)

post_sync_mailboxes = models.Mailbox.objects.filter(domain=domain)
assert post_sync_mailboxes.count() == 3
assert set(models.Mailbox.objects.filter(domain=domain)) == set(pre_sync_mailboxes)


Expand Down Expand Up @@ -92,6 +94,6 @@ def test_dimail_synchronization__synchronize_mailboxes():
status=status.HTTP_200_OK,
content_type="application/json",
)
dimail_client.synchronize_dimail_mailboxes(domain.name)
dimail_client.synchronize_mailboxes_from_dimail(domain.name)

assert models.Mailbox.objects.exists()
19 changes: 11 additions & 8 deletions src/backend/mailbox_manager/utils/dimail.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def send_new_mailbox_notification(self, recipient, mailbox_data):
exception,
)

def synchronize_dimail_mailboxes(self, domain_name):
def synchronize_mailboxes_from_dimail(self, domain_name):
"""Synchronize mailboxes from dimail - open xchange to our database.
This is useful in case of acquisition of a pre-existing mail domain.
This is not considered a new email and will not trigger any mail notification."""
Mailboxes created here are not new mailboxes and will not trigger mail notification."""

domain = models.MailDomain.objects.get(name=domain_name)
people_mailboxes = models.Mailbox.objects.filter(domain=domain)
Expand Down Expand Up @@ -199,16 +199,19 @@ def synchronize_dimail_mailboxes(self, domain_name):
response.content.decode("utf-8")
) # format output str to proper list

for mailbox in content:
local_part = mailbox["email"].split("@")[0]
if not mailbox["email"] in [str(mailbox) for mailbox in people_mailboxes]:
for dimail_mailbox in content:
local_part = dimail_mailbox["email"].split("@")[0]

if not dimail_mailbox["email"] in [
str(people_mailbox) for people_mailbox in people_mailboxes
]:
# creates a mailbox on our end
models.Mailbox.objects.create(
first_name=mailbox["givenName"],
last_name=mailbox["surName"],
first_name=dimail_mailbox["givenName"],
last_name=dimail_mailbox["surName"],
local_part=local_part,
domain=domain,
secondary_email=mailbox[
secondary_email=dimail_mailbox[
"email"
], # secondary email is mandatory. Unfortunately, dimail doesn't store any.
# We temporarily give current email as secondary email.
Expand Down

0 comments on commit 4357e59

Please sign in to comment.