Skip to content

Commit

Permalink
fix code smells and update readme (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
italopessoa authored Sep 21, 2024
1 parent 59e02b8 commit c52ba08
Show file tree
Hide file tree
Showing 35 changed files with 226 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"soat-fiap_FIAP.TechChallenge.ByteMeBurger" /o:"soat-fiap" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.scanner.scanAll=false
dotnet build --no-incremental
.\.sonar\scanner\dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"
.\.sonar\scanner\dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml" --exclude-by-attribute "GeneratedCodeAttribute,ExcludeFromCodeCoverageAttribute"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

[![CI](https://github.com/soat-fiap/FIAP.TechChallenge.ByteMeBurger/actions/workflows/dotnet.yml/badge.svg?branch=main)](https://github.com/soat-fiap/FIAP.TechChallenge.ByteMeBurger/actions/workflows/dotnet.yml)

[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=soat-fiap_FIAP.TechChallenge.ByteMeBurger&metric=coverage)](https://sonarcloud.io/summary/new_code?id=soat-fiap_FIAP.TechChallenge.ByteMeBurger)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=soat-fiap_FIAP.TechChallenge.ByteMeBurger&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=soat-fiap_FIAP.TechChallenge.ByteMeBurger)

## Description
This repository contains the source code for the ByteMeBurger API, part of the FIAP Tech Challenge. The API is designed to manage a burger restaurant's operations, including order placement, customer registration, and product management. The recent updates introduce a new endpoint for customer registration and enhance various aspects of the application, such as error handling, data models, and service configurations.

Endpoints are protected since JWT token is now required. Since this application will run on EKS Cluster inside a private VPC, Customers endpoint will be deprecated or splited into a different service. Customers information can be extracted from token, since it's generated and validated internally.

## Tech challenge deliverables
- You can find all Phase 1 deliverables on the [Wiki page](https://github.com/soat-fiap/FIAP.TechChallenge.ByteMeBurger/wiki)
- Phase 2 Deliverables
Expand All @@ -18,6 +23,11 @@ This repository contains the source code for the ByteMeBurger API, part of the F

## Getting Started

### Dependencies

- [VPC](https://github.com/soat-fiap/bmb.infra)
- [Database](https://github.com/soat-fiap/bmb.database)

### Prerequisites
- Docker
- .NET SDK
Expand Down Expand Up @@ -127,4 +137,7 @@ You can use the [postman collection](/postman) for testing
### Cleanup
Once you are done, you can stop the services running [rollback.ps1](kubernetes/rollback.ps1) script
test ruules
## This repo on the infrastructure
![Architecture Diagram](aws-infra-phase-3.png)
Binary file added aws-infra-phase-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions kubernetes/deployment-api.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
apiVersion: v1
kind: LimitRange
metadata:
name: storage-limit-range
namespace: default
spec:
limits:
- default:
ephemeral-storage: "300Mi"
type: Container
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -15,6 +26,7 @@ spec:
labels:
app: api
spec:
automountServiceAccountToken: false
containers:
- name: api-container
image: ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/api:sha-b83177c
Expand Down
1 change: 1 addition & 0 deletions kubernetes/deployment-db.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ spec:
labels:
app: pod-mysql
spec:
automountServiceAccountToken: false
containers:
- name: mysql-container
image: ghcr.io/soat-fiap/fiap.techchallenge.bytemeburger/db:sha-b83177c
Expand Down
1 change: 1 addition & 0 deletions kubernetes/deployment-seq.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ spec:
labels:
app: seq
spec:
automountServiceAccountToken: false
containers:
- name: seq-container
image: datalust/seq:latest
Expand Down
11 changes: 10 additions & 1 deletion postman/TechChallenge - Workflow testing.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,16 @@
"script": {
"type": "text/javascript",
"exec": [
""
"const profile = pm.environment.get(\"profile\");\r",
"if(profile) {\r",
" console.log(`Adding ${profile} cpf to request header`);\r",
" pm.request.headers.add({\r",
" key: \"cpf\",\r",
" value: pm.environment.get(`${profile}_cpf`),\r",
" });\r",
"} else {\r",
" console.warn(\"making anonymous request\");\r",
"}"
]
}
},
Expand Down
64 changes: 62 additions & 2 deletions postman/TechChallenge.postman_environment.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,69 @@
"value": "123",
"type": "default",
"enabled": true
},
{
"key": "jwt_aud",
"value": "italo.com.br",
"type": "default",
"enabled": true
},
{
"key": "jwt_iss",
"value": "italo.com.uk",
"type": "default",
"enabled": true
},
{
"key": "jwt_iat",
"value": "123123",
"type": "default",
"enabled": true
},
{
"key": "variable_key",
"value": "",
"type": "any",
"enabled": false
},
{
"key": "jwt_exp",
"value": "",
"type": "any",
"enabled": true
},
{
"key": "jwt_secret",
"value": "PkOhRwy6UtniEMo7lLWp3bADctYgnDHCTvH+2YkDeGg=",
"type": "default",
"enabled": true
},
{
"key": "admin_cpf",
"value": "12312312312",
"type": "default",
"enabled": true
},
{
"key": "kitchen_cpf",
"value": "0980980922",
"type": "default",
"enabled": true
},
{
"key": "customer_cpf",
"value": "0987654321",
"type": "default",
"enabled": true
},
{
"key": "profile",
"value": "admin",
"type": "default",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2024-07-29T15:08:33.306Z",
"_postman_exported_using": "Postman/11.5.0"
"_postman_exported_at": "2024-09-20T01:09:04.459Z",
"_postman_exported_using": "Postman/11.12.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace FIAP.TechChallenge.ByteMeBurger.Api.Controllers;
/// </summary>
/// <param name="customerService">Customer service (port implementation).</param>
/// <param name="logger">Logger</param>
[Obsolete("To be migrated to different project")]
[Route("api/[controller]")]
[Produces("application/json")]
[Consumes("application/json")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ public void Dispose()
DomainEventTrigger.OrderStatusChanged -= OnOrderStatusChanged;
DomainEventTrigger.CustomerRegistered -= OnCustomerRegistered;
DomainEventTrigger.PaymentCreated -= OnPaymentCreated;
GC.SuppressFinalize(this);
}
}
11 changes: 11 additions & 0 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/GlobalExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

namespace FIAP.TechChallenge.ByteMeBurger.Api;

/// <summary>
/// Global exception handler
/// </summary>
/// <param name="logger"></param>
[ExcludeFromCodeCoverage]
public class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
/// <summary>
///
/// </summary>
/// <param name="httpContext">Http Context</param>
/// <param name="exception">Exception</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception,
CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public record CreateCustomerRequest
/// </summary>
[Required]
[MaxLength(14)]
public string Cpf { get; set; }
public string Cpf { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class CreateProductRequest
/// </summary>
public string[] Images { get; set; } = Array.Empty<string>();

// TODO make this internal
public Product ToProduct() =>
/// <summary>
/// Convert to Domain
/// </summary>
/// <returns></returns>
internal Product ToProduct() =>
new Product(Guid.NewGuid(), Name, Description, (ProductCategory)Category, Price, Images);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Customers;
using FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Orders;
using FIAP.TechChallenge.ByteMeBurger.Application.UseCases.Payment;
Expand All @@ -6,6 +7,7 @@

namespace FIAP.TechChallenge.ByteMeBurger.Application;

[ExcludeFromCodeCoverage]
public static class ServiceCollectionsExtensions
{
public static void AddUseCases(this IServiceCollection serviceCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ and not PaymentStatus.Cancelled
if (paymentStatusUpdated && payment.IsApproved())
{
DomainEventTrigger.RaisePaymentConfirmed(payment);
// TODO change to eventual consistency. use events to update order status
await _updateOrderStatusUseCase.Execute(payment.OrderId, OrderStatus.Received);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CustomerDto
/// <summary>
/// Customer CPF
/// </summary>
public string Cpf { get; set; }
public string Cpf { get; set; } = string.Empty;

/// <summary>
/// Customer name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ namespace FIAP.TechChallenge.ByteMeBurger.Domain.Entities;

public class Payment : Entity<PaymentId>, IAggregateRoot
{
public string Type { get; set; }

public PaymentType PaymentType { get; set; }
public PaymentType PaymentType { get; set; } = PaymentType.Test;

public Guid OrderId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public override string ToString()
private static string SanityseCpf(string cpf) => cpf
.Replace(".", "")
.Replace("-", "")
.Replace(Environment.NewLine, "")
.Trim();

private static string Validate(string cpf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public async Task<Payment> CreatePaymentAsync(Order order)
Id = new PaymentId(Guid.NewGuid()),
PaymentType = PaymentType.MercadoPago,
QrCode = mercadoPagoPayment.PointOfInteraction.TransactionData.QrCode,
ExternalReference = mercadoPagoPayment.Id.ToString(),
ExternalReference = mercadoPagoPayment!.Id.ToString(),
OrderId = order.Id,
Amount = paymentCreateRequest.TransactionAmount.Value
Amount = paymentCreateRequest!.TransactionAmount.Value
};
}
catch (Exception e)
Expand All @@ -67,7 +67,7 @@ public async Task<Payment> CreatePaymentAsync(Order order)
}
}

private PaymentCreateRequest GetPaymentCreateRequest(Order order)
private static PaymentCreateRequest GetPaymentCreateRequest(Order order)
{
var paymentPayerRequest = order.Customer is null
? new PaymentPayerRequest
Expand Down Expand Up @@ -110,7 +110,7 @@ private PaymentCreateRequest GetPaymentCreateRequest(Order order)
private static PaymentPayerRequest MapPaymentPayerRequest(Order order)
=> new()
{
Email = order.Customer.Email,
Email = order!.Customer.Email,
FirstName = order.Customer.Name,
LastName = "User",
Identification = new IdentificationRequest
Expand Down Expand Up @@ -139,15 +139,13 @@ private static IEnumerable<PaymentItemRequest> MapPaymentItemRequests(Order orde
}
);

private PaymentCreateRequest MapPaymentCreateRequest(Order order, PaymentPayerRequest payer,
private static PaymentCreateRequest MapPaymentCreateRequest(Order order, PaymentPayerRequest payer,
PaymentAdditionalInfoRequest paymentAdditionalInfoRequest, decimal amount)
=> new()
{
Description = $"Payment for Order {order.TrackingCode.Value}",
ExternalReference = order.TrackingCode.Value,
Installments = 1,
// TODO: removed since while the application is deployed the API gateway is unknown
// NotificationUrl = _mercadoPagoOptions.NotificationUrl ?? string.Empty,
Payer = payer,
PaymentMethodId = "pix",
StatementDescriptor = "tech challenge restaurant order",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace FIAP.TechChallenge.ByteMeBurger.MercadoPago.Gateway.Model;

public class MercadoPagoWebhookEvent
{
public string Action { get; set; } = null!;
public string Action { get; set; } = string.Empty;

[JsonPropertyName("api_version")]
public string ApiVersion { get; set; }
public string ApiVersion { get; set; } = string.Empty;

public MercadoPagoWebhookData Data { get; set; }

Expand All @@ -19,13 +19,13 @@ public class MercadoPagoWebhookEvent
[JsonPropertyName("live_mode")]
public bool LiveMode { get; set; }

public string Type { get; set; }
public string Type { get; set; } = string.Empty;

[JsonPropertyName("user_id")]
public long UserId { get; set; }
}

public class MercadoPagoWebhookData
{
public string Id { get; set; }
public string Id { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal class OrderItemDto
{
public Guid OrderId { get; set; }
public Guid ProductId { get; set; }
public string ProductName { get; set; }
public string ProductName { get; set; } = string.Empty;
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class OrderListDto
public int Status { get; set; }
public DateTime Created { get; set; }
public DateTime? Updated { get; set; }
public string TrackingCode { get; set; }
public string TrackingCode { get; set; } = string.Empty;
public Guid? PaymentId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Domain.ValueObjects;

namespace FIAP.TechChallenge.ByteMeBurger.Persistence.Dto;

public class ProductDto
{
public Guid Id { get; set; }

public string Name { get; set; }
public string Name { get; set; } = string.Empty;

public string Description { get; set; }

Expand Down
Loading

0 comments on commit c52ba08

Please sign in to comment.