package customerRepo import ( "fmt" "strings" "git.ma-al.com/goc_daniel/b2b/app/db" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/utils/query/filters" "git.ma-al.com/goc_daniel/b2b/app/utils/query/find" ) type UICustomerRepo interface { Get(id uint) (*model.Customer, error) GetByEmail(email string) (*model.Customer, error) GetByExternalProviderId(provider model.AuthProvider, id string) (*model.Customer, error) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) Save(customer *model.Customer) error Create(customer *model.Customer) error SetCustomerNoVatStatus(customerID uint, isNoVat bool) error } type CustomerRepo struct{} func New() UICustomerRepo { return &CustomerRepo{} } func (repo *CustomerRepo) Get(id uint) (*model.Customer, error) { var customer model.Customer err := db.DB. Preload("Role.Permissions"). First(&customer, id). Error return &customer, err } func (repo *CustomerRepo) GetByEmail(email string) (*model.Customer, error) { var customer model.Customer err := db.DB. Preload("Role.Permissions"). Where("email = ?", email). First(&customer). Error return &customer, err } func (repo *CustomerRepo) GetByExternalProviderId(provider model.AuthProvider, id string) (*model.Customer, error) { var customer model.Customer err := db.DB. Preload("Role.Permissions"). Where("provider = ? AND provider_id = ?", provider, id). First(&customer). Error return &customer, err } func (repo *CustomerRepo) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) { query := db.DB. Table("b2b_customers AS users"). Select(` users.id AS id, users.email AS email, users.first_name AS first_name, users.last_name AS last_name `) if search != "" { words := strings.Fields(search) if len(words) > 5 { words = words[:5] } var conditions []string var args []interface{} for _, word := range words { conditions = append(conditions, ` ( id = ? OR LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR LOWER(email) LIKE ?) `) args = append(args, strings.ToLower(word)) for range 3 { args = append(args, fmt.Sprintf("%%%s%%", strings.ToLower(word))) } } conditionsQuery := strings.Join(conditions, " AND ") query = query.Where(conditionsQuery, args...) } query = query.Scopes(filt.All()...) found, err := find.Paginate[model.UserInList](langId, p, query) return &found, err } func (repo *CustomerRepo) Save(customer *model.Customer) error { return db.DB.Save(customer).Error } func (repo *CustomerRepo) Create(customer *model.Customer) error { return db.DB.Create(customer).Error } func (repo *CustomerRepo) SetCustomerNoVatStatus(customerID uint, isNoVat bool) error { return db.DB.Model(&model.Customer{}).Where("id = ?", customerID).Update("is_no_vat", isNoVat).Error }