515 lines
12 KiB
TypeScript
515 lines
12 KiB
TypeScript
import { ref } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { Pagination } from '../types';
|
|
import { api } from 'src/boot/axios';
|
|
import { Status } from 'stores/types';
|
|
import {
|
|
Employee,
|
|
EmployeeCheckup,
|
|
EmployeeCreate,
|
|
EmployeeOther,
|
|
EmployeeWork,
|
|
EmployeeCheckupCreate,
|
|
EmployeeOtherCreate,
|
|
EmployeeWorkCreate,
|
|
EmployeeVisaPayload,
|
|
} from './types';
|
|
import { CustomerBranch } from '../customer/types';
|
|
import { baseUrl, manageAttachment, manageFile, manageMeta } from '../utils';
|
|
|
|
const useEmployeeStore = defineStore('api-employee', () => {
|
|
const data = ref<Pagination<Employee[]>>();
|
|
const globalOption = ref();
|
|
const ownerOption = ref<CustomerBranch[]>();
|
|
const attachmentManager = manageAttachment(api, 'employee');
|
|
const fileManager = manageFile<'passport' | 'visa'>(api, 'employee');
|
|
const metaManager = manageMeta<'passport' | 'visa'>(api, 'employee');
|
|
|
|
async function fetchById(id: string) {
|
|
const res = await api.get<Employee>(`/employee/${id}`);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function fetchList(opts?: {
|
|
page?: number;
|
|
pageSize?: number;
|
|
query?: string;
|
|
gender?: string;
|
|
status?: Status;
|
|
passport?: boolean;
|
|
visa?: boolean;
|
|
zipCode?: string;
|
|
customerId?: string;
|
|
customerBranchId?: string;
|
|
activeOnly?: boolean;
|
|
startDate?: string;
|
|
endDate?: string;
|
|
payload?: { passport?: string[] };
|
|
}) {
|
|
const { payload, ...params } = opts || {};
|
|
|
|
const res = payload
|
|
? await api.post<Pagination<Employee[]>>(`/employee/list`, payload, {
|
|
params,
|
|
})
|
|
: await api.get<Pagination<Employee[]>>(`/employee`, {
|
|
params,
|
|
});
|
|
|
|
if (res && res.status === 200) {
|
|
data.value = res.data;
|
|
return data.value;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function fetchImageListById(id: string) {
|
|
const res = await api.get(`/employee/${id}/image`);
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
if (res.status === 204) return null;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function addImageList(file: File, customerId: string, name: string) {
|
|
await api
|
|
.put(`/employee/${customerId}/image/${name}`, file, {
|
|
headers: { 'Content-Type': file.type },
|
|
onUploadProgress: (e) => console.log(e),
|
|
})
|
|
.catch((e) => console.error(e));
|
|
|
|
return name;
|
|
}
|
|
|
|
async function deleteImageByName(id: string, name: string) {
|
|
const res = await api.delete(`/employee/${id}/image/${name}`);
|
|
|
|
if (!res) return false;
|
|
}
|
|
|
|
async function create(
|
|
data: EmployeeCreate,
|
|
imgList: {
|
|
selectedImage: string;
|
|
list: { url: string; imgFile: File | null; name: string }[];
|
|
},
|
|
) {
|
|
const {
|
|
id,
|
|
code,
|
|
file,
|
|
zipCode,
|
|
employeeWork,
|
|
employeeCheckup,
|
|
employeePassport,
|
|
employeeVisa,
|
|
employeeInCountryNotice,
|
|
...payload
|
|
} = data;
|
|
const res = await api.post<
|
|
Employee & { profileImageUrl: string; profileImageUploadUrl: string }
|
|
>('/employee', { ...payload, selectedImage: imgList.selectedImage });
|
|
|
|
if (!res) return false;
|
|
|
|
if (res.data.id) {
|
|
if (file) {
|
|
const attachmentUpload = file.map(async ({ group, file, _meta }) => {
|
|
if (file)
|
|
if (file) {
|
|
const _name = file.name;
|
|
const _ext = _name.split('.').at(-1);
|
|
|
|
let filename = (group || 'other') + '-' + Date.now();
|
|
|
|
if (_ext) filename = filename + '.' + _ext;
|
|
|
|
if (group === undefined) return;
|
|
|
|
if (group !== 'attachment' && _meta !== undefined) {
|
|
metaManager.postMeta({
|
|
parentId: res.data.id,
|
|
group: group as 'passport' | 'visa',
|
|
meta: _meta,
|
|
file,
|
|
});
|
|
} else {
|
|
attachmentManager.putAttachment({
|
|
parentId: res.data.id,
|
|
name: file.name,
|
|
file,
|
|
});
|
|
}
|
|
}
|
|
});
|
|
await Promise.all(attachmentUpload);
|
|
}
|
|
if (imgList.list.length > 0 && res.data.id) {
|
|
for (let index = 0; index < imgList.list.length; index++) {
|
|
const imgFile = imgList.list[index].imgFile;
|
|
if (imgFile)
|
|
await addImageList(imgFile, res.data.id, imgList.list[index].name);
|
|
}
|
|
}
|
|
}
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function createEmployeeCheckup(
|
|
employeeId: string,
|
|
data: EmployeeCheckupCreate,
|
|
) {
|
|
const { id, statusSave, ...payload } = data;
|
|
const res = await api.post<EmployeeCheckupCreate>(
|
|
`/employee/${employeeId}/checkup`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function createEmployeeWork(
|
|
employeeId: string,
|
|
data: EmployeeWorkCreate,
|
|
) {
|
|
const { id, ...payload } = data;
|
|
const res = await api.post<EmployeeWorkCreate>(
|
|
`/employee/${employeeId}/work`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function createEmployeeOtherInfo(
|
|
employeeId: string,
|
|
data: EmployeeOtherCreate,
|
|
) {
|
|
const { id, statusSave, ...payload } = data;
|
|
const res = await api.post<EmployeeOtherCreate>(
|
|
`/employee/${employeeId}/other-info`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editByIdEmployeeCheckup(
|
|
employeeOfId: string,
|
|
data: Partial<EmployeeCheckupCreate>,
|
|
) {
|
|
const {
|
|
id,
|
|
createdAt,
|
|
createdByUserId,
|
|
employeeId,
|
|
statusSave,
|
|
updatedAt,
|
|
updatedByUserId,
|
|
...payload
|
|
} = data;
|
|
const res = await api.put<EmployeeCheckupCreate>(
|
|
`/employee/${employeeOfId}/checkup/${id}`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editByIdEmployeeWork(
|
|
employeeOfId: string,
|
|
data: Partial<EmployeeWorkCreate>,
|
|
) {
|
|
const {
|
|
id,
|
|
createdAt,
|
|
createdByUserId,
|
|
employeeId,
|
|
statusSave,
|
|
updatedAt,
|
|
updatedByUserId,
|
|
...payload
|
|
} = data;
|
|
const res = await api.put<EmployeeWorkCreate>(
|
|
`/employee/${employeeOfId}/work/${id}`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editByIdEmployeeOtherInfo(
|
|
employeeOfId: string,
|
|
data: Partial<EmployeeOtherCreate>,
|
|
) {
|
|
const {
|
|
id,
|
|
statusSave,
|
|
updatedByUserId,
|
|
updatedAt,
|
|
createdByUserId,
|
|
createdAt,
|
|
employeeId,
|
|
|
|
...payload
|
|
} = data;
|
|
const res = await api.put<EmployeeOtherCreate>(
|
|
`/employee/${employeeOfId}/other-info/${id}`,
|
|
{ ...payload },
|
|
);
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editById(employeeId: string, data: Partial<EmployeeCreate>) {
|
|
const {
|
|
id,
|
|
code,
|
|
file,
|
|
employeeCheckup,
|
|
employeeWork,
|
|
employeeInCountryNotice,
|
|
employeeVisa,
|
|
employeePassport,
|
|
...payload
|
|
} = data;
|
|
|
|
const res = await api.put<
|
|
Employee & { imageUrl: string; profileImageUploadUrl: string }
|
|
>(`/employee/${employeeId}`, payload);
|
|
|
|
if (!res) return false;
|
|
|
|
if (file) {
|
|
const attachmentUpload = file.map(async ({ group, file }) => {
|
|
if (file) {
|
|
const _name = file.name;
|
|
const _ext = _name.split('.').at(-1);
|
|
|
|
let filename = (group || 'other') + '-' + Date.now();
|
|
|
|
if (_ext) filename = filename + '.' + _ext;
|
|
|
|
await uploadAttachment(employeeId, file, filename);
|
|
}
|
|
});
|
|
await Promise.all(attachmentUpload);
|
|
}
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function deleteByIdCheckUp(opts: {
|
|
employeeId: string;
|
|
checkUpId?: string;
|
|
}) {
|
|
const res = await api.delete<Employee>(
|
|
`/employee/${opts.employeeId}/checkup/${opts.checkUpId}`,
|
|
);
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function deleteByIdWork(opts: { employeeId: string; workId?: string }) {
|
|
const res = await api.delete<Employee>(
|
|
`/employee/${opts.employeeId}/work/${opts.workId}`,
|
|
);
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function deleteById(id: string) {
|
|
const res = await api.delete<Employee>(`/employee/${id}`);
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function fetchCheckup(id: string) {
|
|
const res = await api.get<EmployeeCheckup[]>(`/employee/${id}/checkup`);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function fetchWork(id: string) {
|
|
const res = await api.get<EmployeeWork[]>(`/employee/${id}/work`);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function fetchOther(id: string) {
|
|
const res = await api.get<EmployeeOther>(`/employee/${id}/other-info`);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function getStatsEmployee(customerBranchId?: string) {
|
|
const res = await api.get(
|
|
`/employee/stats${customerBranchId ? '?customerBranchId=' + customerBranchId : ''}/`,
|
|
);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function getStatsEmployeeGender(opts?: {
|
|
customerBranchId?: string;
|
|
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
|
|
query?: string;
|
|
}) {
|
|
const res = await api.get(`/employee/stats/gender`, { params: opts });
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function getEditHistory(employeeId: string) {
|
|
const res = await api.get(`/employee/${employeeId}/edit-history`);
|
|
if (res && res.status === 200) {
|
|
return res.data;
|
|
}
|
|
}
|
|
|
|
async function listAttachment(employeeId: string) {
|
|
const res = await api.get<string[]>(`/employee/${employeeId}/attachment`);
|
|
return !res || res.status >= 400 ? false : res.data;
|
|
}
|
|
|
|
async function getAttachment(
|
|
employeeId: string,
|
|
filename: string,
|
|
download = false,
|
|
) {
|
|
const url = `${baseUrl}/employee/${employeeId}/attachment/${filename}`;
|
|
const res = await api.get<string>(url);
|
|
|
|
if (download) {
|
|
fetch(res.data)
|
|
.then(async (res) => await res.blob())
|
|
.then((blob) => {
|
|
const a = document.createElement('a');
|
|
a.download = filename;
|
|
a.href = window.URL.createObjectURL(blob);
|
|
a.click();
|
|
a.remove();
|
|
});
|
|
}
|
|
|
|
return res.data;
|
|
}
|
|
async function uploadAttachment(
|
|
employeeId: string,
|
|
file: File,
|
|
filename?: string,
|
|
) {
|
|
await attachmentManager.putAttachment({
|
|
parentId: employeeId,
|
|
file,
|
|
name: filename || file.name,
|
|
});
|
|
}
|
|
|
|
async function deleteAttachment(employeeId: string, filename: string) {
|
|
const res = await api.delete(
|
|
`/employee/${employeeId}/attachment/${filename}`,
|
|
);
|
|
return !(!res || res.status >= 400);
|
|
}
|
|
|
|
async function attachment(opts: {
|
|
action: 'get' | 'put' | 'delete';
|
|
type: 'passport' | 'visa' | 'in-country-notice';
|
|
id: string;
|
|
}) {
|
|
const res = await api[opts.action](
|
|
`/employee/file-${opts.type}/${opts.id}`,
|
|
);
|
|
|
|
if (res) return res.data;
|
|
}
|
|
|
|
async function listPassport(employeeId: string) {
|
|
const res = await api.get(`/employee/${employeeId}/passport`);
|
|
|
|
if (res && res.status < 400) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
return {
|
|
data,
|
|
globalOption,
|
|
ownerOption,
|
|
|
|
fetchById,
|
|
fetchList,
|
|
|
|
fetchImageListById,
|
|
addImageList,
|
|
deleteImageByName,
|
|
|
|
create,
|
|
editById,
|
|
deleteById,
|
|
fetchCheckup,
|
|
fetchWork,
|
|
fetchOther,
|
|
getStatsEmployee,
|
|
getStatsEmployeeGender,
|
|
getEditHistory,
|
|
|
|
createEmployeeCheckup,
|
|
editByIdEmployeeCheckup,
|
|
deleteByIdCheckUp,
|
|
|
|
createEmployeeWork,
|
|
editByIdEmployeeWork,
|
|
deleteByIdWork,
|
|
|
|
createEmployeeOtherInfo,
|
|
editByIdEmployeeOtherInfo,
|
|
|
|
uploadAttachment,
|
|
deleteAttachment,
|
|
|
|
attachment,
|
|
|
|
listPassport,
|
|
|
|
...attachmentManager,
|
|
...fileManager,
|
|
...metaManager,
|
|
};
|
|
});
|
|
|
|
export default useEmployeeStore;
|