Skip to content

Commit

Permalink
- ajout / suppression d'assignees dans les cartes
Browse files Browse the repository at this point in the history
- les tests qui vont avec
  • Loading branch information
Christophe Ninucci committed Dec 19, 2023
1 parent bb36881 commit 80464fb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
16 changes: 15 additions & 1 deletion activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Activity struct {
UserID UserID `bson:"userId,omitempty" json:"userId,omitempty"`
Username Username `bson:"username,omitempty" json:"username,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
AssigneeID UserID `bson:"assigneeId,omitempty" json:"assigneeId,omitempty"`
MemberID UserID `bson:"memberId,omitempty" json:"memberId,omitempty"`
ActivityType string `bson:"activityType,omitempty" json:"activityType,omitempty"`
ActivityTypeID string `bson:"activityTypeId,omitempty" json:"activityTypeId,omitempty"`
Expand Down Expand Up @@ -105,6 +106,19 @@ func newActivityCardJoinMember(userID UserID, username Username, memberID UserID
}
}

func newActivityCardJoinAssignee(userID UserID, username Username, memberID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Username: username,
AssigneeID: memberID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
SwimlaneID: swimlaneID,
ActivityType: "joinAssignee",
}
}

func newActivityCardUnjoinMember(userID UserID, username Username, memberID UserID, boardID BoardID, listID ListID, cardID CardID, swimlaneID SwimlaneID) Activity {
return Activity{
UserID: userID,
Expand All @@ -122,7 +136,7 @@ func newActivityCardUnjoinAssignee(userID UserID, username Username, assigneeID
return Activity{
UserID: userID,
Username: username,
MemberID: assigneeID,
AssigneeID: assigneeID,
BoardID: boardID,
CardID: cardID,
ListID: listID,
Expand Down
65 changes: 61 additions & 4 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,19 @@ func (wekan *Wekan) RemoveAssigneeFromCard(ctx context.Context, card Card, user
}

stats, err := wekan.db.Collection("cards").UpdateOne(ctx, bson.M{
"_id": card.ID,
"_id": card.ID,
"assignees": bson.M{"$type": 4},
}, bson.M{
"$pull": bson.M{
"assignees": assignee.ID,
},
})
if stats.ModifiedCount == 0 {
return NothingDoneError{}
}
if err != nil {
return UnexpectedMongoError{err}
}
if stats.ModifiedCount == 0 {
return NothingDoneError{}
}

activity := newActivityCardUnjoinAssignee(user.ID, assignee.Username, assignee.ID, card.BoardID, card.ListID, card.ID, card.SwimlaneID)
_, err = wekan.insertActivity(ctx, activity)
Expand Down Expand Up @@ -552,6 +553,62 @@ func (wekan *Wekan) AddSelfMemberToCard(ctx context.Context, card Card, member U
return wekan.AddMemberToCard(ctx, card, member, member)
}

func (wekan *Wekan) AddAssigneeToCard(ctx context.Context, card Card, user User, assignee User) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
}
board, err := wekan.GetBoardFromID(ctx, card.BoardID)
if err != nil {
return err
}

if !board.UserIsActiveMember(assignee) {
return ForbiddenOperationError{
UserIsNotMemberError{assignee.ID},
}
}

if !board.UserIsActiveMember(user) {
return ForbiddenOperationError{
UserIsNotMemberError{user.ID},
}
}

_, err = wekan.db.Collection("cards").UpdateOne(ctx, bson.M{
"_id": card.ID,
"assignees": nil,
}, bson.M{
"$set": bson.M{"assignees": bson.A{}},
})

if err != nil {
return UnexpectedMongoError{err}
}

stats, err := wekan.db.Collection("cards").UpdateOne(ctx, bson.M{
"_id": card.ID,
}, bson.M{
"$addToSet": bson.M{
"assignees": assignee.ID,
},
})

if err != nil {
return UnexpectedMongoError{err}
}
if stats.ModifiedCount == 0 {
return NothingDoneError{}
}

activity := newActivityCardJoinAssignee(user.ID, assignee.Username, assignee.ID, card.BoardID, card.ListID, card.ID, card.SwimlaneID)
_, err = wekan.insertActivity(ctx, activity)
if err != nil {
return UnexpectedMongoError{err: err}
}

return nil
}

func (wekan *Wekan) AddMemberToCard(ctx context.Context, card Card, user User, member User) error {
if err := wekan.AssertPrivileged(ctx); err != nil {
return err
Expand Down
54 changes: 54 additions & 0 deletions users_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,60 @@ func TestUsers_EnsureMemberOutOfCard(t *testing.T) {
ass.NotContains(actualCard.Members, member.ID)
}

func TestUsers_EnsureAssigneeOutOfCard(t *testing.T) {
ass := assert.New(t)
// GIVEN
board, swimlanes, lists := createTestBoard(t, "", 1, 1)
user := createTestUser(t, "User")
assignee := createTestUser(t, "assignee")
card := createTestCard(t, user.ID, &board.ID, &(swimlanes[0].ID), &(lists[0].ID))
err := wekan.AddMemberToBoard(ctx, board.ID, BoardMember{UserID: assignee.ID, IsActive: true})
ass.NoError(err)

err = wekan.AddAssigneeToCard(ctx, card, assignee, assignee)
ass.NoError(err)

// WHEN
modified, err := wekan.EnsureAssigneeOutOfCard(ctx, card, assignee, assignee)
ass.Nil(err)
activities, err := wekan.SelectActivitiesFromCardID(ctx, card.ID)
ass.Nil(err)

// THEN
ass.Len(activities, 3)
ass.True(modified)
ass.Nil(err)
actualCard, err := card.ID.GetDocument(ctx, &wekan)
ass.NotContains(actualCard.Members, assignee.ID)
}

func TestUsers_EnsureAssigneeOutOfCard_WhenAssigneesPropertyExists(t *testing.T) {
ass := assert.New(t)
// GIVEN
board, swimlanes, lists := createTestBoard(t, "", 1, 1)
user := createTestUser(t, "User")
assignee := createTestUser(t, "assignee")
card := createTestCard(t, user.ID, &board.ID, &(swimlanes[0].ID), &(lists[0].ID))
err := wekan.AddMemberToBoard(ctx, board.ID, BoardMember{UserID: assignee.ID, IsActive: true})
ass.NoError(err)
err = wekan.AddAssigneeToCard(ctx, card, assignee, assignee)
ass.NoError(err)
_, err = wekan.EnsureAssigneeOutOfCard(ctx, card, assignee, assignee)
ass.NoError(err)

// WHEN
err = wekan.AddAssigneeToCard(ctx, card, assignee, assignee)
ass.NoError(err)
activities, err := wekan.SelectActivitiesFromCardID(ctx, card.ID)
ass.Nil(err)

// THEN
ass.Len(activities, 4)
ass.Nil(err)
actualCard, err := card.ID.GetDocument(ctx, &wekan)
ass.NotContains(actualCard.Members, assignee.ID)
}

func TestUsers_EnsureMemberOutOfCard_WhenUserIsNotBoardMember(t *testing.T) {
ass := assert.New(t)
// GIVEN
Expand Down

0 comments on commit 80464fb

Please sign in to comment.