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

✨(api) add count mailboxes to MailDomain serializer #688

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to

### Added

- ✨(api) add count mailboxes to MailDomain serializer
- ✨(dimail) manage 'action required' status for MailDomain
- ✨(domains) add action required status on MailDomain
- ✨(dimail) send pending mailboxes upon domain activation
Expand Down
7 changes: 7 additions & 0 deletions src/backend/mailbox_manager/api/client/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MailDomainSerializer(serializers.ModelSerializer):
"""Serialize mail domain."""

abilities = serializers.SerializerMethodField(read_only=True)
count_mailboxes = serializers.SerializerMethodField(read_only=True)

class Meta:
model = models.MailDomain
Expand All @@ -68,6 +69,7 @@ class Meta:
"abilities",
"created_at",
"updated_at",
"count_mailboxes",
]
read_only_fields = [
"id",
Expand All @@ -76,6 +78,7 @@ class Meta:
"abilities",
"created_at",
"updated_at",
"count_mailboxes",
]

def get_abilities(self, domain) -> dict:
Expand All @@ -85,6 +88,10 @@ def get_abilities(self, domain) -> dict:
return domain.get_abilities(request.user)
return {}

def get_count_mailboxes(self, domain) -> int:
"""Return count of mailboxes for the domain."""
return domain.mailboxes.count()

def create(self, validated_data):
"""
Override create function to fire a request to dimail upon domain creation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def test_api_mail_domains__create_authenticated():
"created_at": domain.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": domain.updated_at.isoformat().replace("+00:00", "Z"),
"abilities": domain.get_abilities(user),
"count_mailboxes": 0,
}

# a new domain with status "pending" is created and authenticated user is the owner
Expand Down Expand Up @@ -185,6 +186,7 @@ def test_api_mail_domains__create_authenticated__dimail_failure():
"created_at": domain.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": domain.updated_at.isoformat().replace("+00:00", "Z"),
"abilities": domain.get_abilities(user),
"count_mailboxes": 0,
}

# a new domain with status "failed" is created and authenticated user is the owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_api_mail_domains__retrieve_authenticated_related():

domain = factories.MailDomainEnabledFactory()
factories.MailDomainAccessFactory(domain=domain, user=user)
factories.MailboxFactory.create_batch(10, domain=domain)

response = client.get(
f"/api/v1.0/mail-domains/{domain.slug}/",
Expand All @@ -85,4 +86,5 @@ def test_api_mail_domains__retrieve_authenticated_related():
"created_at": domain.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": domain.updated_at.isoformat().replace("+00:00", "Z"),
"abilities": domain.get_abilities(user),
"count_mailboxes": 10,
}
Loading