2026-01-09 22:14:46 +01:00
|
|
|
using Insight.Database;
|
2025-03-04 17:13:02 +01:00
|
|
|
using PlanTempus.Core;
|
2026-01-09 22:14:46 +01:00
|
|
|
using PlanTempus.Core.Entities.Accounts;
|
2025-01-21 23:26:05 +01:00
|
|
|
using System.Data;
|
|
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
namespace PlanTempus.Database.Core
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2026-01-09 22:14:46 +01:00
|
|
|
public class AccountService
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2026-01-09 22:14:46 +01:00
|
|
|
public record AccountCreateCommand(string CorrelationId, string Email, string Password);
|
2025-02-14 20:14:01 +01:00
|
|
|
|
2025-01-21 23:26:05 +01:00
|
|
|
private readonly IDbConnection _db;
|
|
|
|
|
|
2026-01-09 22:14:46 +01:00
|
|
|
public AccountService(IDbConnection db)
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 22:14:46 +01:00
|
|
|
public async Task CreateAccount(AccountCreateCommand command)
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2026-01-09 22:14:46 +01:00
|
|
|
var account = new Account
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2025-02-14 20:14:01 +01:00
|
|
|
Email = command.Email,
|
2025-03-04 17:13:02 +01:00
|
|
|
PasswordHash = new SecureTokenizer().TokenizeText(command.Password),
|
2025-02-14 20:14:01 +01:00
|
|
|
SecurityStamp = Guid.NewGuid().ToString(),
|
|
|
|
|
EmailConfirmed = false,
|
|
|
|
|
CreatedDate = DateTime.UtcNow
|
|
|
|
|
};
|
2025-01-21 23:26:05 +01:00
|
|
|
|
2026-01-09 22:14:46 +01:00
|
|
|
var accountId = await _db.ExecuteScalarAsync<int>(@$"
|
|
|
|
|
INSERT INTO accounts (email, password_hash, security_stamp, email_confirmed, created_at)
|
2025-01-21 23:26:05 +01:00
|
|
|
VALUES (@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed, @CreatedDate)
|
2026-01-09 22:14:46 +01:00
|
|
|
RETURNING id", account);
|
2025-02-14 20:14:01 +01:00
|
|
|
}
|
2025-03-06 00:05:58 +01:00
|
|
|
|
2026-01-09 22:14:46 +01:00
|
|
|
public async Task CreateOrganization(int accountId, string organizationConnectionString)
|
2025-02-14 20:14:01 +01:00
|
|
|
{
|
|
|
|
|
var schema = "dev";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using var transaction = _db.OpenWithTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Create organization
|
|
|
|
|
var organization = new Organization
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2025-02-14 20:14:01 +01:00
|
|
|
ConnectionString = organizationConnectionString,
|
2025-01-21 23:26:05 +01:00
|
|
|
CreatedDate = DateTime.UtcNow,
|
2026-01-09 22:14:46 +01:00
|
|
|
CreatedBy = accountId,
|
2025-01-21 23:26:05 +01:00
|
|
|
IsActive = true
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-14 20:14:01 +01:00
|
|
|
var organizationId = await _db.ExecuteScalarAsync<int>(@$"
|
2025-03-06 00:05:58 +01:00
|
|
|
INSERT INTO {schema}.organizations (connection_string, created_date, is_active)
|
|
|
|
|
VALUES (@ConnectionString, @CreatedDate, @IsActive)
|
2025-02-14 20:14:01 +01:00
|
|
|
RETURNING id", organization);
|
2025-01-21 23:26:05 +01:00
|
|
|
|
2026-01-09 22:14:46 +01:00
|
|
|
// Link account to organization
|
|
|
|
|
var accountOrganization = new AccountOrganization
|
2025-01-21 23:26:05 +01:00
|
|
|
{
|
2026-01-09 22:14:46 +01:00
|
|
|
AccountId = accountId,
|
2025-02-14 20:14:01 +01:00
|
|
|
OrganizationId = organizationId,
|
2025-01-21 23:26:05 +01:00
|
|
|
CreatedDate = DateTime.UtcNow
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await _db.ExecuteAsync(@$"
|
2026-01-09 22:14:46 +01:00
|
|
|
INSERT INTO {schema}.account_organizations (account_id, organization_id, created_date)
|
|
|
|
|
VALUES (@AccountId, @OrganizationId, @CreatedDate)", accountOrganization);
|
2025-01-21 23:26:05 +01:00
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-09 22:14:46 +01:00
|
|
|
}
|