task-k.1
This commit is contained in:
78
src/features/crm/reports/server/service.ts
Normal file
78
src/features/crm/reports/server/service.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user