@@ -1613,10 +1613,25 @@ func (ns *NetworkServer) Delete(ctx context.Context, req *ttnpb.EndDeviceIdentif
16131613 return nil , err
16141614 }
16151615 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
16171617 if dev == nil {
16181618 return nil , nil , errDeviceNotFound .New ()
16191619 }
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+ }
16201635 evt = evtDeleteEndDevice .NewWithIdentifiersAndData (ctx , req , nil )
16211636 return nil , nil , nil
16221637 })
@@ -1651,7 +1666,27 @@ func (srv *nsEndDeviceBatchRegistry) Delete(
16511666 ); err != nil {
16521667 return nil , err
16531668 }
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+ })
16551690 if err != nil {
16561691 logRegistryRPCError (ctx , err , "Failed to delete device from registry" )
16571692 return nil , err
0 commit comments