Skip to content

Fixed pagination for myorg endpoint. #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.lowcoder.domain.organization.model.OrganizationState;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;
import java.util.List;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -31,4 +33,7 @@ public interface OrganizationRepository extends ReactiveMongoRepository<Organiza

Flux<Organization> findByOrganizationDomainIsNotNull();
Mono<Boolean> existsBySlug(String slug);

Flux<Organization> findByIdInAndNameContainingIgnoreCase(List<String> ids, String name, Pageable pageable);
Mono<Long> countByIdInAndNameContainingIgnoreCase(List<String> ids, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.lowcoder.infra.annotation.NonEmptyMono;
import org.lowcoder.infra.annotation.PossibleEmptyMono;
import org.springframework.http.codec.multipart.Part;
import org.springframework.data.domain.Pageable;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -52,4 +53,7 @@ public interface OrganizationService {
Mono<Boolean> updateCommonSettings(String orgId, String key, Object value);

Mono<Organization> updateSlug(String organizationId, String newSlug);

Flux<Organization> findUserOrgs(String userId, String orgName, Pageable pageable);
Mono<Long> countUserOrgs(String userId, String orgName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.lowcoder.domain.user.repository.UserRepository;
import org.lowcoder.domain.util.SlugUtils;
import org.lowcoder.infra.annotation.PossibleEmptyMono;
import org.lowcoder.infra.birelation.BiRelationService;
import org.lowcoder.infra.birelation.BiRelation;
import org.lowcoder.infra.mongo.MongoUpsertHelper;
import org.lowcoder.sdk.config.CommonConfig;
import org.lowcoder.sdk.config.dynamic.Conf;
Expand All @@ -31,6 +33,7 @@
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.codec.multipart.Part;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -41,6 +44,7 @@
import static org.lowcoder.domain.organization.model.OrganizationState.DELETED;
import static org.lowcoder.domain.util.QueryDslUtils.fieldName;
import static org.lowcoder.sdk.exception.BizError.UNABLE_TO_FIND_VALID_ORG;
import static org.lowcoder.infra.birelation.BiRelationBizType.ORG_MEMBER;
import static org.lowcoder.sdk.util.ExceptionUtils.deferredError;
import static org.lowcoder.sdk.util.ExceptionUtils.ofError;
import static org.lowcoder.sdk.util.LocaleUtils.getLocale;
Expand All @@ -62,6 +66,7 @@ public class OrganizationServiceImpl implements OrganizationService {
private final ApplicationContext applicationContext;
private final CommonConfig commonConfig;
private final ConfigCenter configCenter;
private final BiRelationService biRelationService;

@PostConstruct
private void init()
Expand Down Expand Up @@ -315,4 +320,31 @@ public Mono<Organization> updateSlug(String organizationId, String newSlug) {
});
});
}

@Override
public Flux<Organization> findUserOrgs(String userId, String orgName, Pageable pageable) {
return biRelationService.getByTargetId(ORG_MEMBER, userId)
.map(BiRelation::getSourceId)
.collectList()
.flatMapMany(orgIds -> {
if (orgIds.isEmpty()) {
return Flux.empty();
}
return repository.findByIdInAndNameContainingIgnoreCase(orgIds, orgName, pageable);
});
}

@Override
public Mono<Long> countUserOrgs(String userId, String orgName) {
String filter = orgName == null ? "" : orgName;
return biRelationService.getByTargetId(ORG_MEMBER, userId)
.map(BiRelation::getSourceId)
.collectList()
.flatMap(orgIds -> {
if (orgIds.isEmpty()) {
return Mono.just(0L);
}
return repository.countByIdInAndNameContainingIgnoreCase(orgIds, filter);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

import static org.lowcoder.sdk.exception.BizError.INVALID_USER_STATUS;
import static org.lowcoder.sdk.util.ExceptionUtils.ofError;

Expand Down Expand Up @@ -70,30 +75,20 @@ public Mono<ResponseView<?>> getUserProfile(ServerWebExchange exchange) {

@Override
public Mono<ResponseView<?>> getUserOrgs(ServerWebExchange exchange,
@RequestParam(required = false) String orgName,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
@RequestParam(required = false) String orgName,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
return sessionUserService.getVisitor()
.flatMap(user -> {
// Get all active organizations for the user
Flux<OrgMember> orgMemberFlux = orgMemberService.getAllActiveOrgs(user.getId());

// If orgName filter is provided, filter organizations by name
if (StringUtils.isNotBlank(orgName)) {
return orgMemberFlux
.flatMap(orgMember -> organizationService.getById(orgMember.getOrgId()))
.filter(org -> StringUtils.containsIgnoreCase(org.getName(), orgName))
.map(OrgView::new)
.collectList()
.map(orgs -> PageResponseView.success(orgs, pageNum, pageSize, orgs.size()));
}

// If no filter, return all organizations
return orgMemberFlux
.flatMap(orgMember -> organizationService.getById(orgMember.getOrgId()))
Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
String filter = orgName == null ? "" : orgName;
return organizationService.findUserOrgs(user.getId(), filter, pageable)
.map(OrgView::new)
.collectList()
.map(orgs -> PageResponseView.success(orgs, pageNum, pageSize, orgs.size()));
.zipWith(organizationService.countUserOrgs(user.getId(), filter))
.map(tuple -> PageResponseView.success(
tuple.getT1(), pageNum, pageSize, tuple.getT2().intValue()
));
})
.map(ResponseView::success);
}
Expand Down
Loading