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,
};
});

View file

@ -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;