Rating the delivery service with feedback (Java)

In this use case, the user rates the delivery service. This rating is usually between 1 to 5. Sometimes, the user is also expected to provide feedback against a rating.

Rating

Code snippets

Client calls the BAP server to trigger rating:

    @PostMapping("/delivery/rate_delivery")
    public ResponseEntity rateDelivery(
            @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": "delivery",
        "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-06-23T08:18:49.303Z"
    },
    "message": {
        "id": "order_1",
        "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 order 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": "delivery",
        "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-06-29T10:52:01.620Z"
    },
    "message": {
        "feedback": {
            "id": "c9f4c283-6e53-4d86-aa2e-98d18d30144d",
            "descriptor": "https://feedback.bpp.com/provider/fast-logistics/1532",
            "parent_id": "order_1"
        }
    }
}
*/
    @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("/delivery/rate")
    public ResponseEntity rate(
            @PathVariable(ClientRoutes.PARAM_MESSAGE_ID) String messageId,
            @RequestHeader HttpHeaders headers) {
        var data = bapApplicationService.get(messageId);
        return ResponseEntity.ok(data);
    }