Compare commits
9 Commits
be6e34914e
...
multitenan
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
353da0039f | ||
|
|
ddd25c47c5 | ||
|
|
b0a45d98ce | ||
|
|
b6cb5652b7 | ||
|
|
fbfaf7bb9f | ||
|
|
2f0aeb948b | ||
|
|
0ad62cbf8d | ||
|
|
5a80f33078 | ||
|
|
57f27d28cd |
1
.env.api
1
.env.api
@@ -2,7 +2,6 @@ NODE_ENV=development
|
||||
PORT=3000
|
||||
|
||||
DATABASE_URL="mysql://platform:platform@db:3306/platform"
|
||||
CENTRAL_DATABASE_URL="mysql://root:asjdnfqTash37faggT@db:3306/central_platform"
|
||||
REDIS_URL="redis://redis:6379"
|
||||
|
||||
# JWT, multi-tenant hints, etc.
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('object_definitions', (table) => {
|
||||
table.string('nameField', 255).comment('API name of the field to use as record display name');
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('object_definitions', (table) => {
|
||||
table.dropColumn('nameField');
|
||||
});
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('object_definitions', (table) => {
|
||||
table.uuid('app_id').nullable()
|
||||
.comment('Optional: App that this object belongs to');
|
||||
|
||||
table
|
||||
.foreign('app_id')
|
||||
.references('id')
|
||||
.inTable('apps')
|
||||
.onDelete('SET NULL');
|
||||
|
||||
table.index(['app_id']);
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('object_definitions', (table) => {
|
||||
table.dropForeign('app_id');
|
||||
table.dropIndex('app_id');
|
||||
table.dropColumn('app_id');
|
||||
});
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `users` (
|
||||
`id` VARCHAR(191) NOT NULL,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`firstName` VARCHAR(191) NULL,
|
||||
`lastName` VARCHAR(191) NULL,
|
||||
`role` VARCHAR(191) NOT NULL DEFAULT 'admin',
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `users_email_key`(`email`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
@@ -8,20 +8,6 @@ datasource db {
|
||||
url = env("CENTRAL_DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
email String @unique
|
||||
password String
|
||||
firstName String?
|
||||
lastName String?
|
||||
role String @default("admin") // admin, superadmin
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Tenant {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
// Central database client
|
||||
const centralPrisma = new CentralPrismaClient();
|
||||
|
||||
async function createAdminUser() {
|
||||
const email = 'admin@example.com';
|
||||
const password = 'admin123';
|
||||
const firstName = 'Admin';
|
||||
const lastName = 'User';
|
||||
|
||||
try {
|
||||
// Check if admin user already exists
|
||||
const existingUser = await centralPrisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
console.log(`User ${email} already exists`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Create admin user in central database
|
||||
const user = await centralPrisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
role: 'superadmin',
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('\nAdmin user created successfully!');
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
console.log('User ID:', user.id);
|
||||
} catch (error) {
|
||||
console.error('Error creating admin user:', error);
|
||||
} finally {
|
||||
await centralPrisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
createAdminUser();
|
||||
@@ -1,138 +0,0 @@
|
||||
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { Knex, knex } from 'knex';
|
||||
|
||||
// Central database client
|
||||
const centralPrisma = new CentralPrismaClient();
|
||||
|
||||
async function createTenantUser() {
|
||||
const tenantSlug = 'tenant1';
|
||||
const email = 'user@example.com';
|
||||
const password = 'user123';
|
||||
const firstName = 'Test';
|
||||
const lastName = 'User';
|
||||
|
||||
try {
|
||||
// Get tenant database connection info
|
||||
const tenant = await centralPrisma.tenant.findFirst({
|
||||
where: { slug: tenantSlug },
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
console.log(`Tenant ${tenantSlug} not found. Creating tenant...`);
|
||||
|
||||
// Create tenant in central database
|
||||
const newTenant = await centralPrisma.tenant.create({
|
||||
data: {
|
||||
name: 'Default Tenant',
|
||||
slug: tenantSlug,
|
||||
dbHost: 'db',
|
||||
dbPort: 3306,
|
||||
dbName: 'platform',
|
||||
dbUsername: 'platform',
|
||||
dbPassword: 'platform',
|
||||
status: 'active',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Tenant created:', newTenant.slug);
|
||||
} else {
|
||||
console.log('Tenant found:', tenant.slug);
|
||||
}
|
||||
|
||||
const tenantInfo = tenant || {
|
||||
dbHost: 'db',
|
||||
dbPort: 3306,
|
||||
dbName: 'platform',
|
||||
dbUsername: 'platform',
|
||||
dbPassword: 'platform',
|
||||
};
|
||||
|
||||
// Connect to tenant database (using root for now since tenant password is encrypted)
|
||||
const tenantDb: Knex = knex({
|
||||
client: 'mysql2',
|
||||
connection: {
|
||||
host: tenantInfo.dbHost,
|
||||
port: tenantInfo.dbPort,
|
||||
database: tenantInfo.dbName,
|
||||
user: 'root',
|
||||
password: 'asjdnfqTash37faggT',
|
||||
},
|
||||
});
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
if (existingUser) {
|
||||
console.log(`User ${email} already exists in tenant ${tenantSlug}`);
|
||||
await tenantDb.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Create user
|
||||
await tenantDb('users').insert({
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
isActive: true,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
console.log(`\nUser created successfully in tenant ${tenantSlug}!`);
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
|
||||
// Create admin role if it doesn't exist
|
||||
let adminRole = await tenantDb('roles')
|
||||
.where({ name: 'admin' })
|
||||
.first();
|
||||
|
||||
if (!adminRole) {
|
||||
await tenantDb('roles').insert({
|
||||
name: 'admin',
|
||||
guardName: 'api',
|
||||
description: 'Administrator role with full access',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
adminRole = await tenantDb('roles')
|
||||
.where({ name: 'admin' })
|
||||
.first();
|
||||
|
||||
console.log('Admin role created');
|
||||
}
|
||||
|
||||
// Get the created user
|
||||
const user = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
// Assign admin role to user
|
||||
if (adminRole && user) {
|
||||
await tenantDb('user_roles').insert({
|
||||
userId: user.id,
|
||||
roleId: adminRole.id,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
console.log('Admin role assigned to user');
|
||||
}
|
||||
|
||||
await tenantDb.destroy();
|
||||
} catch (error) {
|
||||
console.error('Error creating tenant user:', error);
|
||||
} finally {
|
||||
await centralPrisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
createTenantUser();
|
||||
@@ -1,72 +0,0 @@
|
||||
import { getCentralPrisma } from '../src/prisma/central-prisma.service';
|
||||
import * as knex from 'knex';
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
function decrypt(text: string): string {
|
||||
const parts = text.split(':');
|
||||
const iv = Buffer.from(parts.shift()!, 'hex');
|
||||
const encryptedText = Buffer.from(parts.join(':'), 'hex');
|
||||
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
||||
const decipher = crypto.createDecipheriv(
|
||||
'aes-256-cbc',
|
||||
key,
|
||||
iv,
|
||||
);
|
||||
let decrypted = decipher.update(encryptedText);
|
||||
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
||||
return decrypted.toString();
|
||||
}
|
||||
|
||||
async function updateNameField() {
|
||||
const centralPrisma = getCentralPrisma();
|
||||
|
||||
try {
|
||||
// Find tenant1
|
||||
const tenant = await centralPrisma.tenant.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ id: 'tenant1' },
|
||||
{ slug: 'tenant1' },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
console.error('❌ Tenant tenant1 not found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`📋 Tenant: ${tenant.name} (${tenant.slug})`);
|
||||
console.log(`📊 Database: ${tenant.dbName}`);
|
||||
|
||||
// Decrypt password
|
||||
const password = decrypt(tenant.dbPassword);
|
||||
|
||||
// Create connection
|
||||
const tenantKnex = knex.default({
|
||||
client: 'mysql2',
|
||||
connection: {
|
||||
host: tenant.dbHost,
|
||||
port: tenant.dbPort,
|
||||
user: tenant.dbUsername,
|
||||
password: password,
|
||||
database: tenant.dbName,
|
||||
},
|
||||
});
|
||||
|
||||
// Update Account object
|
||||
await tenantKnex('object_definitions')
|
||||
.where({ apiName: 'Account' })
|
||||
.update({ nameField: 'name' });
|
||||
|
||||
console.log('✅ Updated Account object nameField to "name"');
|
||||
|
||||
await tenantKnex.destroy();
|
||||
await centralPrisma.$disconnect();
|
||||
} catch (error) {
|
||||
console.error('❌ Error:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
updateNameField();
|
||||
@@ -12,7 +12,7 @@ import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { TenantId } from '../tenant/tenant.decorator';
|
||||
|
||||
@Controller('setup/apps')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
//@UseGuards(JwtAuthGuard)
|
||||
export class SetupAppController {
|
||||
constructor(private appBuilderService: AppBuilderService) {}
|
||||
|
||||
|
||||
@@ -42,13 +42,8 @@ export class AuthController {
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('login')
|
||||
async login(@TenantId() tenantId: string, @Body() loginDto: LoginDto) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
|
||||
async login(@Body() loginDto: LoginDto) {
|
||||
const user = await this.authService.validateUser(
|
||||
tenantId,
|
||||
loginDto.email,
|
||||
loginDto.password,
|
||||
);
|
||||
@@ -62,15 +57,9 @@ export class AuthController {
|
||||
|
||||
@Post('register')
|
||||
async register(
|
||||
@TenantId() tenantId: string,
|
||||
@Body() registerDto: RegisterDto,
|
||||
) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
|
||||
const user = await this.authService.register(
|
||||
tenantId,
|
||||
registerDto.email,
|
||||
registerDto.password,
|
||||
registerDto.firstName,
|
||||
@@ -79,12 +68,4 @@ export class AuthController {
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('logout')
|
||||
async logout() {
|
||||
// For stateless JWT, logout is handled on client-side
|
||||
// This endpoint exists for consistency and potential future enhancements
|
||||
return { message: 'Logged out successfully' };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import { ConfigService } from '@nestjs/config';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
import { TenantModule } from '../tenant/tenant.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PassportModule,
|
||||
TenantModule,
|
||||
JwtModule.registerAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { TenantDatabaseService } from '../tenant/tenant-database.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private tenantDbService: TenantDatabaseService,
|
||||
private prisma: PrismaService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async validateUser(
|
||||
tenantId: string,
|
||||
email: string,
|
||||
password: string,
|
||||
): Promise<any> {
|
||||
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
include: {
|
||||
userRoles: {
|
||||
include: {
|
||||
role: {
|
||||
include: {
|
||||
rolePermissions: {
|
||||
include: {
|
||||
permission: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const user = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (await bcrypt.compare(password, user.password)) {
|
||||
// Load user roles and permissions
|
||||
const userRoles = await tenantDb('user_roles')
|
||||
.where({ userId: user.id })
|
||||
.join('roles', 'user_roles.roleId', 'roles.id')
|
||||
.select('roles.*');
|
||||
|
||||
const { password: _, ...result } = user;
|
||||
return {
|
||||
...result,
|
||||
tenantId,
|
||||
userRoles,
|
||||
};
|
||||
if (user && (await bcrypt.compare(password, user.password))) {
|
||||
const { password, ...result } = user;
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -61,30 +61,22 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async register(
|
||||
tenantId: string,
|
||||
email: string,
|
||||
password: string,
|
||||
firstName?: string,
|
||||
lastName?: string,
|
||||
) {
|
||||
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
const [userId] = await tenantDb('users').insert({
|
||||
const user = await this.prisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
isActive: true,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
const user = await tenantDb('users')
|
||||
.where({ id: userId })
|
||||
.first();
|
||||
|
||||
const { password: _, ...result } = user;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -8,23 +8,9 @@ export class ObjectService {
|
||||
// Setup endpoints - Object metadata management
|
||||
async getObjectDefinitions(tenantId: string) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
const objects = await knex('object_definitions')
|
||||
.select('object_definitions.*')
|
||||
return knex('object_definitions')
|
||||
.select('*')
|
||||
.orderBy('label', 'asc');
|
||||
|
||||
// Fetch app information for objects that have app_id
|
||||
for (const obj of objects) {
|
||||
if (obj.app_id) {
|
||||
const app = await knex('apps')
|
||||
.where({ id: obj.app_id })
|
||||
.select('id', 'slug', 'label', 'description')
|
||||
.first();
|
||||
obj.app = app;
|
||||
}
|
||||
}
|
||||
|
||||
return objects;
|
||||
}
|
||||
|
||||
async getObjectDefinition(tenantId: string, apiName: string) {
|
||||
@@ -43,19 +29,9 @@ export class ObjectService {
|
||||
.where({ objectDefinitionId: obj.id })
|
||||
.orderBy('label', 'asc');
|
||||
|
||||
// Get app information if object belongs to an app
|
||||
let app = null;
|
||||
if (obj.app_id) {
|
||||
app = await knex('apps')
|
||||
.where({ id: obj.app_id })
|
||||
.select('id', 'slug', 'label', 'description')
|
||||
.first();
|
||||
}
|
||||
|
||||
return {
|
||||
...obj,
|
||||
fields,
|
||||
app,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -108,25 +84,6 @@ export class ObjectService {
|
||||
return knex('field_definitions').where({ id }).first();
|
||||
}
|
||||
|
||||
// Helper to get table name from object definition
|
||||
private getTableName(objectApiName: string): string {
|
||||
// Convert CamelCase to snake_case and pluralize
|
||||
// Account -> accounts, ContactPerson -> contact_persons
|
||||
const snakeCase = objectApiName
|
||||
.replace(/([A-Z])/g, '_$1')
|
||||
.toLowerCase()
|
||||
.replace(/^_/, '');
|
||||
|
||||
// Simple pluralization (can be enhanced)
|
||||
if (snakeCase.endsWith('y')) {
|
||||
return snakeCase.slice(0, -1) + 'ies';
|
||||
} else if (snakeCase.endsWith('s')) {
|
||||
return snakeCase;
|
||||
} else {
|
||||
return snakeCase + 's';
|
||||
}
|
||||
}
|
||||
|
||||
// Runtime endpoints - CRUD operations
|
||||
async getRecords(
|
||||
tenantId: string,
|
||||
@@ -136,25 +93,15 @@ export class ObjectService {
|
||||
) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Verify object exists
|
||||
await this.getObjectDefinition(tenantId, objectApiName);
|
||||
|
||||
const tableName = this.getTableName(objectApiName);
|
||||
|
||||
let query = knex(tableName);
|
||||
|
||||
// Add ownership filter if ownerId field exists
|
||||
const hasOwner = await knex.schema.hasColumn(tableName, 'ownerId');
|
||||
if (hasOwner) {
|
||||
query = query.where({ ownerId: userId });
|
||||
// For demonstration, using Account as example static object
|
||||
if (objectApiName === 'Account') {
|
||||
return knex('accounts')
|
||||
.where({ ownerId: userId })
|
||||
.where(filters || {});
|
||||
}
|
||||
|
||||
// Apply additional filters
|
||||
if (filters) {
|
||||
query = query.where(filters);
|
||||
}
|
||||
|
||||
return query.select('*');
|
||||
// For custom objects, you'd need dynamic query building
|
||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||
}
|
||||
|
||||
async getRecord(
|
||||
@@ -165,20 +112,10 @@ export class ObjectService {
|
||||
) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Verify object exists
|
||||
await this.getObjectDefinition(tenantId, objectApiName);
|
||||
|
||||
const tableName = this.getTableName(objectApiName);
|
||||
|
||||
let query = knex(tableName).where({ id: recordId });
|
||||
|
||||
// Add ownership filter if ownerId field exists
|
||||
const hasOwner = await knex.schema.hasColumn(tableName, 'ownerId');
|
||||
if (hasOwner) {
|
||||
query = query.where({ ownerId: userId });
|
||||
}
|
||||
|
||||
const record = await query.first();
|
||||
if (objectApiName === 'Account') {
|
||||
const record = await knex('accounts')
|
||||
.where({ id: recordId, ownerId: userId })
|
||||
.first();
|
||||
|
||||
if (!record) {
|
||||
throw new NotFoundException('Record not found');
|
||||
@@ -187,6 +124,9 @@ export class ObjectService {
|
||||
return record;
|
||||
}
|
||||
|
||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||
}
|
||||
|
||||
async createRecord(
|
||||
tenantId: string,
|
||||
objectApiName: string,
|
||||
@@ -195,28 +135,19 @@ export class ObjectService {
|
||||
) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Verify object exists
|
||||
await this.getObjectDefinition(tenantId, objectApiName);
|
||||
|
||||
const tableName = this.getTableName(objectApiName);
|
||||
|
||||
// Check if table has ownerId column
|
||||
const hasOwner = await knex.schema.hasColumn(tableName, 'ownerId');
|
||||
|
||||
const recordData: any = {
|
||||
if (objectApiName === 'Account') {
|
||||
const [id] = await knex('accounts').insert({
|
||||
id: knex.raw('(UUID())'),
|
||||
ownerId: userId,
|
||||
...data,
|
||||
created_at: knex.fn.now(),
|
||||
updated_at: knex.fn.now(),
|
||||
};
|
||||
});
|
||||
|
||||
if (hasOwner) {
|
||||
recordData.ownerId = userId;
|
||||
return knex('accounts').where({ id }).first();
|
||||
}
|
||||
|
||||
const [id] = await knex(tableName).insert(recordData);
|
||||
|
||||
return knex(tableName).where({ id }).first();
|
||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||
}
|
||||
|
||||
async updateRecord(
|
||||
@@ -228,16 +159,18 @@ export class ObjectService {
|
||||
) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Verify object exists and user has access
|
||||
if (objectApiName === 'Account') {
|
||||
// Verify ownership
|
||||
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
||||
|
||||
const tableName = this.getTableName(objectApiName);
|
||||
|
||||
await knex(tableName)
|
||||
await knex('accounts')
|
||||
.where({ id: recordId })
|
||||
.update({ ...data, updated_at: knex.fn.now() });
|
||||
|
||||
return knex(tableName).where({ id: recordId }).first();
|
||||
return knex('accounts').where({ id: recordId }).first();
|
||||
}
|
||||
|
||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||
}
|
||||
|
||||
async deleteRecord(
|
||||
@@ -248,13 +181,15 @@ export class ObjectService {
|
||||
) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Verify object exists and user has access
|
||||
if (objectApiName === 'Account') {
|
||||
// Verify ownership
|
||||
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
||||
|
||||
const tableName = this.getTableName(objectApiName);
|
||||
|
||||
await knex(tableName).where({ id: recordId }).delete();
|
||||
await knex('accounts').where({ id: recordId }).delete();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,30 +8,22 @@ export class TenantDatabaseService {
|
||||
private readonly logger = new Logger(TenantDatabaseService.name);
|
||||
private tenantConnections: Map<string, Knex> = new Map();
|
||||
|
||||
async getTenantKnex(tenantIdOrSlug: string): Promise<Knex> {
|
||||
if (this.tenantConnections.has(tenantIdOrSlug)) {
|
||||
return this.tenantConnections.get(tenantIdOrSlug);
|
||||
async getTenantKnex(tenantId: string): Promise<Knex> {
|
||||
if (this.tenantConnections.has(tenantId)) {
|
||||
return this.tenantConnections.get(tenantId);
|
||||
}
|
||||
|
||||
const centralPrisma = getCentralPrisma();
|
||||
|
||||
// Try to find tenant by ID first, then by slug
|
||||
let tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { id: tenantIdOrSlug },
|
||||
const tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { id: tenantId },
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { slug: tenantIdOrSlug },
|
||||
});
|
||||
}
|
||||
|
||||
if (!tenant) {
|
||||
throw new Error(`Tenant ${tenantIdOrSlug} not found`);
|
||||
throw new Error(`Tenant ${tenantId} not found`);
|
||||
}
|
||||
|
||||
if (tenant.status !== 'active') {
|
||||
throw new Error(`Tenant ${tenantIdOrSlug} is not active`);
|
||||
throw new Error(`Tenant ${tenantId} is not active`);
|
||||
}
|
||||
|
||||
// Decrypt password
|
||||
@@ -64,7 +56,7 @@ export class TenantDatabaseService {
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.tenantConnections.set(tenantIdOrSlug, tenantKnex);
|
||||
this.tenantConnections.set(tenantId, tenantKnex);
|
||||
return tenantKnex;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,53 +19,29 @@ export class TenantMiddleware implements NestMiddleware {
|
||||
const hostname = host.split(':')[0]; // Remove port if present
|
||||
const parts = hostname.split('.');
|
||||
|
||||
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}`);
|
||||
|
||||
// For local development, accept x-tenant-id header
|
||||
// For local development, accept x-tenant-id header as fallback
|
||||
let tenantId = req.headers['x-tenant-id'] as string;
|
||||
let subdomain: string | null = null;
|
||||
|
||||
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}, x-tenant-id: ${tenantId}`);
|
||||
|
||||
// If x-tenant-id is explicitly provided, use it directly
|
||||
if (tenantId) {
|
||||
this.logger.log(`Using explicit x-tenant-id: ${tenantId}`);
|
||||
(req as any).tenantId = tenantId;
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract subdomain (e.g., "tenant1" from "tenant1.routebox.co")
|
||||
// For production domains with 3+ parts, extract first part as subdomain
|
||||
if (parts.length >= 3) {
|
||||
// Extract subdomain (e.g., "acme" from "acme.routebox.co")
|
||||
if (parts.length > 2) {
|
||||
subdomain = parts[0];
|
||||
// Ignore www subdomain
|
||||
if (subdomain === 'www') {
|
||||
subdomain = null;
|
||||
}
|
||||
}
|
||||
// For development (e.g., tenant1.localhost), also check 2 parts
|
||||
else if (parts.length === 2 && parts[1] === 'localhost') {
|
||||
subdomain = parts[0];
|
||||
}
|
||||
|
||||
this.logger.log(`Extracted subdomain: ${subdomain}`);
|
||||
|
||||
// Get tenant by subdomain if available
|
||||
if (subdomain) {
|
||||
try {
|
||||
const tenant = await this.tenantDbService.getTenantByDomain(subdomain);
|
||||
if (tenant) {
|
||||
tenantId = tenant.id;
|
||||
this.logger.log(
|
||||
`Tenant identified: ${tenant.name} (${tenant.id}) from subdomain: ${subdomain}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(`No tenant found for subdomain: ${subdomain}`, error.message);
|
||||
// Fall back to using subdomain as tenantId directly if domain lookup fails
|
||||
tenantId = subdomain;
|
||||
this.logger.log(`Using subdomain as tenantId fallback: ${tenantId}`);
|
||||
} else {
|
||||
this.logger.warn(`No tenant found for subdomain: ${subdomain}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { Toaster } from 'vue-sonner'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<Toaster position="top-right" :duration="4000" richColors />
|
||||
<NuxtPage />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import {
|
||||
Sidebar,
|
||||
SidebarContent,
|
||||
@@ -17,56 +16,9 @@ import {
|
||||
SidebarRail,
|
||||
} from '@/components/ui/sidebar'
|
||||
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'
|
||||
import { LayoutGrid, Boxes, Settings, Home, ChevronRight, Database, Layers, LogOut } from 'lucide-vue-next'
|
||||
import { LayoutGrid, Boxes, Settings, Home, ChevronRight, Database, Layers } from 'lucide-vue-next'
|
||||
|
||||
const { logout } = useAuth()
|
||||
const { api } = useApi()
|
||||
|
||||
const handleLogout = async () => {
|
||||
await logout()
|
||||
}
|
||||
|
||||
// Fetch objects and group by app
|
||||
const apps = ref<any[]>([])
|
||||
const topLevelObjects = ref<any[]>([])
|
||||
const loading = ref(true)
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const response = await api.get('/setup/objects')
|
||||
const allObjects = response.data || response || []
|
||||
|
||||
// Group objects by app
|
||||
const appMap = new Map<string, any>()
|
||||
const noAppObjects: any[] = []
|
||||
|
||||
allObjects.forEach((obj: any) => {
|
||||
const appId = obj.app_id || obj.appId
|
||||
if (appId) {
|
||||
if (!appMap.has(appId)) {
|
||||
appMap.set(appId, {
|
||||
id: appId,
|
||||
name: obj.app?.name || obj.app?.label || 'Unknown App',
|
||||
label: obj.app?.label || obj.app?.name || 'Unknown App',
|
||||
objects: []
|
||||
})
|
||||
}
|
||||
appMap.get(appId)!.objects.push(obj)
|
||||
} else {
|
||||
noAppObjects.push(obj)
|
||||
}
|
||||
})
|
||||
|
||||
apps.value = Array.from(appMap.values())
|
||||
topLevelObjects.value = noAppObjects
|
||||
} catch (e) {
|
||||
console.error('Failed to load objects:', e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
|
||||
const staticMenuItems = [
|
||||
const menuItems = [
|
||||
{
|
||||
title: 'Home',
|
||||
url: '/',
|
||||
@@ -88,6 +40,17 @@ const staticMenuItems = [
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'Runtime',
|
||||
icon: Database,
|
||||
items: [
|
||||
{
|
||||
title: 'My Apps',
|
||||
url: '/app',
|
||||
icon: Layers,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
</script>
|
||||
|
||||
@@ -113,12 +76,11 @@ const staticMenuItems = [
|
||||
</SidebarMenu>
|
||||
</SidebarHeader>
|
||||
<SidebarContent>
|
||||
<!-- Static Menu Items -->
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Navigation</SidebarGroupLabel>
|
||||
<SidebarGroupLabel>Application</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
<template v-for="item in staticMenuItems" :key="item.title">
|
||||
<template v-for="item in menuItems" :key="item.title">
|
||||
<!-- Simple menu item -->
|
||||
<SidebarMenuItem v-if="!item.items">
|
||||
<SidebarMenuButton as-child>
|
||||
@@ -159,70 +121,12 @@ const staticMenuItems = [
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
|
||||
<!-- Top-level Objects (no app) -->
|
||||
<SidebarGroup v-if="!loading && topLevelObjects.length > 0">
|
||||
<SidebarGroupLabel>Objects</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
<SidebarMenuItem v-for="obj in topLevelObjects" :key="obj.id">
|
||||
<SidebarMenuButton as-child>
|
||||
<NuxtLink :to="`/${obj.apiName.toLowerCase()}`">
|
||||
<Database class="h-4 w-4" />
|
||||
<span>{{ obj.label || obj.apiName }}</span>
|
||||
</NuxtLink>
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
|
||||
<!-- App-grouped Objects -->
|
||||
<SidebarGroup v-if="!loading && apps.length > 0">
|
||||
<SidebarGroupLabel>Apps</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
<Collapsible
|
||||
v-for="app in apps"
|
||||
:key="app.id"
|
||||
as-child
|
||||
:default-open="true"
|
||||
class="group/collapsible"
|
||||
>
|
||||
<SidebarMenuItem>
|
||||
<CollapsibleTrigger as-child>
|
||||
<SidebarMenuButton :tooltip="app.label">
|
||||
<LayoutGrid class="h-4 w-4" />
|
||||
<span>{{ app.label }}</span>
|
||||
<ChevronRight
|
||||
class="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90"
|
||||
/>
|
||||
</SidebarMenuButton>
|
||||
</CollapsibleTrigger>
|
||||
<CollapsibleContent>
|
||||
<SidebarMenuSub>
|
||||
<SidebarMenuSubItem v-for="obj in app.objects" :key="obj.id">
|
||||
<SidebarMenuSubButton as-child>
|
||||
<NuxtLink :to="`/${obj.apiName.toLowerCase()}`">
|
||||
<Database class="h-4 w-4" />
|
||||
<span>{{ obj.label || obj.apiName }}</span>
|
||||
</NuxtLink>
|
||||
</SidebarMenuSubButton>
|
||||
</SidebarMenuSubItem>
|
||||
</SidebarMenuSub>
|
||||
</CollapsibleContent>
|
||||
</SidebarMenuItem>
|
||||
</Collapsible>
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
</SidebarContent>
|
||||
<SidebarFooter>
|
||||
<SidebarMenu>
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton @click="handleLogout" class="cursor-pointer hover:bg-accent">
|
||||
<LogOut class="h-4 w-4" />
|
||||
<span>Logout</span>
|
||||
<SidebarMenuButton>
|
||||
<span class="text-sm text-muted-foreground">Logged in as user</span>
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
</SidebarMenu>
|
||||
|
||||
@@ -5,34 +5,8 @@ import { Label } from '@/components/ui/label'
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
const { toast } = useToast()
|
||||
|
||||
// Cookie for server-side auth check
|
||||
const tokenCookie = useCookie('token')
|
||||
|
||||
// Extract subdomain from hostname (e.g., tenant1.localhost → tenant1)
|
||||
const getSubdomain = () => {
|
||||
if (!import.meta.client) return null
|
||||
const hostname = window.location.hostname
|
||||
const parts = hostname.split('.')
|
||||
|
||||
console.log('Extracting subdomain from:', hostname, 'parts:', parts)
|
||||
|
||||
// For localhost development: tenant1.localhost or localhost
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return null // Use default tenant for plain localhost
|
||||
}
|
||||
|
||||
// For subdomains like tenant1.routebox.co or tenant1.localhost
|
||||
if (parts.length >= 2 && parts[0] !== 'www') {
|
||||
console.log('Using subdomain:', parts[0])
|
||||
return parts[0] // Return subdomain
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const subdomain = ref(getSubdomain())
|
||||
const tenantId = ref('123')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const loading = ref(false)
|
||||
@@ -43,18 +17,12 @@ const handleLogin = async () => {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
// Only send x-tenant-id if we have a subdomain
|
||||
if (subdomain.value) {
|
||||
headers['x-tenant-id'] = subdomain.value
|
||||
}
|
||||
|
||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-tenant-id': tenantId.value,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email.value,
|
||||
password: password.value,
|
||||
@@ -68,23 +36,15 @@ const handleLogin = async () => {
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Store credentials in localStorage
|
||||
// Store the tenant ID that was used for login
|
||||
const tenantToStore = subdomain.value || data.user?.tenantId || 'tenant1'
|
||||
localStorage.setItem('tenantId', tenantToStore)
|
||||
// Store credentials
|
||||
localStorage.setItem('tenantId', tenantId.value)
|
||||
localStorage.setItem('token', data.access_token)
|
||||
localStorage.setItem('user', JSON.stringify(data.user))
|
||||
|
||||
// Also store token in cookie for server-side auth check
|
||||
tokenCookie.value = data.access_token
|
||||
|
||||
toast.success('Login successful!')
|
||||
|
||||
// Redirect to home
|
||||
router.push('/')
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Login failed'
|
||||
toast.error(e.message || 'Login failed')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -105,6 +65,10 @@ const handleLogin = async () => {
|
||||
</div>
|
||||
|
||||
<div class="grid gap-6">
|
||||
<div class="grid gap-2">
|
||||
<Label for="tenantId">Tenant ID</Label>
|
||||
<Input id="tenantId" v-model="tenantId" type="text" placeholder="123" required />
|
||||
</div>
|
||||
<div class="grid gap-2">
|
||||
<Label for="email">Email</Label>
|
||||
<Input id="email" v-model="email" type="email" placeholder="m@example.com" required />
|
||||
|
||||
@@ -47,22 +47,18 @@ const sections = computed<FieldSection[]>(() => {
|
||||
}
|
||||
|
||||
// Default section with all visible fields
|
||||
const visibleFields = props.config.fields
|
||||
.filter(f => f.showOnEdit !== false)
|
||||
.map(f => f.apiName)
|
||||
|
||||
return [{
|
||||
title: 'Details',
|
||||
fields: visibleFields,
|
||||
fields: props.config.fields
|
||||
.filter(f => f.showOnEdit !== false)
|
||||
.map(f => f.apiName),
|
||||
}]
|
||||
})
|
||||
|
||||
const getFieldsBySection = (section: FieldSection) => {
|
||||
const fields = section.fields
|
||||
return section.fields
|
||||
.map(apiName => props.config.fields.find(f => f.apiName === apiName))
|
||||
.filter(Boolean)
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
const validateField = (field: any): string | null => {
|
||||
|
||||
@@ -231,12 +231,4 @@ const handleAction = (actionId: string) => {
|
||||
.list-view {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.list-view :deep(.border) {
|
||||
background-color: hsl(var(--card));
|
||||
}
|
||||
|
||||
.list-view :deep(input) {
|
||||
background-color: hsl(var(--background));
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
export const useApi = () => {
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
const { toast } = useToast()
|
||||
const { isLoggedIn, logout } = useAuth()
|
||||
|
||||
// Use current domain for API calls (same subdomain routing)
|
||||
const getApiBaseUrl = () => {
|
||||
@@ -37,92 +34,13 @@ export const useApi = () => {
|
||||
return headers
|
||||
}
|
||||
|
||||
const handleResponse = async (response: Response) => {
|
||||
if (response.status === 401) {
|
||||
// Unauthorized - not authenticated
|
||||
if (import.meta.client) {
|
||||
logout()
|
||||
toast.error('Your session has expired. Please login again.')
|
||||
router.push('/login')
|
||||
}
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
// Forbidden - not authorized
|
||||
if (import.meta.client) {
|
||||
toast.error('You do not have permission to perform this action.')
|
||||
// Redirect to home if logged in, otherwise to login
|
||||
if (isLoggedIn()) {
|
||||
router.push('/')
|
||||
} else {
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
throw new Error('Forbidden')
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
// Try to get error details from response
|
||||
const text = await response.text()
|
||||
console.error('API Error Response:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
body: text
|
||||
})
|
||||
|
||||
let errorMessage = `HTTP error! status: ${response.status}`
|
||||
if (text) {
|
||||
try {
|
||||
const errorData = JSON.parse(text)
|
||||
errorMessage = errorData.message || errorData.error || errorMessage
|
||||
} catch (e) {
|
||||
// If not JSON, use the text directly if it's not too long
|
||||
if (text.length < 200) {
|
||||
errorMessage = text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
|
||||
// Handle empty responses
|
||||
const text = await response.text()
|
||||
if (!text) {
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(text)
|
||||
} catch (e) {
|
||||
console.error('Failed to parse JSON response:', text)
|
||||
throw new Error('Invalid JSON response from server')
|
||||
}
|
||||
}
|
||||
|
||||
const api = {
|
||||
async get(path: string, options?: { params?: Record<string, any> }) {
|
||||
let url = `${getApiBaseUrl()}/api${path}`
|
||||
|
||||
// Add query parameters if provided
|
||||
if (options?.params) {
|
||||
const searchParams = new URLSearchParams()
|
||||
Object.entries(options.params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
searchParams.append(key, String(value))
|
||||
}
|
||||
})
|
||||
const queryString = searchParams.toString()
|
||||
if (queryString) {
|
||||
url += `?${queryString}`
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
async get(path: string) {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
headers: getHeaders(),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async post(path: string, data: any) {
|
||||
@@ -131,7 +49,8 @@ export const useApi = () => {
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async put(path: string, data: any) {
|
||||
@@ -140,7 +59,8 @@ export const useApi = () => {
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async delete(path: string) {
|
||||
@@ -148,7 +68,8 @@ export const useApi = () => {
|
||||
method: 'DELETE',
|
||||
headers: getHeaders(),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
export const useAuth = () => {
|
||||
const tokenCookie = useCookie('token')
|
||||
const authMessageCookie = useCookie('authMessage')
|
||||
const router = useRouter()
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const isLoggedIn = () => {
|
||||
if (!import.meta.client) return false
|
||||
const token = localStorage.getItem('token')
|
||||
const tenantId = localStorage.getItem('tenantId')
|
||||
return !!(token && tenantId)
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
if (import.meta.client) {
|
||||
// Call backend logout endpoint
|
||||
try {
|
||||
const token = localStorage.getItem('token')
|
||||
const tenantId = localStorage.getItem('tenantId')
|
||||
|
||||
if (token) {
|
||||
await fetch(`${config.public.apiBaseUrl}/api/auth/logout`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
...(tenantId && { 'x-tenant-id': tenantId }),
|
||||
},
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error)
|
||||
}
|
||||
|
||||
// Clear local storage
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('tenantId')
|
||||
localStorage.removeItem('user')
|
||||
|
||||
// Clear cookie for server-side check
|
||||
tokenCookie.value = null
|
||||
|
||||
// Set flash message for login page
|
||||
authMessageCookie.value = 'Logged out successfully'
|
||||
|
||||
// Redirect to login page
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
|
||||
const getUser = () => {
|
||||
if (!import.meta.client) return null
|
||||
const userStr = localStorage.getItem('user')
|
||||
return userStr ? JSON.parse(userStr) : null
|
||||
}
|
||||
|
||||
return {
|
||||
isLoggedIn,
|
||||
logout,
|
||||
getUser,
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
// Shared state for breadcrumbs
|
||||
const customBreadcrumbs = ref<Array<{ name: string; path?: string; isLast?: boolean }>>([])
|
||||
|
||||
export function useBreadcrumbs() {
|
||||
const setBreadcrumbs = (crumbs: Array<{ name: string; path?: string; isLast?: boolean }>) => {
|
||||
customBreadcrumbs.value = crumbs
|
||||
}
|
||||
|
||||
const clearBreadcrumbs = () => {
|
||||
customBreadcrumbs.value = []
|
||||
}
|
||||
|
||||
return {
|
||||
breadcrumbs: customBreadcrumbs,
|
||||
setBreadcrumbs,
|
||||
clearBreadcrumbs
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,6 @@ export const useFields = () => {
|
||||
* Convert backend field definition to frontend FieldConfig
|
||||
*/
|
||||
const mapFieldDefinitionToConfig = (fieldDef: any): FieldConfig => {
|
||||
// Convert isSystem to boolean (handle 0/1 from database)
|
||||
const isSystemField = Boolean(fieldDef.isSystem)
|
||||
|
||||
// Only truly system fields (id, createdAt, updatedAt, etc.) should be hidden on edit
|
||||
const isAutoGeneratedField = ['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy'].includes(fieldDef.apiName)
|
||||
|
||||
return {
|
||||
id: fieldDef.id,
|
||||
apiName: fieldDef.apiName,
|
||||
@@ -29,13 +23,13 @@ export const useFields = () => {
|
||||
|
||||
// Validation
|
||||
isRequired: fieldDef.isRequired,
|
||||
isReadOnly: isAutoGeneratedField || fieldDef.uiMetadata?.isReadOnly,
|
||||
isReadOnly: fieldDef.isSystem || fieldDef.uiMetadata?.isReadOnly,
|
||||
validationRules: fieldDef.uiMetadata?.validationRules || [],
|
||||
|
||||
// View options - only hide auto-generated fields by default
|
||||
// View options
|
||||
showOnList: fieldDef.uiMetadata?.showOnList ?? true,
|
||||
showOnDetail: fieldDef.uiMetadata?.showOnDetail ?? true,
|
||||
showOnEdit: fieldDef.uiMetadata?.showOnEdit ?? !isAutoGeneratedField,
|
||||
showOnEdit: fieldDef.uiMetadata?.showOnEdit ?? !fieldDef.isSystem,
|
||||
sortable: fieldDef.uiMetadata?.sortable ?? true,
|
||||
|
||||
// Field type specific
|
||||
@@ -182,15 +176,14 @@ export const useViewState = <T extends { id?: string }>(
|
||||
const saving = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const { api } = useApi()
|
||||
const api = useApi()
|
||||
|
||||
const fetchRecords = async (params?: Record<string, any>) => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await api.get(apiEndpoint, { params })
|
||||
// Handle response - data might be directly in response or in response.data
|
||||
records.value = response.data || response || []
|
||||
records.value = response.data
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
console.error('Failed to fetch records:', e)
|
||||
@@ -204,8 +197,7 @@ export const useViewState = <T extends { id?: string }>(
|
||||
error.value = null
|
||||
try {
|
||||
const response = await api.get(`${apiEndpoint}/${id}`)
|
||||
// Handle response - data might be directly in response or in response.data
|
||||
currentRecord.value = response.data || response
|
||||
currentRecord.value = response.data
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
console.error('Failed to fetch record:', e)
|
||||
@@ -219,12 +211,9 @@ export const useViewState = <T extends { id?: string }>(
|
||||
error.value = null
|
||||
try {
|
||||
const response = await api.post(apiEndpoint, data)
|
||||
|
||||
// Handle response - it might be the data directly or wrapped in { data: ... }
|
||||
const recordData = response.data || response
|
||||
records.value.push(recordData)
|
||||
currentRecord.value = recordData
|
||||
return recordData
|
||||
records.value.push(response.data)
|
||||
currentRecord.value = response.data
|
||||
return response.data
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
console.error('Failed to create record:', e)
|
||||
@@ -238,18 +227,13 @@ export const useViewState = <T extends { id?: string }>(
|
||||
saving.value = true
|
||||
error.value = null
|
||||
try {
|
||||
// Remove auto-generated fields that shouldn't be updated
|
||||
const { id: _id, createdAt, created_at, updatedAt, updated_at, createdBy, updatedBy, ...updateData } = data as any
|
||||
|
||||
const response = await api.put(`${apiEndpoint}/${id}`, updateData)
|
||||
// Handle response - data might be directly in response or in response.data
|
||||
const recordData = response.data || response
|
||||
const response = await api.put(`${apiEndpoint}/${id}`, data)
|
||||
const idx = records.value.findIndex(r => r.id === id)
|
||||
if (idx !== -1) {
|
||||
records.value[idx] = recordData
|
||||
records.value[idx] = response.data
|
||||
}
|
||||
currentRecord.value = recordData
|
||||
return recordData
|
||||
currentRecord.value = response.data
|
||||
return response.data
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
console.error('Failed to update record:', e)
|
||||
@@ -308,13 +292,12 @@ export const useViewState = <T extends { id?: string }>(
|
||||
}
|
||||
|
||||
const handleSave = async (data: T) => {
|
||||
let savedRecord
|
||||
if (data.id) {
|
||||
savedRecord = await updateRecord(data.id, data)
|
||||
await updateRecord(data.id, data)
|
||||
} else {
|
||||
savedRecord = await createRecord(data)
|
||||
await createRecord(data)
|
||||
}
|
||||
return savedRecord
|
||||
showDetail(currentRecord.value!)
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { toast as sonnerToast } from 'vue-sonner'
|
||||
|
||||
export const useToast = () => {
|
||||
const toast = {
|
||||
success: (message: string) => {
|
||||
sonnerToast.success(message)
|
||||
},
|
||||
error: (message: string) => {
|
||||
sonnerToast.error(message)
|
||||
},
|
||||
info: (message: string) => {
|
||||
sonnerToast.info(message)
|
||||
},
|
||||
warning: (message: string) => {
|
||||
sonnerToast.warning(message)
|
||||
},
|
||||
}
|
||||
|
||||
return { toast }
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import AppSidebar from '@/components/AppSidebar.vue'
|
||||
import AIChatBar from '@/components/AIChatBar.vue'
|
||||
import {
|
||||
@@ -14,15 +13,8 @@ import { Separator } from '@/components/ui/separator'
|
||||
import { SidebarInset, SidebarProvider, SidebarTrigger } from '@/components/ui/sidebar'
|
||||
|
||||
const route = useRoute()
|
||||
const { breadcrumbs: customBreadcrumbs } = useBreadcrumbs()
|
||||
|
||||
const breadcrumbs = computed(() => {
|
||||
// If custom breadcrumbs are set by the page, use those
|
||||
if (customBreadcrumbs.value.length > 0) {
|
||||
return customBreadcrumbs.value
|
||||
}
|
||||
|
||||
// Otherwise, fall back to URL-based breadcrumbs
|
||||
const paths = route.path.split('/').filter(Boolean)
|
||||
return paths.map((path, index) => ({
|
||||
name: path.charAt(0).toUpperCase() + path.slice(1),
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
// Allow pages to opt-out of auth with definePageMeta({ auth: false })
|
||||
if (to.meta.auth === false) {
|
||||
return
|
||||
}
|
||||
|
||||
// Public routes that don't require authentication
|
||||
const publicRoutes = ['/login', '/register']
|
||||
|
||||
if (publicRoutes.includes(to.path)) {
|
||||
return
|
||||
}
|
||||
|
||||
const token = useCookie('token')
|
||||
const authMessage = useCookie('authMessage')
|
||||
|
||||
// Routes that don't need a toast message (user knows they need to login)
|
||||
const silentRoutes = ['/']
|
||||
|
||||
// Check token cookie (works on both server and client)
|
||||
if (!token.value) {
|
||||
if (!silentRoutes.includes(to.path)) {
|
||||
authMessage.value = 'Please login to access this page'
|
||||
}
|
||||
return navigateTo('/login')
|
||||
}
|
||||
|
||||
// On client side, also verify localStorage is in sync
|
||||
if (import.meta.client) {
|
||||
const { isLoggedIn } = useAuth()
|
||||
if (!isLoggedIn()) {
|
||||
if (!silentRoutes.includes(to.path)) {
|
||||
authMessage.value = 'Please login to access this page'
|
||||
}
|
||||
return navigateTo('/login')
|
||||
}
|
||||
}
|
||||
})
|
||||
9
frontend/package-lock.json
generated
9
frontend/package-lock.json
generated
@@ -21,8 +21,7 @@
|
||||
"shadcn-nuxt": "^2.3.3",
|
||||
"tailwind-merge": "^2.2.1",
|
||||
"vue": "^3.4.15",
|
||||
"vue-router": "^4.2.5",
|
||||
"vue-sonner": "^1.3.2"
|
||||
"vue-router": "^4.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxtjs/color-mode": "^3.3.2",
|
||||
@@ -16037,12 +16036,6 @@
|
||||
"vue": "^3.5.0"
|
||||
}
|
||||
},
|
||||
"node_modules/vue-sonner": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/vue-sonner/-/vue-sonner-1.3.2.tgz",
|
||||
"integrity": "sha512-UbZ48E9VIya3ToiRHAZUbodKute/z/M1iT8/3fU8zEbwBRE11AKuHikssv18LMk2gTTr6eMQT4qf6JoLHWuj/A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/webidl-conversions": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||
|
||||
@@ -27,8 +27,7 @@
|
||||
"shadcn-nuxt": "^2.3.3",
|
||||
"tailwind-merge": "^2.2.1",
|
||||
"vue": "^3.4.15",
|
||||
"vue-router": "^4.2.5",
|
||||
"vue-sonner": "^1.3.2"
|
||||
"vue-router": "^4.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxtjs/color-mode": "^3.3.2",
|
||||
|
||||
@@ -1,301 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, watch, nextTick } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useApi } from '@/composables/useApi'
|
||||
import { useFields, useViewState } from '@/composables/useFieldViews'
|
||||
import ListView from '@/components/views/ListView.vue'
|
||||
import DetailView from '@/components/views/DetailView.vue'
|
||||
import EditView from '@/components/views/EditView.vue'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const { api } = useApi()
|
||||
const { buildListViewConfig, buildDetailViewConfig, buildEditViewConfig } = useFields()
|
||||
|
||||
// Use breadcrumbs composable
|
||||
const { setBreadcrumbs } = useBreadcrumbs()
|
||||
|
||||
// Get object API name from route (case-insensitive)
|
||||
const objectApiName = computed(() => {
|
||||
const name = route.params.objectName as string
|
||||
// We'll look up the actual case-sensitive name from the backend
|
||||
return name
|
||||
})
|
||||
const recordId = computed(() => route.params.recordId as string)
|
||||
const view = computed(() => {
|
||||
// If recordId is 'new', default to 'edit' view
|
||||
if (route.params.recordId === 'new' && !route.params.view) {
|
||||
return 'edit'
|
||||
}
|
||||
return (route.params.view as 'list' | 'detail' | 'edit') || 'list'
|
||||
})
|
||||
|
||||
// State
|
||||
const objectDefinition = ref<any>(null)
|
||||
const loading = ref(true)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
// Use view state composable
|
||||
const {
|
||||
records,
|
||||
currentRecord,
|
||||
loading: dataLoading,
|
||||
saving,
|
||||
fetchRecords,
|
||||
fetchRecord,
|
||||
deleteRecord,
|
||||
deleteRecords,
|
||||
handleSave,
|
||||
} = useViewState(`/runtime/objects/${objectApiName.value}/records`)
|
||||
|
||||
// Compute breadcrumbs based on the current route and object data
|
||||
const updateBreadcrumbs = () => {
|
||||
if (!objectDefinition.value) {
|
||||
return
|
||||
}
|
||||
|
||||
const crumbs: Array<{ name: string; path?: string; isLast?: boolean }> = []
|
||||
|
||||
// Add app breadcrumb if object belongs to an app
|
||||
if (objectDefinition.value?.app) {
|
||||
crumbs.push({
|
||||
name: objectDefinition.value.app.label || objectDefinition.value.app.name,
|
||||
path: undefined, // No path for app grouping
|
||||
})
|
||||
}
|
||||
|
||||
// Add object breadcrumb - always use plural
|
||||
const objectLabel = objectDefinition.value?.pluralLabel || objectDefinition.value?.label || objectApiName.value
|
||||
|
||||
crumbs.push({
|
||||
name: objectLabel,
|
||||
path: `/${objectApiName.value.toLowerCase()}`,
|
||||
})
|
||||
|
||||
// Add record name if viewing/editing a specific record
|
||||
if (recordId.value && recordId.value !== 'new' && currentRecord.value) {
|
||||
const nameField = objectDefinition.value?.nameField
|
||||
let recordName = recordId.value // fallback to ID
|
||||
|
||||
// Try to get the display name from the nameField
|
||||
if (nameField && currentRecord.value[nameField]) {
|
||||
recordName = currentRecord.value[nameField]
|
||||
}
|
||||
|
||||
crumbs.push({
|
||||
name: recordName,
|
||||
isLast: true,
|
||||
})
|
||||
} else if (recordId.value === 'new') {
|
||||
crumbs.push({
|
||||
name: 'New',
|
||||
isLast: true,
|
||||
})
|
||||
}
|
||||
|
||||
setBreadcrumbs(crumbs)
|
||||
}
|
||||
|
||||
// Watch for changes that affect breadcrumbs
|
||||
watch([objectDefinition, currentRecord, recordId], () => {
|
||||
updateBreadcrumbs()
|
||||
}, { deep: true })
|
||||
|
||||
// View configs
|
||||
const listConfig = computed(() => {
|
||||
if (!objectDefinition.value) return null
|
||||
return buildListViewConfig(objectDefinition.value, {
|
||||
searchable: true,
|
||||
exportable: true,
|
||||
filterable: true,
|
||||
})
|
||||
})
|
||||
|
||||
const detailConfig = computed(() => {
|
||||
if (!objectDefinition.value) return null
|
||||
return buildDetailViewConfig(objectDefinition.value)
|
||||
})
|
||||
|
||||
const editConfig = computed(() => {
|
||||
if (!objectDefinition.value) return null
|
||||
return buildEditViewConfig(objectDefinition.value)
|
||||
})
|
||||
|
||||
// Fetch object definition
|
||||
const fetchObjectDefinition = async () => {
|
||||
try {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
const response = await api.get(`/setup/objects/${objectApiName.value}`)
|
||||
objectDefinition.value = response
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to load object definition'
|
||||
console.error('Error fetching object definition:', e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Navigation handlers - use lowercase URLs
|
||||
const handleRowClick = (row: any) => {
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/${row.id}/detail`)
|
||||
}
|
||||
|
||||
const handleCreate = () => {
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/new`)
|
||||
}
|
||||
|
||||
const handleEdit = (row?: any) => {
|
||||
const id = row?.id || recordId.value
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/${id}/edit`)
|
||||
}
|
||||
|
||||
const handleBack = () => {
|
||||
// Navigate to list view explicitly
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/`)
|
||||
}
|
||||
|
||||
const handleDelete = async (rows: any[]) => {
|
||||
if (confirm(`Delete ${rows.length} record(s)? This action cannot be undone.`)) {
|
||||
try {
|
||||
const ids = rows.map(r => r.id)
|
||||
await deleteRecords(ids)
|
||||
if (view.value !== 'list') {
|
||||
await router.push(`/${objectApiName.value.toLowerCase()}/`)
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to delete records'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleSaveRecord = async (data: any) => {
|
||||
try {
|
||||
const savedRecord = await handleSave(data)
|
||||
if (savedRecord?.id) {
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/${savedRecord.id}/detail`)
|
||||
} else {
|
||||
// Fallback to list if no ID available
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/`)
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to save record'
|
||||
}
|
||||
}
|
||||
|
||||
const handleCancel = () => {
|
||||
if (recordId.value && recordId.value !== 'new') {
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/${recordId.value}/detail`)
|
||||
} else {
|
||||
router.push(`/${objectApiName.value.toLowerCase()}/`)
|
||||
}
|
||||
}
|
||||
|
||||
// Watch for route changes
|
||||
watch(() => route.params, async (newParams, oldParams) => {
|
||||
// Reset current record when navigating to 'new'
|
||||
if (newParams.recordId === 'new') {
|
||||
currentRecord.value = null
|
||||
}
|
||||
|
||||
// Fetch record if navigating to existing record
|
||||
if (newParams.recordId && newParams.recordId !== 'new' && newParams.recordId !== oldParams.recordId) {
|
||||
await fetchRecord(newParams.recordId as string)
|
||||
}
|
||||
|
||||
// Fetch records if navigating back to list
|
||||
if (!newParams.recordId && !newParams.view) {
|
||||
await fetchRecords()
|
||||
}
|
||||
}, { deep: true })
|
||||
|
||||
// Initialize
|
||||
onMounted(async () => {
|
||||
await fetchObjectDefinition()
|
||||
|
||||
if (view.value === 'list') {
|
||||
await fetchRecords()
|
||||
} else if (recordId.value && recordId.value !== 'new') {
|
||||
await fetchRecord(recordId.value)
|
||||
}
|
||||
|
||||
// Update breadcrumbs after data is loaded
|
||||
updateBreadcrumbs()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="object-view-container">
|
||||
|
||||
<!-- Page Header -->
|
||||
<div v-if="!loading && !error && view === 'list'" class="mb-6">
|
||||
<h1 class="text-3xl font-bold">{{ objectDefinition?.label || objectApiName }}</h1>
|
||||
<p v-if="objectDefinition?.description" class="text-muted-foreground mt-2">
|
||||
{{ objectDefinition.description }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- Loading State -->
|
||||
<div v-if="loading" class="flex items-center justify-center min-h-screen">
|
||||
<div class="text-center space-y-4">
|
||||
<div class="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto"></div>
|
||||
<p class="text-muted-foreground">Loading {{ objectApiName }}...</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Error State -->
|
||||
<div v-else-if="error" class="flex items-center justify-center min-h-screen">
|
||||
<div class="text-center space-y-4 max-w-md">
|
||||
<div class="text-destructive text-5xl">⚠️</div>
|
||||
<h2 class="text-2xl font-bold">Error</h2>
|
||||
<p class="text-muted-foreground">{{ error }}</p>
|
||||
<Button @click="router.back()">Go Back</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- List View -->
|
||||
<ListView
|
||||
v-else-if="view === 'list' && listConfig"
|
||||
:config="listConfig"
|
||||
:data="records"
|
||||
:loading="dataLoading"
|
||||
selectable
|
||||
@row-click="handleRowClick"
|
||||
@create="handleCreate"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
/>
|
||||
|
||||
<!-- Detail View -->
|
||||
<DetailView
|
||||
v-else-if="view === 'detail' && detailConfig && currentRecord"
|
||||
:config="detailConfig"
|
||||
:data="currentRecord"
|
||||
:loading="dataLoading"
|
||||
@edit="handleEdit"
|
||||
@delete="() => handleDelete([currentRecord])"
|
||||
@back="handleBack"
|
||||
/>
|
||||
|
||||
<!-- Edit View -->
|
||||
<EditView
|
||||
v-else-if="(view === 'edit' || recordId === 'new') && editConfig"
|
||||
:config="editConfig"
|
||||
:data="currentRecord || {}"
|
||||
:loading="dataLoading"
|
||||
:saving="saving"
|
||||
@save="handleSaveRecord"
|
||||
@cancel="handleCancel"
|
||||
@back="handleBack"
|
||||
/>
|
||||
</div>
|
||||
</NuxtLayout>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.object-view-container {
|
||||
min-height: 100vh;
|
||||
padding: 2rem;
|
||||
}
|
||||
</style>
|
||||
@@ -1,16 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
// Redirect to a default page or show dashboard
|
||||
const router = useRouter()
|
||||
|
||||
// You can redirect to a dashboard or objects list
|
||||
// For now, just show a simple message
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="container mx-auto p-8">
|
||||
<h1 class="text-3xl font-bold mb-4">Welcome to Neo Platform</h1>
|
||||
<p class="text-muted-foreground">Select an object from the sidebar to get started.</p>
|
||||
</div>
|
||||
</NuxtLayout>
|
||||
</template>
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, watch, nextTick } from 'vue'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useApi } from '@/composables/useApi'
|
||||
import { useFields, useViewState } from '@/composables/useFieldViews'
|
||||
@@ -9,19 +9,13 @@ import EditView from '@/components/views/EditView.vue'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const { api } = useApi()
|
||||
const api = useApi()
|
||||
const { buildListViewConfig, buildDetailViewConfig, buildEditViewConfig } = useFields()
|
||||
|
||||
// Get object API name from route
|
||||
const objectApiName = computed(() => route.params.objectName as string)
|
||||
const recordId = computed(() => route.params.recordId as string)
|
||||
const view = computed(() => {
|
||||
// If recordId is 'new', default to 'edit' view
|
||||
if (route.params.recordId === 'new' && !route.params.view) {
|
||||
return 'edit'
|
||||
}
|
||||
return (route.params.view as 'list' | 'detail' | 'edit') || 'list'
|
||||
})
|
||||
const view = computed(() => route.params.view as 'list' | 'detail' | 'edit' || 'list')
|
||||
|
||||
// State
|
||||
const objectDefinition = ref<any>(null)
|
||||
@@ -39,7 +33,7 @@ const {
|
||||
deleteRecord,
|
||||
deleteRecords,
|
||||
handleSave,
|
||||
} = useViewState(`/runtime/objects/${objectApiName.value}/records`)
|
||||
} = useViewState(`/api/runtime/objects/${objectApiName.value}`)
|
||||
|
||||
// View configs
|
||||
const listConfig = computed(() => {
|
||||
@@ -66,8 +60,8 @@ const fetchObjectDefinition = async () => {
|
||||
try {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
const response = await api.get(`/setup/objects/${objectApiName.value}`)
|
||||
objectDefinition.value = response
|
||||
const response = await api.get(`/api/runtime/objects/${objectApiName.value}/definition`)
|
||||
objectDefinition.value = response.data
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to load object definition'
|
||||
console.error('Error fetching object definition:', e)
|
||||
@@ -78,7 +72,7 @@ const fetchObjectDefinition = async () => {
|
||||
|
||||
// Navigation handlers
|
||||
const handleRowClick = (row: any) => {
|
||||
router.push(`/app/objects/${objectApiName.value}/${row.id}/detail`)
|
||||
router.push(`/app/objects/${objectApiName.value}/${row.id}`)
|
||||
}
|
||||
|
||||
const handleCreate = () => {
|
||||
@@ -91,8 +85,7 @@ const handleEdit = (row?: any) => {
|
||||
}
|
||||
|
||||
const handleBack = () => {
|
||||
// Navigate to list view explicitly
|
||||
router.push(`/app/objects/${objectApiName.value}/`)
|
||||
router.push(`/app/objects/${objectApiName.value}`)
|
||||
}
|
||||
|
||||
const handleDelete = async (rows: any[]) => {
|
||||
@@ -101,7 +94,7 @@ const handleDelete = async (rows: any[]) => {
|
||||
const ids = rows.map(r => r.id)
|
||||
await deleteRecords(ids)
|
||||
if (view.value !== 'list') {
|
||||
await router.push(`/app/objects/${objectApiName.value}/`)
|
||||
await router.push(`/app/objects/${objectApiName.value}`)
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to delete records'
|
||||
@@ -111,44 +104,21 @@ const handleDelete = async (rows: any[]) => {
|
||||
|
||||
const handleSaveRecord = async (data: any) => {
|
||||
try {
|
||||
const savedRecord = await handleSave(data)
|
||||
if (savedRecord?.id) {
|
||||
router.push(`/app/objects/${objectApiName.value}/${savedRecord.id}/detail`)
|
||||
} else {
|
||||
// Fallback to list if no ID available
|
||||
router.push(`/app/objects/${objectApiName.value}/`)
|
||||
}
|
||||
await handleSave(data)
|
||||
router.push(`/app/objects/${objectApiName.value}/${currentRecord.value?.id || data.id}`)
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Failed to save record'
|
||||
}
|
||||
}
|
||||
|
||||
const handleCancel = () => {
|
||||
if (recordId.value && recordId.value !== 'new') {
|
||||
router.push(`/app/objects/${objectApiName.value}/${recordId.value}/detail`)
|
||||
if (recordId.value) {
|
||||
router.push(`/app/objects/${objectApiName.value}/${recordId.value}`)
|
||||
} else {
|
||||
router.push(`/app/objects/${objectApiName.value}/`)
|
||||
router.push(`/app/objects/${objectApiName.value}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Watch for route changes
|
||||
watch(() => route.params, async (newParams, oldParams) => {
|
||||
// Reset current record when navigating to 'new'
|
||||
if (newParams.recordId === 'new') {
|
||||
currentRecord.value = null
|
||||
}
|
||||
|
||||
// Fetch record if navigating to existing record
|
||||
if (newParams.recordId && newParams.recordId !== 'new' && newParams.recordId !== oldParams.recordId) {
|
||||
await fetchRecord(newParams.recordId as string)
|
||||
}
|
||||
|
||||
// Fetch records if navigating back to list
|
||||
if (!newParams.recordId && !newParams.view) {
|
||||
await fetchRecords()
|
||||
}
|
||||
}, { deep: true })
|
||||
|
||||
// Initialize
|
||||
onMounted(async () => {
|
||||
await fetchObjectDefinition()
|
||||
@@ -162,16 +132,7 @@ onMounted(async () => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="object-view-container">
|
||||
<!-- Page Header -->
|
||||
<div v-if="!loading && !error" class="mb-6">
|
||||
<h1 class="text-3xl font-bold">{{ objectDefinition?.label || objectApiName }}</h1>
|
||||
<p v-if="objectDefinition?.description" class="text-muted-foreground mt-2">
|
||||
{{ objectDefinition.description }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- Loading State -->
|
||||
<div v-if="loading" class="flex items-center justify-center min-h-screen">
|
||||
<div class="text-center space-y-4">
|
||||
@@ -226,7 +187,6 @@ onMounted(async () => {
|
||||
@back="handleBack"
|
||||
/>
|
||||
</div>
|
||||
</NuxtLayout>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
// List all available objects
|
||||
const { api } = useApi()
|
||||
const router = useRouter()
|
||||
|
||||
const objects = ref<any[]>([])
|
||||
const loading = ref(true)
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const response = await api.get('/setup/objects')
|
||||
objects.value = response.data || response || []
|
||||
} catch (e) {
|
||||
console.error('Failed to load objects:', e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="container mx-auto p-8">
|
||||
<h1 class="text-3xl font-bold mb-6">Objects</h1>
|
||||
|
||||
<div v-if="loading" class="text-center py-12">
|
||||
<div class="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto"></div>
|
||||
</div>
|
||||
|
||||
<div v-else class="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
<NuxtLink
|
||||
v-for="obj in objects"
|
||||
:key="obj.id"
|
||||
:to="`/app/objects/${obj.apiName}/`"
|
||||
class="block p-6 border rounded-lg hover:bg-accent transition-colors"
|
||||
>
|
||||
<h3 class="text-xl font-semibold mb-2">{{ obj.label }}</h3>
|
||||
<p v-if="obj.description" class="text-sm text-muted-foreground">{{ obj.description }}</p>
|
||||
</NuxtLink>
|
||||
</div>
|
||||
</div>
|
||||
</NuxtLayout>
|
||||
</template>
|
||||
@@ -1,6 +1,3 @@
|
||||
<script setup lang="ts">
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="text-center space-y-6">
|
||||
|
||||
@@ -1,33 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { LayoutGrid } from 'lucide-vue-next'
|
||||
import LoginForm from '@/components/LoginForm.vue'
|
||||
|
||||
// Skip auth middleware for login page
|
||||
definePageMeta({
|
||||
auth: false
|
||||
})
|
||||
|
||||
const { toast } = useToast()
|
||||
|
||||
// Check for auth message from cookie
|
||||
const authMessage = useCookie('authMessage')
|
||||
|
||||
onMounted(() => {
|
||||
if (authMessage.value) {
|
||||
console.log('Displaying auth message: ' + authMessage.value)
|
||||
const message = authMessage.value
|
||||
|
||||
// Show success toast for logout, error for auth failures
|
||||
if (message.toLowerCase().includes('logged out')) {
|
||||
toast.success(message)
|
||||
} else {
|
||||
toast.error(message)
|
||||
}
|
||||
|
||||
// Clear the message after displaying
|
||||
authMessage.value = null
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
</div>
|
||||
|
||||
<form @submit.prevent="handleRegister" class="space-y-4">
|
||||
<div class="space-y-2">
|
||||
<Label for="tenantId">Tenant ID</Label>
|
||||
<Input id="tenantId" v-model="tenantId" type="text" required placeholder="123" />
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<Label for="email">Email</Label>
|
||||
<Input
|
||||
@@ -69,29 +74,10 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
// Skip auth middleware for register page
|
||||
definePageMeta({
|
||||
auth: false
|
||||
})
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
|
||||
// Extract subdomain from hostname
|
||||
const getSubdomain = () => {
|
||||
if (!import.meta.client) return null
|
||||
const hostname = window.location.hostname
|
||||
const parts = hostname.split('.')
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return null
|
||||
}
|
||||
if (parts.length > 1 && parts[0] !== 'www') {
|
||||
return parts[0]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const subdomain = ref(getSubdomain())
|
||||
const tenantId = ref('123')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const firstName = ref('')
|
||||
@@ -106,17 +92,12 @@ const handleRegister = async () => {
|
||||
error.value = ''
|
||||
success.value = false
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
if (subdomain.value) {
|
||||
headers['x-tenant-id'] = subdomain.value
|
||||
}
|
||||
|
||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/register`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-tenant-id': tenantId.value,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email.value,
|
||||
password: password.value,
|
||||
|
||||
6
package-lock.json
generated
6
package-lock.json
generated
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "neo",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {}
|
||||
}
|
||||
Reference in New Issue
Block a user