sap_code > org value
This commit is contained in:
parent
b02e84b48b
commit
c3561b0f31
|
|
@ -13,7 +13,7 @@ import { Filter } from "@/services/types";
|
||||||
import { useAppDispatch, useAppSelector } from "@/lib/hooks";
|
import { useAppDispatch, useAppSelector } from "@/lib/hooks";
|
||||||
import { setFilter } from "@/lib/slice/filter";
|
import { setFilter } from "@/lib/slice/filter";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
import { useGetFilterOptionsQuery } from "@/services/api";
|
import { useGetFilterOptionsQuery, useGetOrganizationsMapQuery } from "@/services/api";
|
||||||
import { NeedLoginRoute } from "../RouteGuard";
|
import { NeedLoginRoute } from "../RouteGuard";
|
||||||
import { useLocale } from "@/lib/hooks/useLocale";
|
import { useLocale } from "@/lib/hooks/useLocale";
|
||||||
import { LanguageSwitcher } from "@/components/LanguageSwitcher";
|
import { LanguageSwitcher } from "@/components/LanguageSwitcher";
|
||||||
|
|
@ -24,6 +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: filterOptions } = useGetFilterOptionsQuery(filter);
|
const {data: filterOptions } = useGetFilterOptionsQuery(filter);
|
||||||
const [region, setRegion] = React.useState("");
|
const [region, setRegion] = React.useState("");
|
||||||
const { t } = useLocale();
|
const { t } = useLocale();
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,60 @@
|
||||||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
||||||
import { AttendanceRange, AttendanceSummary, CostData, EmployeeMovement, EmployeeSummary, FilterOptions, HrCost, HrCostByJob, HrCostByOrganization, HrCostPerMonth, HrCostPerEmployee, MonthlyAttendance, MonthlyEmployee, MppRecruitment, ProductivityByAge, ProductivityByRegion, ProductivityByTenure, ResignationCategory, ResignationReason, ResignationType as ResignationType, ResignSummary, SanctionSummary, TargetTonnage, TonnageHarvestGroupEmployee, TonnageHarvestByEmployeeOrigin, TonnageHarvestByEmployeeSalary, User } from './types'
|
import { AttendanceRange, AttendanceSummary, CostData, EmployeeMovement, EmployeeSummary, FilterOptions, HrCost, HrCostByJob, HrCostByOrganization, HrCostPerMonth, HrCostPerEmployee, MonthlyAttendance, MonthlyEmployee, MppRecruitment, ProductivityByAge, ProductivityByRegion, ProductivityByTenure, ResignationCategory, ResignationReason, ResignationType as ResignationType, ResignSummary, SanctionSummary, TargetTonnage, TonnageHarvestGroupEmployee, TonnageHarvestByEmployeeOrigin, TonnageHarvestByEmployeeSalary, User, OrganizationMap } from './types'
|
||||||
import { Response , Filter} from './types'
|
import { Response , Filter} from './types'
|
||||||
import { config } from '@/config';
|
import { config } from '@/config';
|
||||||
|
|
||||||
|
const rawBaseQuery = fetchBaseQuery({ baseUrl: config.api.baseApiUrl });
|
||||||
|
const baseQuery = async (args:any, api:any, extraOptions:any) => {
|
||||||
|
const result = await rawBaseQuery(args, api, extraOptions)
|
||||||
|
|
||||||
|
if (result.data && typeof args === 'object' && typeof args.url === 'string') {
|
||||||
|
const url = args.url
|
||||||
|
|
||||||
|
// Match /dashboard/* but exclude /dashboard/filter-options
|
||||||
|
const shouldTranslate =
|
||||||
|
url.startsWith('/dashboard') && !url.startsWith('/dashboard/filter-options')
|
||||||
|
|
||||||
|
if (shouldTranslate) {
|
||||||
|
// Get cached organization map data
|
||||||
|
const orgMap =
|
||||||
|
api.getState().api.queries['getOrganizationsMap(undefined)']?.data ?? []
|
||||||
|
|
||||||
|
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
|
||||||
|
const translateOrgCode = (obj: any): any => {
|
||||||
|
if (Array.isArray(obj)) return obj.map(translateOrgCode)
|
||||||
|
if (obj && typeof obj === 'object') {
|
||||||
|
const newObj: Record<string, any> = {}
|
||||||
|
for (const [key, val] of Object.entries(obj)) {
|
||||||
|
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)
|
||||||
|
newObj[key] = mappedOrg?.value || val
|
||||||
|
} else {
|
||||||
|
newObj[key] = translateOrgCode(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newObj
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
result.data = translateOrgCode(result.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
export const api = createApi({
|
export const api = createApi({
|
||||||
baseQuery: fetchBaseQuery({ baseUrl: config.api.baseApiUrl }),
|
baseQuery,
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
login: builder.mutation<{
|
login: builder.mutation<{
|
||||||
user: User;
|
user: User;
|
||||||
|
|
@ -35,6 +84,14 @@ export const api = createApi({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
getOrganizationsMap: builder.query<OrganizationMap[], void>({
|
||||||
|
query: () => '/organizations-map',
|
||||||
|
transformResponse: (response: Response) => {
|
||||||
|
if (response.status === "success") {
|
||||||
|
return response.data!;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
getFilterOptions: builder.query<FilterOptions, Filter>({
|
getFilterOptions: builder.query<FilterOptions, Filter>({
|
||||||
query: (params) => ({ url: '/dashboard/filter-options', params }),
|
query: (params) => ({ url: '/dashboard/filter-options', params }),
|
||||||
transformResponse: (response: Response) => {
|
transformResponse: (response: Response) => {
|
||||||
|
|
@ -256,7 +313,7 @@ export const api = createApi({
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const { useGetFilterOptionsQuery, useGetEmployeeSummaryQuery, useGetMonthlyEmployeeQuery, useGetMonthlyAttendanceQuery,
|
export const { useGetOrganizationsMapQuery, useGetFilterOptionsQuery, useGetEmployeeSummaryQuery, useGetMonthlyEmployeeQuery, useGetMonthlyAttendanceQuery,
|
||||||
useGetOrganizationAttendanceQuery, useGetAttendanceRangeQuery, useGetResignSummaryQuery, useGetResignTypeQuery, useLazyGetMonthlyEmployeeQuery,
|
useGetOrganizationAttendanceQuery, useGetAttendanceRangeQuery, useGetResignSummaryQuery, useGetResignTypeQuery, useLazyGetMonthlyEmployeeQuery,
|
||||||
useLoginMutation, useAuthCheckQuery, useGetSanctionSummaryQuery,
|
useLoginMutation, useAuthCheckQuery, useGetSanctionSummaryQuery,
|
||||||
useGetResignCategoryQuery, useGetResignReasonQuery, useGetEmployeeMovementQuery, useGetMppRecruitmentQuery,
|
useGetResignCategoryQuery, useGetResignReasonQuery, useGetEmployeeMovementQuery, useGetMppRecruitmentQuery,
|
||||||
|
|
|
||||||
|
|
@ -205,3 +205,8 @@ export type CostData = {
|
||||||
date: string;
|
date: string;
|
||||||
amount: number;
|
amount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type OrganizationMap = {
|
||||||
|
value: string;
|
||||||
|
sap_code: string;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue