feat: employee file upload

This commit is contained in:
Methapon Metanipat 2024-08-28 14:43:51 +07:00
parent cd7bbc21a6
commit 60d27ad809
4 changed files with 93 additions and 23 deletions

View file

@ -3109,11 +3109,44 @@ const emptyCreateDialog = ref(false);
/> />
<UploadFile <UploadFile
:treeFile="[]" :tree-file="
Object.values(
currentFromDataEmployee.file?.reduce<
Record<
string,
{ label: string; file: { label: string }[] }
>
>((a, c) => {
const _group = c.group || 'other';
if (!a[_group]) {
a[_group] = {
label: _group,
file: [
{
label:
c.name ||
`${c.group}-${c.file?.name || Date.now()}`,
},
],
};
} else {
a[_group].file.push({
label:
c.name ||
`${c.group}-${c.file?.name || Date.now()}`,
});
}
return a;
}, {}) || {},
)
"
v-model:file="currentFromDataEmployee.file"
hide-action
:readonly="!employeeFormState.isEmployeeEdit"
:dropdown-list="uploadFileListEmployee" :dropdown-list="uploadFileListEmployee"
@send-ocr=" @send-ocr="
async (v: any, f: any) => { async (_, file) => {
await ocrStore.sendOcr({ file: f }); if (file) await ocrStore.sendOcr({ file });
} }
" "
/> />

View file

@ -881,11 +881,13 @@ export const useEmployeeForm = defineStore('form-employee', () => {
), ),
file: _attach file: _attach
? await Promise.all( ? await Promise.all(
_attach.map(async (v) => { _attach.map(async (name) => {
const group = v.split('-').at(0); const fragment = name.split('-');
const group = fragment.length === 1 ? 'other' : fragment.at(0);
return { return {
url: await employeeStore.getAttachment(_data.id, v), url: await employeeStore.getAttachment(_data.id, name),
name,
group, group,
}; };
}), }),

View file

@ -64,23 +64,41 @@ const useEmployeeStore = defineStore('api-employee', () => {
} }
async function create(data: EmployeeCreate) { async function create(data: EmployeeCreate) {
const { id, code, image, ...payload } = data; const { id, code, image, file, ...payload } = data;
const res = await api.post< const res = await api.post<
Employee & { profileImageUrl: string; profileImageUploadUrl: string } Employee & { profileImageUrl: string; profileImageUploadUrl: string }
>('/employee', payload, { >('/employee', payload, {
headers: { 'X-Rtid': flowStore.rtid }, headers: { 'X-Rtid': flowStore.rtid },
}); });
image &&
(await axios
.put(res.data.profileImageUploadUrl, image, {
headers: { 'Content-Type': image?.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
if (!res) return false; if (!res) return false;
if (res.data.id) {
if (image) {
await api
.put(`/employee/${res.data.id}/image`, image, {
headers: { 'Content-Type': image?.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
}
if (file) {
const attachmentUpload = file.map(async ({ group, file }) => {
if (file) {
const _name = file.name;
const _ext = _name.split('.').at(-1);
let filename = group || 'other' + '-' + Date.now();
if (_ext) filename += '.' + _ext;
await uploadAttachment(res.data.id, file, filename);
}
});
await Promise.all(attachmentUpload);
}
}
return res.data; return res.data;
} }
@ -210,22 +228,38 @@ const useEmployeeStore = defineStore('api-employee', () => {
} }
async function editById(employeeId: string, data: Partial<EmployeeCreate>) { async function editById(employeeId: string, data: Partial<EmployeeCreate>) {
const { id, code, image, ...payload } = data; const { id, code, image, file, ...payload } = data;
const res = await api.put< const res = await api.put<
Employee & { imageUrl: string; profileImageUploadUrl: string } Employee & { imageUrl: string; profileImageUploadUrl: string }
>(`/employee/${employeeId}`, payload, { >(`/employee/${employeeId}`, payload, {
headers: { 'X-Rtid': flowStore.rtid }, headers: { 'X-Rtid': flowStore.rtid },
}); });
image && if (!res) return false;
(await axios
.put(res.data.profileImageUploadUrl, image, { if (image) {
headers: { 'Content-Type': image.type }, await api
.put(`/employee/${employeeId}/image`, image, {
headers: { 'Content-Type': image?.type },
onUploadProgress: (e) => console.log(e), onUploadProgress: (e) => console.log(e),
}) })
.catch((e) => console.error(e))); .catch((e) => console.error(e));
}
if (file) {
const attachmentUpload = file.map(async ({ group, file }) => {
if (file) {
const _name = file.name;
const _ext = _name.split('.').at(-1);
if (!res) return false; let filename = (group || 'other') + '-' + Date.now();
if (_ext) filename = filename + '.' + _ext;
await uploadAttachment(employeeId, file, filename);
}
});
await Promise.all(attachmentUpload);
}
return res.data; return res.data;
} }

View file

@ -110,6 +110,7 @@ export type EmployeeCreate = {
employeeOtherInfo?: EmployeeOtherCreate; employeeOtherInfo?: EmployeeOtherCreate;
file?: { file?: {
name?: string;
group?: string; group?: string;
url?: string; url?: string;
file?: File; file?: File;