feat: Introduce default profile sections and fields, updating schema with isSystem and isSearchableOnly properties.

This commit is contained in:
pradeepkumar
2026-01-24 12:49:25 +05:30
parent 710803a3ec
commit 43b422a81e
4 changed files with 145 additions and 2 deletions

View File

@@ -224,6 +224,7 @@ model ProfileSection {
sortOrder Int @default(0) sortOrder Int @default(0)
isActive Boolean @default(true) isActive Boolean @default(true)
isGlobal Boolean @default(false) // If true, applies to ALL agent types isGlobal Boolean @default(false) // If true, applies to ALL agent types
isSystem Boolean @default(false) // If true, section cannot be deleted (system default)
// Timestamps // Timestamps
createdAt DateTime @default(now()) createdAt DateTime @default(now())
@@ -234,6 +235,7 @@ model ProfileSection {
agentTypeSections AgentTypeSection[] agentTypeSections AgentTypeSection[]
@@index([isActive]) @@index([isActive])
@@index([isSystem])
@@index([sortOrder]) @@index([sortOrder])
@@map("profile_sections") @@map("profile_sections")
} }
@@ -273,6 +275,7 @@ model ProfileField {
sortOrder Int @default(0) sortOrder Int @default(0)
isActive Boolean @default(true) isActive Boolean @default(true)
isRequired Boolean @default(false) isRequired Boolean @default(false)
isSearchableOnly Boolean @default(false) // If true, shown in edit form & search, but hidden on public profile
// Validation rules (JSON) // Validation rules (JSON)
validation Json? // { min, max, minLength, maxLength, pattern, etc. } validation Json? // { min, max, minLength, maxLength, pattern, etc. }

View File

@@ -1,4 +1,4 @@
import { PrismaClient, UserRole, UserStatus, AuthProvider } from '@prisma/client'; import { PrismaClient, UserRole, UserStatus, AuthProvider, FieldType } from '@prisma/client';
import { PrismaPg } from '@prisma/adapter-pg'; import { PrismaPg } from '@prisma/adapter-pg';
import { Pool } from 'pg'; import { Pool } from 'pg';
import * as argon2 from 'argon2'; import * as argon2 from 'argon2';
@@ -49,6 +49,129 @@ async function main() {
console.log(''); console.log('');
// =============================================
// Seed Default Profile Sections & Fields
// =============================================
console.log('📋 Seeding Default Profile Sections...');
// Define default system sections with their fields
const defaultSections = [
{
name: 'Basic Information',
slug: 'basic-information',
description: 'Basic profile information',
icon: 'user',
sortOrder: 1,
isActive: true,
isGlobal: true,
isSystem: true,
fields: [
{
name: 'First Name',
slug: 'first_name',
fieldType: FieldType.TEXT,
description: 'Your first name',
placeholder: 'Enter your first name',
sortOrder: 1,
isActive: true,
isRequired: true,
isSearchableOnly: false,
},
{
name: 'Last Name',
slug: 'last_name',
fieldType: FieldType.TEXT,
description: 'Your last name',
placeholder: 'Enter your last name',
sortOrder: 2,
isActive: true,
isRequired: true,
isSearchableOnly: false,
},
],
},
{
name: 'Contact Information',
slug: 'contact-information',
description: 'Contact details',
icon: 'phone',
sortOrder: 2,
isActive: true,
isGlobal: true,
isSystem: true,
fields: [
{
name: 'Email Address',
slug: 'email_address',
fieldType: FieldType.TEXT,
description: 'Your contact email address',
placeholder: 'Enter your email address',
sortOrder: 1,
isActive: true,
isRequired: true,
isSearchableOnly: false,
},
{
name: 'Phone Number',
slug: 'phone_number',
fieldType: FieldType.TEXT,
description: 'Your contact phone number',
placeholder: 'Enter your phone number',
sortOrder: 2,
isActive: true,
isRequired: false,
isSearchableOnly: false,
},
],
},
];
for (const sectionData of defaultSections) {
const { fields, ...sectionInfo } = sectionData;
// Check if section already exists
const existingSection = await prisma.profileSection.findUnique({
where: { slug: sectionInfo.slug },
});
if (existingSection) {
console.log(` ⚠️ Section "${sectionInfo.name}" already exists`);
// Still check and create missing fields
for (const fieldData of fields) {
const existingField = await prisma.profileField.findFirst({
where: {
sectionId: existingSection.id,
slug: fieldData.slug,
},
});
if (!existingField) {
await prisma.profileField.create({
data: {
...fieldData,
sectionId: existingSection.id,
},
});
console.log(` ✅ Created field: ${fieldData.name}`);
}
}
} else {
// Create section with fields
const section = await prisma.profileSection.create({
data: {
...sectionInfo,
fields: {
create: fields,
},
},
});
console.log(` ✅ Created section: ${sectionInfo.name} with ${fields.length} fields`);
}
}
console.log('');
// ============================================= // =============================================
// Seed Admin User // Seed Admin User
// ============================================= // =============================================
@@ -96,11 +219,16 @@ async function main() {
const userCount = await prisma.user.count(); const userCount = await prisma.user.count();
const adminCount = await prisma.user.count({ where: { role: UserRole.ADMIN } }); const adminCount = await prisma.user.count({ where: { role: UserRole.ADMIN } });
const agentTypeCount = await prisma.agentType.count(); const agentTypeCount = await prisma.agentType.count();
const sectionCount = await prisma.profileSection.count();
const fieldCount = await prisma.profileField.count();
const systemSectionCount = await prisma.profileSection.count({ where: { isSystem: true } });
console.log('📊 Database Summary:'); console.log('📊 Database Summary:');
console.log(` Total Users: ${userCount}`); console.log(` Total Users: ${userCount}`);
console.log(` Admins: ${adminCount}`); console.log(` Admins: ${adminCount}`);
console.log(` Agent Types: ${agentTypeCount}`); console.log(` Agent Types: ${agentTypeCount}`);
console.log(` Profile Sections: ${sectionCount} (${systemSectionCount} system)`);
console.log(` Profile Fields: ${fieldCount}`);
console.log('\n🌱 Seeding completed!\n'); console.log('\n🌱 Seeding completed!\n');
await prisma.$disconnect(); await prisma.$disconnect();

View File

@@ -89,6 +89,11 @@ export class CreateFieldDto {
@IsBoolean() @IsBoolean()
isRequired?: boolean; isRequired?: boolean;
@ApiPropertyOptional({ description: 'Whether this field is for search only (hidden from public profile)', default: false })
@IsOptional()
@IsBoolean()
isSearchableOnly?: boolean;
@ApiPropertyOptional({ description: 'Validation rules (JSON object)' }) @ApiPropertyOptional({ description: 'Validation rules (JSON object)' })
@IsOptional() @IsOptional()
@IsObject() @IsObject()

View File

@@ -1,4 +1,4 @@
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common'; import { Injectable, NotFoundException, ConflictException, ForbiddenException } from '@nestjs/common';
import { PrismaService } from '../prisma'; import { PrismaService } from '../prisma';
import { CreateSectionDto, UpdateSectionDto, AssignSectionDto, UpdateAssignmentDto } from './dto'; import { CreateSectionDto, UpdateSectionDto, AssignSectionDto, UpdateAssignmentDto } from './dto';
@@ -143,6 +143,13 @@ export class ProfileSectionsService {
async remove(id: string) { async remove(id: string) {
const section = await this.findOne(id); const section = await this.findOne(id);
// Check if this is a system section
if (section.isSystem) {
throw new ForbiddenException(
'Cannot delete system sections. You can only deactivate them.',
);
}
// Check if section has any fields // Check if section has any fields
if (section._count.fields > 0) { if (section._count.fields > 0) {
throw new ConflictException( throw new ConflictException(