Skip to content

Commit

Permalink
Merge pull request #49 from KBDS-Dreamverse/feat/#17
Browse files Browse the repository at this point in the history
feat : Contract expire 개발 완료
  • Loading branch information
CEO-Nick authored Feb 11, 2024
2 parents a317423 + e0eae4f commit 84846fa
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.kbds.itamserveradmin.domain.contract.controller;

import com.kbds.itamserveradmin.domain.contract.dto.CalKeyRes;
import com.kbds.itamserveradmin.domain.contract.dto.ContExpireRes;
import com.kbds.itamserveradmin.domain.contract.dto.DashBoardRes;
import com.kbds.itamserveradmin.domain.contract.dto.PasswordReq;
import com.kbds.itamserveradmin.domain.contract.service.ContractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import static com.kbds.itamserveradmin.global.exception.ErrorCode.CONTRACT_IS_NOT_CAL_LIC;
Expand All @@ -26,10 +26,16 @@ public ResponseEntity<DashBoardRes> getDashboard(
@PathVariable String dept,
@PathVariable String contId,
@RequestHeader String userId) {
DashBoardRes dashBoardRes = contractService.createDashBoard(contId, userId);
return ok(dashBoardRes);
try {
DashBoardRes dashBoardRes = contractService.createDashBoard(contId, userId);
return ok(dashBoardRes);
} catch (IllegalArgumentException e) {
// CONTRACT_NOT_FOUND 예외 처리
return ResponseEntity.notFound().build();
}
}


@PostMapping("/kbitam/{dept}/{contId}/cal")
public ResponseEntity<?> findCalKey(
@PathVariable(name = "contId") String contId,
Expand All @@ -47,7 +53,36 @@ public ResponseEntity<?> findCalKey(
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(CONTRACT_IS_NOT_CAL_LIC);
}
return ResponseEntity.ok(calKey);
}

@GetMapping("/kbitam/{dept}/{contId}/expire")
public ResponseEntity<ContExpireRes> expire(
@PathVariable String dept,
@PathVariable String contId) {

return ResponseEntity.ok(contractService.getExpire(contId));
}

// @PatchMapping("/kbitam/{dept}/{contId}/renewal")
// public ResponseEntity<?> renewal(
// @PathVariable String dept,
// @PathVariable String contId) {
//
// }
@PatchMapping("/kbitam/{dept}/{contId}/stop")
public ResponseEntity<?> stop(
@PathVariable String dept,
@PathVariable String contId) {
try {
contractService.stopContract(contId);
return ResponseEntity.ok("Successfully stop the contract!");
} catch (IllegalArgumentException e) {
return ResponseEntity.notFound().build();
} catch (IllegalStateException e) {
// CONTRACT_IS_ALREADY_IN_DISPOSAL 예외 처리
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kbds.itamserveradmin.domain.contract.dto;

import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Builder
public class ContExpireRes {

private String contStartDate;

private String contEndDate;

private Long remainingDays;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kbds.itamserveradmin.domain.contract.dto;

import com.kbds.itamserveradmin.domain.contract.entity.OpStatus;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class StopContRes {
private OpStatus opStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ContractRepository extends JpaRepository<Contract, String> {
Contract findByContId (String contId);
// Contract findByContId (String contId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.kbds.itamserveradmin.domain.contract.service;

import com.kbds.itamserveradmin.domain.asset.entity.Asset;
import com.kbds.itamserveradmin.domain.assetRequest.entity.UserAssetRequestInfo;
import com.kbds.itamserveradmin.domain.assetRequest.repository.AssetRequestRepository;
import com.kbds.itamserveradmin.domain.assetRequest.repository.UserAssetRequestInfoRepository;
import com.kbds.itamserveradmin.domain.assetRequest.service.AssetRequestService;
import com.kbds.itamserveradmin.domain.contract.dto.CalKeyRes;
import com.kbds.itamserveradmin.domain.contract.dto.ContExpireRes;
import com.kbds.itamserveradmin.domain.contract.dto.DashBoardRes;
import com.kbds.itamserveradmin.domain.contract.entity.*;
import com.kbds.itamserveradmin.domain.contract.repository.ContractRepository;
Expand All @@ -16,9 +18,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

import static com.kbds.itamserveradmin.domain.contract.entity.OpStatus.IN_DISPOSAL;
import static com.kbds.itamserveradmin.domain.contract.entity.OpStatus.IN_OPERATION;
import static com.kbds.itamserveradmin.global.exception.ErrorCode.CONTRACT_IS_ALREADY_IN_DISPOSAL;
import static com.kbds.itamserveradmin.global.exception.ErrorCode.CONTRACT_NOT_FOUND;

@Slf4j
Expand Down Expand Up @@ -75,7 +82,8 @@ public static List<String> parseContLicTag(String contLicTag) {
}
//Ast id 찾는 메서드
public Asset getAstIdByContId(String contId){
Contract contract = contractRepository.findByContId(contId);
Contract contract = contractRepository.findById(contId)
.orElseThrow(() -> new IllegalArgumentException(String.valueOf(CONTRACT_NOT_FOUND)));
if (contract == null){
return null;
}
Expand Down Expand Up @@ -114,12 +122,10 @@ public DashBoardRes createDashBoard(String contId, String userId) {

// 3.2 기간 (1. yml에서 write-dates-as-timestamps: false 로 설정해도 날짜 리스트로 출력)
// (2. PeriodType에서 @JsonFormat으로 설정해도 수정 안된다)
PeriodType periodType = periodTypeRepository.findByCont_ContId(contId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedStartDate = periodType.getContStartDate().format(formatter);
String formattedEndDate = periodType.getContEndDate().format(formatter);
licValues.put("contStartDate", formattedStartDate);
licValues.put("contEndDate", formattedEndDate);
List<LocalDateTime> dateTimeList = getPeriod(contId);
List<String> formattedDate = getFormattedDate(dateTimeList);
licValues.put("contStartDate", formattedDate.get(0));
licValues.put("contEndDate", formattedDate.get(1));


// 3.3 사용자
Expand Down Expand Up @@ -192,4 +198,47 @@ public CalKeyRes getCalKey(String userId, String contId) {
.build();
}

public List<LocalDateTime> getPeriod(String contId) {
List<LocalDateTime> period = new ArrayList<>(2);
PeriodType periodType = periodTypeRepository.findByCont_ContId(contId);
period.add(periodType.getContStartDate());
period.add(periodType.getContEndDate());
return period;
}

public List<String> getFormattedDate(List<LocalDateTime> dateTimeList) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedStartDate = dateTimeList.get(0).format(formatter);
String formattedEndDate = dateTimeList.get(1).format(formatter);

List<String> formatted = new ArrayList<>();
formatted.add(formattedStartDate);
formatted.add(formattedEndDate);
return formatted;
}

public ContExpireRes getExpire(String contId) {
List<LocalDateTime> period = getPeriod(contId);
List<String> formattedDate = getFormattedDate(period);

Long between = ChronoUnit.DAYS.between(LocalDateTime.now(), period.get(1));

return ContExpireRes.builder()
.contStartDate(formattedDate.get(0))
.contEndDate(formattedDate.get(1))
.remainingDays(between)
.build();
}

public void stopContract(String contId) {
Contract contract = contractRepository.findById(contId)
.orElseThrow(() -> new IllegalArgumentException(String.valueOf(CONTRACT_NOT_FOUND)));

if (contract.getContOpStatus() == IN_OPERATION) {
contract.setContOpStatus(IN_DISPOSAL);
} else {
throw new IllegalStateException(String.valueOf(CONTRACT_IS_ALREADY_IN_DISPOSAL));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ErrorCode {
CONTRACT_RECORD_TYPE_NOT_FOUND(HttpStatus.NOT_FOUND, "C-001", "활동 기록 타입을 찾을 수 없습니다."),
CONTRACT_NOT_FOUND(HttpStatus.NOT_FOUND, "C-002", "해당 계약을 찾을 수 없습니다."),
CONTRACT_IS_NOT_CAL_LIC(HttpStatus.FORBIDDEN, "C-003", "해당 계약은 cal 라이선스가 아닙니다."),
CONTRACT_IS_ALREADY_IN_DISPOSAL(HttpStatus.CONFLICT, "C-004", "해당 계약은 이미 IN_DISPOSAL 상태입니다."),

//기타
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "I-001", "내부 에러가 발생했습니다."),
Expand Down

0 comments on commit 84846fa

Please sign in to comment.