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

[#36] Multiple notifications sent for single To-Do item #37

Merged
merged 1 commit into from
Feb 20, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Multiple notifications sent for single To-Do item [#36](https://github.com/rokwire/wellness-building-block/issues/36)

## [1.3.0] - 2023-04-26
### Changed
- Updated Docker file (use alpine:3.17)
Expand Down
6 changes: 3 additions & 3 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ type Storage interface {
GetTodoEntriesWithCurrentDueTime(context storage.TransactionContext, dueTime time.Time) ([]model.TodoEntry, error)
GetTodoEntries(appID string, orgID string, userID string) ([]model.TodoEntry, error)
GetTodoEntriesForMigration() ([]model.TodoEntry, error)
GetTodoEntry(appID string, orgID string, userID string, id string) (*model.TodoEntry, error)
GetTodoEntry(context storage.TransactionContext, appID string, orgID string, userID string, id string) (*model.TodoEntry, error)
CreateTodoEntry(appID string, orgID string, userID string, todo *model.TodoEntry, messageIDs model.MessageIDs, entityID string) (*model.TodoEntry, error)
UpdateTodoEntry(appID string, orgID string, userID string, todo *model.TodoEntry, id string) (*model.TodoEntry, error)
UpdateTodoEntry(context storage.TransactionContext, appID string, orgID string, userID string, todo *model.TodoEntry, id string) (*model.TodoEntry, error)
UpdateTodoEntriesTaskTime(context storage.TransactionContext, ids []string, taskTime time.Time) error
DeleteTodoEntry(appID string, orgID string, userID string, id string) error
DeleteTodoEntry(context storage.TransactionContext, appID string, orgID string, userID string, id string) error
DeleteCompletedTodoEntries(appID string, orgID string, userID string) error

GetRings(appID string, orgID string, userID string) ([]model.Ring, error)
Expand Down
17 changes: 9 additions & 8 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (app *Application) getTodoEntries(appID string, orgID string, userID string
}

func (app *Application) getTodoEntry(appID string, orgID string, userID string, id string) (*model.TodoEntry, error) {
return app.storage.GetTodoEntry(appID, orgID, userID, id)
return app.storage.GetTodoEntry(nil, appID, orgID, userID, id)
}

func (app *Application) createTodoEntry(appID string, orgID string, userID string, todo *model.TodoEntry) (*model.TodoEntry, error) {
Expand Down Expand Up @@ -106,15 +106,15 @@ func (app *Application) createTodoEntry(appID string, orgID string, userID strin
func (app *Application) updateTodoEntry(appID string, orgID string, userID string, todo *model.TodoEntry, id string) (*model.TodoEntry, error) {
var updateTodoEntry *model.TodoEntry
err := app.storage.PerformTransaction(func(context storage.TransactionContext) error {
todoEntry, err := app.storage.GetTodoEntry(appID, orgID, userID, id)
todoEntry, err := app.storage.GetTodoEntry(context, appID, orgID, userID, id)
if err != nil {
log.Printf("Error on getting todo entry: %s", err)
}
if todoEntry.MessageIDs.DueDateMessageID != nil {
err = app.notifications.DeleteNotification(appID, orgID, *todoEntry.MessageIDs.DueDateMessageID)
if err != nil {
log.Printf("Error on delete notification with DueDateMessageID %s", *todoEntry.MessageIDs.DueDateMessageID)
//return err // Don't propagate the error. Just create the reminder.
return err
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func (app *Application) updateTodoEntry(appID string, orgID string, userID strin
err = app.notifications.DeleteNotification(appID, orgID, *todoEntry.MessageIDs.ReminderDateMessageID)
if err != nil {
log.Printf("Error on delete notification with ReminderDateMessageID %s", *todoEntry.MessageIDs.ReminderDateMessageID)
//return err // Don't propagate the error. Just create the reminder.
return err
}
}
if todo.ReminderDateTime != nil {
Expand All @@ -160,17 +160,18 @@ func (app *Application) updateTodoEntry(appID string, orgID string, userID strin

if err != nil {
log.Printf("Error on sending ReminderDateTime notification %s inbox message: %s", id, err)
//return err // Don't propagate the error. Just create the reminder.
return err
}

todo.MessageIDs.ReminderDateMessageID = reminderMsg
log.Printf("Sent ReminderDateTime notification %s successfully", id)
}
}

updateTodoEntry, err = app.storage.UpdateTodoEntry(appID, orgID, userID, todo, id)
updateTodoEntry, err = app.storage.UpdateTodoEntry(context, appID, orgID, userID, todo, id)
if err != nil {
log.Printf("Error on updating todo entry: %s", err)
return err
}

return nil
Expand All @@ -181,7 +182,7 @@ func (app *Application) updateTodoEntry(appID string, orgID string, userID strin
func (app *Application) deleteTodoEntry(appID string, orgID string, userID string, id string) error {

return app.storage.PerformTransaction(func(context storage.TransactionContext) error {
todoEntry, err := app.storage.GetTodoEntry(appID, orgID, userID, id)
todoEntry, err := app.storage.GetTodoEntry(context, appID, orgID, userID, id)
if err != nil {
log.Printf("Error on getting todo entry: %s", err)
}
Expand All @@ -199,7 +200,7 @@ func (app *Application) deleteTodoEntry(appID string, orgID string, userID strin
//return err // Don't propagate the error. Just create the reminder.
}
}
err = app.storage.DeleteTodoEntry(appID, orgID, userID, id)
err = app.storage.DeleteTodoEntry(context, appID, orgID, userID, id)
if err != nil {
log.Printf("Error on delete todo entry: %s", err)
}
Expand Down
15 changes: 8 additions & 7 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (sa *Adapter) GetTodoEntriesForMigration() ([]model.TodoEntry, error) {
}

// GetTodoEntry get a single todo entry
func (sa *Adapter) GetTodoEntry(appID string, orgID string, userID string, id string) (*model.TodoEntry, error) {
func (sa *Adapter) GetTodoEntry(context TransactionContext, appID string, orgID string, userID string, id string) (*model.TodoEntry, error) {
filter := bson.D{
primitive.E{Key: "org_id", Value: orgID},
primitive.E{Key: "app_id", Value: appID},
Expand All @@ -297,7 +297,7 @@ func (sa *Adapter) GetTodoEntry(appID string, orgID string, userID string, id st
}

var result []model.TodoEntry
err := sa.db.todoEntries.Find(filter, &result, &options.FindOptions{Sort: bson.D{{"name", 1}}})
err := sa.db.todoEntries.FindWithContext(context, filter, &result, &options.FindOptions{Sort: bson.D{{"name", 1}}})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func (sa *Adapter) CreateTodoEntry(appID string, orgID string, userID string, ca
}

// UpdateTodoEntry updates a todo entry
func (sa *Adapter) UpdateTodoEntry(appID string, orgID string, userID string, todo *model.TodoEntry, id string) (*model.TodoEntry, error) {
func (sa *Adapter) UpdateTodoEntry(context TransactionContext, appID string, orgID string, userID string, todo *model.TodoEntry, id string) (*model.TodoEntry, error) {

filter := bson.D{
primitive.E{Key: "app_id", Value: appID},
Expand All @@ -349,24 +349,25 @@ func (sa *Adapter) UpdateTodoEntry(appID string, orgID string, userID string, to
primitive.E{Key: "date_updated", Value: time.Now().UTC()},
}},
}
_, err := sa.db.todoEntries.UpdateOne(filter, update, nil)

_, err := sa.db.todoEntries.UpdateOneWithContext(context, filter, update, nil)
if err != nil {
log.Printf("error updating user defined todo entry: %s", err)
return nil, err
}

return sa.GetTodoEntry(appID, orgID, userID, id)
return sa.GetTodoEntry(nil, appID, orgID, userID, id)
}

// DeleteTodoEntry deletes a todo entry
func (sa *Adapter) DeleteTodoEntry(appID string, orgID string, userID string, id string) error {
func (sa *Adapter) DeleteTodoEntry(context TransactionContext, appID string, orgID string, userID string, id string) error {
filter := bson.D{
primitive.E{Key: "app_id", Value: appID},
primitive.E{Key: "org_id", Value: orgID},
primitive.E{Key: "user_id", Value: userID},
primitive.E{Key: "_id", Value: id}}

_, err := sa.db.todoEntries.DeleteOne(filter, nil)
_, err := sa.db.todoEntries.DeleteOneWithContext(context, filter, nil)
if err != nil {
log.Printf("error deleting todo entry: %s", err)
return err
Expand Down
31 changes: 31 additions & 0 deletions driven/storage/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (collWrapper *collectionWrapper) FindOne(filter interface{}, result interfa
}

func (collWrapper *collectionWrapper) FindOneWithContext(ctx context.Context, filter interface{}, result interface{}, findOptions *options.FindOneOptions) error {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand All @@ -96,6 +100,10 @@ func (collWrapper *collectionWrapper) ReplaceOne(filter interface{}, replacement
}

func (collWrapper *collectionWrapper) ReplaceOneWithContext(ctx context.Context, filter interface{}, replacement interface{}, replaceOptions *options.ReplaceOptions) error {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand Down Expand Up @@ -128,6 +136,10 @@ func (collWrapper *collectionWrapper) InsertOne(data interface{}) (interface{},
}

func (collWrapper *collectionWrapper) InsertOneWithContext(ctx context.Context, data interface{}) (interface{}, error) {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)

ins, err := collWrapper.coll.InsertOne(ctx, data)
Expand All @@ -145,6 +157,10 @@ func (collWrapper *collectionWrapper) InsertMany(documents []interface{}, opts *
}

func (collWrapper *collectionWrapper) InsertManyWithContext(ctx context.Context, documents []interface{}, opts *options.InsertManyOptions) (*mongo.InsertManyResult, error) {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand All @@ -165,6 +181,9 @@ func (collWrapper *collectionWrapper) DeleteManyWithContext(ctx context.Context,
}

func (collWrapper *collectionWrapper) DeleteManyWithParams(ctx context.Context, filter interface{}, opts *options.DeleteOptions, timeout *time.Duration) (*mongo.DeleteResult, error) {
if ctx == nil {
ctx = context.Background()
}

//set timeout
if timeout == nil {
Expand All @@ -187,6 +206,10 @@ func (collWrapper *collectionWrapper) DeleteOne(filter interface{}, opts *option
}

func (collWrapper *collectionWrapper) DeleteOneWithContext(ctx context.Context, filter interface{}, opts *options.DeleteOptions) (*mongo.DeleteResult, error) {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand All @@ -203,6 +226,10 @@ func (collWrapper *collectionWrapper) UpdateOne(filter interface{}, update inter
}

func (collWrapper *collectionWrapper) UpdateOneWithContext(ctx context.Context, filter interface{}, update interface{}, opts *options.UpdateOptions) (*mongo.UpdateResult, error) {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand Down Expand Up @@ -240,6 +267,10 @@ func (collWrapper *collectionWrapper) FindOneAndUpdate(filter interface{}, updat
}

func (collWrapper *collectionWrapper) FindOneAndUpdateWithContext(ctx context.Context, filter interface{}, update interface{}, result interface{}, opts *options.FindOneAndUpdateOptions) error {
if ctx == nil {
ctx = context.Background()
}

ctx, cancel := context.WithTimeout(ctx, collWrapper.database.mongoTimeout)
defer cancel()

Expand Down
Loading