diff --git a/notification/notification.go b/notification/notification.go index 4421ff81..473c7faf 100644 --- a/notification/notification.go +++ b/notification/notification.go @@ -6,11 +6,13 @@ import ( "strings" "time" + "github.com/Gleipnir-Technology/bob/dialect/psql" + "github.com/Gleipnir-Technology/bob/dialect/psql/im" "github.com/Gleipnir-Technology/nidus-sync/db" enums "github.com/Gleipnir-Technology/nidus-sync/db/enums" "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/debug" - "github.com/aarondl/opt/omit" + //"github.com/aarondl/opt/omit" "github.com/aarondl/opt/omitnull" "github.com/rs/zerolog/log" ) @@ -45,15 +47,32 @@ func ClearOauth(ctx context.Context, user *models.User) { func NotifyOauthInvalid(ctx context.Context, user *models.User) { msg := "Oauth token invalidated" - notificationSetter := models.NotificationSetter{ - Created: omit.From(time.Now()), - Message: omit.From(msg), - Link: omit.From(NotificationPathOauthReset), - Type: omit.From(enums.NotificationtypeOauthTokenInvalidated), - } - err := user.InsertUserNotifications(ctx, db.PGInstance.BobDB, ¬ificationSetter) + _, err := psql.Insert( + im.Into("notification", "created", "id", "link", "message", "resolved_at", "type", "user_id"), + im.Values( + psql.Arg(time.Now()), + psql.Raw("DEFAULT"), + psql.Arg(NotificationPathOauthReset), + psql.Arg(msg), + psql.Raw("NULL"), + psql.Arg(enums.NotificationtypeOauthTokenInvalidated), + psql.Arg(user.ID), + ), + //im.OnConflict("user_id", "link").DoNothing(), + //im.OnConflictOnConstraint("unique_user_link_not_resolved").DoNothing(), + im.OnConflict("user_id", "link").Where("resolved_at IS NULL").DoNothing(), + ).Exec(ctx, db.PGInstance.BobDB) + /* + notificationSetter := models.NotificationSetter{ + Created: omit.From(time.Now()), + Message: omit.From(msg), + Link: omit.From(NotificationPathOauthReset), + Type: omit.From(enums.NotificationtypeOauthTokenInvalidated), + } + err := user.InsertUserNotifications(ctx, db.PGInstance.BobDB, ¬ificationSetter) + */ if err != nil { - if strings.HasPrefix(err.Error(), "ERROR: duplicate key value violates unique constraint") { + if strings.Contains(err.Error(), "ERROR: duplicate key value violates unique constraint") { log.Info().Str("msg", msg).Int("user_id", int(user.ID)).Msg("Refusing to add another notification with the same type") return }