Skip to content

Commit

Permalink
feat(notifications): u#3985 notify when a story has changed workflow …
Browse files Browse the repository at this point in the history
…(and status)
  • Loading branch information
yamila-moreno committed Nov 7, 2023
1 parent 5955497 commit 9fa010e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from taiga.stories.stories.notifications.content import (
StoryDeleteNotificationContent,
StoryStatusChangeNotificationContent,
StoryWorkflowChangeNotificationContent,
)
from taiga.users.models import User

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


Expand All @@ -39,6 +41,29 @@ async def notify_when_story_status_change(story: Story, status: str, emitted_by:
)


async def notify_when_story_workflow_change(story: Story, workflow: str, status: str, emitted_by: User) -> None:
"""
Emit notification when a story workflow changes
"""
notified_users = {u async for u in story.assignees.all()}
if story.created_by:
notified_users.add(story.created_by)
notified_users.discard(emitted_by)

await notifications_services.notify_users(
type=STORIES_WORKFLOW_CHANGE,
emitted_by=emitted_by,
notified_users=notified_users,
content=StoryWorkflowChangeNotificationContent(
project=story.project,
story=story,
changed_by=emitted_by,
workflow=workflow,
status=status,
),
)


async def notify_when_story_is_deleted(story: Story, emitted_by: User) -> None:
"""
Emit notification when a story is deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ class StoryStatusChangeNotificationContent(BaseModel):

class Config:
orm_mode = True


class StoryWorkflowChangeNotificationContent(BaseModel):
project: ProjectLinkNestedSerializer
story: StoryNestedSerializer
changed_by: UserNestedSerializer
status: str
workflow: str

class Config:
orm_mode = True
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,14 @@ async def update_story(
)

# Emit notifications
if "status" in update_values:
if "workflow" in update_values:
await stories_notifications.notify_when_story_workflow_change(
story=story,
workflow=update_values["workflow"].name,
status=update_values["status"].name,
emitted_by=updated_by,
)
elif "status" in update_values:
await stories_notifications.notify_when_story_status_change(
story=story,
status=update_values["status"].name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ async def test_update_story_ok():
updates_attrs=[*values],
)
fake_notifications.notify_when_story_status_change.assert_not_awaited()
fake_notifications.notify_when_story_workflow_change.assert_not_awaited()

assert updated_story == detailed_story

Expand Down Expand Up @@ -324,7 +325,8 @@ async def test_update_story_workflow_ok():
updates_attrs=[*values],
)

fake_notifications.notify_when_story_status_change.assert_awaited_once()
fake_notifications.notify_when_story_status_change.assert_not_awaited()
fake_notifications.notify_when_story_workflow_change.assert_awaited_once()


async def test_update_story_error_wrong_version():
Expand Down Expand Up @@ -365,6 +367,7 @@ async def test_update_story_error_wrong_version():
fake_get_story_detail.assert_not_awaited()
fake_stories_events.emit_event_when_story_is_updated.assert_not_awaited()
fake_notifications.notify_when_story_status_change.assert_not_awaited()
fake_notifications.notify_when_story_workflow_change.assert_not_awaited()


#######################################################
Expand Down

0 comments on commit 9fa010e

Please sign in to comment.