added delete API for delegation

This commit is contained in:
rajesh
2024-10-09 12:52:34 +05:30
parent a244e9ef44
commit e8fc901b3f
9 changed files with 67 additions and 7 deletions

View File

@@ -195,5 +195,8 @@ public class GepafinConstant {
public static final String CALL_NOT_STARTED_YET = "call.not.started.yet";
public static final String CALL_ALREADY_ENDED = "call.already.ended";
public static final String APPLICATION_STATUS_UPDATED_SUCCESSFULLY = "application.status.updated.successfully";
public static final String DELEGATION_NOT_FOUND = "delegation.not.found";
public static final String USER_COMPANY_RELATION_NOT_FOUND = "user.company.relation.not.found";
public static final String DELEGATION_DELETE_SUCCESS = "delegation.delete.success";
}

View File

@@ -156,7 +156,7 @@ public class CompanyDao {
setIfUpdated(companyEntity::getContactName,companyEntity::setContactName,companyRequest.getContactName());
setIfUpdated(companyEntity::getContactEmail,companyEntity::setContactEmail,companyRequest.getContactEmail());
companyRepository.save(companyEntity);
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyId).orElse(null);
UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyId);
Utils.setIfUpdated(userWithCompanyEntity::getIsLegalRepresentant, userWithCompanyEntity::setIsLegalRepresentant,
companyRequest.getIsLegalRepresentant());
userWithCompanyRepository.save(userWithCompanyEntity);
@@ -169,7 +169,7 @@ public class CompanyDao {
}
public CompanyResponse getCompany(UserEntity userEntity, Long companyId) {
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyId).orElse(null);
UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyId);
return convertCompanyEntityToCompanyResponse(validateCompany(companyId), userWithCompanyEntity);
}
@@ -184,7 +184,7 @@ public class CompanyDao {
List<Long> companyIds = userWithCompanyRepository.findCompanyIdByUserId(userEntity.getId());
List<CompanyEntity> list = companyRepository.findByIdIn(companyIds);
return list.stream().map(companyEntity->{
UserWithCompanyEntity userWithCompanyEntity = userWithCompanyRepository.findByUserIdAndCompanyId(userEntity.getId(), companyEntity.getId()).orElse(null);
UserWithCompanyEntity userWithCompanyEntity = getUserWithCompany(userEntity.getId(), companyEntity.getId());
return convertCompanyEntityToCompanyResponse(companyEntity, userWithCompanyEntity);
}).toList();
}
@@ -193,5 +193,10 @@ public class CompanyDao {
return userWithCompanyRepository.findByUserIdAndCompanyId(userId, companyId).orElseThrow(() -> new CustomValidationException(Status.UNAUTHORIZED,
Translator.toLocale(GepafinConstant.UNAUTHORIZED)));
}
public UserWithCompanyEntity getUserWithCompany(Long userId, Long compnayId) {
return userWithCompanyRepository.findByUserIdAndCompanyId(userId, compnayId).orElseThrow(
() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_COMPANY_RELATION_NOT_FOUND)));
}
}

View File

@@ -93,6 +93,7 @@ public class DelegationDao {
Map<String, String> placeholders = getDefaultPlaceholders();
UserResponseBean user = userService.getUserById(userEntity.getId());
CompanyEntity companyEntity = companyDao.validateCompany(companyId);
companyDao.getUserWithCompany(userEntity.getId(), companyId);
updatePlaceholdersForDelegation(user, companyEntity, placeholders, companyDelegationRequest);
DocumentEntity documentEntity = documentRepository.findBySource(GepafinConstant.DELEGATION_TEMPLATE).get(0);
return generateDocument(placeholders, documentEntity.getFileName());
@@ -165,10 +166,11 @@ public class DelegationDao {
placeholders.put(key, formatter.apply(value));
}
}
public CompanyDelegationResponse uploadCompanyDelegation(UserEntity userEntity, Long companyId, MultipartFile file) {
companyDao.validateCompany(companyId);
companyDao.getUserWithCompany(userEntity.getId(), companyId);
validateFileType(file);
UserCompanyDelegationEntity userCompanyDelegationEntity = userCompanyDelegationRepository
.findByUserIdAndCompanyIdAndStatus(userEntity.getId(), companyId,
@@ -231,10 +233,24 @@ public class DelegationDao {
UserCompanyDelegationEntity userCompanyDelegationEntity = userCompanyDelegationRepository
.findByUserIdAndCompanyIdAndStatus(userEntity.getId(), companyId,
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
companyDao.getUserWithCompany(userEntity.getId(), companyId);
if(userCompanyDelegationEntity == null) {
throw new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale("validation.error.file.invalidType"));
Translator.toLocale(GepafinConstant.DELEGATION_NOT_FOUND));
}
return convertUserCompanyDelegationToCompanyDelegationResponse(userCompanyDelegationEntity);
}
public void deleteCompanyDelegation(UserEntity userEntity, Long companyId) {
UserCompanyDelegationEntity userCompanyDelegationEntity = userCompanyDelegationRepository
.findByUserIdAndCompanyIdAndStatus(userEntity.getId(), companyId,
UserCompanyDelegationStatusEnum.ACTIVE.getValue());
companyDao.getUserWithCompany(userEntity.getId(), companyId);
if(userCompanyDelegationEntity == null) {
throw new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.DELEGATION_NOT_FOUND));
}
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.INACTIVE.getValue());
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
}
}

View File

@@ -38,4 +38,6 @@ public interface CompanyService {
CompanyDelegationResponse getCompanyDelegation(HttpServletRequest request, Long companyId);
void deleteCompanyDelegation(HttpServletRequest request, Long companyId);
}

View File

@@ -108,4 +108,10 @@ public class CompanyServiceImpl implements CompanyService {
UserEntity userEntity =validator.validateUser(request);
return delegationDao.getCompanyDelegation(userEntity, companyId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteCompanyDelegation(HttpServletRequest request, Long companyId) {
UserEntity userEntity =validator.validateUser(request);
delegationDao.deleteCompanyDelegation(userEntity, companyId);
}
}

View File

@@ -131,5 +131,16 @@ public interface CompanyApi {
@GetMapping(value = "{companyId}/delegation", produces = { "application/json" })
ResponseEntity<Response<CompanyDelegationResponse>> getCompanyDelegation(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId);
@Operation(summary = "Api to delete company delegation", responses = { @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
@DeleteMapping(value = "{companyId}/delegation", produces = { "application/json" })
ResponseEntity<Response<Void>> deleteCompanyDelegation(HttpServletRequest request,
@Parameter(description = "The company id", required = true) @PathVariable("companyId") Long companyId);
}

View File

@@ -108,17 +108,24 @@ public class CompanyApiController implements CompanyApi{
MultipartFile file) {
log.info("upload company delegation with companyId: {}", companyId);
CompanyDelegationResponse companyDelegationResponse = companyService.uploadCompanyDelegation(request, companyId, file);
return ResponseEntity.status(HttpStatus.OK)
return ResponseEntity.status(HttpStatus.CREATED)
.body(new Response<>(companyDelegationResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.DELEGATION_FILE_UPLOAD_SUCCESS)));
}
@Override
public ResponseEntity<Response<CompanyDelegationResponse>> getCompanyDelegation(HttpServletRequest request,
Long companyId) {
log.info("get company delegation with companyId: {}", companyId);
CompanyDelegationResponse companyDelegationResponse = companyService.getCompanyDelegation(request, companyId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(companyDelegationResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.DELEGATION_FETCH_SUCCESS)));
}
@Override
public ResponseEntity<Response<Void>> deleteCompanyDelegation(HttpServletRequest request,
Long companyId) {
log.info("delete company delegation with companyId: {}", companyId);
companyService.deleteCompanyDelegation(request, companyId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(null, Status.SUCCESS, Translator.toLocale(GepafinConstant.DELEGATION_DELETE_SUCCESS)));
}
}

View File

@@ -220,3 +220,8 @@ call.not.started.yet = The call has not started yet. Please wait until the speci
call.already.ended = The call has already ended. You cannot submit the application after the deadline.
status.updated.successfully=Status updated successfully.
application.status.updated.successfully = Application status updated successfully.
delegation.not.found=Delegation not found.
user.company.relation.not.found=User with the specified company relation not found.
delegation.delete.success=Delegation deleted successfully.

View File

@@ -215,3 +215,8 @@ call.not.started.yet = La chiamata non <20> ancora iniziata. Attendere fino alla
call.already.ended = La chiamata <20> gi<67> terminata. Non <20> possibile inviare l'applicazione dopo la scadenza.
status.updated.successfully=Stato aggiornato con successo.
application.status.updated.successfully = Stato dell'applicazione aggiornato con successo.
delegation.not.found=Delega non trovata.
user.company.relation.not.found=Relazione utente con l'azienda specificata non trovata.
delegation.delete.success=Delega eliminata con successo.