diff --git a/settings.gradle.kts b/settings.gradle.kts index f42c3a3..c37ff10 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,3 @@ pluginManagement { repositories { gradlePluginPortal() } } -rootProject.name = "pagopa-ecommerce-helpdesk-service" \ No newline at end of file +rootProject.name = "pagopa-ecommerce-helpdesk-service" diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmQueries.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmQueries.kt new file mode 100644 index 0000000..b313564 --- /dev/null +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmQueries.kt @@ -0,0 +1,65 @@ +package it.pagopa.ecommerce.helpdesk.services + +fun buildTransactionByUserEmailQuery(userEmail: String) = + """ + SELECT pu.FISCAL_CODE , pu.NOTIFICATION_EMAIL , pu.SURNAME , pu.NAME , pu.USERNAME , pu.STATUS , + pt.CREATION_DATE , pt.STATUS ,pt.ACCOUNTING_STATUS , pt.AMOUNT , pt.FEE , pt.GRAND_TOTAL ,pt.FK_PAYMENT , + pp.ID ,pp.AMOUNT , pp.SUBJECT , pp.ORIGIN , + ppd.IUV , ppd.CCP, ppd.ENTE_BENEFICIARIO , ppd.IMPORTO , ppd.ID_DOMINIO , + pp2.ID_PSP , pp2.BUSINESS_NAME , pp2.ID_CHANNEL + FROM AGID_USER.PP_USER pu + left JOIN AGID_USER.PP_TRANSACTION pt ON pu.ID_USER =pt.FK_USER + left JOIN AGID_USER.PP_PAYMENT pp ON pt.FK_PAYMENT = pp.ID + left JOIN AGID_USER.PP_PAYMENT_DETAIL ppd ON pp.ID =ppd.PAYMENT_ID + left JOIN AGID_USER.PP_PSP pp2 ON pt.FK_PSP = pp2.ID + WHERE pu.NOTIFICATION_EMAIL ='$userEmail' + AND PT.AMOUNT > 1 + ORDER BY PT.CREATION_DATE DESC + """ + .trimIndent() + +fun buildTransactionByUserEmailCountQuery(userEmail: String) = + """ + SELECT COUNT(*) + FROM AGID_USER.PP_USER pu + left JOIN AGID_USER.PP_TRANSACTION pt ON pu.ID_USER =pt.FK_USER + left JOIN AGID_USER.PP_PAYMENT pp ON pt.FK_PAYMENT = pp.ID + left JOIN AGID_USER.PP_PAYMENT_DETAIL ppd ON pp.ID =ppd.PAYMENT_ID + left JOIN AGID_USER.PP_PSP pp2 ON pt.FK_PSP = pp2.ID + WHERE pu.NOTIFICATION_EMAIL ='$userEmail' + AND PT.AMOUNT > 1 + ORDER BY PT.CREATION_DATE DESC + """ + .trimIndent() + +fun buildTransactionByFiscalCodeQuery(fiscalCode: String) = + """ + SELECT pu.FISCAL_CODE , pu.NOTIFICATION_EMAIL , pu.SURNAME , pu.NAME , pu.USERNAME , + pt.CREATION_DATE , pt.STATUS ,pt.ACCOUNTING_STATUS , pt.AMOUNT , pt.FEE , pt.GRAND_TOTAL ,pt.FK_PAYMENT , + pp.ID ,pp.AMOUNT , pp.SUBJECT , pp.ORIGIN , + ppd.IUV , ppd.CCP, ppd.ENTE_BENEFICIARIO , ppd.IMPORTO , ppd.ID_DOMINIO , + pp2.ID_PSP , pp2.BUSINESS_NAME , pp2.ID_CHANNEL + FROM AGID_USER.PP_USER pu + left JOIN AGID_USER.PP_TRANSACTION pt ON pu.ID_USER =pt.FK_USER + left JOIN AGID_USER.PP_PAYMENT pp ON pt.FK_PAYMENT = pp.ID + left JOIN AGID_USER.PP_PAYMENT_DETAIL ppd ON pp.ID =ppd.PAYMENT_ID + left JOIN AGID_USER.PP_PSP pp2 ON pt.FK_PSP = pp2.ID + WHERE pu.FISCAL_CODE ='$fiscalCode' + AND PT.AMOUNT > 1 + AND pu.STATUS IN ('11', '12') + """ + .trimIndent() + +fun buildTransactionByFiscalCodeCountQuery(fiscalCode: String) = + """ + SELECT COUNT(*) + FROM AGID_USER.PP_USER pu + left JOIN AGID_USER.PP_TRANSACTION pt ON pu.ID_USER =pt.FK_USER + left JOIN AGID_USER.PP_PAYMENT pp ON pt.FK_PAYMENT = pp.ID + left JOIN AGID_USER.PP_PAYMENT_DETAIL ppd ON pp.ID =ppd.PAYMENT_ID + left JOIN AGID_USER.PP_PSP pp2 ON pt.FK_PSP = pp2.ID + WHERE pu.FISCAL_CODE ='$fiscalCode' + AND PT.AMOUNT > 1 + AND pu.STATUS IN ('11', '12') + """ + .trimIndent() diff --git a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmService.kt b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmService.kt index 6b0f375..19edfe7 100644 --- a/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmService.kt +++ b/src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmService.kt @@ -3,11 +3,13 @@ package it.pagopa.ecommerce.helpdesk.services import io.r2dbc.spi.ConnectionFactory import it.pagopa.generated.ecommerce.helpdesk.model.* import java.time.OffsetDateTime +import java.util.stream.IntStream import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service import reactor.core.publisher.Flux import reactor.core.publisher.Mono +import reactor.kotlin.core.publisher.toMono @Service class PmService(@Autowired val connectionFactory: ConnectionFactory) { @@ -23,23 +25,83 @@ class PmService(@Autowired val connectionFactory: ConnectionFactory) { return pmSearchTransactionRequestDto .doOnNext { logger.info("Search type: ${it.type}") } - .flatMapMany { + .flatMap { + Flux.usingWhen( + connectionFactory.create(), + { connection -> + Flux.from( + connection + .createStatement( + buildTransactionByUserEmailCountQuery( + "marco.tormen@pagopa.it" + ) + ) + .execute() + ) + .flatMap { result -> + result.map { row -> row[0, Integer::class.java] } + } + .toMono() + .flatMapMany { totalCount -> + logger.info("Total records found: $totalCount") + Flux.from( + connection + .createStatement( + buildTransactionByUserEmailQuery( + "email@email.it" + ) + ) + .execute() + ) + .flatMap { result -> + result.map { row -> + val stringBuilder = StringBuilder() + IntStream.range(0, 22).forEach { + stringBuilder.append( + "Column $it, value ${row[it, String::class.java]}\n" + ) + } + stringBuilder + } + } + .doOnNext { logger.info("Row read from DB: $it") } + } + }, + { it.close() } + ) + .toMono() + } + .flatMapMany { totalCount -> + logger.info("Total records found: $totalCount") Flux.usingWhen( connectionFactory.create(), { connection -> Flux.from( connection - .createStatement("SELECT 'Hello, Oracle' FROM sys.dual") + .createStatement( + buildTransactionByUserEmailQuery("marco.tormen@pagopa.it") + ) .execute() ) - .flatMap { result -> result.map { row -> row[0, String::class.java] } } - .doOnNext { logger.info("Read from DB: $it") } + .flatMap { result -> + result.map { row -> + val stringBuilder = StringBuilder() + IntStream.range(0, 22).forEach { + stringBuilder.append( + "Column $it, value ${row[it, String::class.java]}\n" + ) + } + stringBuilder + } + } + .doOnNext { logger.info("Row read from DB: $it") } }, { it.close() } ) } .collectList() .map { + logger.info("all rows $it") SearchTransactionResponseDto() .page(PageInfoDto().current(0).results(3).total(1)) .transactions(