Skip to content

Commit

Permalink
Merge pull request #50 from TrueSparrowSystems/delete-account-note
Browse files Browse the repository at this point in the history
Implement API Endpoint for Delete Account Note
  • Loading branch information
AMAN-BARBARIA authored Aug 25, 2023
2 parents a15a087 + 84e7d4a commit 886bde9
Show file tree
Hide file tree
Showing 23 changed files with 504 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.salessparrow.api.dto.formatter.CreateNoteFormatterDto;
import com.salessparrow.api.dto.formatter.GetAccountsFormatterDto;
import com.salessparrow.api.dto.formatter.GetNoteDetailsFormatterDto;
import com.salessparrow.api.dto.formatter.GetNotesListFormatterDto;
import com.salessparrow.api.dto.requestMapper.GetAccountsDto;
import com.salessparrow.api.dto.requestMapper.NoteDto;
import com.salessparrow.api.services.accounts.CreateNoteService;
import com.salessparrow.api.services.accounts.GetAccountListService;
import com.salessparrow.api.services.accounts.GetNoteDetailsService;
import com.salessparrow.api.services.accounts.GetNotesListService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
Expand All @@ -36,26 +26,6 @@ public class AccountController {
@Autowired
private GetAccountListService getAccountListService;

@Autowired
private GetNotesListService getNotesListService;

@Autowired
private GetNoteDetailsService getNoteDetailsService;

@Autowired
private CreateNoteService createNoteService;

@PostMapping("/{account_id}/notes")
public ResponseEntity<CreateNoteFormatterDto> addNoteToAccount(
HttpServletRequest request,
@PathVariable("account_id") String accountId,
@Valid @RequestBody NoteDto note
) {
CreateNoteFormatterDto createNoteFormatterDto = createNoteService.createNote(request, accountId, note);

return ResponseEntity.ok().body(createNoteFormatterDto);
}

@GetMapping("")
public ResponseEntity<GetAccountsFormatterDto> getAccounts(
HttpServletRequest request,
Expand All @@ -66,24 +36,4 @@ public ResponseEntity<GetAccountsFormatterDto> getAccounts(

return ResponseEntity.ok().body(getAccountsResponse);
}

@GetMapping("/{account_id}/notes")
public ResponseEntity<GetNotesListFormatterDto> getNotesList(HttpServletRequest request,@PathVariable("account_id") String accountId) {

GetNotesListFormatterDto getNotesListResponse = getNotesListService.getNotesList(request, accountId);

return ResponseEntity.ok().body(getNotesListResponse);
}

@GetMapping("/{account_id}/notes/{note_id}")
public ResponseEntity<GetNoteDetailsFormatterDto> getNoteFromAccount(
HttpServletRequest request,
@PathVariable("account_id") String accountId,
@PathVariable("note_id") String noteId
) {

GetNoteDetailsFormatterDto getNoteDetailsResponse = getNoteDetailsService.getNoteDetails(request, noteId);

return ResponseEntity.ok().body(getNoteDetailsResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.salessparrow.api.controllers;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.salessparrow.api.dto.formatter.CreateNoteFormatterDto;
import com.salessparrow.api.dto.formatter.GetNoteDetailsFormatterDto;
import com.salessparrow.api.dto.formatter.GetNotesListFormatterDto;
import com.salessparrow.api.dto.requestMapper.NoteDto;
import com.salessparrow.api.services.accountNotes.CreateAccountNoteService;
import com.salessparrow.api.services.accountNotes.DeleteAccountNoteService;
import com.salessparrow.api.services.accountNotes.GetAccountNoteDetailsService;
import com.salessparrow.api.services.accountNotes.GetAccountNotesListService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/v1/accounts/{account_id}/notes")
@Validated
public class AccountNoteController {

private Logger logger = org.slf4j.LoggerFactory.getLogger(AccountNoteController.class);

@Autowired
private GetAccountNotesListService getNotesListService;

@Autowired
private GetAccountNoteDetailsService getNoteDetailsService;

@Autowired
private CreateAccountNoteService createNoteService;

@Autowired
private DeleteAccountNoteService deleteAccountNoteService;

@PostMapping("")
public ResponseEntity<CreateNoteFormatterDto> addNoteToAccount(
HttpServletRequest request,
@PathVariable("account_id") String accountId,
@Valid @RequestBody NoteDto note
) {
logger.info("Create Note request received");

CreateNoteFormatterDto createNoteFormatterDto = createNoteService.createNote(request, accountId, note);

return ResponseEntity.ok().body(createNoteFormatterDto);
}

@GetMapping("")
public ResponseEntity<GetNotesListFormatterDto> getNotesList(
HttpServletRequest request,
@PathVariable("account_id") String accountId
) {
logger.info("Get Note List request received");

GetNotesListFormatterDto getNotesListResponse = getNotesListService.getNotesList(request, accountId);

return ResponseEntity.ok().body(getNotesListResponse);
}

@GetMapping("/{note_id}")
public ResponseEntity<GetNoteDetailsFormatterDto> getNoteFromAccount(
HttpServletRequest request,
@PathVariable("account_id") String accountId,
@PathVariable("note_id") String noteId
) {
logger.info("Get Note request received");

GetNoteDetailsFormatterDto getNoteDetailsResponse = getNoteDetailsService.getNoteDetails(request, noteId);

return ResponseEntity.ok().body(getNoteDetailsResponse);
}

@DeleteMapping("/{note_id}")
public ResponseEntity<GetNoteDetailsFormatterDto> deleteNote(
HttpServletRequest request,
@PathVariable("account_id") String accountId,
@PathVariable("note_id") String noteId
) {
logger.info("Delete Note request received");

deleteAccountNoteService.deleteAccountNote(request, accountId, noteId);

return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.createNote;
package com.salessparrow.api.lib.crmActions.createAccountNote;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.createNote;
package com.salessparrow.api.lib.crmActions.createAccountNote;

import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.createNote;
package com.salessparrow.api.lib.crmActions.createAccountNote;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.salessparrow.api.lib.crmActions.deleteAccountNote;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.salessparrow.api.domain.User;
import com.salessparrow.api.exception.CustomException;
import com.salessparrow.api.lib.errorLib.ErrorObject;
import com.salessparrow.api.lib.globalConstants.UserConstants;

/**
* DeleteAccountNoteFactory is a factory class for the DeleteAccountNote action for the CRM.
*/
@Component
public class DeleteAccountNoteFactory {
private Logger logger = org.slf4j.LoggerFactory.getLogger(DeleteAccountNoteFactory.class);

@Autowired
DeleteSalesforceAccountNote getSalesforceNoteDetails;

@Autowired
DeleteSalesforceAccountNote deleteAccountSalesforceNote;

/**
* deleteAccountNote is a method that makes call to delete note based on user kind.
*
* @param user
* @param noteId
*
* @return void
*/
public void deleteAccountNote(User user, String noteId) {
logger.info("Delete Account Note Factory called");

switch(user.getUserKind()) {
case UserConstants.SALESFORCE_USER_KIND:
deleteAccountSalesforceNote.deleteAccountNote(user, noteId);
break;
default:
throw new CustomException(
new ErrorObject(
"l_ca_dan_danf_dn_1",
"something_went_wrong",
"Invalid user kind."));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.salessparrow.api.lib.crmActions.deleteAccountNote;

import org.springframework.stereotype.Component;

import com.salessparrow.api.domain.User;

/**
* DeleteAccountNoteInterface is an interface for the DeleteAccountNote action for the CRM.
*/
@Component
public interface DeleteAccountNoteInterface {
public void deleteAccountNote(User user, String noteId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.salessparrow.api.lib.crmActions.deleteAccountNote;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.salessparrow.api.domain.User;
import com.salessparrow.api.exception.CustomException;
import com.salessparrow.api.lib.Util;
import com.salessparrow.api.lib.errorLib.ErrorObject;
import com.salessparrow.api.lib.globalConstants.SalesforceConstants;
import com.salessparrow.api.lib.httpLib.HttpClient;
import com.salessparrow.api.lib.salesforce.dto.CompositeRequestDto;
import com.salessparrow.api.lib.salesforce.helper.MakeCompositeRequest;

/**
* DeleteAccountSalesforceNote is a class for the DeleteAccountNote service for the Salesforce CRM.
**/
@Component
public class DeleteSalesforceAccountNote implements DeleteAccountNoteInterface {
private Logger logger = org.slf4j.LoggerFactory.getLogger(DeleteSalesforceAccountNote.class);

@Autowired
private SalesforceConstants salesforceConstants;

@Autowired
private MakeCompositeRequest makeCompositeRequest;

/**
* Deletes a note from salesforce
*
* @param user
* @param noteId
*
* @return void
**/
public void deleteAccountNote(User user, String noteId) {
logger.info("Delete Salesforce Account Note called");

String salesforceUserId = user.getExternalUserId();

String url = salesforceConstants.salesforceDeleteNoteUrl(noteId);

CompositeRequestDto compositeReq = new CompositeRequestDto("DELETE", url, "DeleteNote");

List<CompositeRequestDto> compositeRequests = new ArrayList<CompositeRequestDto>();
compositeRequests.add(compositeReq);

HttpClient.HttpResponse response = makeCompositeRequest.makePostRequest(compositeRequests, salesforceUserId);

parseResponse(response.getResponseBody());
}

/**
* Parse Response
*
* @param responseBody
*
* @return void
**/
public void parseResponse(String responseBody) {

Util util = new Util();
JsonNode rootNode = util.getJsonNode(responseBody);

JsonNode deleteNoteCompositeResponse = rootNode.get("compositeResponse").get(0);
Integer deleteNoteStatusCode = deleteNoteCompositeResponse.get("httpStatusCode").asInt();

if (deleteNoteStatusCode != 200 && deleteNoteStatusCode != 201 && deleteNoteStatusCode != 204) {
String errorBody = deleteNoteCompositeResponse.get("body").asText();

throw new CustomException(
new ErrorObject(
"l_ca_dan_dasn_pr_1",
"internal_server_error",
errorBody));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.getNoteDetails;
package com.salessparrow.api.lib.crmActions.getAccountNoteDetails;

import org.springframework.stereotype.Component;

Expand All @@ -9,6 +9,6 @@
* GetNoteDetails is an interface for the GetNoteDetails action for the CRM.
*/
@Component
public interface GetNoteDetails {
public interface GetAccountNoteDetails {
public GetNoteDetailsFormatterDto getNoteDetails(User user, String noteId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.getNoteDetails;
package com.salessparrow.api.lib.crmActions.getAccountNoteDetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -13,9 +13,9 @@
* GetNoteDetailsFactory is a factory class for the GetNoteDetails action for the CRM.
*/
@Component
public class GetNoteDetailsFactory {
public class GetAccountNoteDetailsFactory {
@Autowired
GetSalesforceNoteDetails getSalesforceNoteDetails;
GetSalesforceAccountNoteDetails getSalesforceNoteDetails;

/**
* getNoteDetails is a method that returns the details of a note.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.salessparrow.api.lib.crmActions.getNoteDetails;
package com.salessparrow.api.lib.crmActions.getAccountNoteDetails;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,7 +24,7 @@
import com.salessparrow.api.lib.salesforce.helper.SalesforceQueryBuilder;

@Component
public class GetSalesforceNoteDetails implements GetNoteDetails {
public class GetSalesforceAccountNoteDetails implements GetAccountNoteDetails {

@Autowired
private SalesforceConstants salesforceConstants;
Expand Down
Loading

0 comments on commit 886bde9

Please sign in to comment.