From abb6799c3d6c7d3c516aaaef42b46dd63103a2d8 Mon Sep 17 00:00:00 2001 From: maaz519 Date: Sat, 20 Jun 2026 17:03:51 +0530 Subject: [PATCH] feat: admin org management API + member login fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add NestJS endpoints the admin panel org UI was calling (all returning 404): - GET/POST /admin/orgs — list and create organizations - GET /admin/orgs/:id — org detail with tenants and orgAdmins - POST /admin/orgs/:id/admins — create OrgAdmin with hashed password - PATCH /admin/tenants/:id/org — assign/unassign tenant from org Also ship the member login page and sidebar fix from the previous session: - Move /my/login to /member-login to break infinite redirect loop - Add /member-login to PUBLIC_PATHS in sidebar so it is not intercepted Co-Authored-By: Claude Sonnet 4.6 --- .../super-admin/admin-orgs.controller.ts | 50 ++++++++++++ .../modules/super-admin/super-admin.module.ts | 3 +- .../super-admin/super-admin.service.ts | 79 ++++++++++++++++++- apps/web/app/_lib/sidebar.tsx | 2 +- .../app/{my/login => member-login}/page.tsx | 0 apps/web/app/my/layout.tsx | 2 +- 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 apps/api/src/modules/super-admin/admin-orgs.controller.ts rename apps/web/app/{my/login => member-login}/page.tsx (100%) diff --git a/apps/api/src/modules/super-admin/admin-orgs.controller.ts b/apps/api/src/modules/super-admin/admin-orgs.controller.ts new file mode 100644 index 0000000..dc916ff --- /dev/null +++ b/apps/api/src/modules/super-admin/admin-orgs.controller.ts @@ -0,0 +1,50 @@ +import { Body, Controller, Get, Param, Patch, Post, UseGuards } from '@nestjs/common'; +import { IsOptional, IsString, MinLength } from 'class-validator'; +import { SuperAdminGuard } from './super-admin.guard'; +import { SuperAdminService } from './super-admin.service'; + +class CreateOrgDto { + @IsString() @MinLength(2) slug!: string; + @IsString() @MinLength(1) name!: string; +} + +class CreateOrgAdminDto { + @IsString() email!: string; + @IsString() @IsOptional() name?: string; + @IsString() @MinLength(8) password!: string; +} + +class AssignTenantOrgDto { + @IsString() @IsOptional() organizationId?: string | null; +} + +@UseGuards(SuperAdminGuard) +@Controller('admin') +export class AdminOrgsController { + constructor(private readonly service: SuperAdminService) {} + + @Get('orgs') + listOrgs() { + return this.service.listOrgs(); + } + + @Post('orgs') + createOrg(@Body() body: CreateOrgDto) { + return this.service.createOrg(body.slug, body.name); + } + + @Get('orgs/:id') + getOrg(@Param('id') id: string) { + return this.service.getOrg(id); + } + + @Post('orgs/:id/admins') + createOrgAdmin(@Param('id') id: string, @Body() body: CreateOrgAdminDto) { + return this.service.createOrgAdmin(id, body.email, body.name, body.password); + } + + @Patch('tenants/:id/org') + assignTenantToOrg(@Param('id') id: string, @Body() body: AssignTenantOrgDto) { + return this.service.assignTenantToOrg(id, body.organizationId ?? null); + } +} diff --git a/apps/api/src/modules/super-admin/super-admin.module.ts b/apps/api/src/modules/super-admin/super-admin.module.ts index bb34ee8..99f9e5a 100644 --- a/apps/api/src/modules/super-admin/super-admin.module.ts +++ b/apps/api/src/modules/super-admin/super-admin.module.ts @@ -2,6 +2,7 @@ import { Global, Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { SuperAdminController } from './super-admin.controller'; +import { AdminOrgsController } from './admin-orgs.controller'; import { SuperAdminService } from './super-admin.service'; import { SuperAdminGuard } from './super-admin.guard'; import { PrismaModule } from '../../prisma/prisma.module'; @@ -18,7 +19,7 @@ import { PrismaModule } from '../../prisma/prisma.module'; }), }), ], - controllers: [SuperAdminController], + controllers: [SuperAdminController, AdminOrgsController], providers: [SuperAdminService, SuperAdminGuard], exports: [SuperAdminGuard, JwtModule], }) diff --git a/apps/api/src/modules/super-admin/super-admin.service.ts b/apps/api/src/modules/super-admin/super-admin.service.ts index 9e57ba1..2fed997 100644 --- a/apps/api/src/modules/super-admin/super-admin.service.ts +++ b/apps/api/src/modules/super-admin/super-admin.service.ts @@ -1,4 +1,4 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { ConflictException, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; import * as bcrypt from 'bcryptjs'; @@ -32,4 +32,81 @@ export class SuperAdminService { if (!admin) throw new UnauthorizedException('Super admin not found'); return { id: admin.id, email: admin.email, name: admin.name }; } + + async listOrgs() { + return this.prisma.organization.findMany({ + orderBy: { createdAt: 'desc' }, + select: { + id: true, + slug: true, + name: true, + createdAt: true, + _count: { select: { tenants: true, orgAdmins: true } }, + }, + }); + } + + async createOrg(slug: string, name: string) { + const existing = await this.prisma.organization.findUnique({ where: { slug } }); + if (existing) throw new ConflictException(`Organization with slug "${slug}" already exists`); + return this.prisma.organization.create({ + data: { slug, name }, + select: { id: true, slug: true, name: true, createdAt: true }, + }); + } + + async getOrg(id: string) { + const org = await this.prisma.organization.findUnique({ + where: { id }, + select: { + id: true, + slug: true, + name: true, + createdAt: true, + tenants: { + select: { id: true, slug: true, name: true, isActive: true, createdAt: true }, + orderBy: { createdAt: 'desc' }, + }, + orgAdmins: { + select: { id: true, email: true, name: true, role: true, createdAt: true }, + orderBy: { createdAt: 'desc' }, + }, + }, + }); + if (!org) throw new NotFoundException('Organization not found'); + return org; + } + + async createOrgAdmin(orgId: string, email: string, name: string | undefined, password: string) { + const org = await this.prisma.organization.findUnique({ where: { id: orgId } }); + if (!org) throw new NotFoundException('Organization not found'); + + const existing = await this.prisma.orgAdmin.findUnique({ + where: { organizationId_email: { organizationId: orgId, email } }, + }); + if (existing) throw new ConflictException('An admin with this email already exists in this organization'); + + const passwordHash = await bcrypt.hash(password, 10); + const admin = await this.prisma.orgAdmin.create({ + data: { organizationId: orgId, email, name, passwordHash }, + select: { id: true, email: true, name: true, role: true, createdAt: true }, + }); + return admin; + } + + async assignTenantToOrg(tenantId: string, organizationId: string | null) { + const tenant = await this.prisma.tenant.findUnique({ where: { id: tenantId } }); + if (!tenant) throw new NotFoundException('Tenant not found'); + + if (organizationId !== null) { + const org = await this.prisma.organization.findUnique({ where: { id: organizationId } }); + if (!org) throw new NotFoundException('Organization not found'); + } + + return this.prisma.tenant.update({ + where: { id: tenantId }, + data: { organizationId }, + select: { id: true, slug: true, name: true, organizationId: true }, + }); + } } diff --git a/apps/web/app/_lib/sidebar.tsx b/apps/web/app/_lib/sidebar.tsx index 8c55f3a..58ca1be 100644 --- a/apps/web/app/_lib/sidebar.tsx +++ b/apps/web/app/_lib/sidebar.tsx @@ -22,7 +22,7 @@ const SUPER_ADMIN_LINKS = [ { href: '/admin/drafts', label: 'AI Drafts' }, ]; -const PUBLIC_PATHS = ['/login', '/signup', '/onboard']; +const PUBLIC_PATHS = ['/login', '/signup', '/onboard', '/member-login']; const ADMIN_PATHS = ['/admin']; const MEMBER_PATHS = ['/my']; diff --git a/apps/web/app/my/login/page.tsx b/apps/web/app/member-login/page.tsx similarity index 100% rename from apps/web/app/my/login/page.tsx rename to apps/web/app/member-login/page.tsx diff --git a/apps/web/app/my/layout.tsx b/apps/web/app/my/layout.tsx index e9d20b8..5e436e9 100644 --- a/apps/web/app/my/layout.tsx +++ b/apps/web/app/my/layout.tsx @@ -18,7 +18,7 @@ const NAV = [ export default async function MemberLayout({ children }: { children: React.ReactNode }) { const token = await getMemberToken(); - if (!token) redirect('/my/login'); + if (!token) redirect('/member-login'); return (