Migration of UserTermsOfServiceStore to return plain errors (#14788)

* Migration of UserTermsOfService Store

* Ordering translations file

* Fix imports

* Fix translations]

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Rodrigo Villablanca
2020-06-19 08:19:30 -04:00
коммит произвёл GitHub
родитель d21f1183ab
Коммит 2760497660
10 изменённых файлов: 80 добавлений и 59 удалений

Просмотреть файл

@@ -3,10 +3,27 @@
package app
import "github.com/mattermost/mattermost-server/v5/model"
import (
"errors"
"net/http"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
)
func (a *App) GetUserTermsOfService(userId string) (*model.UserTermsOfService, *model.AppError) {
return a.Srv().Store.UserTermsOfService().GetByUser(userId)
u, err := a.Srv().Store.UserTermsOfService().GetByUser(userId)
if err != nil {
var nfErr *store.ErrNotFound
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("GetUserTermsOfService", "app.user_terms_of_service.get_by_user.no_rows.app_error", nil, nfErr.Error(), http.StatusNotFound)
default:
return nil, model.NewAppError("GetUserTermsOfService", "app.user_terms_of_service.get_by_user.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return u, nil
}
func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted bool) *model.AppError {
@@ -17,11 +34,17 @@ func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted b
}
if _, err := a.Srv().Store.UserTermsOfService().Save(userTermsOfService); err != nil {
return err
var appErr *model.AppError
switch {
case errors.As(err, &appErr):
return appErr
default:
return model.NewAppError("SaveUserTermsOfService", "app.user_terms_of_service.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
} else {
if err := a.Srv().Store.UserTermsOfService().Delete(userId, termsOfServiceId); err != nil {
return err
return model.NewAppError("SaveUserTermsOfService", "app.user_terms_of_service.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}

Просмотреть файл

@@ -16,7 +16,7 @@ func TestUserTermsOfService(t *testing.T) {
userTermsOfService, err := th.App.GetUserTermsOfService(th.BasicUser.Id)
checkError(t, err)
assert.Nil(t, userTermsOfService)
assert.Equal(t, "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", err.Id)
assert.Equal(t, "app.user_terms_of_service.get_by_user.no_rows.app_error", err.Id)
termsOfService, err := th.App.CreateTermsOfService("terms of service", th.BasicUser.Id)
checkNoError(t, err)