OpenEnergyDashboard / OED

Open Energy Dashboard (OED)
Mozilla Public License 2.0
82 stars 307 forks source link

protect admin when deleting a conversion #905

Open huss opened 1 year ago

huss commented 1 year ago

Is your feature request related to a problem? Please describe.

Admins can delete a conversion. This can cause undesirable effects including:

There may be other cases but hopefully this covers them.

Describe the solution you'd like

Checks need to be added to warning the admin. As was done for groups, the default graphic unit is set to no unit if needed but not allowed if there is no remaining default graphic unit. If members of a group become invalid then the change is not allowed (as is done in group edit page).

Describe alternatives you've considered

The admin is on their own and that is not the philosophy of OED.

Additional context

None

huss commented 1 year ago

After talking with @mrathgeber and thinking about this, I have decided that deleting a conversion involving any unit that is compatible with any used meter unit is problematic. This same type of issue (but more complex here) came up about deleting a unit (see issue #1020) and it was decided you cannot delete a unit that is used in places. The same ideas will be used here.

The basic outline of a solution is:

  1. Use Redux meter state to find all the meter ids that are used. Technically if two meters have the same meter unit then you only need to check one of them. However, this should be fast without this, easier and will give the admin better warning messages.
  2. Use unitsCompatibleWithMeters() in src/client/app/utils/determineCompatibleUnits.ts where supply the meter ids just found to the function. In principle you could do them all at once but to make the messages for the admin better the meter ids are done one at a time (each argument is a set of size 1).
  3. See if the returned set of compatible units contains the source unit id for the conversion that is being deleted. If so, it may be an issue to delete the conversion so it will not be allowed. Add this meter to the message. The start of the message can be something along the lines of "Deleting this conversion may cause the compatible units to change on the following meter(s) so deleting it is not allowed".
  4. Once all meters have been processed, pop up the message if any meter is an issue. See validateGroupPostAddChild() in src/client/app/components/groups/EditGroupModalComponent.tsx for how it looks and works there as what is desired. In this case the delete is not executed. Otherwise the delete happens.

A few notes:

This isn't trivial and I may have missed things. Thoughts and other ideas are welcome.

danielshid commented 3 weeks ago

I will be working on this.