192 lines
4.2 KiB
TypeScript
192 lines
4.2 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class ObjectService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
// Setup endpoints - Object metadata management
|
|
async getObjectDefinitions(tenantId: string) {
|
|
return this.prisma.objectDefinition.findMany({
|
|
where: { tenantId },
|
|
include: {
|
|
fields: true,
|
|
},
|
|
orderBy: { label: 'asc' },
|
|
});
|
|
}
|
|
|
|
async getObjectDefinition(tenantId: string, apiName: string) {
|
|
const obj = await this.prisma.objectDefinition.findUnique({
|
|
where: {
|
|
tenantId_apiName: {
|
|
tenantId,
|
|
apiName,
|
|
},
|
|
},
|
|
include: {
|
|
fields: {
|
|
where: { isActive: true },
|
|
orderBy: { label: 'asc' },
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!obj) {
|
|
throw new NotFoundException(`Object ${apiName} not found`);
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
async createObjectDefinition(
|
|
tenantId: string,
|
|
data: {
|
|
apiName: string;
|
|
label: string;
|
|
pluralLabel?: string;
|
|
description?: string;
|
|
isSystem?: boolean;
|
|
},
|
|
) {
|
|
return this.prisma.objectDefinition.create({
|
|
data: {
|
|
tenantId,
|
|
...data,
|
|
tableName: `custom_${data.apiName.toLowerCase()}`,
|
|
},
|
|
});
|
|
}
|
|
|
|
async createFieldDefinition(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
data: {
|
|
apiName: string;
|
|
label: string;
|
|
type: string;
|
|
description?: string;
|
|
isRequired?: boolean;
|
|
isUnique?: boolean;
|
|
isLookup?: boolean;
|
|
referenceTo?: string;
|
|
defaultValue?: string;
|
|
options?: any;
|
|
},
|
|
) {
|
|
const obj = await this.getObjectDefinition(tenantId, objectApiName);
|
|
|
|
return this.prisma.fieldDefinition.create({
|
|
data: {
|
|
objectId: obj.id,
|
|
...data,
|
|
},
|
|
});
|
|
}
|
|
|
|
// Runtime endpoints - CRUD operations
|
|
async getRecords(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
userId: string,
|
|
filters?: any,
|
|
) {
|
|
// For demonstration, using Account as example static object
|
|
if (objectApiName === 'Account') {
|
|
return this.prisma.account.findMany({
|
|
where: {
|
|
tenantId,
|
|
ownerId: userId, // Basic sharing rule
|
|
...filters,
|
|
},
|
|
});
|
|
}
|
|
|
|
// For custom objects, you'd need dynamic query building
|
|
// This is a simplified version
|
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
|
}
|
|
|
|
async getRecord(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
recordId: string,
|
|
userId: string,
|
|
) {
|
|
if (objectApiName === 'Account') {
|
|
const record = await this.prisma.account.findFirst({
|
|
where: {
|
|
id: recordId,
|
|
tenantId,
|
|
ownerId: userId,
|
|
},
|
|
});
|
|
|
|
if (!record) {
|
|
throw new NotFoundException('Record not found');
|
|
}
|
|
|
|
return record;
|
|
}
|
|
|
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
|
}
|
|
|
|
async createRecord(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
data: any,
|
|
userId: string,
|
|
) {
|
|
if (objectApiName === 'Account') {
|
|
return this.prisma.account.create({
|
|
data: {
|
|
tenantId,
|
|
ownerId: userId,
|
|
...data,
|
|
},
|
|
});
|
|
}
|
|
|
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
|
}
|
|
|
|
async updateRecord(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
recordId: string,
|
|
data: any,
|
|
userId: string,
|
|
) {
|
|
if (objectApiName === 'Account') {
|
|
// Verify ownership
|
|
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
|
|
|
return this.prisma.account.update({
|
|
where: { id: recordId },
|
|
data,
|
|
});
|
|
}
|
|
|
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
|
}
|
|
|
|
async deleteRecord(
|
|
tenantId: string,
|
|
objectApiName: string,
|
|
recordId: string,
|
|
userId: string,
|
|
) {
|
|
if (objectApiName === 'Account') {
|
|
// Verify ownership
|
|
await this.getRecord(tenantId, objectApiName, recordId, userId);
|
|
|
|
return this.prisma.account.delete({
|
|
where: { id: recordId },
|
|
});
|
|
}
|
|
|
|
throw new Error(`Runtime queries for ${objectApiName} not yet implemented`);
|
|
}
|
|
}
|