Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Srivastava, Ayushee authored and Srivastava, Ayushee committed Feb 2, 2023
2 parents b1bb7ab + 88b878b commit fdbcf50
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ See also:
## Use-Cases <a name="use-cases"></a>
1. **Get Webview URL** <br/>
endpoint "/issuers/users/{userid}/dashboards"
Use this endpoint to get a new token with expiry specified. Language code "lang=en-US" can be passed as query parameter (Not mandatory). Supported lang "en-US,es-419,sv,es-CL,es-CR". Default is "en-US".
Use this endpoint to get a new token with expiry specified. Language code "lang=en-US" can be passed as query parameter (Not mandatory). Supported lang "en-US,es-419,sv,es-CL,es-CR,pl-PL". Default is "en-US".

2. **Get Current Months Carbon Score** <br/>
endpoint "/issuers/users/{userid}/aggregate-carbon-scores"<br/>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
**/com/mastercard/developers/carbontracker/exception/*,
**/com/mastercard/developers/carbontracker/util/*,
**/com/mastercard/developers/carbontracker/usecases/*,
**/com/mastercard/developers/carbontracker/service/impl/UserRegistrationServiceImpl.java
**/com/mastercard/developers/carbontracker/service/impl/UserRegistrationServiceImpl.java,
**/com/mastercard/developers/carbontracker/service/impl/UpdateUserServiceImpl.java
</sonar.coverage.exclusions>

</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mastercard.developers.carbontracker.exception.ServiceException;
import com.mastercard.developers.carbontracker.service.GetDashboardService;
import com.mastercard.developers.carbontracker.service.IssuerService;
import com.mastercard.developers.carbontracker.service.UpdateUserService;
import com.mastercard.developers.carbontracker.service.UserRegistrationService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,6 +12,7 @@
import org.openapitools.client.model.IssuerConfiguration;
import org.openapitools.client.model.IssuerProfile;
import org.openapitools.client.model.IssuerProfileDetails;
import org.openapitools.client.model.UpdateUserProfile;
import org.openapitools.client.model.UserProfile;
import org.openapitools.client.model.UserReference;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -34,6 +36,7 @@
import static com.mastercard.developers.carbontracker.util.ServiceEndpoints.DASHBOARDS;
import static com.mastercard.developers.carbontracker.util.ServiceEndpoints.DELETE_USER;
import static com.mastercard.developers.carbontracker.util.ServiceEndpoints.GET_ISSUER;
import static com.mastercard.developers.carbontracker.util.ServiceEndpoints.UPDATE_ISSUER;
import static com.mastercard.developers.carbontracker.util.ServiceEndpoints.UPDATE_USER;

@RestController
Expand All @@ -47,11 +50,14 @@ public class IssuerController {

private final GetDashboardService getDashboardService;

private final UpdateUserService updateUserService;

@Autowired
public IssuerController(IssuerService issuerService, UserRegistrationService userRegistrationService, GetDashboardService getDashboardService) {
public IssuerController(IssuerService issuerService, UserRegistrationService userRegistrationService, GetDashboardService getDashboardService, UpdateUserService updateUserService) {
this.issuerService = issuerService;
this.userRegistrationService = userRegistrationService;
this.getDashboardService = getDashboardService;
this.updateUserService = updateUserService;
}

@GetMapping(DASHBOARDS)
Expand All @@ -71,13 +77,20 @@ public ResponseEntity<UserReference> userRegistration(@ApiParam(value = "User's
return ResponseEntity.ok(userRegistrationService.userRegistration(userProfile));
}

@PutMapping(UPDATE_USER)
@PutMapping(UPDATE_ISSUER)
public ResponseEntity<IssuerProfile> updateIssuer(@ApiParam(value = " issuer configuration", required = true) @Valid @RequestBody IssuerConfiguration issuerConfiguration) throws ServiceException {

IssuerProfile issuerProfile = issuerService.updateIssuer(issuerConfiguration);
return ResponseEntity.ok(issuerProfile);
}

@PutMapping(UPDATE_USER)
public ResponseEntity<UserReference> updateUser(@Pattern(regexp = "^[0-9A-Fa-f-]{36}") @Size(min = 36, max = 36) @ApiParam(value = "Unique identifier for a cardholder enrolled into Priceless Planet Carbon Tracker Service.", required = true) @PathVariable("userid") String userId, @ApiParam(value = " User's Personal information which needs to be updated for enrolled user onto Priceless Planet Carbon Tracker platform. This endpoint uses Mastercard payload encryption. Please refer to the **[Payload Encryption](https://mstr.cd/2UPfda0)** page for implementation details.", required = true) @Valid @RequestBody UpdateUserProfile updateUserProfile) throws ServiceException {

UserReference userReference = updateUserService.updateUser(userId, updateUserProfile);
return ResponseEntity.ok(userReference);
}

@PostMapping(DELETE_USER)
public ResponseEntity<List<String>> deleteUsers(@ApiParam(value = " User ids", required = true) @Valid @RequestBody List<String> userIds) throws ServiceException {
return issuerService.deleteUsers(userIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mastercard.developers.carbontracker.service;

import com.mastercard.developers.carbontracker.exception.ServiceException;
import org.openapitools.client.model.UpdateUserProfile;
import org.openapitools.client.model.UserReference;

public interface UpdateUserService {

UserReference updateUser(String userId, UpdateUserProfile updateUserProfile) throws ServiceException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.mastercard.developers.carbontracker.service.impl;

import com.mastercard.developer.interceptors.OkHttpFieldLevelEncryptionInterceptor;
import com.mastercard.developer.interceptors.OkHttpOAuth1Interceptor;
import com.mastercard.developers.carbontracker.configuration.ApiConfiguration;
import com.mastercard.developers.carbontracker.exception.ServiceException;
import com.mastercard.developers.carbontracker.service.UpdateUserService;
import com.mastercard.developers.carbontracker.util.EncryptionHelper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.api.IssuerApi;
import org.openapitools.client.model.UpdateUserProfile;
import org.openapitools.client.model.UserReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.mastercard.developers.carbontracker.util.JSON.deserializeErrors;

@Slf4j
@Service
public class UpdateUserServiceImpl implements UpdateUserService {

private final IssuerApi issuerApiForEncryptedPayload;

@Autowired
public UpdateUserServiceImpl(ApiConfiguration apiConfiguration) throws ServiceException {
log.info("Initializing User Registration Service");
issuerApiForEncryptedPayload = new IssuerApi(setupForEncryptedPayload(apiConfiguration));
}

private ApiClient setupForEncryptedPayload(ApiConfiguration apiConfiguration) throws ServiceException {
OkHttpClient client = new OkHttpClient().newBuilder().
addInterceptor(
new OkHttpFieldLevelEncryptionInterceptor(
EncryptionHelper.encryptionConfig(apiConfiguration.getEncryptionKeyFile()))).
addInterceptor(
new OkHttpOAuth1Interceptor(apiConfiguration.getConsumerKey(), apiConfiguration.getSigningKey()))
.build();

return new ApiClient().setHttpClient(client).setBasePath(apiConfiguration.getBasePath());
}

@Override
public UserReference updateUser(String userId, UpdateUserProfile updateUserProfile) throws ServiceException {
UserReference userReference;
try {
userReference = issuerApiForEncryptedPayload.updateUser(userId, updateUserProfile);
} catch (ApiException e) {
log.error("Exception occurred while registering new user {}", e.getResponseBody());

throw new ServiceException(e.getMessage(), deserializeErrors(e.getResponseBody()));
}

return userReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mastercard.developers.carbontracker.exception.ServiceException;
import com.mastercard.developers.carbontracker.service.GetDashboardService;
import com.mastercard.developers.carbontracker.service.IssuerService;
import com.mastercard.developers.carbontracker.service.UpdateUserService;
import com.mastercard.developers.carbontracker.service.UserRegistrationService;
import com.mastercard.developers.carbontracker.util.CreditCardGenerator;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -15,6 +16,7 @@
import org.openapitools.client.model.IssuerConfiguration;
import org.openapitools.client.model.IssuerProfile;
import org.openapitools.client.model.IssuerProfileDetails;
import org.openapitools.client.model.UpdateUserProfile;
import org.openapitools.client.model.UserName;
import org.openapitools.client.model.UserProfile;
import org.openapitools.client.model.UserReference;
Expand All @@ -36,17 +38,20 @@ public class IssuerControllerUseCase {

private final GetDashboardService getDashboardService;

private final UpdateUserService updateUserService;

@Value("${binRange}")
private String binRange;

@Value("${lang:en-US}")
private String lang;

@Autowired
public IssuerControllerUseCase(IssuerService issuerService, UserRegistrationService userRegistrationService, GetDashboardService getDashboardService) {
public IssuerControllerUseCase(IssuerService issuerService, UserRegistrationService userRegistrationService, GetDashboardService getDashboardService, UpdateUserService updateUserService) {
this.issuerService = issuerService;
this.userRegistrationService = userRegistrationService;
this.getDashboardService = getDashboardService;
this.updateUserService = updateUserService;
}

public void b2BCalls() {
Expand All @@ -57,6 +62,7 @@ public void b2BCalls() {
String userId = userRegistration();
getAggregateScores(userId);
getDashboardUrl(userId, lang);
updateUser(userId);
deleteUser(userId);

}
Expand Down Expand Up @@ -124,6 +130,16 @@ public void getDashboardUrl(String userId, String lang) {

}

private void updateUser(String userId) {
try {
log.info("Updating a enrolled User");
UserReference userReference = updateUserService.updateUser(userId, getUpdateUserProfile());
log.info("Response for the User information updated for given userId {} is {}", userId, userReference);
} catch (ServiceException ex) {
log.info("Exception occurred while updating enrolled a new user {}", ex.getServiceErrors());
}
}

public void deleteUser(String userId) {
log.info("Deleting user for given user {} ", userId);
try {
Expand Down Expand Up @@ -156,6 +172,27 @@ public UserProfile getUserProfile() {
CardExpiry cardExpiry = new CardExpiry();
cardExpiry.setMonth("09");
cardExpiry.setYear("2024");
userProfile.setBillingAddress(getAddress());
userProfile.setExpiryInfo(cardExpiry);
userProfile.setLocale("en-US");

return userProfile;
}

public UpdateUserProfile getUpdateUserProfile() {
UpdateUserProfile updateUserProfile = new UpdateUserProfile();

UserName userName = new UserName();
userName.setFirstName("dummmyUserrz");
userName.setLastName("Useraf");
updateUserProfile.setName(userName);
updateUserProfile.setBillingAddress(getAddress());
updateUserProfile.setLocale("en-US");

return updateUserProfile;
}

public Address getAddress() {
Address address = new Address();

address.setCountryCode("USA");
Expand All @@ -167,11 +204,7 @@ public UserProfile getUserProfile() {
address.setLine3("7832 West ");
address.setType("work");
address.setCity("Brooklyn");
userProfile.setBillingAddress(address);
userProfile.setExpiryInfo(cardExpiry);
userProfile.setLocale("en-US");

return userProfile;
return address;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ private ServiceEndpoints() {
public static final String AGGREGATE_CARBON_SCORE = "/cts/issuers/users/{userid}/aggregate-carbon-scores";
public static final String DASHBOARDS = "/cts/issuers/users/{userid}/dashboards";
public static final String DELETE_USER = "/cts/issuers/user-deletions";
public static final String UPDATE_USER = "/cts/issuers";
public static final String UPDATE_ISSUER = "/cts/issuers";
public static final String GET_ISSUER = "/cts/issuers";
public static final String UPDATE_USER = "/cts/issuers/users/{userid}";
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ mastercard:

binRange: #Bin range for the given issuer

lang: en-US #Provide language code in which webview should be rendered. Supported lang "en-US,es-419,sv,es-CL,es-CR". Default is "en-US" .
lang: en-US #Provide language code in which webview should be rendered. Supported lang "en-US,es-419,sv,es-CL,es-CR,pl-PL". Default is "en-US" .
Loading

0 comments on commit fdbcf50

Please sign in to comment.