118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { FastifyInstance } from "fastify";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
import {
|
|
authTenantUsers,
|
|
authUsers,
|
|
tenants,
|
|
} from "../../db/schema";
|
|
|
|
export default async function adminRoutes(server: FastifyInstance) {
|
|
|
|
// -------------------------------------------------------------
|
|
// POST /admin/add-user-to-tenant
|
|
// -------------------------------------------------------------
|
|
server.post("/admin/add-user-to-tenant", async (req, reply) => {
|
|
try {
|
|
const body = req.body as {
|
|
user_id: string;
|
|
tenant_id: number;
|
|
role?: string;
|
|
mode?: "single" | "multi";
|
|
};
|
|
|
|
if (!body.user_id || !body.tenant_id) {
|
|
return reply.code(400).send({
|
|
error: "user_id and tenant_id required"
|
|
});
|
|
}
|
|
|
|
const mode = body.mode ?? "multi";
|
|
|
|
// ----------------------------
|
|
// SINGLE MODE → alte Verknüpfungen löschen
|
|
// ----------------------------
|
|
if (mode === "single") {
|
|
await server.db
|
|
.delete(authTenantUsers)
|
|
.where(eq(authTenantUsers.user_id, body.user_id));
|
|
}
|
|
|
|
// ----------------------------
|
|
// Neue Verknüpfung hinzufügen
|
|
// ----------------------------
|
|
|
|
await server.db
|
|
.insert(authTenantUsers)
|
|
// @ts-ignore
|
|
.values({
|
|
user_id: body.user_id,
|
|
tenantId: body.tenant_id,
|
|
role: body.role ?? "member",
|
|
});
|
|
|
|
return { success: true, mode };
|
|
|
|
} catch (err) {
|
|
console.error("ERROR /admin/add-user-to-tenant:", err);
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
}
|
|
});
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
// GET /admin/user-tenants/:user_id
|
|
// -------------------------------------------------------------
|
|
server.get("/admin/user-tenants/:user_id", async (req, reply) => {
|
|
try {
|
|
const { user_id } = req.params as { user_id: string };
|
|
|
|
if (!user_id) {
|
|
return reply.code(400).send({ error: "user_id required" });
|
|
}
|
|
|
|
// ----------------------------
|
|
// 1) User existiert?
|
|
// ----------------------------
|
|
const [user] = await server.db
|
|
.select()
|
|
.from(authUsers)
|
|
.where(eq(authUsers.id, user_id))
|
|
.limit(1);
|
|
|
|
if (!user) {
|
|
return reply.code(400).send({ error: "faulty user_id presented" });
|
|
}
|
|
|
|
// ----------------------------
|
|
// 2) Tenants Join über auth_tenant_users
|
|
// ----------------------------
|
|
const tenantRecords = await server.db
|
|
.select({
|
|
id: tenants.id,
|
|
name: tenants.name,
|
|
short: tenants.short,
|
|
locked: tenants.locked,
|
|
numberRanges: tenants.numberRanges,
|
|
extraModules: tenants.extraModules,
|
|
})
|
|
.from(authTenantUsers)
|
|
.innerJoin(
|
|
tenants,
|
|
eq(authTenantUsers.tenant_id, tenants.id)
|
|
)
|
|
.where(eq(authTenantUsers.user_id, user_id));
|
|
|
|
return {
|
|
user_id,
|
|
tenants: tenantRecords,
|
|
};
|
|
|
|
} catch (err) {
|
|
console.error("ERROR /admin/user-tenants:", err);
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
}
|
|
});
|
|
|
|
}
|