refactor: handle attachment emplpyee

This commit is contained in:
Thanaphon Frappet 2024-11-12 15:58:28 +07:00
parent e502153018
commit 5be282b8d0

View file

@ -15,12 +15,13 @@ import {
EmployeeVisaPayload,
} from './types';
import { CustomerBranch } from '../customer/types';
import { baseUrl, manageFile, manageMeta } from '../utils';
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');
@ -118,13 +119,21 @@ const useEmployeeStore = defineStore('api-employee', () => {
if (_ext) filename = filename + '.' + _ext;
if (group !== undefined && _meta !== undefined) {
if (group === undefined) return;
if (group !== 'attachment' && _meta !== undefined) {
metaManager.postMeta({
parentId: res.data.id,
group: group as 'passport' | 'visa' | 'in-country-notice',
group: group as 'passport' | 'visa',
meta: _meta,
file,
});
} else {
attachmentManager.putAttachment({
parentId: res.data.id,
name: file.name,
file,
});
}
}
});
@ -384,6 +393,7 @@ const useEmployeeStore = defineStore('api-employee', () => {
const res = await api.get<string[]>(`/employee/${employeeId}/attachment`);
return !res || res.status >= 400 ? false : res.data;
}
async function getAttachment(
employeeId: string,
filename: string,
@ -411,11 +421,11 @@ const useEmployeeStore = defineStore('api-employee', () => {
file: File,
filename?: string,
) {
const res = await api.put(
`/employee/${employeeId}/attachment/${filename || file.name}`,
await attachmentManager.putAttachment({
parentId: employeeId,
file,
);
return !(!res || res.status >= 400);
name: filename || file.name,
});
}
async function deleteAttachment(employeeId: string, filename: string) {
@ -478,14 +488,14 @@ const useEmployeeStore = defineStore('api-employee', () => {
createEmployeeOtherInfo,
editByIdEmployeeOtherInfo,
listAttachment,
getAttachment,
uploadAttachment,
deleteAttachment,
attachment,
listPassport,
...attachmentManager,
...fileManager,
...metaManager,
};