diff --git a/src/stores/employee/index.ts b/src/stores/employee/index.ts index 6069fa0a..c8e3a646 100644 --- a/src/stores/employee/index.ts +++ b/src/stores/employee/index.ts @@ -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>(); const globalOption = ref(); const ownerOption = ref(); + 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/${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, }; }); diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index d5027c32..345b3b94 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -347,4 +347,72 @@ export function manageFile(api: AxiosInstance, base: string) { }; } +export function manageMeta(api: AxiosInstance, base: string) { + return { + postMeta: async (opts: { + group: T; + parentId: string; + meta: any; + file: File; + }) => { + const url = `${base}/${opts.parentId}/${opts.group}`; + const res = await api.post(url, opts.meta); + + if (res.status < 400) { + await manageFile(api, base).putFile({ + parentId: opts.parentId, + group: opts.group, + fileId: res.data.id, + file: opts.file, + }); + + return res.data; + } + }, + getMetaList: async (opts: { group: T; parentId: string }) => { + const url = `${base}/${opts.parentId}/${opts.group}`; + const res = await api.get(url); + return res.data; + }, + getMeta: async (opts: { group: T; parentId: string; metaId: string }) => { + const url = `${base}/${opts.parentId}/${opts.group}/${opts.metaId}`; + const res = await api.get(url); + return res.data; + }, + putMeta: async (opts: { + group: T; + parentId: string; + metaId: string; + meta: any; + file?: File; + }) => { + const res = await api.put( + `${base}/${opts.parentId}/${opts.group}/${opts.metaId}`, + opts.meta, + ); + if (res.status < 400) { + if (opts.file !== undefined) + await manageFile(api, base).putFile({ + parentId: opts.parentId, + group: opts.group, + fileId: res.data.id, + file: opts.file, + }); + return res.data; + } + return false; + }, + delMeta: async (opts: { group: T; parentId: string; metaId: string }) => { + const res = await api.delete( + `${base}/${opts.parentId}/${opts.group}/${opts.metaId}`, + ); + if (res.status < 400) return true; + return false; + }, + }; +} +export async function waitAll[]>(arr: T) { + return await Promise.all(arr); +} + export default useUtilsStore;