refactor: add function manageMeta
This commit is contained in:
parent
3586a623de
commit
48bf222f4e
2 changed files with 120 additions and 11 deletions
|
|
@ -12,17 +12,25 @@ import {
|
||||||
EmployeeCheckupCreate,
|
EmployeeCheckupCreate,
|
||||||
EmployeeOtherCreate,
|
EmployeeOtherCreate,
|
||||||
EmployeeWorkCreate,
|
EmployeeWorkCreate,
|
||||||
|
EmployeeVisaPayload,
|
||||||
} from './types';
|
} from './types';
|
||||||
import { CustomerBranch } from '../customer/types';
|
import { CustomerBranch } from '../customer/types';
|
||||||
import axios from 'axios';
|
|
||||||
import useFlowStore from '../flow';
|
import useFlowStore from '../flow';
|
||||||
import { baseUrl } from '../utils';
|
import { baseUrl, manageFile, manageMeta } from '../utils';
|
||||||
|
|
||||||
const useEmployeeStore = defineStore('api-employee', () => {
|
const useEmployeeStore = defineStore('api-employee', () => {
|
||||||
const flowStore = useFlowStore();
|
const flowStore = useFlowStore();
|
||||||
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 fileManager = manageFile<'passport' | 'visa' | 'in-country-notice'>(
|
||||||
|
api,
|
||||||
|
'employee',
|
||||||
|
);
|
||||||
|
const metaManager = manageMeta<'passport' | 'visa' | 'in-country-notice'>(
|
||||||
|
api,
|
||||||
|
'employee',
|
||||||
|
);
|
||||||
|
|
||||||
async function fetchById(id: string) {
|
async function fetchById(id: string) {
|
||||||
const res = await api.get<Employee>(`/employee/${id}`);
|
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 }[];
|
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<
|
const res = await api.post<
|
||||||
Employee & { profileImageUrl: string; profileImageUploadUrl: string }
|
Employee & { profileImageUrl: string; profileImageUploadUrl: string }
|
||||||
>(
|
>(
|
||||||
|
|
@ -139,17 +157,25 @@ const useEmployeeStore = defineStore('api-employee', () => {
|
||||||
|
|
||||||
if (res.data.id) {
|
if (res.data.id) {
|
||||||
if (file) {
|
if (file) {
|
||||||
const attachmentUpload = file.map(async ({ group, file }) => {
|
const attachmentUpload = file.map(async ({ group, file, _meta }) => {
|
||||||
if (file) {
|
if (file)
|
||||||
const _name = file.name;
|
if (file) {
|
||||||
const _ext = _name.split('.').at(-1);
|
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);
|
await Promise.all(attachmentUpload);
|
||||||
}
|
}
|
||||||
|
|
@ -482,6 +508,7 @@ const useEmployeeStore = defineStore('api-employee', () => {
|
||||||
);
|
);
|
||||||
return !(!res || res.status >= 400);
|
return !(!res || res.status >= 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteAttachment(employeeId: string, filename: string) {
|
async function deleteAttachment(employeeId: string, filename: string) {
|
||||||
const res = await api.delete(
|
const res = await api.delete(
|
||||||
`/employee/${employeeId}/attachment/${filename}`,
|
`/employee/${employeeId}/attachment/${filename}`,
|
||||||
|
|
@ -502,6 +529,16 @@ const useEmployeeStore = defineStore('api-employee', () => {
|
||||||
if (res) return res.data;
|
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 {
|
return {
|
||||||
data,
|
data,
|
||||||
globalOption,
|
globalOption,
|
||||||
|
|
@ -541,6 +578,10 @@ const useEmployeeStore = defineStore('api-employee', () => {
|
||||||
deleteAttachment,
|
deleteAttachment,
|
||||||
|
|
||||||
attachment,
|
attachment,
|
||||||
|
|
||||||
|
listPassport,
|
||||||
|
...fileManager,
|
||||||
|
...metaManager,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -347,4 +347,72 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function manageMeta<T extends string>(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<typeof opts.meta>(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<string>(url);
|
||||||
|
return res.data;
|
||||||
|
},
|
||||||
|
putMeta: async (opts: {
|
||||||
|
group: T;
|
||||||
|
parentId: string;
|
||||||
|
metaId: string;
|
||||||
|
meta: any;
|
||||||
|
file?: File;
|
||||||
|
}) => {
|
||||||
|
const res = await api.put<typeof opts.meta>(
|
||||||
|
`${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<T extends Promise<any>[]>(arr: T) {
|
||||||
|
return await Promise.all(arr);
|
||||||
|
}
|
||||||
|
|
||||||
export default useUtilsStore;
|
export default useUtilsStore;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue