Skip to content

Commit

Permalink
feat: add query by user email implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pietro-tota committed Aug 8, 2023
1 parent 5cb2434 commit 3e014d1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginManagement { repositories { gradlePluginPortal() } }

rootProject.name = "pagopa-ecommerce-helpdesk-service"
rootProject.name = "pagopa-ecommerce-helpdesk-service"
65 changes: 65 additions & 0 deletions src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmQueries.kt
Original file line number Diff line number Diff line change
@@ -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()
70 changes: 66 additions & 4 deletions src/main/kotlin/it/pagopa/ecommerce/helpdesk/services/PmService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
"[email protected]"
)
)
.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 protected]"
)
)
.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("[email protected]")
)
.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(
Expand Down

0 comments on commit 3e014d1

Please sign in to comment.