github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/app/user_terms_of_service.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"errors"
     8  	"net/http"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/store"
    12  )
    13  
    14  func (a *App) GetUserTermsOfService(userID string) (*model.UserTermsOfService, *model.AppError) {
    15  	u, err := a.Srv().Store.UserTermsOfService().GetByUser(userID)
    16  	if err != nil {
    17  		var nfErr *store.ErrNotFound
    18  		switch {
    19  		case errors.As(err, &nfErr):
    20  			return nil, model.NewAppError("GetUserTermsOfService", "app.user_terms_of_service.get_by_user.no_rows.app_error", nil, nfErr.Error(), http.StatusNotFound)
    21  		default:
    22  			return nil, model.NewAppError("GetUserTermsOfService", "app.user_terms_of_service.get_by_user.app_error", nil, err.Error(), http.StatusInternalServerError)
    23  		}
    24  	}
    25  
    26  	return u, nil
    27  }
    28  
    29  func (a *App) SaveUserTermsOfService(userID, termsOfServiceId string, accepted bool) *model.AppError {
    30  	if accepted {
    31  		userTermsOfService := &model.UserTermsOfService{
    32  			UserId:           userID,
    33  			TermsOfServiceId: termsOfServiceId,
    34  		}
    35  
    36  		if _, err := a.Srv().Store.UserTermsOfService().Save(userTermsOfService); err != nil {
    37  			var appErr *model.AppError
    38  			switch {
    39  			case errors.As(err, &appErr):
    40  				return appErr
    41  			default:
    42  				return model.NewAppError("SaveUserTermsOfService", "app.user_terms_of_service.save.app_error", nil, err.Error(), http.StatusInternalServerError)
    43  			}
    44  		}
    45  	} else {
    46  		if err := a.Srv().Store.UserTermsOfService().Delete(userID, termsOfServiceId); err != nil {
    47  			return model.NewAppError("SaveUserTermsOfService", "app.user_terms_of_service.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
    48  		}
    49  	}
    50  
    51  	return nil
    52  }