Skip to content

Commit

Permalink
feat : set amfi when ISIN is null (#395)
Browse files Browse the repository at this point in the history
* feat : set amfi when ISIN is null

* address code review comment
  • Loading branch information
rajadilipkolli authored Aug 10, 2024
1 parent 8f7dd2a commit 61be2ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,36 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface MFSchemeRepository extends JpaRepository<MFSchemeEntity, Long> {

@Transactional(readOnly = true)
Optional<MFSchemeEntity> findByPayOut(String payOut);

@EntityGraph(attributePaths = {"mfSchemeTypeEntity", "mfSchemeNavEntities"})
@Transactional(readOnly = true)
Optional<MFSchemeEntity> findBySchemeIdAndMfSchemeNavEntities_NavDate(
@Param("schemeCode") Long schemeCode, @Param("date") LocalDate navDate);

@EntityGraph(attributePaths = {"mfSchemeTypeEntity", "mfSchemeNavEntities"})
@Transactional(readOnly = true)
Optional<MFSchemeEntity> findBySchemeId(@Param("schemeId") Long schemeId);

@Query(
"""
select new com.learning.mfscreener.models.projection.FundDetailProjection(m.schemeId, m.schemeName) from MFSchemeEntity m
where upper(m.schemeName) like upper(:schemeName) order by m.schemeId
where UPPER(REPLACE(m.schemeName, '- ', '')) like upper(:schemeName) order by m.schemeId
""")
@Transactional(readOnly = true)
List<FundDetailProjection> findBySchemeNameLikeIgnoreCaseOrderBySchemeIdAsc(@Param("schemeName") String schemeName);

@Query(
"""
select new com.learning.mfscreener.models.projection.FundDetailProjection(m.schemeId, m.schemeName) from MFSchemeEntity m
where upper(m.fundHouse) like upper(:fName) order by m.schemeId
""")
@Transactional(readOnly = true)
List<FundDetailProjection> findByFundHouseLikeIgnoringCaseOrderBySchemeIdAsc(@Param("fName") String fName);

@Query("select m.schemeId from MFSchemeEntity m where m.payOut = :isin order by m.schemeId")
List<Long> getSchemeIdByISIN(@Param("isin") String isin);

@Transactional(readOnly = true)
@Query("select o.schemeId from MFSchemeEntity o")
List<Long> findAllSchemeIds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
Expand All @@ -17,7 +18,7 @@ public interface UserSchemeDetailsEntityRepository extends JpaRepository<UserSch
@Transactional(readOnly = true)
List<UserSchemeDetailsEntity> findByAmfiIsNull();

@Transactional
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Modifying
@Query("update UserSchemeDetailsEntity u set u.amfi = ?1, u.isin = ?2 where u.id = ?3")
int updateAmfiAndIsinById(Long amfi, String isin, Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.learning.mfscreener.config.logging.Loggable;
import com.learning.mfscreener.entities.MFSchemeEntity;
import com.learning.mfscreener.entities.UserSchemeDetailsEntity;
import com.learning.mfscreener.models.projection.FundDetailProjection;
import com.learning.mfscreener.models.projection.SchemeNameAndISIN;
import com.learning.mfscreener.repository.UserSchemeDetailsEntityRepository;
import java.util.List;
Expand Down Expand Up @@ -58,10 +59,31 @@ public void setAMFIIfNull() {
mfSchemeEntity.ifPresent(schemeEntity -> userSchemeDetailsEntityRepository.updateAmfiAndIsinById(
schemeEntity.getSchemeId(), isin, userSchemeDetailsEntity.getId()));
}
} else {
// case where isin and amfi is null
List<FundDetailProjection> fundDetailProjections = schemeService.fetchSchemes(scheme);
if (!fundDetailProjections.isEmpty()) {
Long schemeId = getSchemeId(fundDetailProjections, scheme);
if (null != schemeId) {
userSchemeDetailsEntityRepository.updateAmfiAndIsinById(
schemeId, null, userSchemeDetailsEntity.getId());
}
}
}
});
}

Long getSchemeId(List<FundDetailProjection> fundDetailProjections, String scheme) {
return fundDetailProjections.stream()
.filter(fundDetailProjection -> (scheme.contains("Income")
&& fundDetailProjection.schemeName().contains("IDCW"))
|| (!scheme.contains("Income")
&& !fundDetailProjection.schemeName().contains("IDCW")))
.map(FundDetailProjection::schemeId)
.findFirst()
.orElse(null);
}

@Loggable
public void loadHistoricalDataIfNotExists() {
List<Long> historicalDataNotLoadedSchemeIdList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class SQLContainersConfig {
@ServiceConnection
@RestartScope
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.3-alpine"));
return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.4-alpine"));
}
}

0 comments on commit 61be2ca

Please sign in to comment.