@@ -1613,10 +1613,25 @@ func (ns *NetworkServer) Delete(ctx context.Context, req *ttnpb.EndDeviceIdentif
1613
1613
return nil , err
1614
1614
}
1615
1615
var evt events.Event
1616
- _ , _ , err := ns .devices .SetByID (ctx , req .ApplicationIds , req .DeviceId , nil , func (ctx context.Context , dev * ttnpb.EndDevice ) (* ttnpb.EndDevice , []string , error ) {
1616
+ _ , _ , err := ns .devices .SetByID (ctx , req .ApplicationIds , req .DeviceId , [] string { "mac_settings_profile_ids" } , func (ctx context.Context , dev * ttnpb.EndDevice ) (* ttnpb.EndDevice , []string , error ) { // nolint: lll
1617
1617
if dev == nil {
1618
1618
return nil , nil , errDeviceNotFound .New ()
1619
1619
}
1620
+ if dev .MacSettingsProfileIds != nil {
1621
+ _ , err := ns .macSettingsProfiles .Set (
1622
+ ctx ,
1623
+ dev .MacSettingsProfileIds ,
1624
+ []string {"ids" , "mac_settings" , "end_devices_count" },
1625
+ func (_ context.Context , existing * ttnpb.MACSettingsProfile ) (* ttnpb.MACSettingsProfile , []string , error ) {
1626
+ if existing .EndDevicesCount > 0 {
1627
+ existing .EndDevicesCount --
1628
+ }
1629
+ return existing , []string {"ids" , "mac_settings" , "end_devices_count" }, nil
1630
+ })
1631
+ if err != nil {
1632
+ return nil , nil , err
1633
+ }
1634
+ }
1620
1635
evt = evtDeleteEndDevice .NewWithIdentifiersAndData (ctx , req , nil )
1621
1636
return nil , nil , nil
1622
1637
})
@@ -1651,7 +1666,27 @@ func (srv *nsEndDeviceBatchRegistry) Delete(
1651
1666
); err != nil {
1652
1667
return nil , err
1653
1668
}
1654
- deleted , err := srv .devices .BatchDelete (ctx , req .ApplicationIds , req .DeviceIds )
1669
+ deleted , err := srv .devices .BatchDelete (ctx , req .ApplicationIds , req .DeviceIds , func (dev * ttnpb.EndDevice ) error {
1670
+ if dev == nil {
1671
+ return errDeviceNotFound .New ()
1672
+ }
1673
+ if dev .MacSettingsProfileIds != nil {
1674
+ _ , err := srv .macSettingsProfiles .Set (
1675
+ ctx ,
1676
+ dev .MacSettingsProfileIds ,
1677
+ []string {"ids" , "mac_settings" , "end_devices_count" },
1678
+ func (_ context.Context , existing * ttnpb.MACSettingsProfile ) (* ttnpb.MACSettingsProfile , []string , error ) {
1679
+ if existing .EndDevicesCount > 0 {
1680
+ existing .EndDevicesCount --
1681
+ }
1682
+ return existing , []string {"ids" , "mac_settings" , "end_devices_count" }, nil
1683
+ })
1684
+ if err != nil {
1685
+ return err
1686
+ }
1687
+ }
1688
+ return nil
1689
+ })
1655
1690
if err != nil {
1656
1691
logRegistryRPCError (ctx , err , "Failed to delete device from registry" )
1657
1692
return nil , err
0 commit comments