organization code
This commit is contained in:
parent
c3561b0f31
commit
5ce8910325
|
|
@ -24,7 +24,7 @@ export default function DashboardLayout({children}:{children: React.ReactNode})
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const filter = useAppSelector((state) => state.filter.filter);
|
const filter = useAppSelector((state) => state.filter.filter);
|
||||||
const {data: orgMap } = useGetOrganizationsMapQuery();
|
const {data: orgMap, isLoading: isLoadingOrgMap } = useGetOrganizationsMapQuery();
|
||||||
const {data: filterOptions } = useGetFilterOptionsQuery(filter);
|
const {data: filterOptions } = useGetFilterOptionsQuery(filter);
|
||||||
const [region, setRegion] = React.useState("");
|
const [region, setRegion] = React.useState("");
|
||||||
const { t } = useLocale();
|
const { t } = useLocale();
|
||||||
|
|
@ -62,6 +62,12 @@ export default function DashboardLayout({children}:{children: React.ReactNode})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (isLoadingOrgMap) return (
|
||||||
|
<div className="flex justify-center items-center h-screen">
|
||||||
|
<RefreshCcw className="animate-spin h-12 w-12 text-white" />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NeedLoginRoute role="Dashboard">
|
<NeedLoginRoute role="Dashboard">
|
||||||
<div className="flex min-h-screen">
|
<div className="flex min-h-screen">
|
||||||
|
|
|
||||||
|
|
@ -3,36 +3,60 @@ import { AttendanceRange, AttendanceSummary, CostData, EmployeeMovement, Employe
|
||||||
import { Response , Filter} from './types'
|
import { Response , Filter} from './types'
|
||||||
import { config } from '@/config';
|
import { config } from '@/config';
|
||||||
|
|
||||||
|
|
||||||
|
let valueToSap: Record<string, string> = {}
|
||||||
|
let sapToValue: Record<string, string> = {}
|
||||||
|
|
||||||
|
const deepClone = (obj: any) => JSON.parse(JSON.stringify(obj))
|
||||||
const rawBaseQuery = fetchBaseQuery({ baseUrl: config.api.baseApiUrl });
|
const rawBaseQuery = fetchBaseQuery({ baseUrl: config.api.baseApiUrl });
|
||||||
const baseQuery = async (args:any, api:any, extraOptions:any) => {
|
const translatedBaseQuery: typeof rawBaseQuery = async (args, api, extraOptions) => {
|
||||||
const result = await rawBaseQuery(args, api, extraOptions)
|
const request =
|
||||||
|
typeof args === 'string'
|
||||||
|
? { url: args }
|
||||||
|
: { ...(args as any), params: args.params ? { ...args.params } : undefined }
|
||||||
|
|
||||||
if (result.data && typeof args === 'object' && typeof args.url === 'string') {
|
const url = request.url
|
||||||
const url = args.url
|
const isDashboard = url?.startsWith('/dashboard') && url !== '/dashboard/filter-options'
|
||||||
|
|
||||||
// Match /dashboard/* but exclude /dashboard/filter-options
|
// Build mapping dictionaries from organizations map in state
|
||||||
const shouldTranslate =
|
const orgMapPre = (api.getState() as any).api.queries['getOrganizationsMap(undefined)']?.data ?? []
|
||||||
url.startsWith('/dashboard') && !url.startsWith('/dashboard/filter-options')
|
if (Array.isArray(orgMapPre) && orgMapPre.length) {
|
||||||
|
valueToSap = {}
|
||||||
|
sapToValue = {}
|
||||||
|
orgMapPre.forEach((org: OrganizationMap) => {
|
||||||
|
valueToSap[org.value] = org.sap_code
|
||||||
|
sapToValue[org.sap_code] = org.value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldTranslate) {
|
// === Translate REQUEST (value -> sap_code) ===
|
||||||
// Get cached organization map data
|
if (isDashboard) {
|
||||||
const orgMap =
|
// ✅ Case 1: params object exists
|
||||||
api.getState().api.queries['getOrganizationsMap(undefined)']?.data ?? []
|
if (request.params?.organization_code) {
|
||||||
|
const mapped = String(request.params.organization_code)
|
||||||
|
.split(',')
|
||||||
|
.map((o) => valueToSap[o] ?? o)
|
||||||
|
.join(',')
|
||||||
|
request.params = { ...request.params, organization_code: mapped }
|
||||||
|
} else if (url.includes('organization_code=')) {
|
||||||
|
const u = new URL(url, 'http://dummy')
|
||||||
|
const orgs = u.searchParams.get('organization_code')?.split(',') ?? []
|
||||||
|
const mapped = orgs.map((o) => valueToSap[o] ?? o)
|
||||||
|
u.searchParams.set('organization_code', mapped.join(','))
|
||||||
|
request.url = u.pathname + '?' + u.searchParams.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (orgMap.length > 0) {
|
|
||||||
// Build a fast lookup table for translation
|
|
||||||
const orgLookup = Object.fromEntries(
|
|
||||||
orgMap.map((org: OrganizationMap) => [org.value, org.sap_code || org.value])
|
|
||||||
)
|
|
||||||
|
|
||||||
// Helper: recursively find and translate organization_code
|
// 2️⃣ Do the request
|
||||||
|
const result = await rawBaseQuery(request, api, extraOptions)
|
||||||
|
const orgMap = (api.getState() as any).api.queries['getOrganizationsMap(undefined)']?.data ?? []
|
||||||
const translateOrgCode = (obj: any): any => {
|
const translateOrgCode = (obj: any): any => {
|
||||||
if (Array.isArray(obj)) return obj.map(translateOrgCode)
|
if (Array.isArray(obj)) return obj.map(translateOrgCode)
|
||||||
if (obj && typeof obj === 'object') {
|
if (obj && typeof obj === 'object') {
|
||||||
const newObj: Record<string, any> = {}
|
const newObj: Record<string, any> = {}
|
||||||
for (const [key, val] of Object.entries(obj)) {
|
for (const [key, val] of Object.entries(obj)) {
|
||||||
if (key === 'organization_code' && typeof val === 'string') {
|
if (key === 'organization_code' && typeof val === 'string') {
|
||||||
// ✅ Find by sap_code and replace with its value
|
|
||||||
const mappedOrg = orgMap.find((org: any) => org.sap_code === val)
|
const mappedOrg = orgMap.find((org: any) => org.sap_code === val)
|
||||||
newObj[key] = mappedOrg?.value || val
|
newObj[key] = mappedOrg?.value || val
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -45,16 +69,16 @@ const baseQuery = async (args:any, api:any, extraOptions:any) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 3️⃣ Translate RESPONSE (sap_code -> value)
|
||||||
|
if (isDashboard && result.data) {
|
||||||
result.data = translateOrgCode(result.data)
|
result.data = translateOrgCode(result.data)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export const api = createApi({
|
export const api = createApi({
|
||||||
baseQuery,
|
baseQuery: translatedBaseQuery,
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
login: builder.mutation<{
|
login: builder.mutation<{
|
||||||
user: User;
|
user: User;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue