Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions API/Controller/Device/Pair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ private async Task<IActionResult> 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);
}
}
}
5 changes: 4 additions & 1 deletion API/Controller/Device/_ApiController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<DeviceController> _logger;

public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, ILogger<DeviceController> logger)
public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, IDeviceUpdateService deviceUpdateService, ILogger<DeviceController> logger)
{
_db = db;
_redis = redis;
_deviceUpdateService = deviceUpdateService;
_logger = logger;
}
}
4 changes: 2 additions & 2 deletions Common/Models/DeviceUpdateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}