Skip to content
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

MOSIP:36804 - Filter related changes in API GET /partners/v3 #955

Closed
wants to merge 4 commits into from
Closed
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 @@ -18,6 +18,7 @@ public enum ErrorCode {
INVALID_SORT_FIELD("PMS_PRT_357", "Invalid sort field %s"),
INVALID_PAGE_NO("PMS_PRT_360", "Invalid Page No"),
INVALID_PAGE_SIZE("PMS_PRT_361", "Invalid page size"),
INVALID_CERTIFICATE_UPLOAD_STATUS("PMS_PRT_362", "The certificate upload status can only be either 'uploaded' or 'not_uploaded"),
INVALID_VALUE("KER_PRT_390", "Invalid filter value"),
PARTNER_ALREADY_REGISTERED_EXCEPTION("PMS_PRT_001", "A Partner is already registered with the same Name"),
PARTNER_ALREADY_REGISTERED_WITH_ID_EXCEPTION("PMS_PRT_051", "A Partner is already registered with the same Id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.mosip.pms.common.response.dto.ResponseWrapperV2;
import io.mosip.pms.partner.manager.dto.*;
import io.mosip.pms.partner.util.PartnerHelper;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -279,6 +281,11 @@ public ResponseWrapperV2<PageResponseV2Dto<PartnerSummaryDto>> getAllPartners(
@RequestParam(value = "isActive", required = false) Boolean isActive,
@RequestParam(value = "orgName", required = false) String orgName,
@RequestParam(value = "emailAddress", required = false) String emailAddress,
@Parameter(
in = ParameterIn.QUERY,
description = "Certificate upload status",
schema = @Schema(allowableValues = {"uploaded", "not_uploaded"})
)
@RequestParam(value = "certificateUploadStatus", required = false) String certificateUploadStatus,
@RequestParam(value = "policyGroupName", required = false) String policyGroupName
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,22 +828,23 @@ public ResponseWrapperV2<PageResponseV2Dto<PartnerSummaryDto>> getAllPartners(St
try {
PageResponseV2Dto<PartnerSummaryDto> pageResponseV2Dto = new PageResponseV2Dto<>();
// Pagination
Pageable pageable = PageRequest.of(pageNo, pageSize);

//Sorting
if (Objects.nonNull(sortFieldName) && Objects.nonNull(sortType)) {
if (sortFieldName.equalsIgnoreCase("certificateUploadStatus") || sortFieldName.equalsIgnoreCase("isActive")) {
sortType = sortType.equalsIgnoreCase(PartnerConstants.ASC) ? PartnerConstants.DESC : PartnerConstants.ASC;
}
Sort sort = partnerHelper.getSortingRequest(getSortColumn(sortFieldName), sortType);
pageable = PageRequest.of(pageNo, pageSize, sort);
}
Pageable pageable = createPageable(pageNo, pageSize, sortFieldName, sortType);

Page<PartnerSummaryEntity> page = partnerSummaryRepository.
getSummaryOfAllPartners(filterDto.getPartnerId(), filterDto.getPartnerTypeCode(),
filterDto.getOrganizationName(), filterDto.getPolicyGroupName(),
filterDto.getCertificateUploadStatus(), filterDto.getEmailAddress(),
filterDto.getIsActive(), pageable);

int totalPages = page.getTotalPages();
if (page.getContent().isEmpty() && totalPages > 0 && pageNo >= page.getTotalPages()) {
pageNo = 0;
pageable = createPageable(pageNo, pageSize, sortFieldName, sortType);
page = partnerSummaryRepository.getSummaryOfAllPartners(filterDto.getPartnerId(), filterDto.getPartnerTypeCode(),
filterDto.getOrganizationName(), filterDto.getPolicyGroupName(), filterDto.getCertificateUploadStatus(),
filterDto.getEmailAddress(), filterDto.getIsActive(), pageable);
}

if (Objects.nonNull(page) && !page.getContent().isEmpty()) {
List<PartnerSummaryDto> partnerSummaryDtoList = MapperUtils.mapAll(page.getContent(), PartnerSummaryDto.class);
pageResponseV2Dto.setPageNo(pageNo);
Expand All @@ -868,6 +869,26 @@ public ResponseWrapperV2<PageResponseV2Dto<PartnerSummaryDto>> getAllPartners(St
return responseWrapper;
}

private Pageable createPageable(int pageNo, int pageSize, String sortFieldName, String sortType) {
//Pageable
Pageable pageable = PageRequest.of(pageNo, pageSize);

//Sorting
if (Objects.nonNull(sortFieldName) && Objects.nonNull(sortType)) {
if ("certificateUploadStatus".equalsIgnoreCase(sortFieldName) ||
"isActive".equalsIgnoreCase(sortFieldName)) {
sortType = sortType.equalsIgnoreCase(PartnerConstants.ASC)
? PartnerConstants.DESC
: PartnerConstants.ASC;
}

// Create sorting request
Sort sort = partnerHelper.getSortingRequest(getSortColumn(sortFieldName), sortType);
pageable = PageRequest.of(pageNo, pageSize, sort);
}
return pageable;
}

public String getSortColumn(String alias) {
return partnerHelper.aliasToColumnMap.getOrDefault(alias, alias); // Return alias if no match found
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class PartnerHelper {
private static final Logger LOGGER = PMSLogger.getLogger(PartnerHelper.class);
public static final String APPROVED = "approved";
public static final String PENDING_APPROVAL = "pending_approval";
public static final String CERTIFICATE_UPLOADED = "uploaded";
public static final String CERTIFICATE_NOT_UPLOADED = "not_uploaded";

public final Map<String, String> aliasToColumnMap = new HashMap<>();
{
Expand Down