refactor: remove static routes and stop multiple token api call in sse

This commit is contained in:
2026-06-23 22:48:34 +05:30
parent b282fdf56d
commit 5e4cf61d6e
25 changed files with 129 additions and 2086 deletions

View File

@@ -1,62 +0,0 @@
import axiosClient from '../axios/axios';
import { API_ROUTES } from '@/constants/apiRoutes';
import { Detection } from '@/types';
import { projectService } from './project.service';
/**
* Detection Service
*/
export const detectionService = {
/**
* Fetch all detections from completed videos
* Uses the summary endpoint to get detections for each project
*/
getAllDetections: async (): Promise<Detection[]> => {
try {
// First get all projects
const projectsResponse = await projectService.getProjects();
const projects = projectsResponse.items;
// Then fetch detections for each project
const allDetections: Detection[] = [];
for (const project of projects) {
try {
const response = await axiosClient.get<{
packages: {
[key: string]: {
chainages: {
[key: string]: {
detections: Detection[];
};
};
};
};
}>(API_ROUTES.DASHBOARD.OVERVIEW, {
params: { project_id: project.id },
});
const summary = response.data;
// Extract detections from the nested structure
for (const pkg of Object.values(summary.packages || {})) {
for (const loc of Object.values(pkg.chainages || {})) {
allDetections.push(...(loc.detections || []));
}
}
} catch (e) {
// Skip projects that fail to load
console.warn(
`Failed to load detections for project ${project.id}:`,
e,
);
}
}
return allDetections;
} catch (e) {
console.error('Failed to fetch all detections:', e);
return [];
}
},
};

View File

@@ -1,10 +1,8 @@
export * from './project.service';
export * from './project-summary.service';
export * from './package.service';
export * from './auth.service';
export { chainageService } from './chainage.service';
export * from './video.service';
export * from './detection.service';
export * from './permission.service';
export * from './role.service';
export * from './user.service';

View File

@@ -1,52 +0,0 @@
import { API_ROUTES } from '@/constants/apiRoutes';
import axiosClient from '../axios/axios';
export const projectSummaryService = {
getProjectSummary: async <T = unknown>(projectId: string): Promise<T> => {
const response = await axiosClient.get<T>(API_ROUTES.DASHBOARD.OVERVIEW, {
params: { project_id: projectId },
});
return response.data;
},
getProjectSummaryByVideo: async <T = unknown>(
projectId: string,
videoId: string,
): Promise<T> => {
const response = await axiosClient.get<T>(API_ROUTES.DASHBOARD.OVERVIEW, {
params: { project_id: projectId, video_id: videoId },
});
return response.data;
},
};
export const projectDataService = {
extractDetections(
projectSummary: any,
selectedPackageId?: string | null,
selectedChainageId?: string | null,
): any[] {
if (!projectSummary) return [];
const detections: any[] = [];
const packagesToProcess =
selectedPackageId && selectedPackageId !== 'all'
? { [selectedPackageId]: projectSummary.packages[selectedPackageId] }
: projectSummary.packages || {};
for (const pkg of Object.values(packagesToProcess)) {
const chainagesToProcess =
selectedChainageId && selectedChainageId !== 'all'
? { [selectedChainageId]: (pkg as any).chainages[selectedChainageId] }
: (pkg as any).chainages || {};
for (const chainage of Object.values(chainagesToProcess)) {
if (!chainage) continue;
detections.push(...((chainage as any).detections || []));
}
}
return detections;
},
};

View File

@@ -1,16 +1,34 @@
import { getFirstAccessiblePathForPermissions } from '@/config/menu.config';
import { authService } from '@/services/api/auth.service';
import { useAppStore } from '@/store/app.store';
import type { UserProfile } from '@/types';
let authenticatedAppInitialization: Promise<void> | null = null;
export async function initializeAuthenticatedApp() {
if (authenticatedAppInitialization) {
return authenticatedAppInitialization;
}
authenticatedAppInitialization = loadAuthenticatedApp().finally(() => {
authenticatedAppInitialization = null;
});
return authenticatedAppInitialization;
}
async function loadAuthenticatedApp() {
const [{ user, tenant }, permissionsResponse] = await Promise.all([
authService.me(),
authService.permissions(),
]);
const permissions = permissionsResponse.granted_permissions ?? [];
useAppStore.getState().setUserContext({
user: user as UserProfile,
tenant,
permissions: permissionsResponse.granted_permissions ?? [],
permissions,
defaultRoute: getFirstAccessiblePathForPermissions(permissions),
});
}