Skip to content

Commit

Permalink
add activity on card creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Ninucci committed Nov 10, 2023
1 parent 4901b1a commit 63c9801
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
32 changes: 22 additions & 10 deletions activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

type ActivityID string
Expand Down Expand Up @@ -124,6 +123,18 @@ func newActivityAddedLabel(userID UserID, boardLabelID BoardLabelID, boardID Boa
}
}

func (wekan *Wekan) newActivityCreateCardFromCard(ctx context.Context, card Card) (Activity, error) {
list, err := wekan.GetListFromID(ctx, card.ListID)
if err != nil {
return Activity{}, err
}
swimlane, err := wekan.GetSwimlaneFromID(ctx, card.SwimlaneID)
if err != nil {
return Activity{}, err
}
return newActivityCreateCard(card.UserID, list, card, swimlane), nil
}

func newActivityCreateCard(userID UserID, list List, card Card, swimlane Swimlane) Activity {
return Activity{
UserID: userID,
Expand All @@ -150,19 +161,20 @@ func (wekan *Wekan) insertActivity(ctx context.Context, activity Activity) (Acti
return insertable, nil
}

func (wekan *Wekan) GetActivityFromID(ctx context.Context, activityID ActivityID) (Activity, error) {
var activity Activity
err := wekan.db.Collection("activities").FindOne(ctx, bson.M{"_id": activityID}).Decode(&activity)
func (wekan *Wekan) GetActivitiesFromCardID(ctx context.Context, cardID CardID) ([]Activity, error) {
var activities []Activity
cur, err := wekan.db.Collection("activities").Find(ctx, bson.M{"cardId": cardID})
if err != nil {
if err == mongo.ErrNoDocuments {
return Activity{}, UnknownActivityError{string(activityID)}
}
return Activity{}, UnexpectedMongoError{err}
return nil, UnexpectedMongoError{err}
}
return activity, nil
err = cur.All(ctx, &activities)
if err != nil {
return nil, UnexpectedMongoError{err}
}
return activities, nil
}

func (wekan *Wekan) SelectActivityFromID(ctx context.Context, activityID ActivityID) (Activity, error) {
func (wekan *Wekan) GetActivityFromID(ctx context.Context, activityID ActivityID) (Activity, error) {
var activity Activity
err := wekan.db.Collection("activities").FindOne(ctx, bson.M{"_id": activityID}).Decode(&activity)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion activities_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestActivities_insertActivity_whenEverythingsFine(t *testing.T) {
ass.Nil(err)

// THEN
selectedActivity, err := wekan.SelectActivityFromID(ctx, insertedActivity.ID)
selectedActivity, err := wekan.GetActivityFromID(ctx, insertedActivity.ID)
ass.Nil(err)
ass.Equal(insertedActivity, selectedActivity)
}
Expand Down
8 changes: 7 additions & 1 deletion cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ func (wekan *Wekan) InsertCard(ctx context.Context, card Card) error {
if _, err := wekan.db.Collection("cards").InsertOne(ctx, card); err != nil {
return UnexpectedMongoError{err}
}
return nil

activity, err := wekan.newActivityCreateCardFromCard(ctx, card)
if err != nil {
return err
}
_, err = wekan.insertActivity(ctx, activity)
return err
}

func (wekan *Wekan) AddLabelToCard(ctx context.Context, cardID CardID, labelID BoardLabelID) error {
Expand Down
17 changes: 17 additions & 0 deletions cards_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ func TestCards_InsertCard_withGetCardFromID(t *testing.T) {
ass.Equal(card, actualCard)
}

func TestCards_InsertCard_withGetActivitiesFromCardID(t *testing.T) {
ass := assert.New(t)

// GIVEN
card := createTestCard(t, createTestUser(t, "").ID, nil, nil, nil)
// WHEN
actualActivities, err := wekan.GetActivitiesFromCardID(ctx, card.ID)
ass.Nil(err)

// THEN
ass.Len(actualActivities, 1)
ass.Equal(card.ID, actualActivities[0].CardID)
ass.Equal(card.BoardID, actualActivities[0].BoardID)
ass.Equal(card.ListID, actualActivities[0].ListID)
ass.Equal(card.SwimlaneID, actualActivities[0].SwimlaneID)
}

func TestCards_GetCardsFromID_whenCardDoesntExists(t *testing.T) {
_, err := wekan.GetCardFromID(ctx, CardID(t.Name()+"CatchMeIfYouCan"))
assert.IsType(t, CardNotFoundError{}, err)
Expand Down
2 changes: 1 addition & 1 deletion errors_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestErrors_UpstreamDeadlineExceeded(t *testing.T) {
errs = append(errs, err)
_, err = badWekan.SelectActivitiesFromQuery(ctx, bson.M{})
errs = append(errs, err)
_, err = badWekan.SelectActivityFromID(ctx, "")
_, err = badWekan.GetActivityFromID(ctx, "")
errs = append(errs, err)
_, err = badWekan.SelectBoardsFromMemberID(ctx, "")
errs = append(errs, err)
Expand Down

0 comments on commit 63c9801

Please sign in to comment.