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 9fa7151
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 21 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
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"),
}
}


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,16 @@ 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 {
return orderGateway.confirmOrder(orderNumber)
val order = orderGateway.confirmOrder(orderNumber)
log.info("Order #$orderNumber confirmed")
return order
}
}
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.number}")

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

paymentRepository.create(payment)
log.info("Payment stored for order #${order.number}")

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 $newStatus payment for order #$orderNumber")

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 9fa7151

Please sign in to comment.