260727_01_ENH: call history에 상태 추가 및 call start, call close로 분리 #7

Merged
gitadmin merged 1 commits from develop into master 2026-07-27 16:39:12 +09:00
10 changed files with 216 additions and 51 deletions
Showing only changes of commit a44ade105d - Show all commits

View File

@@ -0,0 +1,35 @@
package com.kri.contact.server.common.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.kri.common.feature.enums.CodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter(onMethod_ = @JsonValue)
@RequiredArgsConstructor
@Schema(description = "전화 상태")
public enum CallStatus implements CodeEnum<Short> {
@Schema(description = "전화 시작")
CALL_START((short) 2),
@Schema(description = "전화 종료")
CALL_CLOSE((short) 4),
@Schema(description = "사용자 전화 종료")
USER_HANGUP((short) 9);
private final Short code;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static CallStatus fromCode(Short code) {
if (code == null) {
return null;
}
for (CallStatus s : values()) {
if (s.code.equals(code)) {
return s;
}
}
throw new IllegalArgumentException("Unknown CallStatus code: " + code);
}
}

View File

@@ -0,0 +1,13 @@
package com.kri.contact.server.common.jpa;
import com.kri.common.feature.jpa.AbstractCodeEnumConverter;
import com.kri.contact.server.common.enums.CallStatus;
import jakarta.persistence.Converter;
@Converter(autoApply = false)
public class CallStatusConverter extends AbstractCodeEnumConverter<CallStatus, Short> {
public CallStatusConverter() {
super(CallStatus.class);
}
}

View File

@@ -3,7 +3,8 @@ package com.kri.contact.server.controller;
import com.kri.common.feature.datetime.DateTimeUtil;
import com.kri.common.feature.web.RequestUserContext;
import com.kri.contact.server.dto.list.ListCallHistDto;
import com.kri.contact.server.dto.list.ListCallHistRequestDto;
import com.kri.contact.server.dto.list.ListCallCloseRequestDto;
import com.kri.contact.server.dto.list.ListCallStartRequestDto;
import com.kri.contact.server.service.ListCallHistService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -11,6 +12,7 @@ import jakarta.validation.Valid;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -25,16 +27,36 @@ public class ListCallHistController {
private final ListCallHistService listCallHistService;
@Operation(summary = "Save list call history")
@PutMapping("/{prjctIdx}/{sid}")
public ListCallHistDto saveCallHistory(
@Operation(summary = "Start list call history")
@PutMapping("/{prjctIdx}/{sid}/call-start")
public ListCallHistDto callStart(
@PathVariable Long prjctIdx,
@PathVariable Integer sid,
@Valid @RequestBody ListCallHistRequestDto request,
@Valid @RequestBody ListCallStartRequestDto request,
RequestUserContext ctx
) {
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
return listCallHistService.saveCallHistory(
return listCallHistService.callStart(
prjctIdx,
sid,
request,
ctx.getUserId(),
ctx.getUserCmpn(),
ctx.getUserRole(),
now
);
}
@Operation(summary = "Close list call history")
@PutMapping("/{prjctIdx}/{sid}/call-close")
public ListCallHistDto callClose(
@PathVariable Long prjctIdx,
@PathVariable Integer sid,
@Valid @RequestBody ListCallCloseRequestDto request,
RequestUserContext ctx
) {
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
return listCallHistService.callClose(
prjctIdx,
sid,
request,
@@ -58,4 +80,13 @@ public class ListCallHistController {
ctx.getUserRole()
);
}
@Operation(summary = "Update Call History at Client Hangup")
@PatchMapping("hangup/{agentSessionId}")
public boolean hangupClient(
@PathVariable String agentSessionId,
RequestUserContext ctx
) {
return listCallHistService.hangupClient(agentSessionId);
}
}

View File

@@ -0,0 +1,22 @@
package com.kri.contact.server.dto.list;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@Schema(description = "ListCallCloseRequestDto")
public class ListCallCloseRequestDto extends ListCallStartRequestDto {
@NotNull
@Schema(description = "totalTime")
private Integer totalTime;
@NotNull
@Schema(description = "talkTime")
private Integer talkTime;
}

View File

@@ -1,8 +1,8 @@
package com.kri.contact.server.dto.list;
import io.swagger.v3.oas.annotations.media.Schema;
import com.kri.contact.server.common.enums.CallStatus;
import com.kri.contact.server.persistence.entity.TListCallHistEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
@@ -32,6 +32,8 @@ public class ListCallHistDto {
private String destSessionId;
@Schema(description = "callTy")
private String callTy;
@Schema(description = "callStatus")
private CallStatus callStatus;
@Schema(description = "startDt")
private LocalDateTime startDt;
@Schema(description = "endDt")
@@ -63,6 +65,7 @@ public class ListCallHistDto {
.agentSessionId(entity.getAgentSessionId())
.destSessionId(entity.getDestSessionId())
.callTy(entity.getCallTy())
.callStatus(entity.getCallStatus())
.startDt(entity.getStartDt())
.endDt(entity.getEndDt())
.totalTime(entity.getTotalTime())

View File

@@ -1,11 +1,9 @@
package com.kri.contact.server.dto.list;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.Instant;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -14,8 +12,8 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@Schema(description = "ListCallHistRequestDto")
public class ListCallHistRequestDto {
@Schema(description = "ListCallStartRequestDto")
public class ListCallStartRequestDto {
@NotNull
@Schema(description = "listHderId")
@@ -55,14 +53,6 @@ public class ListCallHistRequestDto {
@Schema(description = "startDt")
private LocalDateTime startDt;
@NotNull
@Schema(description = "totalTime")
private Integer totalTime;
@NotNull
@Schema(description = "talkTime")
private Integer talkTime;
@NotNull
@Schema(description = "mediaServerId")
private Integer mediaServerId;

View File

@@ -1,7 +1,10 @@
package com.kri.contact.server.persistence.entity;
import com.kri.contact.server.dto.list.ListCallHistRequestDto;
import com.kri.contact.server.common.enums.CallStatus;
import com.kri.contact.server.common.jpa.CallStatusConverter;
import com.kri.contact.server.dto.list.ListCallCloseRequestDto;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@@ -10,6 +13,7 @@ import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.Duration;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -76,6 +80,11 @@ public class TListCallHistEntity {
@Column(name = "call_ty", nullable = false, length = 64)
private String callTy;
@NotNull
@Convert(converter = CallStatusConverter.class)
@Column(name = "call_status", nullable = false)
private CallStatus callStatus;
@NotNull
@Column(name = "start_dt", nullable = false)
private LocalDateTime startDt;
@@ -118,7 +127,7 @@ public class TListCallHistEntity {
public static TListCallHistEntity from(
Long prjctIdx,
Integer sid,
ListCallHistRequestDto request,
ListCallCloseRequestDto request,
String regUser,
LocalDateTime regDt
) {
@@ -132,6 +141,7 @@ public class TListCallHistEntity {
.agentSessionId(request.getAgentSessionId())
.destSessionId(request.getDestSessionId())
.callTy(request.getCallTy())
.callStatus(resolveCallStatus(request))
.startDt(request.getStartDt())
.endDt(resolveEndDt(request, regDt))
.totalTime(request.getTotalTime())
@@ -144,9 +154,10 @@ public class TListCallHistEntity {
.build();
}
public void updateCallResult(ListCallHistRequestDto request, LocalDateTime endDt) {
public void updateCallResult(ListCallCloseRequestDto request, LocalDateTime endDt) {
if (isCompletedRequest(request)) {
this.endDt = endDt;
this.callStatus = CallStatus.CALL_CLOSE;
}
if (isPositive(request.getTotalTime())) {
this.totalTime = request.getTotalTime();
@@ -161,14 +172,38 @@ public class TListCallHistEntity {
}
}
private static LocalDateTime resolveEndDt(ListCallHistRequestDto request, LocalDateTime now) {
public boolean markUserHangup(LocalDateTime endDt) {
if (this.callStatus == CallStatus.CALL_CLOSE) {
return false;
}
LocalDateTime resolvedEndDt = this.endDt == null ? endDt : this.endDt;
int durationSeconds = durationSeconds(this.startDt, resolvedEndDt);
this.endDt = resolvedEndDt;
this.totalTime = durationSeconds;
this.talkTime = durationSeconds;
this.callStatus = CallStatus.USER_HANGUP;
return true;
}
private static LocalDateTime resolveEndDt(ListCallCloseRequestDto request, LocalDateTime now) {
return isCompletedRequest(request) ? now : null;
}
private static boolean isCompletedRequest(ListCallHistRequestDto request) {
private static CallStatus resolveCallStatus(ListCallCloseRequestDto request) {
return isCompletedRequest(request) ? CallStatus.CALL_CLOSE : CallStatus.CALL_START;
}
private static boolean isCompletedRequest(ListCallCloseRequestDto request) {
return isPositive(request.getTotalTime())
|| isPositive(request.getTalkTime())
|| hasText(request.getRecordFileName());
|| isPositive(request.getTalkTime());
}
private static int durationSeconds(LocalDateTime startDt, LocalDateTime endDt) {
if (startDt == null || endDt == null || endDt.isBefore(startDt)) {
return 0;
}
long seconds = Duration.between(startDt, endDt).getSeconds();
return seconds > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) seconds;
}
private static boolean isPositive(Integer value) {

View File

@@ -1,6 +1,8 @@
package com.kri.contact.server.persistence.jdbc;
import com.kri.contact.server.dto.list.ListCallHistRequestDto;
import com.kri.contact.server.common.enums.CallStatus;
import com.kri.contact.server.dto.list.ListCallCloseRequestDto;
import com.kri.contact.server.dto.list.ListCallStartRequestDto;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -15,11 +17,12 @@ public class ListCallHistJdbcSupport {
public void upsertCallHistory(
Long prjctIdx,
Integer sid,
ListCallHistRequestDto request,
ListCallStartRequestDto request,
CallStatus callStatus,
String regUser,
LocalDateTime now
) {
LocalDateTime endDt = isCompletedRequest(request) ? now : null;
LocalDateTime endDt = callStatus == CallStatus.CALL_CLOSE ? now : null;
jdbcTemplate.update("""
insert into t_list_call_hist (
prjct_idx,
@@ -31,6 +34,7 @@ public class ListCallHistJdbcSupport {
agent_session_id,
dest_session_id,
call_ty,
call_status,
start_dt,
end_dt,
total_time,
@@ -40,7 +44,7 @@ public class ListCallHistJdbcSupport {
record_file_name,
reg_user,
reg_dt
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
on duplicate key update
list_call_hist_idx = last_insert_id(list_call_hist_idx),
dest_num = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))), values(dest_num), dest_num),
@@ -48,6 +52,7 @@ public class ListCallHistJdbcSupport {
ani_num = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))), values(ani_num), ani_num),
dest_session_id = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))) and (dest_session_id is null or dest_session_id = ''), values(dest_session_id), dest_session_id),
call_ty = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))), values(call_ty), call_ty),
call_status = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))) and values(call_status) = 4, values(call_status), call_status),
start_dt = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))) and values(start_dt) < start_dt, values(start_dt), start_dt),
end_dt = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))) and values(end_dt) is not null, values(end_dt), end_dt),
total_time = if((prjct_idx <=> values(prjct_idx) and sid <=> values(sid) and list_hder_id <=> values(list_hder_id) and (dest_session_id is null or dest_session_id = '' or dest_session_id = values(dest_session_id))) and values(total_time) > 0, values(total_time), total_time),
@@ -65,10 +70,11 @@ public class ListCallHistJdbcSupport {
request.getAgentSessionId(),
request.getDestSessionId(),
request.getCallTy(),
callStatus.getCode(),
request.getStartDt(),
endDt,
request.getTotalTime(),
request.getTalkTime(),
totalTime(request),
talkTime(request),
request.getMediaServerId(),
request.getMediaServerName(),
defaultString(request.getRecordFileName()),
@@ -77,18 +83,12 @@ public class ListCallHistJdbcSupport {
);
}
private boolean isCompletedRequest(ListCallHistRequestDto request) {
return isPositive(request.getTotalTime())
|| isPositive(request.getTalkTime())
|| hasText(request.getRecordFileName());
private Integer totalTime(ListCallStartRequestDto request) {
return request instanceof ListCallCloseRequestDto closeRequest ? closeRequest.getTotalTime() : 0;
}
private boolean isPositive(Integer value) {
return value != null && value > 0;
}
private boolean hasText(String value) {
return value != null && !value.isBlank();
private Integer talkTime(ListCallStartRequestDto request) {
return request instanceof ListCallCloseRequestDto closeRequest ? closeRequest.getTalkTime() : 0;
}
private String defaultString(String value) {
@@ -96,4 +96,3 @@ public class ListCallHistJdbcSupport {
}
}

View File

@@ -6,7 +6,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface ListCallHistRepository extends JpaRepository<TListCallHistEntity, Long> {
Optional<TListCallHistEntity> findFirstByAgentSessionId(String agentSessionId);
Optional<TListCallHistEntity> findByAgentSessionId(String agentSessionId);
}

View File

@@ -2,8 +2,10 @@ package com.kri.contact.server.service;
import com.kri.common.feature.web.BusinessException;
import com.kri.contact.server.common.consts.ContactConstant;
import com.kri.contact.server.common.enums.CallStatus;
import com.kri.contact.server.dto.list.ListCallHistDto;
import com.kri.contact.server.dto.list.ListCallHistRequestDto;
import com.kri.contact.server.dto.list.ListCallCloseRequestDto;
import com.kri.contact.server.dto.list.ListCallStartRequestDto;
import com.kri.contact.server.persistence.entity.TListCallHistEntity;
import com.kri.contact.server.persistence.jdbc.ListCallHistJdbcSupport;
import com.kri.contact.server.persistence.repository.ListAssignCmpnRepository;
@@ -33,10 +35,36 @@ public class ListCallHistService {
private final ListAssignCmpnRepository listAssignCmpnRepository;
@Transactional
public ListCallHistDto saveCallHistory(
public ListCallHistDto callStart(
Long prjctIdx,
Integer sid,
ListCallHistRequestDto request,
ListCallStartRequestDto request,
String regUser,
String userCmpn,
Integer userRole,
LocalDateTime regDt
) {
return saveCallHistory(prjctIdx, sid, request, CallStatus.CALL_START, regUser, userCmpn, userRole, regDt);
}
@Transactional
public ListCallHistDto callClose(
Long prjctIdx,
Integer sid,
ListCallCloseRequestDto request,
String regUser,
String userCmpn,
Integer userRole,
LocalDateTime regDt
) {
return saveCallHistory(prjctIdx, sid, request, CallStatus.CALL_CLOSE, regUser, userCmpn, userRole, regDt);
}
private ListCallHistDto saveCallHistory(
Long prjctIdx,
Integer sid,
ListCallStartRequestDto request,
CallStatus callStatus,
String regUser,
String userCmpn,
Integer userRole,
@@ -60,7 +88,7 @@ public class ListCallHistService {
validateUsableCallHistory(existing, regUser, userCmpn, userRole, "CALL_HISTORY_NOT_FOUND");
});
listCallHistJdbcSupport.upsertCallHistory(prjctIdx, sid, request, regUser, regDt);
listCallHistJdbcSupport.upsertCallHistory(prjctIdx, sid, request, callStatus, regUser, regDt);
TListCallHistEntity entity = listCallHistRepository.findByAgentSessionId(request.getAgentSessionId())
.orElseThrow(() -> BusinessException.notFound(
@@ -125,6 +153,16 @@ public class ListCallHistService {
validateUsableCallHistory(entity, userId, userCmpn, userRole, "CALL_HISTORY_FORBIDDEN");
}
@Transactional
public boolean hangupClient(
String agentSessionId
) {
validateAgentSessionId(agentSessionId);
return listCallHistRepository.findByAgentSessionId(agentSessionId)
.map(entity -> entity.markUserHangup(LocalDateTime.now()))
.orElse(false);
}
private void validateAgentSessionId(String agentSessionId) {
if (!hasText(agentSessionId) || agentSessionId.length() > 64) {
throw BusinessException.badRequest(
@@ -139,7 +177,7 @@ public class ListCallHistService {
TListCallHistEntity existing,
Long prjctIdx,
Integer sid,
ListCallHistRequestDto request
ListCallStartRequestDto request
) {
rejectDifferent("prjctIdx", existing.getPrjctIdx(), prjctIdx);
rejectDifferent("sid", existing.getSid(), sid);
@@ -223,4 +261,5 @@ public class ListCallHistService {
private boolean hasText(String value) {
return value != null && !value.isBlank();
}
}