Open platform to collect and prioritize feedback
refactor: add/remove supporters (#320)
Guilherme Oenning59d03f774c04
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.goindex c8b98526a..59b4e378f 100644--- a/app/pkg/dbx/dbx.go+++ b/app/pkg/dbx/dbx.go@@ -107,7 +107,7 @@ type Trx struct {var formatter = strings.NewReplacer("\t", "", "\n", " ")// Execute given SQL command-func (trx Trx) Execute(command string, args ...interface{}) error {+func (trx Trx) Execute(command string, args ...interface{}) (int64, error) {if trx.logger.IsEnabled(log.DEBUG) {command = formatter.Replace(command)start := time.Now()@@ -116,8 +116,11 @@ func (trx Trx) Execute(command string, args ...interface{}) error {}()}- _, err := trx.tx.Exec(command, args...)- return err+ result, err := trx.tx.Exec(command, args...)+ if err != nil {+ return 0, err+ }+ return result.RowsAffected()}// Scalar returns first row and first columndiff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.goindex c355bd28f..663b93704 100644--- a/app/storage/postgres/idea.go+++ b/app/storage/postgres/idea.go@@ -338,8 +338,8 @@ func (s *IdeaStorage) GetCommentsByIdea(number int) ([]*models.Comment, error) {// Update given ideafunc (s *IdeaStorage) Update(number int, title, description string) (*models.Idea, error) {- err := s.trx.Execute(`UPDATE ideas SET title = $1, slug = $2, description = $3- WHERE number = $4 AND tenant_id = $5`, title, slug.Make(title), description, number, s.tenant.ID)+ _, err := s.trx.Execute(`UPDATE ideas SET title = $1, slug = $2, description = $3+ WHERE number = $4 AND tenant_id = $5`, title, slug.Make(title), description, number, s.tenant.ID)if err != nil {return nil, err}@@ -426,8 +426,10 @@ func (s *IdeaStorage) GetCommentByID(id int) (*models.Comment, error) {// UpdateComment with given ID and contentfunc (s *IdeaStorage) UpdateComment(id int, content string) error {- return s.trx.Execute(`UPDATE comments SET content = $1, edited_on = $2, edited_by_id = $3- WHERE id = $4 AND tenant_id = $5`, content, time.Now(), s.user.ID, id, s.tenant.ID)+ _, err := s.trx.Execute(`+ UPDATE comments SET content = $1, edited_on = $2, edited_by_id = $3+ WHERE id = $4 AND tenant_id = $5`, content, time.Now(), s.user.ID, id, s.tenant.ID)+ return err}// AddSupporter adds user to idea list of supporters@@ -441,23 +443,19 @@ func (s *IdeaStorage) AddSupporter(number, userID int) error {return nil}- alreadySupported, err := s.trx.Exists("SELECT 1 FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3", userID, idea.ID, s.tenant.ID)- if err != nil {- return err- }-- if alreadySupported {- return nil- }+ rows, err := s.trx.Execute(+ `INSERT INTO idea_supporters (tenant_id, user_id, idea_id, created_on) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,+ s.tenant.ID, userID, idea.ID, time.Now())- if err := s.trx.Execute(`UPDATE ideas SET supporters = supporters + 1 WHERE id = $1 AND tenant_id = $2`, idea.ID, s.tenant.ID); err != nil {+ if err != nil {return err}- if err := s.trx.Execute(- `INSERT INTO idea_supporters (tenant_id, user_id, idea_id, created_on) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,- s.tenant.ID, userID, idea.ID, time.Now()); err != nil {- return err+ if rows == 1 {+ _, err := s.trx.Execute(`UPDATE ideas SET supporters = supporters + 1 WHERE id = $1 AND tenant_id = $2`, idea.ID, s.tenant.ID)+ if err != nil {+ return err+ }}return s.internalAddSubscriber(number, userID, false)@@ -474,20 +472,15 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {return nil}- didSupport, err := s.trx.Exists("SELECT 1 FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3", userID, idea.ID, s.tenant.ID)- if err != nil {- return err- }-- if !didSupport {- return nil- }+ rows, err := s.trx.Execute(`DELETE FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3`, userID, idea.ID, s.tenant.ID)- if err := s.trx.Execute(`UPDATE ideas SET supporters = supporters - 1 WHERE id = $1 AND tenant_id = $2`, idea.ID, s.tenant.ID); err != nil {- return err+ if rows == 1 {+ _, err := s.trx.Execute(`UPDATE ideas SET supporters = supporters - 1 WHERE id = $1 AND tenant_id = $2`, idea.ID, s.tenant.ID)+ if err != nil {+ return err+ }}-- return s.trx.Execute(`DELETE FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3`, userID, idea.ID, s.tenant.ID)+ return err}// AddSubscriber adds user to the idea list of subscribers@@ -506,11 +499,12 @@ func (s *IdeaStorage) internalAddSubscriber(number, userID int, force bool) erroconflict = "(user_id, idea_id) DO UPDATE SET status = $5, updated_on = $4"}- return s.trx.Execute(fmt.Sprintf(`+ _, err = s.trx.Execute(fmt.Sprintf(`INSERT INTO idea_subscribers (tenant_id, user_id, idea_id, created_on, updated_on, status)VALUES ($1, $2, $3, $4, $4, $5) ON CONFLICT %s`, conflict),s.tenant.ID, userID, idea.ID, time.Now(), models.SubscriberActive,)+ return err}// RemoveSubscriber removes user from idea list of subscribers@@ -520,12 +514,13 @@ func (s *IdeaStorage) RemoveSubscriber(number, userID int) error {return err}- return s.trx.Execute(`+ _, err = s.trx.Execute(`INSERT INTO idea_subscribers (tenant_id, user_id, idea_id, created_on, updated_on, status)VALUES ($1, $2, $3, $4, $4, 0) ON CONFLICT (user_id, idea_id)DO UPDATE SET status = 0, updated_on = $4`,s.tenant.ID, userID, idea.ID, time.Now(),)+ return err}// GetActiveSubscribers based on input and settings@@ -609,11 +604,13 @@ func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) eif idea.Status == status && idea.Response != nil {respondedOn = idea.Response.RespondedOn}- return s.trx.Execute(`++ _, err = s.trx.Execute(`UPDATE ideasSET response = $3, original_id = NULL, response_date = $4, response_user_id = $5, status = $6WHERE id = $1 and tenant_id = $2`, idea.ID, s.tenant.ID, text, respondedOn, userID, status)+ return err}// MarkAsDuplicate set idea as a duplicate of another idea@@ -643,11 +640,12 @@ func (s *IdeaStorage) MarkAsDuplicate(number, originalNumber, userID int) error}}- return s.trx.Execute(`+ _, err = s.trx.Execute(`UPDATE ideasSET response = '', original_id = $3, response_date = $4, response_user_id = $5, status = $6WHERE id = $1 and tenant_id = $2`, idea.ID, s.tenant.ID, original.ID, respondedOn, userID, models.IdeaDuplicate)+ return err}// IsReferenced returns true if another idea is referencing given ideadiff --git a/app/storage/postgres/notifications.go b/app/storage/postgres/notifications.goindex e6018a467..2a066eb08 100644--- a/app/storage/postgres/notifications.go+++ b/app/storage/postgres/notifications.go@@ -72,10 +72,11 @@ func (s *NotificationStorage) MarkAsRead(id int) error {if s.user == nil {return nil}- return s.trx.Execute(`+ _, err := s.trx.Execute(`UPDATE notifications SET read = true, updated_on = $1WHERE id = $2 AND tenant_id = $3 AND user_id = $4 AND read = false`, time.Now(), id, s.tenant.ID, s.user.ID)+ return err}// MarkAllAsRead of current user@@ -83,10 +84,11 @@ func (s *NotificationStorage) MarkAllAsRead() error {if s.user == nil {return nil}- return s.trx.Execute(`+ _, err := s.trx.Execute(`UPDATE notifications SET read = true, updated_on = $1WHERE tenant_id = $2 AND user_id = $3 AND read = false`, time.Now(), s.tenant.ID, s.user.ID)+ return err}// GetActiveNotifications returns all unread notifications and last 30 days of read notificationsdiff --git a/app/storage/postgres/tag.go b/app/storage/postgres/tag.goindex ff164cab8..71de47ac7 100644--- a/app/storage/postgres/tag.go+++ b/app/storage/postgres/tag.go@@ -83,7 +83,7 @@ func (s *TagStorage) GetBySlug(slug string) (*models.Tag, error) {func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*models.Tag, error) {tagSlug := slug.Make(name)- err := s.trx.Execute(`UPDATE tags SET name = $1, slug = $2, color = $3, is_public = $4+ _, err := s.trx.Execute(`UPDATE tags SET name = $1, slug = $2, color = $3, is_public = $4WHERE id = $5 AND tenant_id = $6`, name, tagSlug, color, isPublic, tagID, s.tenant.ID)if err != nil {return nil, err@@ -94,11 +94,12 @@ func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*mode// Delete a tag by its idfunc (s *TagStorage) Delete(tagID int) error {- err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)+ _, err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)if err != nil {return err}- return s.trx.Execute(`DELETE FROM tags WHERE id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)+ _, err = s.trx.Execute(`DELETE FROM tags WHERE id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)+ return err}// AssignTag adds a tag to an idea@@ -112,18 +113,20 @@ func (s *TagStorage) AssignTag(tagID, ideaID, userID int) error {return nil}- return s.trx.Execute(+ _, err = s.trx.Execute(`INSERT INTO idea_tags (tag_id, idea_id, created_on, created_by_id, tenant_id) VALUES ($1, $2, $3, $4, $5)`,tagID, ideaID, time.Now(), userID, s.tenant.ID,)+ return err}// UnassignTag removes a tag from an ideafunc (s *TagStorage) UnassignTag(tagID, ideaID int) error {- return s.trx.Execute(+ _, err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND idea_id = $2 AND tenant_id = $3`,tagID, ideaID, s.tenant.ID,)+ return err}// GetAssigned returns all tags assigned to given ideadiff --git a/app/storage/postgres/tenant.go b/app/storage/postgres/tenant.goindex 3d7ac7fbf..93d960f2d 100644--- a/app/storage/postgres/tenant.go+++ b/app/storage/postgres/tenant.go@@ -132,7 +132,8 @@ func (s *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {// UpdateSettings of given tenantfunc (s *TenantStorage) UpdateSettings(settings *models.UpdateTenantSettings) error {query := "UPDATE tenants SET name = $1, invitation = $2, welcome_message = $3, cname = $4 WHERE id = $5"- return s.trx.Execute(query, settings.Title, settings.Invitation, settings.WelcomeMessage, settings.CNAME, s.current.ID)+ _, err := s.trx.Execute(query, settings.Title, settings.Invitation, settings.WelcomeMessage, settings.CNAME, s.current.ID)+ return err}// IsSubdomainAvailable returns true if subdomain is available to use@@ -150,7 +151,8 @@ func (s *TenantStorage) IsCNAMEAvailable(cname string) (bool, error) {// Activate given tenantfunc (s *TenantStorage) Activate(id int) error {query := "UPDATE tenants SET status = $1 WHERE id = $2"- return s.trx.Execute(query, models.TenantActive, id)+ _, err := s.trx.Execute(query, models.TenantActive, id)+ return err}// SaveVerificationKey used by email verification process@@ -160,7 +162,8 @@ func (s *TenantStorage) SaveVerificationKey(key string, duration time.Duration,userID = request.GetUser().ID}query := "INSERT INTO email_verifications (tenant_id, email, created_on, expires_on, key, name, kind, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"- return s.trx.Execute(query, s.current.ID, request.GetEmail(), time.Now(), time.Now().Add(duration), key, request.GetName(), request.GetKind(), userID)+ _, err := s.trx.Execute(query, s.current.ID, request.GetEmail(), time.Now(), time.Now().Add(duration), key, request.GetName(), request.GetKind(), userID)+ return err}// FindVerificationByKey based on current tenant@@ -179,7 +182,8 @@ func (s *TenantStorage) FindVerificationByKey(kind models.EmailVerificationKind,// SetKeyAsVerified so that it cannot be used anymorefunc (s *TenantStorage) SetKeyAsVerified(key string) error {query := "UPDATE email_verifications SET verified_on = $1 WHERE tenant_id = $2 AND key = $3"- return s.trx.Execute(query, time.Now(), s.current.ID, key)+ _, err := s.trx.Execute(query, time.Now(), s.current.ID, key)+ return err}func extractSubdomain(hostname string) string {diff --git a/app/storage/postgres/user.go b/app/storage/postgres/user.goindex 3cb3a423f..292b20261 100644--- a/app/storage/postgres/user.go+++ b/app/storage/postgres/user.go@@ -115,7 +115,7 @@ func (s *UserStorage) Register(user *models.User) error {}for _, provider := range user.Providers {- if err := s.trx.Execute("INSERT INTO user_providers (tenant_id, user_id, provider, provider_uid, created_on) VALUES ($1, $2, $3, $4, $5)", s.tenant.ID, user.ID, provider.Name, provider.UID, now); err != nil {+ if _, err := s.trx.Execute("INSERT INTO user_providers (tenant_id, user_id, provider, provider_uid, created_on) VALUES ($1, $2, $3, $4, $5)", s.tenant.ID, user.ID, provider.Name, provider.UID, now); err != nil {return err}}@@ -126,13 +126,15 @@ func (s *UserStorage) Register(user *models.User) error {// RegisterProvider adds given provider to userIDfunc (s *UserStorage) RegisterProvider(userID int, provider *models.UserProvider) error {cmd := "INSERT INTO user_providers (tenant_id, user_id, provider, provider_uid, created_on) VALUES ($1, $2, $3, $4, $5)"- return s.trx.Execute(cmd, s.tenant.ID, userID, provider.Name, provider.UID, time.Now())+ _, err := s.trx.Execute(cmd, s.tenant.ID, userID, provider.Name, provider.UID, time.Now())+ return err}// Update user profilefunc (s *UserStorage) Update(settings *models.UpdateUserSettings) error {cmd := "UPDATE users SET name = $2 WHERE id = $1 AND tenant_id = $3"- return s.trx.Execute(cmd, s.user.ID, settings.Name, s.tenant.ID)+ _, err := s.trx.Execute(cmd, s.user.ID, settings.Name, s.tenant.ID)+ return err}// UpdateSettings of given user@@ -144,7 +146,7 @@ func (s *UserStorage) UpdateSettings(settings map[string]string) error {`for key, value := range settings {- err := s.trx.Execute(query, s.tenant.ID, s.user.ID, key, value)+ _, err := s.trx.Execute(query, s.tenant.ID, s.user.ID, key, value)if err != nil {return err}@@ -185,13 +187,15 @@ func (s *UserStorage) GetUserSettings() (map[string]string, error) {// ChangeRole of given userfunc (s *UserStorage) ChangeRole(userID int, role models.Role) error {cmd := "UPDATE users SET role = $3 WHERE id = $1 AND tenant_id = $2"- return s.trx.Execute(cmd, userID, s.tenant.ID, role)+ _, err := s.trx.Execute(cmd, userID, s.tenant.ID, role)+ return err}// ChangeEmail of given userfunc (s *UserStorage) ChangeEmail(userID int, email string) error {cmd := "UPDATE users SET email = $3 WHERE id = $1 AND tenant_id = $2"- return s.trx.Execute(cmd, userID, s.tenant.ID, email)+ _, err := s.trx.Execute(cmd, userID, s.tenant.ID, email)+ return err}// GetByID returns a user based on given id