refactor: add function manageMeta

This commit is contained in:
Net 2024-09-19 10:15:50 +07:00
parent 3586a623de
commit 48bf222f4e
2 changed files with 120 additions and 11 deletions

View file

@ -12,17 +12,25 @@ import {
EmployeeCheckupCreate,
EmployeeOtherCreate,
EmployeeWorkCreate,
EmployeeVisaPayload,
} from './types';
import { CustomerBranch } from '../customer/types';
import axios from 'axios';
import useFlowStore from '../flow';
import { baseUrl } from '../utils';
import { baseUrl, manageFile, manageMeta } from '../utils';
const useEmployeeStore = defineStore('api-employee', () => {
const flowStore = useFlowStore();
const data = ref<Pagination<Employee[]>>();
const globalOption = ref();
const ownerOption = ref<CustomerBranch[]>();
const fileManager = manageFile<'passport' | 'visa' | 'in-country-notice'>(
api,
'employee',
);
const metaManager = manageMeta<'passport' | 'visa' | 'in-country-notice'>(
api,
'employee',
);
async function fetchById(id: string) {
const res = await api.get<Employee>(`/employee/${id}`);
@ -124,7 +132,17 @@ const useEmployeeStore = defineStore('api-employee', () => {
list: { url: string; imgFile: File | null; name: string }[];
},
) {
const { id, code, image, file, zipCode, ...payload } = data;
console.log(data);
const {
id,
code,
image,
file,
zipCode,
employeeWork,
employeeCheckup,
...payload
} = data;
const res = await api.post<
Employee & { profileImageUrl: string; profileImageUploadUrl: string }
>(
@ -139,17 +157,25 @@ const useEmployeeStore = defineStore('api-employee', () => {
if (res.data.id) {
if (file) {
const attachmentUpload = file.map(async ({ group, file }) => {
if (file) {
const _name = file.name;
const _ext = _name.split('.').at(-1);
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();
let filename = (group || 'other') + '-' + Date.now();
if (_ext) filename = filename + '.' + _ext;
if (_ext) filename = filename + '.' + _ext;
await uploadAttachment(res.data.id, file, filename);
}
if (group !== undefined && _meta !== undefined) {
metaManager.postMeta({
parentId: res.data.id,
group: group as 'passport' | 'visa' | 'in-country-notice',
meta: _meta,
file,
});
}
}
});
await Promise.all(attachmentUpload);
}
@ -482,6 +508,7 @@ const useEmployeeStore = defineStore('api-employee', () => {
);
return !(!res || res.status >= 400);
}
async function deleteAttachment(employeeId: string, filename: string) {
const res = await api.delete(
`/employee/${employeeId}/attachment/${filename}`,
@ -502,6 +529,16 @@ const useEmployeeStore = defineStore('api-employee', () => {
if (res) return res.data;
}
async function listPassport(employeeId: string) {
const res = await api.get(`/employee/${employeeId}/passport`, {
headers: { 'X-Rtid': flowStore.rtid },
});
if (res && res.status < 400) return res.data;
return false;
}
return {
data,
globalOption,
@ -541,6 +578,10 @@ const useEmployeeStore = defineStore('api-employee', () => {
deleteAttachment,
attachment,
listPassport,
...fileManager,
...metaManager,
};
});