Repository: kalmarenergi-api
KalmarEnergi API
Section titled “KalmarEnergi API”This repository contains all backend services for KalmarEnergi. It follows monorepo architecture.
Description
Section titled “Description”This project contains services that provide APIs to different process and use-cases. This project contains APIs:
- Aurora API: This API handles the core functionalities related to energy data management, including data collection, processing, storage, and reporting. It provides endpoints for manage nodes and accumulators that will be used to gather data that will be sent to Sigholm via API.
- Customer Formula API: This API provides functionalities to manage customer-specific formulas such as move application and customer agents.
- Customer Portal API: This API provides API to interact with Bright App. It includes functions to get information about customers, measurements, and contracts. The API uses mainly Integration Business API to retrieve data about customers. It also exposes ReachMee ads through its Career endpoint class under the
v1/careerpath prefix. - Integration Business API: This service provides API to retrieve customer, services, measurements, and meters from BFUS.
- Integration Lime: This service provides API to communicate with Lime System. It provide API to create and update errands.
- Integration Gateware: This service provides API to connect or disconnect circuit breakers using Gateware. It also provides retry policy for commands.
- Logging API: This service provides API to retrieve logging messages about other services. Furthermore, it saves log messages from other services.
- Measurement API: This API provides functionalities to retrieve measurement data for companies and power of attorney users. It also offers an API to manage the data needed to fulfill meter data requests for users such as facilities and customers.
- Meter Control API: This service acts as a coordinator to automatically connect or disconnect flows or Från och Tillkoppling. It is responsible for sending commands to connect or disconnect circuit breakers to meters based on contracts or for new customers moving in.
- Notification API: This service has the resposibility to send notification and messages such as Email, SMS, and Push.
- Outage API: This API provides endpoints to manage and retrieve information about power outages. It also includes API to handle messages that will be sent to customers or subscribers.
- Postal API: This API provides functionalities to retrieve postal information, including address validation, postal code lookup, and address formatting. It ensures that all postal data is accurate and up-to-date.
- Schedule API: This API provides functionalities to manage and manipulate jobs that will occur in the future. When a service needs to execute a function at a later time, a message will be sent to the Schedule API. A job is triggered based on the scheduled time or CRON expression, and a new message is sent from the Schedule API to the topic specified in the request message.
- Maintenance API: This API provides functionalities to manage messages the needs to be displayed on Portal.
It also contains integration services:
- Integration Billogram: This service provides API to retrieve billing information or invoice to customers using Billogram.
- Integration Creditsafe: This integration retrieves the registered address of a Swedish person or company from Creditsafe, using credentials from Azure Key Vault and a cached bearer token. See
Integrations/IntegrationCreditsafe/README.md. - Integration Elområden: This integration looks up the electricity area (SE1–SE4), grid owner and network area of a Swedish postal code or street address from Elområden, using credentials from Azure Key Vault. See
Integrations/IntegrationElomrade/README.md. - Integration ReachMee: This integration retrieves ads, hires, and projects from ReachMee using OAuth2 client credentials.
- Integration Telenor: This integration sends SMS messages through Telenor SMS Pro, looks up their delivery status and parses delivery report callbacks, using credentials from Azure Key Vault. See
Integrations/IntegrationTelenor/README.md.
It also contains Models and Types projects that are used as references to APIs projects.
Optional embedded response fields
Section titled “Optional embedded response fields”Use [EmbeddedField] on a property in a Models class when the field should only be included in the response when the client asks for it through fields.
using KalmarEnergi.Core.AutoMapper.Attributes;
public record Outage : DateModel{ [EmbeddedField] public List<OutageUpdate> Updates { get; set; }}Example from OutageApi: Updates can be returned when the request includes fields=updates, and it is left out when updates is not included in fields.
When using [EmbeddedField], make sure the endpoint forwards request.Fields to the mapper, for example:
.ProjectTo<Models.Outage>(mappingProfile, request.Fields)Runtime
Section titled “Runtime”.Net10
Architecture
Section titled “Architecture”Minimal API. For more info
Dependency Graph
Section titled “Dependency Graph”A dependency graph is a directed graph that represents dependencies between different entities, such as software components, tasks, or modules. Each node in the graph represents an entity, while edges indicate dependencies, showing which entities rely on others to function correctly. This is particularly useful in various fields, including software development, project management, and systems design. A “dot” file is generated and updated when adding a new project to solution. Extensions for VSCode needed to generate and view graph are:
Database Migrations
Section titled “Database Migrations”To get start with database migrations, there are tasks for VSCode that could be used to add, apply, and view script for migrations.
Getting Started
Section titled “Getting Started”To get started with .NET 9 and Minimal API, follow these steps:
Prerequisites
Section titled “Prerequisites”- An IDE or text editor of your choice (e.g., Visual Studio 2022 Professional Edition or later, Visual Studio Code with C# Dev Kit extension). Please ask BaseIT to get your subscription for Visual Studio 2022 Professional Edition.
- .NET 9 SDK installed on your machine. This step could be skipped if you install Visual Studio with .NET 9 SDK.
- Access to KalmarEnerg Nuget packages. More information could be found here. Package Source name is KalmarEnergi and source is
https://pkgs.dev.azure.com/KalmarEnergi/_packaging/KalmarEnergi/nuget/v3/index.json - Clone kalmarenergi-api repository using your IDE.
Note: If you are using VSCode and you need to get packages from KalmarEnergi packages, you need to edit file %AppData%/Nuget/Nuget.Config. Create a new PAT from Azure.
<?xml version="1.0" encoding="utf-8"?><configuration> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="KalmarEnergi" value="https://pkgs.dev.azure.com/KalmarEnergi/_packaging/KalmarEnergi/nuget/v3/index.json" /> <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" /> </packageSources> <packageRestore> <add key="enabled" value="True" /> </packageRestore> <bindingRedirects> <add key="skip" value="False" /> </bindingRedirects> <packageSourceCredentials> <KalmarEnergi> <add key="Username" value="Your Email" /> <add key="ClearTextPassword" value="Your PAT From Azure" /> </KalmarEnergi> </packageSourceCredentials></configuration>Creating a New Project
Section titled “Creating a New Project”-
Open a terminal or command prompt.
-
Create a new Minimal API project using the .NET CLI:
Terminal window dotnet new web -n MyMinimalApi -
Navigate to the project directory:
Terminal window cd MyMinimalApi
Building the Minimal API
Section titled “Building the Minimal API”-
Open the
Program.csfile and replace its contents with the following code:var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.MapGet("/", () => "Hello, World!");app.Run(); -
Run the application:
Terminal window dotnet run -
Open a web browser and navigate to
http://localhost:5000to see the “Hello, World!” message.
Adding Endpoints
Section titled “Adding Endpoints”To add more endpoints, use the MapGet, MapPost, MapPut, and MapDelete methods. For example:
app.MapGet("/api/customers", () => new List<string> { "Customer1", "Customer2" });app.MapPost("/api/customers", (string customer) => $"Customer {customer} added.");Running and Testing
Section titled “Running and Testing”-
Run the application again:
Terminal window dotnet run
That’s it! You’ve set up a Minimal API with .NET 9.
Automated tests
Section titled “Automated tests”The repository now includes automated tests for Meter Control API under tests/:
tests/MeterControlApi.UnitTests(MSTest + NSubstitute + Shouldly)tests/MeterControlApi.IntegrationTests(MSTest + WebApplicationFactory + Testcontainers + Shouldly)tests/KalmarEnergi.Testing(shared integration fixtures and test host setup)
Prerequisites
Section titled “Prerequisites”- .NET 9 SDK
- Docker (required for integration tests powered by Testcontainers)
Run tests
Section titled “Run tests”Run unit tests:
dotnet test tests/MeterControlApi.UnitTests/MeterControlApi.UnitTests.csprojRun integration tests:
dotnet test tests/MeterControlApi.IntegrationTests/MeterControlApi.IntegrationTests.csprojRun all tests with coverage:
dotnet test KalmarEnergiApi.slnx --collect:"XPlat Code Coverage"CI pipeline ownership
Section titled “CI pipeline ownership”CI pipeline YAML is maintained in the Azure DevOps CI repository (../ci), not in this repository.
Agentic TDD workflow
Section titled “Agentic TDD workflow”For agentic development and test strategy, see:
Docs/agentic-tdd-workflow.md