diff --git a/src/main/java/com/salessparrow/api/controllers/AccountController.java b/src/main/java/com/salessparrow/api/controllers/AccountController.java index 44058227..18829e72 100644 --- a/src/main/java/com/salessparrow/api/controllers/AccountController.java +++ b/src/main/java/com/salessparrow/api/controllers/AccountController.java @@ -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; @@ -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 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 getAccounts( HttpServletRequest request, @@ -66,24 +36,4 @@ public ResponseEntity getAccounts( return ResponseEntity.ok().body(getAccountsResponse); } - - @GetMapping("/{account_id}/notes") - public ResponseEntity 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 getNoteFromAccount( - HttpServletRequest request, - @PathVariable("account_id") String accountId, - @PathVariable("note_id") String noteId - ) { - - GetNoteDetailsFormatterDto getNoteDetailsResponse = getNoteDetailsService.getNoteDetails(request, noteId); - - return ResponseEntity.ok().body(getNoteDetailsResponse); - } } diff --git a/src/main/java/com/salessparrow/api/controllers/AccountNoteController.java b/src/main/java/com/salessparrow/api/controllers/AccountNoteController.java new file mode 100644 index 00000000..16d32670 --- /dev/null +++ b/src/main/java/com/salessparrow/api/controllers/AccountNoteController.java @@ -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 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 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 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 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(); + } +} diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteFactory.java b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteFactory.java similarity index 94% rename from src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteFactory.java rename to src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteFactory.java index 99b4dfee..86d5e17e 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteFactory.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteFactory.java @@ -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; diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteInterface.java b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteInterface.java similarity index 88% rename from src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteInterface.java rename to src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteInterface.java index 0d3903db..29a8301e 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateNoteInterface.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateNoteInterface.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.lib.crmActions.createNote; +package com.salessparrow.api.lib.crmActions.createAccountNote; import org.springframework.stereotype.Component; diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateSalesforceNote.java b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateSalesforceNote.java similarity index 98% rename from src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateSalesforceNote.java rename to src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateSalesforceNote.java index 0665a8ff..c3742d16 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/createNote/CreateSalesforceNote.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/createAccountNote/CreateSalesforceNote.java @@ -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; diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteFactory.java b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteFactory.java new file mode 100644 index 00000000..1774cb63 --- /dev/null +++ b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteFactory.java @@ -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.")); + } + } +} diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteInterface.java b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteInterface.java new file mode 100644 index 00000000..59c43a34 --- /dev/null +++ b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteAccountNoteInterface.java @@ -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); +} diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteSalesforceAccountNote.java b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteSalesforceAccountNote.java new file mode 100644 index 00000000..5509fb4d --- /dev/null +++ b/src/main/java/com/salessparrow/api/lib/crmActions/deleteAccountNote/DeleteSalesforceAccountNote.java @@ -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 compositeRequests = new ArrayList(); + 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)); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetails.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetails.java similarity index 75% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetails.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetails.java index f774626c..a8a0677a 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetails.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetails.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.lib.crmActions.getNoteDetails; +package com.salessparrow.api.lib.crmActions.getAccountNoteDetails; import org.springframework.stereotype.Component; @@ -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); } diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetailsFactory.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetailsFactory.java similarity index 86% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetailsFactory.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetailsFactory.java index 41754968..fa9b715b 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetNoteDetailsFactory.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetAccountNoteDetailsFactory.java @@ -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; @@ -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. diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetSalesforceNoteDetails.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetSalesforceAccountNoteDetails.java similarity index 97% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetSalesforceNoteDetails.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetSalesforceAccountNoteDetails.java index bac6f1ea..183ae279 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNoteDetails/GetSalesforceNoteDetails.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNoteDetails/GetSalesforceAccountNoteDetails.java @@ -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; @@ -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; diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNoteListFactory.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNoteListFactory.java similarity index 86% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNoteListFactory.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNoteListFactory.java index 3556e5db..462d11ca 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNoteListFactory.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNoteListFactory.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.lib.crmActions.getNotesList; +package com.salessparrow.api.lib.crmActions.getAccountNotesList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -14,9 +14,9 @@ * CRM. */ @Component -public class GetNoteListFactory { +public class GetAccountNoteListFactory { @Autowired - private GetSalesforceNotesList getSalesforceNotesList; + private GetSalesforceAccountNotesList getSalesforceNotesList; /** * Get the list of notes for a given account. diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNotesList.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNotesList.java similarity index 76% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNotesList.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNotesList.java index 7d4ba68e..1bc60ac7 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetNotesList.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetAccountNotesList.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.lib.crmActions.getNotesList; +package com.salessparrow.api.lib.crmActions.getAccountNotesList; import org.springframework.stereotype.Component; @@ -9,7 +9,7 @@ * GetNotesList is an interface for the GetNotesList action for the CRM. */ @Component -public interface GetNotesList { +public interface GetAccountNotesList { public GetNotesListFormatterDto getNotesList(User user, String accountId); } diff --git a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetSalesforceNotesList.java b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetSalesforceAccountNotesList.java similarity index 98% rename from src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetSalesforceNotesList.java rename to src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetSalesforceAccountNotesList.java index c6bb9ba9..0618e186 100644 --- a/src/main/java/com/salessparrow/api/lib/crmActions/getNotesList/GetSalesforceNotesList.java +++ b/src/main/java/com/salessparrow/api/lib/crmActions/getAccountNotesList/GetSalesforceAccountNotesList.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.lib.crmActions.getNotesList; +package com.salessparrow.api.lib.crmActions.getAccountNotesList; import java.util.ArrayList; import java.util.HashMap; @@ -29,7 +29,7 @@ * GetSalesforceNotesList is a class for the GetNotesList service for the Salesforce CRM. */ @Component -public class GetSalesforceNotesList implements GetNotesList{ +public class GetSalesforceAccountNotesList implements GetAccountNotesList{ @Autowired private SalesforceConstants salesforceConstants; diff --git a/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java b/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java index d379d8d0..cda58fdb 100644 --- a/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java +++ b/src/main/java/com/salessparrow/api/lib/globalConstants/SalesforceConstants.java @@ -34,6 +34,10 @@ public String salesforceCreateNoteUrl() { return sObjectsPath() + "/ContentNote"; } + public String salesforceDeleteNoteUrl(String noteId) { + return sObjectsPath() + "/ContentNote/" + noteId; + } + public String salesforceAttachNoteUrl() { return sObjectsPath() + "/ContentDocumentLink"; } diff --git a/src/main/java/com/salessparrow/api/services/accounts/CreateNoteService.java b/src/main/java/com/salessparrow/api/services/accountNotes/CreateAccountNoteService.java similarity index 84% rename from src/main/java/com/salessparrow/api/services/accounts/CreateNoteService.java rename to src/main/java/com/salessparrow/api/services/accountNotes/CreateAccountNoteService.java index 5793ebcf..0105fe9c 100644 --- a/src/main/java/com/salessparrow/api/services/accounts/CreateNoteService.java +++ b/src/main/java/com/salessparrow/api/services/accountNotes/CreateAccountNoteService.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.services.accounts; +package com.salessparrow.api.services.accountNotes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -6,7 +6,7 @@ import com.salessparrow.api.domain.SalesforceUser; import com.salessparrow.api.dto.formatter.CreateNoteFormatterDto; import com.salessparrow.api.dto.requestMapper.NoteDto; -import com.salessparrow.api.lib.crmActions.createNote.CreateNoteFactory; +import com.salessparrow.api.lib.crmActions.createAccountNote.CreateNoteFactory; import jakarta.servlet.http.HttpServletRequest; @@ -14,7 +14,7 @@ * CreateNoteService is a service class for the create note action for the CRM. */ @Service -public class CreateNoteService { +public class CreateAccountNoteService { @Autowired private CreateNoteFactory createNoteFactory; diff --git a/src/main/java/com/salessparrow/api/services/accountNotes/DeleteAccountNoteService.java b/src/main/java/com/salessparrow/api/services/accountNotes/DeleteAccountNoteService.java new file mode 100644 index 00000000..3ed5ea2f --- /dev/null +++ b/src/main/java/com/salessparrow/api/services/accountNotes/DeleteAccountNoteService.java @@ -0,0 +1,37 @@ +package com.salessparrow.api.services.accountNotes; + +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.salessparrow.api.domain.User; +import com.salessparrow.api.lib.crmActions.deleteAccountNote.DeleteAccountNoteFactory; + +import jakarta.servlet.http.HttpServletRequest; + +/** + * DeleteAccountNoteService is a service class for the DeleteAccountNote action for the CRM. + */ +@Service +public class DeleteAccountNoteService { + private Logger logger = org.slf4j.LoggerFactory.getLogger(DeleteAccountNoteService.class); + + @Autowired + private DeleteAccountNoteFactory deleteAccountNoteFactory; + + /** + * Delete note for the given note id + * + * @param accountId + * @param noteId + * + * @return void + */ + public void deleteAccountNote(HttpServletRequest request, String accountId, String noteId) { + logger.info("Delete Account Note Service called"); + + User currentUser = (User) request.getAttribute("current_user"); + + deleteAccountNoteFactory.deleteAccountNote(currentUser, noteId); + } +} diff --git a/src/main/java/com/salessparrow/api/services/accounts/GetNoteDetailsService.java b/src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNoteDetailsService.java similarity index 75% rename from src/main/java/com/salessparrow/api/services/accounts/GetNoteDetailsService.java rename to src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNoteDetailsService.java index a4af37df..342328d7 100644 --- a/src/main/java/com/salessparrow/api/services/accounts/GetNoteDetailsService.java +++ b/src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNoteDetailsService.java @@ -1,11 +1,11 @@ -package com.salessparrow.api.services.accounts; +package com.salessparrow.api.services.accountNotes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.salessparrow.api.domain.User; import com.salessparrow.api.dto.formatter.GetNoteDetailsFormatterDto; -import com.salessparrow.api.lib.crmActions.getNoteDetails.GetNoteDetailsFactory; +import com.salessparrow.api.lib.crmActions.getAccountNoteDetails.GetAccountNoteDetailsFactory; import jakarta.servlet.http.HttpServletRequest; @@ -13,9 +13,9 @@ * GetNoteDetailsService is a service class for the GetNoteDetails action for the CRM. */ @Service -public class GetNoteDetailsService { +public class GetAccountNoteDetailsService { @Autowired - private GetNoteDetailsFactory getNoteDetailsFactory; + private GetAccountNoteDetailsFactory getNoteDetailsFactory; /** * Get the details of a note diff --git a/src/main/java/com/salessparrow/api/services/accounts/GetNotesListService.java b/src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNotesListService.java similarity index 76% rename from src/main/java/com/salessparrow/api/services/accounts/GetNotesListService.java rename to src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNotesListService.java index 2d59f3bd..16da0d23 100644 --- a/src/main/java/com/salessparrow/api/services/accounts/GetNotesListService.java +++ b/src/main/java/com/salessparrow/api/services/accountNotes/GetAccountNotesListService.java @@ -1,4 +1,4 @@ -package com.salessparrow.api.services.accounts; +package com.salessparrow.api.services.accountNotes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -7,15 +7,15 @@ import com.salessparrow.api.domain.User; import com.salessparrow.api.dto.formatter.GetNotesListFormatterDto; -import com.salessparrow.api.lib.crmActions.getNotesList.GetNoteListFactory; +import com.salessparrow.api.lib.crmActions.getAccountNotesList.GetAccountNoteListFactory; /** * GetNotesListService is a service class for the GetNotesList action for the CRM. */ @Service -public class GetNotesListService { +public class GetAccountNotesListService { @Autowired - private GetNoteListFactory getNotesListFactory; + private GetAccountNoteListFactory getNotesListFactory; /** * Get the list of notes for a given account diff --git a/src/test/java/com/salessparrow/api/functional/controllers/accountNoteController/DeleteAccountNoteTest.java b/src/test/java/com/salessparrow/api/functional/controllers/accountNoteController/DeleteAccountNoteTest.java new file mode 100644 index 00000000..3bbee4c1 --- /dev/null +++ b/src/test/java/com/salessparrow/api/functional/controllers/accountNoteController/DeleteAccountNoteTest.java @@ -0,0 +1,128 @@ +package com.salessparrow.api.functional.controllers.accountNoteController; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.http.MediaType; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.dynamobee.exception.DynamobeeException; +import com.salessparrow.api.helper.Cleanup; +import com.salessparrow.api.helper.Common; +import com.salessparrow.api.helper.Constants; +import com.salessparrow.api.helper.FixtureData; +import com.salessparrow.api.helper.LoadFixture; +import com.salessparrow.api.helper.Scenario; +import com.salessparrow.api.helper.Setup; +import com.salessparrow.api.lib.globalConstants.CookieConstants; +import com.salessparrow.api.lib.httpLib.HttpClient.HttpResponse; +import com.salessparrow.api.lib.salesforce.helper.MakeCompositeRequest; + +import jakarta.servlet.http.Cookie; + +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +@Import({ Setup.class, Cleanup.class, Common.class, LoadFixture.class }) +public class DeleteAccountNoteTest { + + @Autowired + private MockMvc mockMvc; + @Autowired + private Common common; + @Autowired + private LoadFixture loadFixture; + @Autowired + private Setup setup; + @Autowired + private Cleanup cleanup; + + @MockBean + private MakeCompositeRequest makeCompositeRequestMock; + + @BeforeEach + public void setUp() throws DynamobeeException { + setup.perform(); + } + + @AfterEach + public void tearDown() { + cleanup.perform(); + } + + @ParameterizedTest + @MethodSource("testScenariosProvider") + public void deleteAccountNote(Scenario testScenario) throws Exception { + + // Load fixture data + String currentFunctionName = new Object(){}.getClass().getEnclosingMethod().getName(); + FixtureData fixtureData = common.loadFixture("classpath:fixtures/controllers/accountNoteController/deleteAccountNote.fixtures.json", currentFunctionName); + loadFixture.perform(fixtureData); + + // Read data from the scenario + ObjectMapper objectMapper = new ObjectMapper(); + String cookieValue = Constants.SALESFORCE_ACTIVE_USET_COOKIE_VALUE; + String accountId = (String) testScenario.getInput().get("accountId"); + String noteId = (String) testScenario.getInput().get("noteId"); + + // Prepare mock responses + HttpResponse deleteNoteMockResponse = new HttpResponse(); + deleteNoteMockResponse.setResponseBody(objectMapper.writeValueAsString(testScenario.getMocks().get("makeCompositeRequest"))); + when(makeCompositeRequestMock.makePostRequest(any(), any())).thenReturn(deleteNoteMockResponse); + + // Perform the request + String url = "/api/v1/accounts/" + accountId + "/notes/" + noteId ; + + ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.delete(url) + .cookie(new Cookie(CookieConstants.USER_LOGIN_COOKIE_NAME, cookieValue)) + .contentType(MediaType.APPLICATION_JSON)); + + // Check the response + String expectedOutput = objectMapper.writeValueAsString(testScenario.getOutput()); + String actualOutput = resultActions.andReturn().getResponse().getContentAsString(); + assertCustomEquals(expectedOutput, actualOutput); + } + + static Stream testScenariosProvider() throws IOException { + List testScenarios = loadScenarios(); + return testScenarios.stream(); + } + + private static List loadScenarios() throws IOException { + String scenariosPath = "classpath:data/controllers/accountNoteController/deleteAccountNote.scenarios.json"; + Resource resource = new DefaultResourceLoader().getResource(scenariosPath); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(resource.getInputStream(), new TypeReference>() {}); + } + + // Custom assertion method that treats and <> as the same + public static void assertCustomEquals(String expected, String actual) { + + if (expected.equals("{}")) { + expected = ""; + } + + assertEquals(expected, actual); + } +} diff --git a/src/test/java/com/salessparrow/api/unit/utility/MemcachedTest.java b/src/test/java/com/salessparrow/api/unit/utility/MemcachedTest.java index 5316e39b..a3941aee 100644 --- a/src/test/java/com/salessparrow/api/unit/utility/MemcachedTest.java +++ b/src/test/java/com/salessparrow/api/unit/utility/MemcachedTest.java @@ -4,7 +4,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.cache.Cache; diff --git a/src/test/resources/data/controllers/accountNoteController/deleteAccountNote.scenarios.json b/src/test/resources/data/controllers/accountNoteController/deleteAccountNote.scenarios.json new file mode 100644 index 00000000..1fb5d853 --- /dev/null +++ b/src/test/resources/data/controllers/accountNoteController/deleteAccountNote.scenarios.json @@ -0,0 +1,53 @@ +[ + { + "description": "Should successfully delete the note for given note id", + "input": { + "accountId": "0011e00000bWSxdAAG", + "noteId":"0691e000001X1yTAAS" + }, + "mocks": { + "makeCompositeRequest": { + "compositeResponse": [ + { + "body": null, + "httpHeaders": {}, + "httpStatusCode": 204, + "referenceId": "DeleteNote" + } + ] + } + }, + "output": {} + }, + { + "description": "should return error response when note id is invalid", + "input": { + "accountId": "0011e00000bWSxdAAG", + "noteId":"invalidNoteId" + }, + "mocks": { + "makeCompositeRequest": { + "compositeResponse": [ + { + "body": [ + { + "errorCode": "NOT_FOUND", + "message": "Provided external ID field does not exist or is not accessible: INVALID_NOTE_ID" + } + ], + "httpHeaders": {}, + "httpStatusCode": 404, + "referenceId": "DeleteNote" + } + ] + } + }, + "output": { + "http_code": 500, + "message": "Something went wrong.", + "code": "INTERNAL_SERVER_ERROR", + "internal_error_identifier": "l_ca_dan_dasn_pr_1", + "param_errors": [] + } + } +] \ No newline at end of file diff --git a/src/test/resources/fixtures/controllers/accountNoteController/deleteAccountNote.fixtures.json b/src/test/resources/fixtures/controllers/accountNoteController/deleteAccountNote.fixtures.json new file mode 100644 index 00000000..bb801c75 --- /dev/null +++ b/src/test/resources/fixtures/controllers/accountNoteController/deleteAccountNote.fixtures.json @@ -0,0 +1,14 @@ +{ + "deleteAccountNote": { + "salesforce_users": [ + { + "filepath": "classpath:fixtures/repositories/salesforceUser/ActiveSalesforceUser.json" + } + ], + "salesforce_oauth_tokens": [ + { + "filepath": "classpath:fixtures/repositories/salesforceOauthToken/ActiveSalesforceOauthToken.json" + } + ] + } +} \ No newline at end of file