Files
houston-be/model/user/user.go
Vijay Joshi 288a7f8457 INFRA-3664 : Make get users in conversation api more performant, add api to sync incident users, module to track request statuses and remove old add incident-user api (#451)
* INFRA-3664 : Make get users in conversation api more performant

* INFRA-3664 : Add apis for incident user sync and get users in incident performance improvements

* INFRA-3664 : Self review

* INFRA-3664 : Add migration script

* INFRA-3664 : Review comments

* INFRA-3664 : Constant chanes

* INFRA-3664 : Add rate limit constants

* INFRA-3664 : Add rate limit constants

* INFRA-3664 : Fix failing tests

* INFRA-3664 : Add UT's
2024-08-29 11:52:56 +05:30

147 lines
3.7 KiB
Go

package user
import (
"errors"
"gorm.io/gorm"
)
type Repository struct {
gormClient *gorm.DB
}
func NewUserRepository(gormClient *gorm.DB) *Repository {
return &Repository{
gormClient: gormClient,
}
}
func (r *Repository) Insert(users []UserEntity) error {
result := r.gormClient.Create(&users)
if result.Error != nil || result.RowsAffected != int64(len(users)) {
return result.Error
}
return nil
}
func (r *Repository) Update(users []UserEntity) error {
result := r.gormClient.Save(&users)
if result.Error != nil {
return result.Error
}
return nil
}
func (r *Repository) UpdateHoustonUser(user UserEntity) error {
result := r.gormClient.Select("*").Updates(&user)
if result.Error != nil || result.RowsAffected != 1 {
return result.Error
}
return nil
}
func (r *Repository) GetAllHoustonUsers() (*[]UserEntity, error) {
var users []UserEntity
result := r.gormClient.Find(&users)
if result.Error != nil {
return nil, result.Error
}
return &users, nil
}
func (r *Repository) IsAHoustonUser(emailOrSlackUserId string) (bool, *UserEntity) {
var existingUser UserEntity
findByName := r.gormClient.Where("email = ?", emailOrSlackUserId).First(&existingUser)
if findByName.Error != nil {
findBySlackUserId := r.gormClient.Where("slack_user_id = ?", emailOrSlackUserId).First(&existingUser)
if findBySlackUserId.Error != nil {
return false, nil
}
}
return true, &existingUser
}
func (r *Repository) FindHoustonUserBySlackUserId(slackUserId string) (*UserEntity, error) {
var user UserEntity
result := r.gormClient.Where("slack_user_id = ?", slackUserId).First(&user)
if result.Error != nil {
// If the user is not found, return both entity and error as nil
if errors.Is(gorm.ErrRecordNotFound, result.Error) {
return nil, nil
}
return nil, result.Error
}
return &user, nil
}
func (r *Repository) InsertHoustonUser(user *UserEntity) error {
result := r.gormClient.Create(&user)
if result.Error != nil || result.RowsAffected != 1 {
return result.Error
}
return nil
}
func (r *Repository) GetHoustonUsersBySlackId(slackUserId []string) (*[]UserEntity, error) {
var users []UserEntity
result := r.gormClient.Where("slack_user_id IN ?", slackUserId).Find(&users)
if result.Error != nil {
return nil, result.Error
}
return &users, nil
}
func (r *Repository) GetAllActiveHoustonUserBots() ([]UserEntity, error) {
var users []UserEntity
result := r.gormClient.Where("is_bot = ? AND active = ?", true, true).Find(&users)
if result.Error != nil {
return nil, result.Error
}
return users, nil
}
func (r *Repository) GetHoustonUserByEmailId(emailId string) (*UserEntity, error) {
var user UserEntity
result := r.gormClient.Where("email = ?", emailId).First(&user)
if result.Error != nil {
if errors.Is(gorm.ErrRecordNotFound, result.Error) {
return nil, nil
}
return nil, result.Error
}
return &user, nil
}
func (r *Repository) GetHoustonUsersByEmailIds(emailIds []string) (*[]UserEntity, error) {
var users []UserEntity
result := r.gormClient.Where("email IN ?", emailIds).Find(&users)
if result.Error != nil {
return nil, result.Error
}
return &users, nil
}
func (r *Repository) GetHoustonUserById(id uint) (*UserEntity, error) {
var user UserEntity
result := r.gormClient.Where("id = ?", id).First(&user)
if result.Error != nil {
if errors.Is(gorm.ErrRecordNotFound, result.Error) {
return nil, nil
}
return nil, result.Error
}
return &user, nil
}
func (r *Repository) FindHoustonUsersBySlackUserIds(slackUserIds []string) ([]UserEntity, error) {
var users []UserEntity
result := r.gormClient.Where("slack_user_id IN ?", slackUserIds).Find(&users)
if result.Error != nil {
return nil, result.Error
}
return users, nil
}