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

[back] feat(notifications): u#3207 notify when a story has been deleted #535

Merged
merged 1 commit into from
Oct 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

from taiga.notifications import services as notifications_services
from taiga.stories.stories.models import Story
from taiga.stories.stories.notifications.content import StoryStatusChangeNotificationContent
from taiga.stories.stories.notifications.content import (
StoryDeleteNotificationContent,
StoryStatusChangeNotificationContent,
)
from taiga.users.models import User

STORIES_STATUS_CHANGE = "stories.status_change"
STORIES_DELETE = "stories.delete"


async def notify_when_story_status_change(story: Story, status: str, emitted_by: User) -> None:
Expand All @@ -33,3 +37,24 @@ async def notify_when_story_status_change(story: Story, status: str, emitted_by:
status=status,
),
)


async def notify_when_story_is_deleted(story: Story, emitted_by: User) -> None:
"""
Emit notification when a story is deleted
"""
notified_users = set()
if story.created_by:
notified_users.add(story.created_by)
notified_users.discard(emitted_by)

await notifications_services.notify_users(
type=STORIES_DELETE,
emitted_by=emitted_by,
notified_users=notified_users,
content=StoryDeleteNotificationContent(
projects=story.project,
story=story,
deleted_by=emitted_by,
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
from taiga.users.serializers.nested import UserNestedSerializer


class StoryDeleteNotificationContent(BaseModel):
projects: ProjectLinkNestedSerializer
story: StoryNestedSerializer
deleted_by: UserNestedSerializer

class Config:
orm_mode = True


class StoryStatusChangeNotificationContent(BaseModel):
projects: ProjectLinkNestedSerializer
story: StoryNestedSerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from taiga.stories.stories.serializers import ReorderStoriesSerializer, StoryDetailSerializer, StorySummarySerializer
from taiga.stories.stories.serializers import services as serializers_services
from taiga.stories.stories.services import exceptions as ex
from taiga.users.models import AnyUser, User
from taiga.users.models import User
from taiga.workflows import repositories as workflows_repositories
from taiga.workflows.models import Workflow, WorkflowStatus

Expand Down Expand Up @@ -391,12 +391,15 @@ async def _calculate_next_order(status_id: UUID) -> Decimal:
##########################################################


async def delete_story(story: Story, deleted_by: AnyUser) -> bool:
async def delete_story(story: Story, deleted_by: User) -> bool:
deleted = await stories_repositories.delete_stories(filters={"id": story.id})
if deleted > 0:
await stories_events.emit_event_when_story_is_deleted(
project=story.project, ref=story.ref, deleted_by=deleted_by
)
await stories_notifications.notify_when_story_is_deleted(
story=story,
emitted_by=deleted_by,
)
return True

return False
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,17 @@ async def test_delete_story_fail():
with (
patch("taiga.stories.stories.services.stories_repositories", autospec=True) as fake_story_repo,
patch("taiga.stories.stories.services.stories_events", autospec=True) as fake_stories_events,
patch("taiga.stories.stories.services.stories_notifications", autospec=True) as fake_notifications,
):
fake_story_repo.delete_stories.return_value = 0

await services.delete_story(story=story, deleted_by=user)
fake_stories_events.emit_event_when_story_is_deleted.assert_not_awaited()
assert not (await services.delete_story(story=story, deleted_by=user))

fake_story_repo.delete_stories.assert_awaited_once_with(
filters={"id": story.id},
)
fake_stories_events.emit_event_when_story_is_deleted.assert_not_awaited()
fake_notifications.notify_when_story_is_deleted.assert_not_awaited()


async def test_delete_story_ok():
Expand All @@ -810,16 +813,18 @@ async def test_delete_story_ok():
with (
patch("taiga.stories.stories.services.stories_repositories", autospec=True) as fake_story_repo,
patch("taiga.stories.stories.services.stories_events", autospec=True) as fake_stories_events,
patch("taiga.stories.stories.services.stories_notifications", autospec=True) as fake_notifications,
):
fake_story_repo.delete_stories.return_value = 1

await services.delete_story(story=story, deleted_by=user)
fake_stories_events.emit_event_when_story_is_deleted.assert_awaited_once_with(
project=story.project, ref=story.ref, deleted_by=user
)
assert await services.delete_story(story=story, deleted_by=user)
fake_story_repo.delete_stories.assert_awaited_once_with(
filters={"id": story.id},
)
fake_stories_events.emit_event_when_story_is_deleted.assert_awaited_once_with(
project=story.project, ref=story.ref, deleted_by=user
)
fake_notifications.notify_when_story_is_deleted.assert_awaited_once_with(story=story, emitted_by=user)


#######################################################
Expand Down
Loading