Redone
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
|
||||
export default async function userRoutes(server: FastifyInstance) {
|
||||
//TODO: PERMISSIONS Rückmeldung beschränken
|
||||
|
||||
server.get("/user/:id", async (req, reply) => {
|
||||
const authUser = req.user // kommt aus JWT (user_id + tenant_id)
|
||||
|
||||
const { id } = req.params as { id?: string }
|
||||
|
||||
if (!authUser) {
|
||||
return reply.code(401).send({ error: "Unauthorized" })
|
||||
}
|
||||
|
||||
|
||||
// 1. User laden
|
||||
const { data: user, error: userError } = await server.supabase
|
||||
.from("auth_users")
|
||||
.select("id, email, created_at, must_change_password")
|
||||
.eq("id", id)
|
||||
.single()
|
||||
|
||||
if (userError || !user) {
|
||||
return reply.code(401).send({ error: "User not found" })
|
||||
}
|
||||
|
||||
// 2. Tenants laden (alle Tenants des Users)
|
||||
/*const { data: tenantLinks, error: tenantLinksError } = await server.supabase
|
||||
.from("auth_users")
|
||||
.select(`*, tenants!auth_tenant_users ( id, name, locked )`)
|
||||
.eq("id", authUser.user_id)
|
||||
.single();
|
||||
|
||||
if (tenantLinksError) {
|
||||
|
||||
console.log(tenantLinksError)
|
||||
|
||||
return reply.code(401).send({ error: "Tenant Error" })
|
||||
}
|
||||
|
||||
const tenants = tenantLinks?.tenants*/
|
||||
|
||||
// 3. Aktiven Tenant bestimmen
|
||||
const activeTenant = authUser.tenant_id /*|| tenants[0].id*/
|
||||
|
||||
// 4. Profil für den aktiven Tenant laden
|
||||
let profile = null
|
||||
if (activeTenant) {
|
||||
const { data: profileData } = await server.supabase
|
||||
.from("auth_profiles")
|
||||
.select("*")
|
||||
.eq("user_id", id)
|
||||
.eq("tenant_id", activeTenant)
|
||||
.single()
|
||||
|
||||
profile = profileData
|
||||
}
|
||||
|
||||
// 5. Permissions laden (über Funktion)
|
||||
|
||||
// 6. Response zurückgeben
|
||||
return {
|
||||
user,
|
||||
profile,
|
||||
}
|
||||
})
|
||||
|
||||
server.put("/user/:id/profile", async (req, reply) => {
|
||||
|
||||
const { id } = req.params as { id?: string }
|
||||
|
||||
const { data } = req.body as { data?: object }
|
||||
|
||||
// 4. Profil für den aktiven Tenant laden
|
||||
let profile = null
|
||||
if (req.user.tenant_id) {
|
||||
const { data: profileData } = await server.supabase
|
||||
.from("auth_profiles")
|
||||
.select("*")
|
||||
.eq("user_id", req.user.user_id)
|
||||
.eq("tenant_id", req.user.tenant_id)
|
||||
.single()
|
||||
|
||||
profile = profileData
|
||||
}
|
||||
|
||||
console.log(data)
|
||||
|
||||
//Update Profile
|
||||
const { data: updatedProfileData, error: updateError } = await server.supabase
|
||||
.from("auth_profiles")
|
||||
.update(data)
|
||||
.eq("user_id", id)
|
||||
.eq("id", profile?.id)
|
||||
.select("*")
|
||||
.single()
|
||||
|
||||
console.log(updateError)
|
||||
console.log(updatedProfileData)
|
||||
|
||||
// 5. Permissions laden (über Funktion)
|
||||
|
||||
// 6. Response zurückgeben
|
||||
return {
|
||||
data,
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user