hunk.code_window: import lombok.RequiredArgsConstructor;
   import lombok.extern.slf4j.Slf4j;
   import org.apache.commons.lang.ObjectUtils;
   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
  
hunk.labels: keep keep add keep keep keep keep
hunk.after_edit: import org.springframework.beans.factory.annotation.Autowired;
  
hunk.file_path: app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/SegmentConfig.java
hunk.type: add
file: package com.appsmith.server.repositories;
  
  import com.appsmith.server.acl.AclPermission;
  import com.appsmith.server.domains.Comment;
  import com.appsmith.server.domains.Organization;
  import com.appsmith.server.domains.QOrganization;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.data.domain.Sort;
  import org.springframework.data.mongodb.core.ReactiveMongoOperations;
  import org.springframework.data.mongodb.core.convert.MongoConverter;
  import org.springframework.data.mongodb.core.query.Criteria;
  import org.springframework.data.mongodb.core.query.Query;
  import org.springframework.data.mongodb.core.query.Update;
  import org.springframework.stereotype.Component;
  import reactor.core.publisher.Flux;
  import reactor.core.publisher.Mono;
  
  import java.util.List;
  import java.util.Set;
  import java.util.stream.Collectors;
  
  import static org.springframework.data.mongodb.core.query.Criteria.where;
  import static org.springframework.data.mongodb.core.query.Query.query;
  
  @Component
  @Slf4j
  public class CustomOrganizationRepositoryImpl extends BaseAppsmithRepositoryImpl<Organization>
          implements CustomOrganizationRepository {
  
      public CustomOrganizationRepositoryImpl(ReactiveMongoOperations mongoOperations, MongoConverter mongoConverter) {
          super(mongoOperations, mongoConverter);
      }
  
      @Override
      public Mono<Organization> findByName(String name, AclPermission aclPermission) {
          Criteria nameCriteria = where(fieldName(QOrganization.organization.name)).is(name);
  
          return queryOne(List.of(nameCriteria), aclPermission);
      }
  
      @Override
      public Flux<Organization> findByIdsIn(Set<String> orgIds, AclPermission aclPermission, Sort sort) {
          Criteria orgIdsCriteria = where(fieldName(QOrganization.organization.id)).in(orgIds);
  
          return queryAll(List.of(orgIdsCriteria), aclPermission, sort);
      }
  
      @Override
      public Mono<Long> nextSlugNumber(String slugPrefix) {
          final String slugField = fieldName(QOrganization.organization.slug);
          final Query slugPrefixQuery = query(where(slugField).regex("^" + slugPrefix + "\\d*$"));
          slugPrefixQuery.fields().include(slugField);
          return mongoOperations
                  .find(slugPrefixQuery, Organization.class)
                  .map(Organization::getSlug)
                  .collect(Collectors.toSet())
                  .map(slugs -> {
                      if (slugs.isEmpty() || !slugs.contains(slugPrefix)) {
                          return 0L;
                      }
  
                      long number = 1L;
                      while (slugs.contains(slugPrefix + number)) {
                          ++number;
                      }
  
                      return number;
                  });
      }
  
      @Override
      public Mono<Void> updateUserRoleNames(String userId, String userName) {
          return mongoOperations
                  .updateMulti(
                          Query.query(Criteria.where("userRoles.userId").is(userId)),
                          Update.update("userRoles.$.name", userName),
                          Organization.class
                  )
                  .then();
      }
  }
  