import useDatabase from '~/composables/useDatabase';
import { hasPermissions } from '~/shared/auth.util';

export default defineEventHandler(async (e) => {
    const session = await getUserSession(e);
    
    if(!session || !session.user || !hasPermissions(session.user.permissions, ['admin']))
    {
        throw createError({
            statusCode: 401,
            message: 'Unauthorized',
        });
    }

    const db = useDatabase();
    return db.query.usersTable.findMany({
        columns: {
            email: false,
            hash: false,
        },
        with: {
            data: true,
            permission: true,
            session: {
                columns: {
                    timestamp: false,
                    user_id: false,
                }
            }
        }
    }).sync();
})