i have odoo17 enterprise project and i created user groups and rules, i want to adopt my model to these groups, my model is old,  we need to adopt logic in group part of my model to my new groups;
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data noupdate="1">
        
        <record id="mahallam.group_admin" model="res.groups">
            <field name="name">Group admin</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_republic" model="res.groups">
            <field name="name">Group republic</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_region" model="res.groups">
            <field name="name">Group region</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_district" model="res.groups">
            <field name="name">Group district</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_mahalla" model="res.groups">
            <field name="name">Group mahalla</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_reader" model="res.groups">
            <field name="name">Group reader</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_moderator" model="res.groups">
            <field name="name">Group moderator</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_hierarchical" model="res.groups">
            <field name="name">Group hierarchical</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_localization" model="res.groups">
            <field name="name">Group localization</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_application" model="res.groups">
            <field name="name">Group application</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_announcement" model="res.groups">
            <field name="name">Group announcement</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_notification" model="res.groups">
            <field name="name">Group notification</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_questionnaire" model="res.groups">
            <field name="name">Group questionnaire</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="mahallam.group_user_questionnaire" model="res.groups">
            <field name="name">Group user questionnaire</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="base.group_system" model="res.groups">
            <field name="name">Group system</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
        <record id="base.group_user" model="res.groups">
            <field name="name">Group user</field>
            <field name="category_id" ref="base.module_category_hidden"/>
        </record>
        
    </data>
</odoo>

from odoo import fields, models, api
from odoo.exceptions import AccessError


class Users(models.Model):
    _inherit = "res.users"

    position = fields.Char(string='Position')
    type = fields.Selection([
        ('admin', 'Admin'),
        ('moderator', 'Moderator'),
        ('reader', 'Reader'),
    ], string='User Type', required=True, default='reader')
    
    role = fields.Selection([
        ('republic', 'Republic'),
        ('region', 'Region'),
        ('district', 'District'),
        ('mahalla', 'Mahalla'),
    ], string='Role', default='mahalla')
    
    region_id = fields.Many2one('region', string="Region")
    district_id = fields.Many2one('district', string="District")
    mahalla_id = fields.Many2one('mahalla', string="Mahalla")

    @api.onchange('type')
    def _onchange_type(self):
        if self.type == 'admin':
            self.role = False
            self.region_id = False
            self.district_id = False
            self.mahalla_id = False
        self._update_role_visibility()
    
    @api.onchange('role')
    def _onchange_role(self):
        if self.type != 'admin':
            self._update_role_visibility()
    
    def _update_role_visibility(self):
        if self.type == 'admin':
            pass
        elif self.role == 'republic':
            pass
        elif self.role == 'region':
            self.district_id = False
            self.mahalla_id = False
        elif self.role == 'district':
            self.mahalla_id = False
        elif self.role == 'mahalla':
            pass

    def action_change_password(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'change.password.wizard',
            'view_mode': 'form',
            'view_id': self.env.ref('base.change_password_wizard_view').id,
            'target': 'new',
            'context': {'default_user_id': self.id},
        }

    def assign_group_based_on_role(self):
        self.ensure_one()

        super_admin_group = self.env.ref('mahallam.group_super_admin')
        moderator_group = self.env.ref('mahallam.group_moderator')
        reader_group = self.env.ref('mahallam.group_reader')
        republic_moderator_group = self.env.ref('mahallam.group_republic_moderator')
        republic_reader_group = self.env.ref('mahallam.group_republic_reader')
        region_moderator_group = self.env.ref('mahallam.group_region_moderator')
        region_reader_group = self.env.ref('mahallam.group_region_reader')
        district_moderator_group = self.env.ref('mahallam.group_district_moderator')
        district_reader_group = self.env.ref('mahallam.group_district_reader')
        mahalla_moderator_group = self.env.ref('mahallam.group_mahalla_moderator')
        mahalla_reader_group = self.env.ref('mahallam.group_mahalla_reader')

        self.groups_id = [(3, super_admin_group.id), (3, moderator_group.id), (3, reader_group.id),
                            (3, republic_moderator_group.id), (3, republic_reader_group.id),
                            (3, region_moderator_group.id), (3, region_reader_group.id),
                            (3, district_moderator_group.id), (3, district_reader_group.id),
                            (3, mahalla_moderator_group.id), (3, mahalla_reader_group.id)]

        if self.type == 'admin':
            self.groups_id = [(4, super_admin_group.id)]
        elif self.type == 'moderator':
            self.groups_id = [(4, moderator_group.id)]
            if self.role == 'republic':
                self.groups_id = [(4, republic_moderator_group.id)]
            elif self.role == 'region':
                self.groups_id = [(4, region_moderator_group.id)]
            elif self.role == 'district':
                self.groups_id = [(4, district_moderator_group.id)]
            elif self.role == 'mahalla':
                self.groups_id = [(4, mahalla_moderator_group.id)]
        elif self.type == 'reader':
            self.groups_id = [(4, reader_group.id)]
            if self.role == 'republic':
                self.groups_id = [(4, republic_reader_group.id)]
            elif self.role == 'region':
                self.groups_id = [(4, region_reader_group.id)]
            elif self.role == 'district':
                self.groups_id = [(4, district_reader_group.id)]
            elif self.role == 'mahalla':
                self.groups_id = [(4, mahalla_reader_group.id)]

    @api.model
    def create(self, vals):
        user = super(Users, self).create(vals)
        user.assign_group_based_on_role()
        return user

    def write(self, vals):
        res = super(Users, self).write(vals)
        if 'type' in vals or 'role' in vals:
            self.assign_group_based_on_role()
        return res