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