Rating the diagnostic lab or hospital with feedback (Java)

In this use case, the user rates the diagnostic center. This rating is usually between 1 to 5. Sometimes the user may also be asked to provide feedback with rating

Rating

Code snippets

Client calls the BAP server to trigger rating:

    @PostMapping("/healthcare_diagnostics/rate_lab")
    public ResponseEntity rateLab(
            @RequestHeader HttpHeaders headers,
            @RequestBody ClientRatingRequest request) {
        var response = bapApplicationService.rateOrder(request, headers);
        return ResponseEntity.ok(response);
    }

BAP server generates the protocol request body


/*
Example Request JSON:
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "rating",
        "core_version": "0.9.2",
        "bap_id": "https://mock_bap.com/",
        "bap_uri": "https://mock_bap.com/beckn/",
        "transaction_id": "1239890342",
        "message_id": "123793824",
        "timestamp": "2021-03-23T10:00:40.065Z"
    },
    "message": {
        "id": "good-health-clinic",
        "value": 5
    }
}
*/

    public Response rateOrder(ClientRatingRequest request, HttpHeaders headers) {

        // Generate message id
        var messageId = UUID.randomUUID().toString();

        // Check if transaction id exists in the request.
        // Generate if not exists
        var txnId = StringUtils.hasText(request.getTransactionId())
                ? request.getTransactionId()
                : UUID.randomUUID().toString();

        // Construct the Context based on the request parameters
        var context = Context.builder().domain(request.getDomain())
                .action(Context.ActionEnum.rating)
                .messageId(messageId)
                .transactionId(txnId)
                .transactionId(UUID.randomUUID().toString())
                .timestamp(new Date().toString())
                .build();

        // Construct the protocol specific object to be passed to the BPP
        // to rate the lab with the rating category received from the BPP's /get_rating_categories endpoint
        var ratingRequest = RatingRequest.builder()
                .context(context)
                .message(RatingMessage.builder()
                        .id(request.getOrderId())
                        .value(BigDecimal.valueOf(request.getRating()))
                        .build())
                .build();

        return invokeRating(ratingRequest, headers);
    }
}

BAP server calls protocol rating to the network

    public Response invokeRating(RatingRequest request, HttpHeaders headers) {

        // Call to look up function which returns the the public key and BPP Endpoint to be called
        var url = lookUp(headers);

        // Call BPP Rating api from the returned endpoint.
        // Construct request headers with the public key
        var responseEntity = apiClient.post(url[0] + Context.ActionEnum.rating,
                constructRequestHeaders(),
                request,
                Response.class);

        // Validate the received response
        if (responseEntity.getBody() == null || responseEntity.getBody().getError() != null ||
                "NACK".equals(responseEntity.getBody().getMessage().getAck().getStatus())) {
            // Return custom error to the client
            return null;
        }
        return Response.of("ACK", null);
    }

BAP receives protocol on_rating

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "on_rating",
        "core_version": "0.9.2",
        "bap_id": "https://mock_bap.com/",
        "bap_uri": "https://mock_bap.com/beckn/",
        "transaction_id": "1239890342",
        "message_id": "123793824",
        "timestamp": "2021-03-23T10:00:40.065Z"
    },
    "message": {
        "feedback": {
            "id": "rating_1",
            "descriptor": "https://feedback.bpp.com/provider/good-health-clinic/2839",
            "parent_id": "good-health-labs"
        }
    }
}
*/
    @PostMapping("/bap/on_rating")
    public ResponseEntity onRating(
            @RequestHeader HttpHeaders headers,
            @RequestBody OnRatingRequest request) {
        var response = bapCallbackApplicationService.onRating(request, headers);
        return ResponseEntity.ok(response);
    }

    public Response onRating(OnRatingRequest request, HttpHeaders headers) {
        // Validate the headers received
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error if the received headers are invalid
        if (!isHeadersValid) return null;

        // Store the data received based on message id for the client to poll
        saveToDB(request);

        return Response.of("ACK", null);
    }

Client polls BAP to get the on_rating results

    // Endpoint for the client to poll the acknowledgement for the rating
    @GetMapping("/healthcare_diagnostics/rate")
    public ResponseEntity rate(
            @PathVariable(ClientRoutes.PARAM_MESSAGE_ID) String messageId,
            @RequestHeader HttpHeaders headers) {
        var data = bapApplicationService.get(messageId);
        return ResponseEntity.ok(data);
    }