pmtool / src /services /employeeDetailsApi.ts
devarshia5's picture
Upload 487 files
d97b8f9 verified
Raw
History Blame Contribute Delete
5.54 kB
import apiClient from '@/lib/api/axios-instance';
import { API_BASE_URL } from '@/config';
export interface EmployeeDetails {
employeeDetailsId: number;
employeeId: number;
photo: string | null;
employmentStatus: string;
maritalStatus: string;
workLocation: string;
designation: string;
department: string;
technology: string;
reportingTo: string;
alternateMobileNo: string;
diploma: string;
graduation: string;
postGraduation: string;
panNo: string;
aadharNo: string;
nameAsPerAadhar: string;
esicNo: string;
pfMemberID: string;
uan: string;
passportNo: string;
permanentAddress: string;
presentAddress: string;
emergencyContactPerson: string;
emergencyContactRelationship: string;
emergencyContactNo: string;
bloodGroup: string;
personalEmailId: string;
personalBankName: string;
personalBankAccountNo: string;
personalBankBranch: string;
personalBankIFSC: string;
corporateBankName: string;
corporateBankAccountNo: string;
corporateBranch: string;
corporateIFSCCode: string;
certifications: string;
dateOfJoining: string | null;
probationEndDate: string | null;
lastWorkingDate: string | null;
ctcPA: number | null;
ctcPerMonth: number | null;
dob: string | null;
previousExperience: number | null;
passportDateOfIssue: string | null;
passportDateOfExpiry: string | null;
}
// EmployeeDetails API service
export const employeeDetailsApi = {
// Get all employee details
getAll: async (): Promise<EmployeeDetails[]> => {
try {
console.log('Fetching all employee details...');
const response = await apiClient.get('/api/EmployeeDetails');
console.log('EmployeeDetails getAll response:', response.data);
return response.data;
} catch (error) {
console.error('Failed to get employee details:', error);
return [];
}
},
// Get employee details by ID
getById: async (id: number): Promise<EmployeeDetails | null> => {
try {
console.log(`Fetching employee details with ID ${id}...`);
const response = await apiClient.get(`/api/EmployeeDetails/${id}`);
console.log(`EmployeeDetails getById(${id}) response:`, response.data);
return response.data;
} catch (error) {
console.error(`Failed to get employee details with ID ${id}:`, error);
return null;
}
},
// Get employee details by employee ID
getByEmployeeId: async (employeeId: number): Promise<EmployeeDetails | null> => {
try {
console.log(`Fetching details for employee ID ${employeeId}...`);
// First, try the by-employee endpoint format
try {
const response = await apiClient.get(`/api/EmployeeDetails/by-employee/${employeeId}`);
console.log(`EmployeeDetails getByEmployeeId(${employeeId}) response:`, response.data);
return response.data;
} catch (firstError) {
console.log(`First endpoint attempt failed, trying alternative format...`);
// If that fails, try the format from the curl examples
const altResponse = await apiClient.get(`/api/EmployeeDetails/employee/${employeeId}`);
console.log(`EmployeeDetails alternative endpoint response:`, altResponse.data);
return altResponse.data;
}
} catch (error) {
console.error(`Failed to get details for employee ID ${employeeId}:`, error);
return null;
}
},
// Create employee details
create: async (details: Omit<EmployeeDetails, 'employeeDetailsId'>): Promise<EmployeeDetails | null> => {
try {
console.log('Creating employee details:', details);
const response = await apiClient.post('/api/EmployeeDetails', details);
console.log('EmployeeDetails create response:', response.data);
return response.data;
} catch (error) {
console.error('Failed to create employee details:', error);
return null;
}
},
// Update employee details
update: async (id: number, details: EmployeeDetails): Promise<EmployeeDetails | null> => {
try {
console.log(`Updating employee details with ID ${id}:`, details);
const response = await apiClient.put(`/api/EmployeeDetails/${id}`, details);
console.log(`EmployeeDetails update(${id}) response:`, response.data);
return response.data;
} catch (error) {
console.error(`Failed to update employee details with ID ${id}:`, error);
return null;
}
},
// Update just the employee photo by employee ID
updatePhoto: async (employeeId: number, photoBase64: string): Promise<boolean> => {
try {
console.log(`Updating photo for employee ID ${employeeId}`);
// First, get the employee details
const details = await employeeDetailsApi.getByEmployeeId(employeeId);
if (!details) {
console.error(`No details found for employee ID ${employeeId}`);
return false;
}
// Update only the photo field
const updatedDetails = {
...details,
photo: photoBase64
};
// Update the employee details with the new photo
const response = await employeeDetailsApi.update(details.employeeDetailsId, updatedDetails);
return !!response; // Return true if response exists, false otherwise
} catch (error) {
console.error(`Failed to update photo for employee ID ${employeeId}:`, error);
return false;
}
}
};