fix: add_sub_distributor_admin - 用orgtypeid+name查role, 补parentid+org_type

This commit is contained in:
yumoqing 2026-07-13 11:14:02 +08:00
parent 9afb3c279b
commit e7a4535423

View File

@ -10,7 +10,7 @@ if not userorgid:
dbname = get_module_dbname('supplychain')
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
'SELECT id, sub_dist_name FROM sub_distributors WHERE id = ${sid}$',
'SELECT id, sub_dist_name, resellerid FROM sub_distributors WHERE id = ${sid}$',
{'sid': sub_distributor_id}
)
if not recs:
@ -21,14 +21,25 @@ org_id = sd.id
admin_username = f'admin_{sub_distributor_id[:8]}'
async with DBPools().sqlorContext('sage') as sor_sage:
# Check/ensure org exists
parent_reseller_id = sd.resellerid if hasattr(sd, 'resellerid') else '0'
# Check/ensure org exists with correct parentid and org_type
org_recs = await sor_sage.sqlExe(
'SELECT id FROM organization WHERE id = ${oid}$', {'oid': org_id}
)
if not org_recs:
await sor_sage.C('organization', {
'id': org_id, 'orgname': sd.sub_dist_name,
'org_type': 'reseller', 'parentid': parent_reseller_id,
})
else:
# Fix missing parentid/org_type on existing org
org = org_recs[0]
if not getattr(org, 'parentid', None) or org.parentid == '0':
await sor_sage.sqlExe(
'UPDATE organization SET parentid = ${pid}$, org_type = ${ot}$ WHERE id = ${oid}$',
{'pid': parent_reseller_id, 'ot': 'reseller', 'oid': org_id}
)
# Check if admin already exists
user_recs = await sor_sage.sqlExe(
@ -55,9 +66,10 @@ async with DBPools().sqlorContext('sage') as sor_sage:
'user_status': '1',
})
# Assign roles: logined + reseller.admin + reseller.operator
for role_name in ['logined', 'reseller.admin', 'reseller.operator']:
role_recs = await sor_sage.R('role', {'role': role_name})
# Assign roles: reseller.admin + reseller.operator
# (logined is auto-added by get_userroles(), no need to assign)
for orgtypeid, role_name in [('reseller', 'admin'), ('reseller', 'operator')]:
role_recs = await sor_sage.R('role', {'orgtypeid': orgtypeid, 'name': role_name})
if role_recs:
await sor_sage.C('userrole', {
'id': getID(), 'userid': user_id, 'roleid': role_recs[0].id,