V1
This commit is contained in:
@@ -68,11 +68,18 @@ Deno.serve(async (req) => {
|
||||
const byId = new Map((profiles ?? []).map((pr: any) => [pr.id, pr]))
|
||||
const users = list.users.map((u) => {
|
||||
const pr: any = byId.get(u.id) ?? {}
|
||||
const roles =
|
||||
Array.isArray(pr.roles) && pr.roles.length
|
||||
? pr.roles
|
||||
: pr.role
|
||||
? [pr.role]
|
||||
: []
|
||||
return {
|
||||
id: u.id,
|
||||
email: u.email ?? '',
|
||||
full_name: pr.full_name ?? '',
|
||||
role: pr.role ?? 'other',
|
||||
roles,
|
||||
is_admin: pr.is_admin ?? false,
|
||||
created_at: pr.created_at ?? u.created_at
|
||||
}
|
||||
@@ -81,17 +88,21 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
case 'create': {
|
||||
const { email, password, full_name, role, is_admin } = p
|
||||
const { email, password, full_name, role, roles, is_admin } = p
|
||||
if (!email || !password) return json({ error: 'Email et mot de passe requis.' }, 400)
|
||||
if (String(password).length < 6)
|
||||
return json({ error: 'Le mot de passe doit faire au moins 6 caractères.' }, 400)
|
||||
|
||||
// Liste de pôles (rôles) ; le premier fait office de rôle principal.
|
||||
const roleList: string[] = Array.isArray(roles) && roles.length ? roles : role ? [role] : []
|
||||
const primary = roleList[0] ?? 'other'
|
||||
|
||||
// Crée le compte (email déjà confirmé : connexion immédiate possible).
|
||||
const { data: created, error } = await admin.auth.admin.createUser({
|
||||
email,
|
||||
password,
|
||||
email_confirm: true,
|
||||
user_metadata: { full_name: full_name || email.split('@')[0], role: role || 'other' }
|
||||
user_metadata: { full_name: full_name || email.split('@')[0], role: primary }
|
||||
})
|
||||
if (error) return json({ error: error.message }, 400)
|
||||
|
||||
@@ -100,7 +111,8 @@ Deno.serve(async (req) => {
|
||||
.from('profiles')
|
||||
.update({
|
||||
full_name: full_name || email.split('@')[0],
|
||||
role: role || 'other',
|
||||
role: primary,
|
||||
roles: roleList.length ? roleList : [primary],
|
||||
is_admin: !!is_admin
|
||||
})
|
||||
.eq('id', created.user.id)
|
||||
@@ -119,15 +131,23 @@ Deno.serve(async (req) => {
|
||||
}
|
||||
|
||||
case 'update': {
|
||||
const { id, full_name, role, is_admin } = p
|
||||
const { id, full_name, role, roles, is_admin } = p
|
||||
if (!id) return json({ error: 'Identifiant manquant.' }, 400)
|
||||
if (id === user.id && is_admin === false)
|
||||
return json({ error: 'Tu ne peux pas retirer ton propre statut admin.' }, 400)
|
||||
|
||||
const patch: Record<string, unknown> = {}
|
||||
if (full_name !== undefined) patch.full_name = full_name
|
||||
if (role !== undefined) patch.role = role
|
||||
if (is_admin !== undefined) patch.is_admin = !!is_admin
|
||||
// Gestion des pôles : roles = liste complète, role = principal (roles[0]).
|
||||
if (Array.isArray(roles)) {
|
||||
const primary = roles[0] ?? 'other'
|
||||
patch.roles = roles.length ? roles : [primary]
|
||||
patch.role = primary
|
||||
} else if (role !== undefined) {
|
||||
patch.role = role
|
||||
patch.roles = [role]
|
||||
}
|
||||
if (Object.keys(patch).length === 0) return json({ error: 'Rien à modifier.' }, 400)
|
||||
|
||||
const { error } = await admin.from('profiles').update(patch).eq('id', id)
|
||||
|
||||
Reference in New Issue
Block a user