Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ApiOperation(value = "Partial update Account",
- notes = "Updates the specified Account by setting the values of the parameters passed. " +
- "Any parameters not provided will be left unchanged. " +
- "If the Account is not associated with current User or some of the fields provided " +
- "can not be changed, an appropriate message is returned.",
- authorizations = {
- @Authorization(value = BENCORP_MICROSERVICES, scopes = {
- @AuthorizationScope(scope = USERDATA_WRITE_SCOPE, description = USERDATA_WRITE_DESCRIPTION)
- })
- }
- )
- @PatchMapping(path = "/accounts/{accountId}")
- public JsonResponse partialUpdateAccount(@PathVariable Long accountId,
- @RequestBody AccountDto update,
- HttpServletRequest httpRequest) {
- BencorpUser user = fetchUserWithAccount(httpRequest);
- Optional<AccountEntity> optional = accountRepository.findById(accountId);
- if (!optional.isPresent()) {
- return JsonResponse.failed(JsonResponseCode.ENTITY_NOT_FOUND,
- "Account with given ID is not found");
- }
- AccountEntity account = optional.get();
- if (!user.extractCompositeUserId().equals(account.getUser().getAuth0Id())) {
- return JsonResponse.failed(JsonResponseCode.FORBIDDEN,
- "Account with given ID is associated to with the user");
- }
- if (update.getStripeCustomerId() != null) {
- if (!AccountRole.OWNER.equals(account.getRole())) {
- return JsonResponse.failed(JsonResponseCode.FORBIDDEN,
- "stripeCustomerId can only be attached to Account with OWNER role, " +
- "but current has " + account.getRole());
- }
- account.setStripeCustomerId(update.getStripeCustomerId());
- }
- if (update.getStripeChargeable() != null) {
- account.setStripeChargeable(update.getStripeChargeable());
- }
- if (update.getId() != null
- || update.getRole() != null
- || update.getUser() != null
- || update.getStatus() != null) {
- return JsonResponse.failed(JsonResponseCode.FORBIDDEN,
- "Updating one of the fields provided is forbidden");
- }
- return JsonResponse.ok(accountRepository.save(account));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement