40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using Fitness_core_BE.Interfaces;
|
|
using Fitness_core_BE.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Fitness_core_BE.Repos
|
|
{
|
|
public class PairingCodeRepo : IPairingCodeRepo
|
|
{
|
|
// Central: created by platform Admin (no TenantId claim) and redeemed by an anonymous
|
|
// agent enroll call (no tenant context at all yet). See CentralDbContext.PairingCodes.
|
|
private readonly CentralDbContext _dbContext;
|
|
|
|
public PairingCodeRepo(CentralDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<PairingCode> AddAsync(PairingCode pairingCode)
|
|
{
|
|
await _dbContext.PairingCodes.AddAsync(pairingCode);
|
|
return pairingCode;
|
|
}
|
|
|
|
// Active = not used and not expired.
|
|
public async Task<PairingCode?> GetActiveByCodeAsync(string code)
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
return await _dbContext.PairingCodes
|
|
.FirstOrDefaultAsync(p => p.Code == code && !p.IsUsed && p.ExpiresAt > now);
|
|
}
|
|
|
|
public Task MarkUsedAsync(PairingCode pairingCode)
|
|
{
|
|
pairingCode.IsUsed = true;
|
|
_dbContext.PairingCodes.Update(pairingCode);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|