Skip to content

Commit

Permalink
Add DynamoDB policy
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyfrs committed May 19, 2024
1 parent acda1a3 commit 74baa03
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/provision.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ jobs:
run: exit 1

- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
#if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
<executions>
<execution>
<configuration>
<mainClass>com.fiap.payments.PaymentsApplication</mainClass>
<mainClass>com.fiap.payments.PaymentsApiApp</mainClass>
</configuration>
</execution>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration
)
@EnableFeignClients
@ImportAutoConfiguration(value = [FeignAutoConfiguration::class])
class PaymentsApp
class PaymentsApiApp

fun main(args: Array<String>) {
runApplication<PaymentsApp>(*args)
runApplication<PaymentsApiApp>(*args)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fiap.payments.adapter.controller

import com.fiap.payments.domain.entities.Order
import com.fiap.payments.domain.entities.Payment
import com.fiap.payments.domain.entities.PaymentRequest
import com.fiap.payments.driver.web.PaymentAPI
Expand Down Expand Up @@ -57,17 +56,14 @@ class PaymentController(
}
}

override fun create(orderNumber: Long, order: OrderRequest): ResponseEntity<PaymentRequest> {
override fun create(order: OrderRequest): ResponseEntity<PaymentRequest> {
return ResponseEntity.ok(providePaymentRequestUseCase.providePaymentRequest(order.toDomain()));
}


enum class IPNType(val ipnType: String) {
MERCHANT_ORDER("merchant_order"),
PAYMENT("payment"),
CHARGEBACK("chargebacks"),
POINT_INTEGRATION_IPN("point_integration_ipn"),
}
}


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fiap.payments.adapter.controller.configuration

import com.fiap.payments.PaymentsApp
import com.fiap.payments.PaymentsApiApp
import com.fiap.payments.adapter.gateway.OrderGateway
import com.fiap.payments.adapter.gateway.PaymentGateway
import com.fiap.payments.adapter.gateway.PaymentProviderGateway
Expand All @@ -14,7 +14,7 @@ import com.fiap.payments.usecases.services.PaymentService
import com.fiap.payments.usecases.services.OrderService

@Configuration
@ComponentScan(basePackageClasses = [PaymentsApp::class])
@ComponentScan(basePackageClasses = [PaymentsApiApp::class])
class ServiceConfig {


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fiap.payments.driver.database.configuration

import com.fiap.payments.PaymentsApp
import com.fiap.payments.PaymentsApiApp
import com.fiap.payments.adapter.gateway.*
import com.fiap.payments.adapter.gateway.impl.*
import com.fiap.payments.client.OrderApiClient
Expand All @@ -10,7 +10,7 @@ import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@ComponentScan(basePackageClasses = [PaymentsApp::class])
@ComponentScan(basePackageClasses = [PaymentsApiApp::class])
class GatewayConfig {

@Bean("PaymentGateway")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fiap.payments.driver.database.configuration

import com.fiap.payments.PaymentsApp
import com.fiap.payments.PaymentsApiApp
import com.fiap.payments.adapter.gateway.PaymentProviderGateway
import com.fiap.payments.client.MercadoPagoClient
import com.fiap.payments.driver.database.provider.MercadoPagoPaymentProvider
Expand All @@ -12,7 +12,7 @@ import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@ComponentScan(basePackageClasses = [PaymentsApp::class])
@ComponentScan(basePackageClasses = [PaymentsApiApp::class])
class PaymentGatewayConfig {
@Bean("PaymentProvider")
@ConditionalOnProperty("payment-provider.mock", havingValue = "false")
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/com/fiap/payments/driver/web/PaymentAPI.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fiap.payments.driver.web

import com.fiap.payments.domain.entities.Order
import com.fiap.payments.domain.entities.Payment
import com.fiap.payments.domain.entities.PaymentRequest
import com.fiap.payments.driver.web.request.OrderRequest
Expand Down Expand Up @@ -69,10 +68,12 @@ interface PaymentAPI {
@RequestParam topic: String,
): ResponseEntity<Any>

@PostMapping("/create/{orderNumber}")
fun create(
@Parameter(description = "Número do pedido") @PathVariable orderNumber: Long,
@RequestBody order: OrderRequest
): ResponseEntity<PaymentRequest>
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "Operação bem-sucedida"),
],
)
@PostMapping("/create")
fun create(@RequestBody order: OrderRequest): ResponseEntity<PaymentRequest>

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package com.fiap.payments.usecases.services
import com.fiap.payments.adapter.gateway.OrderGateway
import com.fiap.payments.domain.entities.Order
import com.fiap.payments.usecases.ConfirmOrderUseCase
import org.slf4j.LoggerFactory

open class OrderService(
private val orderGateway: OrderGateway,
): ConfirmOrderUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun confirmOrder(orderNumber: Long): Order {
log.info("Requesting order [$orderNumber] to be confirmed")
return orderGateway.confirmOrder(orderNumber)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import com.fiap.payments.domain.errors.PaymentsException
import com.fiap.payments.domain.valueobjects.PaymentStatus
import com.fiap.payments.usecases.LoadPaymentUseCase
import com.fiap.payments.usecases.ProvidePaymentRequestUseCase
import org.slf4j.LoggerFactory
import java.time.LocalDateTime

class PaymentService(
private val paymentRepository: PaymentGateway,
private val paymentProvider: PaymentProviderGateway,
) :
LoadPaymentUseCase,
ProvidePaymentRequestUseCase {
) : LoadPaymentUseCase,
ProvidePaymentRequestUseCase
{
private val log = LoggerFactory.getLogger(javaClass)

override fun getByOrderNumber(orderNumber: Long): Payment {
return paymentRepository.findByOrderNumber(orderNumber)
?: throw PaymentsException(
Expand All @@ -36,6 +39,8 @@ class PaymentService(

override fun providePaymentRequest(order: Order): PaymentRequest {
val paymentRequest = paymentProvider.createExternalOrder(order)
log.info("Payment request created for order $order")

val payment =
Payment(
orderNumber = order.number!!,
Expand All @@ -48,6 +53,7 @@ class PaymentService(
)

paymentRepository.create(payment)
log.info("Payment stored for order $order")

return paymentRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fiap.payments.domain.valueobjects.PaymentStatus
import com.fiap.payments.usecases.ConfirmOrderUseCase
import com.fiap.payments.usecases.LoadPaymentUseCase
import com.fiap.payments.usecases.SyncPaymentUseCase
import org.slf4j.LoggerFactory
import java.time.LocalDateTime

class PaymentSyncService(
Expand All @@ -14,6 +15,7 @@ class PaymentSyncService(
private val paymentProviderGateway: PaymentProviderGateway,
private val confirmOrderUseCase: ConfirmOrderUseCase
): SyncPaymentUseCase {
private val log = LoggerFactory.getLogger(javaClass)

override fun syncPayment(orderNumber: Long, externalOrderGlobalId: String) {
val payment = loadPaymentUseCase.getByOrderNumber(orderNumber)
Expand All @@ -23,6 +25,7 @@ class PaymentSyncService(
}

val newStatus = paymentProviderGateway.checkExternalOrderStatus(externalOrderGlobalId)
log.info("Checked payment status for order [$orderNumber]: $newStatus")

if (payment.status != newStatus) {
paymentGateway.update(
Expand All @@ -31,6 +34,7 @@ class PaymentSyncService(
statusChangedAt = LocalDateTime.now(),
)
)
log.info("Changed payment status for order [$orderNumber] from ${payment.status} to $newStatus")

if (newStatus == PaymentStatus.CONFIRMED) {
confirmOrderUseCase.confirmOrder(orderNumber)
Expand Down
43 changes: 38 additions & 5 deletions terraform/dynamodb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,52 @@ module "dynamodb_table" {

name = "payments"
hash_key = "payment_order_number"
range_key = "payment_created_at"
table_class = "STANDARD"

attributes = [
{
name = "payment_order_number"
type = "S"
},
{
name = "payment_created_at"
type = "S"
}
]

tags = var.tags
}

resource "aws_iam_policy" "payments_dynamodb_table_policy" {
name = "TechChallengePaymentsDynamoDBTablePolicy"

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Sid" : "ListAndDescribe",
"Effect" : "Allow",
"Action" : [
"dynamodb:List*",
"dynamodb:DescribeReservedCapacity*",
"dynamodb:DescribeLimits",
"dynamodb:DescribeTimeToLive"
],
"Resource" : "*"
},
{
Effect = "Allow"
Action = [
"dynamodb:BatchGet*",
"dynamodb:DescribeStream",
"dynamodb:DescribeTable",
"dynamodb:Get*",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:BatchWrite*",
"dynamodb:CreateTable",
"dynamodb:Delete*",
"dynamodb:Update*",
"dynamodb:PutItem"
],
Resource = module.dynamodb_table.dynamodb_table_arn
}
]
})
}
5 changes: 5 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ output "mercado_pago_secrets_read_only_policy_arn" {
description = "The ARN of the Mercado Pago secrets"
value = aws_iam_policy.mercado_pago_secrets_read_only_policy.arn
}

output "payments_dynamodb_table_policy_arn" {
description = "The ARN of the DynamoDB table for payments"
value = aws_iam_policy.payments_dynamodb_table_policy.arn
}

0 comments on commit 74baa03

Please sign in to comment.