From 5f799dade77ce007d492b4e596ff81c2c09bfe27 Mon Sep 17 00:00:00 2001 From: Vince Jerald Villamora Date: Tue, 29 Aug 2023 23:18:10 +0800 Subject: [PATCH] fix(m360-api): codec rework --- .../fpi/sms/codec/M360CodecProvider.java | 9 ++- .../abavilla/fpi/sms/entity/sms/MsgReq.java | 4 +- .../fpi/sms/mapper/sms/MsgReqMapper.java | 1 + .../abavilla/fpi/sms/service/sms/M360Svc.java | 2 - .../fpi/sms/service/sms/MsgAckSvc.java | 59 +++++++++---------- pom.xml | 2 +- 6 files changed, 38 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/com/abavilla/fpi/sms/codec/M360CodecProvider.java b/core/src/main/java/com/abavilla/fpi/sms/codec/M360CodecProvider.java index a053afb..574063f 100644 --- a/core/src/main/java/com/abavilla/fpi/sms/codec/M360CodecProvider.java +++ b/core/src/main/java/com/abavilla/fpi/sms/codec/M360CodecProvider.java @@ -38,8 +38,7 @@ */ public class M360CodecProvider implements CodecProvider { - @SuppressWarnings("rawtypes") - private static final List discriminatorClasses; + private static final List> discriminatorClasses; private static final List ignoredFields; @@ -60,13 +59,13 @@ public Codec get(Class clazz, CodecRegistry registry) { } private Codec buildDiscriminatorCodec(Class clazz, CodecRegistry registry) { - var dscmntrMdlBldr = ClassModel.builder(clazz) + var discriminatorModelBuilder = ClassModel.builder(clazz) .enableDiscriminator(true); if (clazz == BroadcastRequest.class) { - stripNonProperties(dscmntrMdlBldr); + stripNonProperties(discriminatorModelBuilder); } return PojoCodecProvider.builder() - .register(dscmntrMdlBldr.build()) + .register(discriminatorModelBuilder.build()) .build().get(clazz, registry); } diff --git a/core/src/main/java/com/abavilla/fpi/sms/entity/sms/MsgReq.java b/core/src/main/java/com/abavilla/fpi/sms/entity/sms/MsgReq.java index 1837e58..4a827d2 100644 --- a/core/src/main/java/com/abavilla/fpi/sms/entity/sms/MsgReq.java +++ b/core/src/main/java/com/abavilla/fpi/sms/entity/sms/MsgReq.java @@ -25,6 +25,7 @@ import com.abavilla.fpi.telco.ext.enums.Telco; import com.vincejv.m360.dto.ApiRequest; import io.quarkus.mongodb.panache.common.MongoEntity; +import io.quarkus.runtime.annotations.RegisterForReflection; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -33,6 +34,7 @@ @Data @EqualsAndHashCode(callSuper = true) +@RegisterForReflection @NoArgsConstructor @BsonDiscriminator @MongoEntity(collection="m360_log") @@ -49,6 +51,6 @@ public class MsgReq extends AbsMongoItem { private String messageId; private List apiStatus; private List message; - @BsonProperty("request") + @BsonProperty(value = "request", useDiscriminator = true) private ApiRequest broadcastRequest; } diff --git a/core/src/main/java/com/abavilla/fpi/sms/mapper/sms/MsgReqMapper.java b/core/src/main/java/com/abavilla/fpi/sms/mapper/sms/MsgReqMapper.java index 33c6983..e6ad735 100644 --- a/core/src/main/java/com/abavilla/fpi/sms/mapper/sms/MsgReqMapper.java +++ b/core/src/main/java/com/abavilla/fpi/sms/mapper/sms/MsgReqMapper.java @@ -40,6 +40,7 @@ public interface MsgReqMapper extends IDtoToEntityMapper { MsgReq mapToEntity(MsgReqDto dto); MsgReq mapFromResponse(BroadcastResponseDto broadcastResponseDto); + @AfterMapping default void afterMappingFromResponse(BroadcastResponseDto broadcastResponseDto, @MappingTarget MsgReq msgReq) { msgReq.setTelco(Telco.fromId(broadcastResponseDto.getTelcoId())); diff --git a/core/src/main/java/com/abavilla/fpi/sms/service/sms/M360Svc.java b/core/src/main/java/com/abavilla/fpi/sms/service/sms/M360Svc.java index 3e1f8fa..34ce084 100644 --- a/core/src/main/java/com/abavilla/fpi/sms/service/sms/M360Svc.java +++ b/core/src/main/java/com/abavilla/fpi/sms/service/sms/M360Svc.java @@ -22,7 +22,6 @@ import com.abavilla.fpi.fw.exceptions.ApiSvcEx; import com.abavilla.fpi.fw.service.ISvc; -import com.abavilla.fpi.fw.util.DateUtil; import com.abavilla.fpi.sms.dto.api.m360.BroadcastResponseDto; import com.abavilla.fpi.sms.entity.enums.DCSCoding; import com.abavilla.fpi.sms.ext.dto.MsgReqDto; @@ -64,7 +63,6 @@ public Uni sendMsg(MsgReqDto msgReqDto) { } else { // fail dto.setCode(resp.getCode()); dto.setName(HttpResponseStatus.valueOf(resp.getCode()).reasonPhrase()); - dto.setTimestamp(DateUtil.now()); dto.setMessage(resp.getErrors().stream().map(ApiError::getMessage).collect(Collectors.toList())); } dto.setBroadcastRequest(smsRequest); diff --git a/core/src/main/java/com/abavilla/fpi/sms/service/sms/MsgAckSvc.java b/core/src/main/java/com/abavilla/fpi/sms/service/sms/MsgAckSvc.java index cbad5a2..9031d4b 100644 --- a/core/src/main/java/com/abavilla/fpi/sms/service/sms/MsgAckSvc.java +++ b/core/src/main/java/com/abavilla/fpi/sms/service/sms/MsgAckSvc.java @@ -59,36 +59,35 @@ public Uni acknowledge(String msgId, String ackStsCde, String ackTimestamp var byMsgId = msgReqRepo.findByMsgId(msgId); /* run in background, immediately return response to webhook */ - executor.execute(() -> - byMsgId.map(msgReqOpt -> msgReqOpt.orElseThrow(() -> - new ApiSvcEx("Message Id for acknowledgement not found: " + msgId))) - .onFailure(OptimisticLockEx.class).retry().indefinitely() - .onFailure(ApiSvcEx.class) - .retry().withBackOff(Duration.ofSeconds(3)).withJitter(0.2) - .atMost(5) // Retry for item not found and nothing else - .chain(msgReq -> { - var stateItem = new StateEncap(apiStatus, ackTime); - if (ObjectUtils.isEmpty(msgReq.getApiStatus())) { - msgReq.setApiStatus(List.of(stateItem)); - } else { - msgReq.getApiStatus().add(stateItem); - } - msgReq.setLastAcknowledgement(ackTime); - msgReq.setDateUpdated(LocalDateTime.now(ZoneOffset.UTC)); - return msgReqRepo.persistOrUpdate(msgReq); - }) - .onFailure().call(ex -> { - Log.error("Error leak ack", ex); - var leak = new LeakAck(); - leak.setDateCreated(LocalDateTime.now(ZoneOffset.UTC)); - leak.setDateUpdated(LocalDateTime.now(ZoneOffset.UTC)); - leak.setMsgId(msgId); - leak.setApiStatus(apiStatus); - leak.setTimestamp(ackTime); - return repo.persist(leak); - }) - .await().indefinitely() - ); + byMsgId.map(msgReqOpt -> msgReqOpt.orElseThrow(() -> + new ApiSvcEx("Message Id for acknowledgement not found: " + msgId))) + .onFailure(OptimisticLockEx.class).retry().indefinitely() + .onFailure(ApiSvcEx.class) + .retry().withBackOff(Duration.ofSeconds(3)).withJitter(0.2) + .atMost(5) // Retry for item not found and nothing else + .chain(msgReq -> { + var stateItem = new StateEncap(apiStatus, ackTime); + if (ObjectUtils.isEmpty(msgReq.getApiStatus())) { + msgReq.setApiStatus(List.of(stateItem)); + } else { + msgReq.getApiStatus().add(stateItem); + } + msgReq.setLastAcknowledgement(ackTime); + msgReq.setDateUpdated(LocalDateTime.now(ZoneOffset.UTC)); + return msgReqRepo.update(msgReq); + }) + .onFailure().call(ex -> { + Log.error("Error leak ack", ex); + var leak = new LeakAck(); + leak.setDateCreated(LocalDateTime.now(ZoneOffset.UTC)); + leak.setDateUpdated(LocalDateTime.now(ZoneOffset.UTC)); + leak.setMsgId(msgId); + leak.setApiStatus(apiStatus); + leak.setTimestamp(ackTime); + return repo.persist(leak); + }) + .subscribe().with(ignored -> { + }); return Uni.createFrom().voidItem(); } diff --git a/pom.xml b/pom.xml index 041b95b..c3ada08 100644 --- a/pom.xml +++ b/pom.xml @@ -98,7 +98,7 @@ com.vincejv m360-api-client - 1.0.5 + 1.0.7