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 6, 2023
1 parent 843d242 commit e387f71
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 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 @@ -31,14 +33,37 @@ async def notify_when_story_status_change(story: Story, status: str, emitted_by:
emitted_by=emitted_by,
notified_users=notified_users,
content=StoryStatusChangeNotificationContent(
projects=story.project,
project=story.project,
story=story,
changed_by=emitted_by,
status=status,
),
)


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 All @@ -53,7 +78,7 @@ async def notify_when_story_is_deleted(story: Story, emitted_by: User) -> None:
emitted_by=emitted_by,
notified_users=notified_users,
content=StoryDeleteNotificationContent(
projects=story.project,
project=story.project,
story=story,
deleted_by=emitted_by,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


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

Expand All @@ -21,10 +21,21 @@ class Config:


class StoryStatusChangeNotificationContent(BaseModel):
projects: ProjectLinkNestedSerializer
project: ProjectLinkNestedSerializer
story: StoryNestedSerializer
changed_by: UserNestedSerializer
status: str

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 Expand Up @@ -706,6 +709,7 @@ async def test_reorder_stories_ok():
)
fake_stories_events.emit_when_stories_are_reordered.assert_awaited_once()
assert fake_notifications.notify_when_story_status_change.await_count == 3
assert fake_notifications.notify_when_story_workflow_change.assert_awaited()


async def test_reorder_story_workflowstatus_does_not_exist():
Expand Down

0 comments on commit e387f71

Please sign in to comment.