79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { auditAction } from '@/features/foundation/audit-log/service';
|
|
import type {
|
|
CrmReportCatalogResponse,
|
|
CrmReportFiltersResponse,
|
|
CrmReportsResponse
|
|
} from '../api/types';
|
|
import { buildReportCatalogGroups } from './builders/catalog-builder';
|
|
import { resolveCrmAccess } from './context';
|
|
import { listReportRegistry } from './datasets/catalog';
|
|
import { buildSharedReportFilters } from './filters/shared';
|
|
import type { ResolvedReportContext } from './types';
|
|
|
|
export function buildResolvedReportContext(input: Parameters<typeof resolveCrmAccess>[0]) {
|
|
return resolveCrmAccess(input);
|
|
}
|
|
|
|
async function auditReportView(context: ResolvedReportContext, reportCode: string, filters?: Record<string, unknown>) {
|
|
await auditAction({
|
|
organizationId: context.organizationId,
|
|
userId: context.userId,
|
|
entityType: 'crm_report',
|
|
entityId: reportCode,
|
|
action: 'view',
|
|
afterData: {
|
|
reportCode,
|
|
filters: filters ?? {},
|
|
userId: context.userId
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function getCrmReportsResponse(
|
|
context: ResolvedReportContext
|
|
): Promise<CrmReportsResponse> {
|
|
const items = await listReportRegistry(context);
|
|
const groups = buildReportCatalogGroups(items);
|
|
|
|
await auditReportView(context, 'report_center');
|
|
|
|
return {
|
|
success: true,
|
|
time: new Date().toISOString(),
|
|
message: 'CRM reports loaded successfully',
|
|
items,
|
|
groups
|
|
};
|
|
}
|
|
|
|
export async function getCrmReportCatalogResponse(
|
|
context: ResolvedReportContext
|
|
): Promise<CrmReportCatalogResponse> {
|
|
const items = await listReportRegistry(context);
|
|
const groups = buildReportCatalogGroups(items);
|
|
|
|
await auditReportView(context, 'report_catalog');
|
|
|
|
return {
|
|
success: true,
|
|
time: new Date().toISOString(),
|
|
message: 'CRM report catalog loaded successfully',
|
|
groups
|
|
};
|
|
}
|
|
|
|
export async function getCrmReportFiltersResponse(
|
|
context: ResolvedReportContext
|
|
): Promise<CrmReportFiltersResponse> {
|
|
const filters = await buildSharedReportFilters(context);
|
|
|
|
await auditReportView(context, 'report_filters');
|
|
|
|
return {
|
|
success: true,
|
|
time: new Date().toISOString(),
|
|
message: 'CRM report filters loaded successfully',
|
|
filters
|
|
};
|
|
}
|