diff --git a/API/Controller/Device/Pair.cs b/API/Controller/Device/Pair.cs index 7b04ad77..061f9ebe 100644 --- a/API/Controller/Device/Pair.cs +++ b/API/Controller/Device/Pair.cs @@ -54,9 +54,18 @@ private async Task PairInternal(string pairCode) if (pair is null) return Problem(PairError.PairCodeNotFound); await devicePairs.DeleteAsync(pair); - var deviceToken = await _db.Devices.Where(x => x.Id == pair.Id).Select(x => x.Token).FirstOrDefaultAsync(); - if (deviceToken is null) throw new Exception("Device not found for pair code"); + var device = await _db.Devices.Where(x => x.Id == pair.Id).Select(x => new { x.Token, x.OwnerId }).FirstOrDefaultAsync(); + if (device is null) throw new Exception("Device not found for pair code"); - return LegacyDataOk(deviceToken); + try + { + await _deviceUpdateService.UpdateDevice(device.OwnerId, pair.Id, DeviceUpdateType.Paired); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to emit paired update for device {DeviceId}", pair.Id); + } + + return LegacyDataOk(device.Token); } -} \ No newline at end of file +} diff --git a/API/Controller/Device/_ApiController.cs b/API/Controller/Device/_ApiController.cs index 74c7a249..270b6a40 100644 --- a/API/Controller/Device/_ApiController.cs +++ b/API/Controller/Device/_ApiController.cs @@ -1,6 +1,7 @@ using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using OpenShock.API.Services.DeviceUpdate; using OpenShock.Common; using OpenShock.Common.Authentication; using OpenShock.Common.OpenShockDb; @@ -21,12 +22,14 @@ public sealed partial class DeviceController : OpenShockControllerBase { private readonly OpenShockContext _db; private readonly IRedisConnectionProvider _redis; + private readonly IDeviceUpdateService _deviceUpdateService; private readonly ILogger _logger; - public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, ILogger logger) + public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, IDeviceUpdateService deviceUpdateService, ILogger logger) { _db = db; _redis = redis; + _deviceUpdateService = deviceUpdateService; _logger = logger; } } \ No newline at end of file diff --git a/Common/Models/DeviceUpdateType.cs b/Common/Models/DeviceUpdateType.cs index 9b5f337f..f2e77ddf 100644 --- a/Common/Models/DeviceUpdateType.cs +++ b/Common/Models/DeviceUpdateType.cs @@ -5,6 +5,6 @@ public enum DeviceUpdateType Created, // Whenever a new device is created Updated, // Whenever name or something else directly related to the device is updated ShockerUpdated, // Whenever a shocker is updated, name or limits for a person - Deleted // Whenever a device is deleted - + Deleted, // Whenever a device is deleted + Paired // Whenever a device consumes a pair code } \ No newline at end of file