0% found this document useful (0 votes)
20 views

Weight Desrepency Ideas

The document describes automating weight discrepancy escalations for subscription users by storing product information like SKUs and weights in a database and using QR codes and images. It then provides steps to implement this using the Microsoft Graph API and integrating it with a Java/Spring Boot application.

Uploaded by

Manoj Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Weight Desrepency Ideas

The document describes automating weight discrepancy escalations for subscription users by storing product information like SKUs and weights in a database and using QR codes and images. It then provides steps to implement this using the Microsoft Graph API and integrating it with a Java/Spring Boot application.

Uploaded by

Manoj Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Automating Weight Discrepancy Escalations for Ultimate Subscription Users

To streamline weight discrepancy escalations for our ultimate subscription users,


we propose the following approach:

We will store all SKUs and their corresponding weights as JSON in our database.
This eliminates the need to store images for every SKU, especially when a seller
has a large number of products. However, if a seller provides images, we will store
them in our S3 and include the image URLs in the JSON along with the SKUs and
weights.

Benefits: When a user places an order, we will retrieve the SKUs from the order and
cross-reference them with the predefined set of SKUs from the seller. If a SKU is
not found in the JSON, we will send a warning message.

Why QR Code: In case a carrier partner raises a discrepancy, we must provide an


image of the SKU showing its weight to the carrier since weight cannot be read
directly from the shipping label. By embedding the SKU images in QR codes, we can
easily fetch the shipping label, then the QR, and finally query against the SKU for
the images. This eliminates the need for manual intervention in weight discrepancy
escalations.

Why Sellers Trust Us:


We will inform sellers that we automate this process in our ultimate plan. However,
if a seller repeatedly enters false weights, we reserve the right to block this
functionality, thus incentivizing accurate reporting.

Additional Benefit of Storing SKUs and Weights:


Even if a seller is genuine, discrepancies may arise due to differences in the
weighing scales used by the seller and the carrier partner. By analyzing the data,
we can identify patterns in scale reading differences and adjust the saved weights
of each SKU in our database accordingly.

Question: Won't it be difficult to find such patterns with different carriers used
by the seller?
Since most carriers use high-standard weighing machines, the probability of errors
from the carrier partner's scale is minimal. Therefore, we can confidently assume
consistency across carriers.

MICROSOFT GRAPH API- TO GET DATA FROM WEIGHT DISCREPENCY MAILS


===================================================================

Sure, I can guide you on how to implement this solution using Java and Spring Boot.
Here are the steps to achieve this:

1. **Register an Application in Azure**: First, register your application in the


Azure portal to get the necessary credentials (client ID, tenant ID, and client
secret) to access the Microsoft Graph API.

2. **Get Access Token**: Use the credentials to obtain an OAuth2 token to


authenticate API requests.

3. **Create a Subscription**: Use the Microsoft Graph API to create a subscription


to the mailbox where the carrier partner sends emails.

4. **Set Up a Webhook Receiver**: Implement a webhook endpoint in your Spring Boot


application to receive notifications from Microsoft Graph.
5. **Process the Emails**: Once a notification is received, fetch the email details
using the Microsoft Graph API, extract the relevant data, and save it to your
database.

### Detailed Implementation

#### 1. Register an Application in Azure

Follow the steps in the Azure portal to register a new application. Note down the
client ID, tenant ID, and client secret.

#### 2. Get Access Token

Use the following Java code to obtain an access token:

```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.json.JSONObject;

public class OAuth2Client {


private String tenantId = "your-tenant-id";
private String clientId = "your-client-id";
private String clientSecret = "your-client-secret";
private String scope = "https://graph.microsoft.com/.default";
private String grantType = "client_credentials";

public String getAccessToken() {


RestTemplate restTemplate = new RestTemplate();
String url = "https://login.microsoftonline.com/" + tenantId +
"/oauth2/v2.0/token";

HttpHeaders headers = new HttpHeaders();

headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCOD
ED);

String body = "client_id=" + clientId +


"&client_secret=" + clientSecret +
"&scope=" + scope +
"&grant_type=" + grantType;

HttpEntity<String> request = new HttpEntity<>(body, headers);

ResponseEntity<String> response = restTemplate.exchange(url,


HttpMethod.POST, request, String.class);
JSONObject jsonResponse = new JSONObject(response.getBody());
return jsonResponse.getString("access_token");
}
}
```

#### 3. Create a Subscription

Use the following code to create a subscription to the mailbox:


```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.json.JSONObject;

public class GraphClient {


private String accessToken;

public GraphClient(String accessToken) {


this.accessToken = accessToken;
}

public void createSubscription() {


RestTemplate restTemplate = new RestTemplate();
String url = "https://graph.microsoft.com/v1.0/subscriptions";

HttpHeaders headers = new HttpHeaders();


headers.set("Authorization", "Bearer " + accessToken);

headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);

JSONObject subscriptionData = new JSONObject();


subscriptionData.put("changeType", "created");
subscriptionData.put("notificationUrl",
"https://your-webhook-url/webhook");
subscriptionData.put("resource", "/me/mailFolders('inbox')/messages");
subscriptionData.put("expirationDateTime", "2024-05-18T11:00:00.000Z");
subscriptionData.put("clientState", "your-client-state");

HttpEntity<String> request = new HttpEntity<>(subscriptionData.toString(),


headers);
ResponseEntity<String> response = restTemplate.exchange(url,
HttpMethod.POST, request, String.class);

JSONObject jsonResponse = new JSONObject(response.getBody());


String subscriptionId = jsonResponse.getString("id");
System.out.println("Subscription ID: " + subscriptionId);
}
}
```

#### 4. Set Up a Webhook Receiver

Create a Spring Boot controller to handle webhook notifications:

```java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebhookController {

@PostMapping("/webhook")
public String handleWebhook(@RequestBody String requestBody) {
// Validate and process the notification
JSONObject json = new JSONObject(requestBody);
if (json.has("validationToken")) {
return json.getString("validationToken");
}

JSONArray notifications = json.getJSONArray("value");


for (int i = 0; i < notifications.length(); i++) {
JSONObject notification = notifications.getJSONObject(i);
if (notification.getString("clientState").equals("your-client-state"))
{
String messageId =
notification.getJSONObject("resourceData").getString("id");
processEmail(messageId);
}
}

return "success";
}

private void processEmail(String messageId) {


// Fetch and process the email
// This will involve calling the Microsoft Graph API to get the email
details
}
}
```

#### 5. Process the Emails

Fetch the email details and extract the data:

```java
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

public class EmailProcessor {


private String accessToken;

public EmailProcessor(String accessToken) {


this.accessToken = accessToken;
}

public void processEmail(String messageId) {


RestTemplate restTemplate = new RestTemplate();
String url = "https://graph.microsoft.com/v1.0/me/messages/" + messageId;

HttpHeaders headers = new HttpHeaders();


headers.set("Authorization", "Bearer " + accessToken);

HttpEntity<String> request = new HttpEntity<>(headers);


ResponseEntity<String> response = restTemplate.exchange(url,
HttpMethod.GET, request, String.class);
JSONObject emailData = new JSONObject(response.getBody());

// Extract relevant information


String emailBody = emailData.getString("body");
String extraWeight = extractData(emailBody, "Extra weight: ");
String cost = extractData(emailBody, "Cost: ");

// Save data to database


saveToDatabase(extraWeight, cost);
}

private String extractData(String text, String pattern) {


Pattern p = Pattern.compile(pattern + "(\\d+\\.?\\d*)");
Matcher m = p.matcher(text);
if (m.find()) {
return m.group(1);
}
return null;
}

private void saveToDatabase(String extraWeight, String cost) {


// Implement database saving logic here
}
}
```

### Conclusion

By following these steps, you can automate the process of reading emails regarding
weight discrepancies, extracting relevant data, and saving it to your database
using Java and Spring Boot. This solution leverages the Microsoft Graph API for
email management and integrates with your Spring Boot application to handle webhook
notifications and process the email content.

You might also like