profit-planet-frontend/src/app/admin/matrix-management/hooks/createMatrix.ts
2025-10-16 16:35:17 +02:00

46 lines
1.1 KiB
TypeScript

export type CreateMatrixResult = {
ok: boolean
status: number
body?: any
message?: string
}
export async function createMatrix(params: {
token: string
name: string
email: string
force?: boolean
baseUrl?: string
}): Promise<CreateMatrixResult> {
const { token, name, email, force = false, baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || '' } = params
if (!token) return { ok: false, status: 401, message: 'Missing token' }
const url = new URL(`${baseUrl}/api/matrix/create`)
url.searchParams.set('name', name)
url.searchParams.set('email', email)
if (force) url.searchParams.set('force', 'true')
try {
const res = await fetch(url.toString(), {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
credentials: 'include',
})
let body: any = null
try { body = await res.json() } catch {}
if (!res.ok) {
return {
ok: false,
status: res.status,
body,
message: body?.message || `Create matrix failed (${res.status})`
}
}
return { ok: true, status: res.status, body }
} catch (err) {
return { ok: false, status: 0, message: 'Network error' }
}
}