In case of adding a single user to a group, it is obvious that you need to check that there is at least one available license. But what should be checked when a group is added to a group?
That there is enough licenses for every group member. Ideally skip disabled ones and such that already has such license (thanks to membership in other already added group etc).
What exactly do you mean? Do you have groups named after Microsoft 365 licenses? Pay attention, that only SKU part numbers can be used to check/assign licenses.
I meant I want solution that will use one (or as few as possible) custom rule (or whatever) that will accept parameters aka it will be possible to use one rule for all those "licensing groups". So the maintenance of such solution will be easy and it will be easily understandable.
I am really looking for the logic how to set this up, more than the scripts. Aka create business rule, scope it to xyz, create custom command with following parameters and attach it to the business rule etc.
For your information, Microsoft 365 has a group-based licensing feature which does exactly what you described. The following article should be helpful: https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-groups-assign.
Yes I know, but I want have mentioned pre-checks that will warn user that there is not enough licenses aka he wont be able to add new members to "licensing group" before needed licenses will be purchased.