Added page layouts
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('page_layouts', (table) => {
|
||||
table.uuid('id').primary().defaultTo(knex.raw('(UUID())'));
|
||||
table.string('name').notNullable();
|
||||
table.uuid('object_id').notNullable();
|
||||
table.boolean('is_default').defaultTo(false);
|
||||
table.json('layout_config').notNullable();
|
||||
table.text('description');
|
||||
table.timestamps(true, true);
|
||||
|
||||
// Foreign key to object_definitions
|
||||
table.foreign('object_id').references('id').inTable('object_definitions').onDelete('CASCADE');
|
||||
|
||||
// Index for faster lookups
|
||||
table.index(['object_id', 'is_default']);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTable('page_layouts');
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
output = "../node_modules/.prisma/central"
|
||||
provider = "prisma-client-js"
|
||||
output = "../node_modules/.prisma/central"
|
||||
binaryTargets = ["native", "debian-openssl-3.0.x"]
|
||||
}
|
||||
|
||||
datasource db {
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
// NOTE: Each tenant has its own database, so there is NO tenantId column in these tables
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
output = "../node_modules/.prisma/tenant"
|
||||
provider = "prisma-client-js"
|
||||
output = "../node_modules/.prisma/tenant"
|
||||
binaryTargets = ["native", "debian-openssl-3.0.x"]
|
||||
}
|
||||
|
||||
datasource db {
|
||||
|
||||
@@ -43,10 +43,13 @@ function decryptPassword(encryptedPassword: string): string {
|
||||
function createTenantKnexConnection(tenant: any): Knex {
|
||||
const decryptedPassword = decryptPassword(tenant.dbPassword);
|
||||
|
||||
// Replace 'db' hostname with 'localhost' when running outside Docker
|
||||
const dbHost = tenant.dbHost === 'db' ? 'localhost' : tenant.dbHost;
|
||||
|
||||
return knex({
|
||||
client: 'mysql2',
|
||||
connection: {
|
||||
host: tenant.dbHost,
|
||||
host: dbHost,
|
||||
port: tenant.dbPort,
|
||||
user: tenant.dbUsername,
|
||||
password: decryptedPassword,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { AuthModule } from './auth/auth.module';
|
||||
import { RbacModule } from './rbac/rbac.module';
|
||||
import { ObjectModule } from './object/object.module';
|
||||
import { AppBuilderModule } from './app-builder/app-builder.module';
|
||||
import { PageLayoutModule } from './page-layout/page-layout.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -18,6 +19,7 @@ import { AppBuilderModule } from './app-builder/app-builder.module';
|
||||
RbacModule,
|
||||
ObjectModule,
|
||||
AppBuilderModule,
|
||||
PageLayoutModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
54
backend/src/page-layout/dto/page-layout.dto.ts
Normal file
54
backend/src/page-layout/dto/page-layout.dto.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { IsString, IsUUID, IsBoolean, IsOptional, IsObject } from 'class-validator';
|
||||
|
||||
export class CreatePageLayoutDto {
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
@IsUUID()
|
||||
objectId: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isDefault?: boolean;
|
||||
|
||||
@IsObject()
|
||||
layoutConfig: {
|
||||
fields: Array<{
|
||||
fieldId: string;
|
||||
x: number;
|
||||
y: number;
|
||||
w: number;
|
||||
h: number;
|
||||
}>;
|
||||
};
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export class UpdatePageLayoutDto {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
name?: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isDefault?: boolean;
|
||||
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
layoutConfig?: {
|
||||
fields: Array<{
|
||||
fieldId: string;
|
||||
x: number;
|
||||
y: number;
|
||||
w: number;
|
||||
h: number;
|
||||
}>;
|
||||
};
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
}
|
||||
55
backend/src/page-layout/page-layout.controller.ts
Normal file
55
backend/src/page-layout/page-layout.controller.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { PageLayoutService } from './page-layout.service';
|
||||
import { CreatePageLayoutDto, UpdatePageLayoutDto } from './dto/page-layout.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { TenantId } from '../tenant/tenant.decorator';
|
||||
|
||||
@Controller('page-layouts')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class PageLayoutController {
|
||||
constructor(private readonly pageLayoutService: PageLayoutService) {}
|
||||
|
||||
@Post()
|
||||
create(@TenantId() tenantId: string, @Body() createPageLayoutDto: CreatePageLayoutDto) {
|
||||
return this.pageLayoutService.create(tenantId, createPageLayoutDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@TenantId() tenantId: string, @Query('objectId') objectId?: string) {
|
||||
return this.pageLayoutService.findAll(tenantId, objectId);
|
||||
}
|
||||
|
||||
@Get('default/:objectId')
|
||||
findDefaultByObject(@TenantId() tenantId: string, @Param('objectId') objectId: string) {
|
||||
return this.pageLayoutService.findDefaultByObject(tenantId, objectId);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@TenantId() tenantId: string, @Param('id') id: string) {
|
||||
return this.pageLayoutService.findOne(tenantId, id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@TenantId() tenantId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() updatePageLayoutDto: UpdatePageLayoutDto,
|
||||
) {
|
||||
return this.pageLayoutService.update(tenantId, id, updatePageLayoutDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@TenantId() tenantId: string, @Param('id') id: string) {
|
||||
return this.pageLayoutService.remove(tenantId, id);
|
||||
}
|
||||
}
|
||||
12
backend/src/page-layout/page-layout.module.ts
Normal file
12
backend/src/page-layout/page-layout.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PageLayoutService } from './page-layout.service';
|
||||
import { PageLayoutController } from './page-layout.controller';
|
||||
import { TenantModule } from '../tenant/tenant.module';
|
||||
|
||||
@Module({
|
||||
imports: [TenantModule],
|
||||
controllers: [PageLayoutController],
|
||||
providers: [PageLayoutService],
|
||||
exports: [PageLayoutService],
|
||||
})
|
||||
export class PageLayoutModule {}
|
||||
118
backend/src/page-layout/page-layout.service.ts
Normal file
118
backend/src/page-layout/page-layout.service.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { TenantDatabaseService } from '../tenant/tenant-database.service';
|
||||
import { CreatePageLayoutDto, UpdatePageLayoutDto } from './dto/page-layout.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PageLayoutService {
|
||||
constructor(private tenantDbService: TenantDatabaseService) {}
|
||||
|
||||
async create(tenantId: string, createDto: CreatePageLayoutDto) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// If this layout is set as default, unset other defaults for the same object
|
||||
if (createDto.isDefault) {
|
||||
await knex('page_layouts')
|
||||
.where({ object_id: createDto.objectId })
|
||||
.update({ is_default: false });
|
||||
}
|
||||
|
||||
const [id] = await knex('page_layouts').insert({
|
||||
name: createDto.name,
|
||||
object_id: createDto.objectId,
|
||||
is_default: createDto.isDefault || false,
|
||||
layout_config: JSON.stringify(createDto.layoutConfig),
|
||||
description: createDto.description || null,
|
||||
});
|
||||
|
||||
// Get the inserted record
|
||||
const result = await knex('page_layouts').where({ id }).first();
|
||||
return result;
|
||||
}
|
||||
|
||||
async findAll(tenantId: string, objectId?: string) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
let query = knex('page_layouts');
|
||||
|
||||
if (objectId) {
|
||||
query = query.where({ object_id: objectId });
|
||||
}
|
||||
|
||||
const layouts = await query.orderByRaw('is_default DESC, name ASC');
|
||||
return layouts;
|
||||
}
|
||||
|
||||
async findOne(tenantId: string, id: string) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
const layout = await knex('page_layouts').where({ id }).first();
|
||||
|
||||
if (!layout) {
|
||||
throw new NotFoundException(`Page layout with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
async findDefaultByObject(tenantId: string, objectId: string) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
const layout = await knex('page_layouts')
|
||||
.where({ object_id: objectId, is_default: true })
|
||||
.first();
|
||||
|
||||
return layout || null;
|
||||
}
|
||||
|
||||
async update(tenantId: string, id: string, updateDto: UpdatePageLayoutDto) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
// Check if layout exists
|
||||
await this.findOne(tenantId, id);
|
||||
|
||||
// If setting as default, unset other defaults for the same object
|
||||
if (updateDto.isDefault) {
|
||||
const layout = await this.findOne(tenantId, id);
|
||||
await knex('page_layouts')
|
||||
.where({ object_id: layout.object_id })
|
||||
.whereNot({ id })
|
||||
.update({ is_default: false });
|
||||
}
|
||||
|
||||
const updates: any = {};
|
||||
|
||||
if (updateDto.name !== undefined) {
|
||||
updates.name = updateDto.name;
|
||||
}
|
||||
|
||||
if (updateDto.isDefault !== undefined) {
|
||||
updates.is_default = updateDto.isDefault;
|
||||
}
|
||||
|
||||
if (updateDto.layoutConfig !== undefined) {
|
||||
updates.layout_config = JSON.stringify(updateDto.layoutConfig);
|
||||
}
|
||||
|
||||
if (updateDto.description !== undefined) {
|
||||
updates.description = updateDto.description;
|
||||
}
|
||||
|
||||
updates.updated_at = knex.fn.now();
|
||||
|
||||
await knex('page_layouts').where({ id }).update(updates);
|
||||
|
||||
// Get the updated record
|
||||
const result = await knex('page_layouts').where({ id }).first();
|
||||
return result;
|
||||
}
|
||||
|
||||
async remove(tenantId: string, id: string) {
|
||||
const knex = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
await this.findOne(tenantId, id);
|
||||
|
||||
await knex('page_layouts').where({ id }).delete();
|
||||
|
||||
return { message: 'Page layout deleted successfully' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user