feat: update emploee store for attachment function

This commit is contained in:
Methapon Metanipat 2024-08-28 09:54:44 +07:00
parent 69d1e1bc72
commit 1b040210b7
3 changed files with 94 additions and 18 deletions

View file

@ -830,10 +830,12 @@ export const useEmployeeForm = defineStore('form-employee', () => {
return; return;
} }
const res = await employeeStore.fetchById(id); const _data = await employeeStore.fetchById(id);
if (res) { if (_data) {
state.value.currentEmployee = res; const _attach = await employeeStore.listAttachment(_data.id);
state.value.currentEmployee = _data;
const { const {
createdAt, createdAt,
@ -847,48 +849,59 @@ export const useEmployeeForm = defineStore('form-employee', () => {
createdBy, createdBy,
updatedBy, updatedBy,
profileImageUrl, profileImageUrl,
...playlond ...payload
} = res; } = _data;
resetEmployeeData = { resetEmployeeData = {
...playlond, ...payload,
provinceId: province?.id, provinceId: province?.id,
districtId: district?.id, districtId: district?.id,
subDistrictId: subDistrict?.id, subDistrictId: subDistrict?.id,
employeeCheckup: structuredClone( employeeCheckup: structuredClone(
playlond.employeeCheckup?.length === 0 payload.employeeCheckup?.length === 0
? defaultFormData.employeeCheckup ? defaultFormData.employeeCheckup
: playlond.employeeCheckup?.map((item) => ({ : payload.employeeCheckup?.map((item) => ({
...item, ...item,
statusSave: true, statusSave: true,
})), })),
), ),
employeeOtherInfo: structuredClone( employeeOtherInfo: structuredClone(
{ {
...playlond.employeeOtherInfo, ...payload.employeeOtherInfo,
statusSave: !!playlond.employeeOtherInfo?.id ? true : false, statusSave: !!payload.employeeOtherInfo?.id ? true : false,
} || {}, } || {},
), ),
employeeWork: structuredClone( employeeWork: structuredClone(
playlond.employeeWork?.length === 0 payload.employeeWork?.length === 0
? defaultFormData.employeeWork ? defaultFormData.employeeWork
: playlond.employeeWork?.map((item) => ({ : payload.employeeWork?.map((item) => ({
...item, ...item,
statusSave: true, statusSave: true,
})), })),
), ),
file: _attach
? await Promise.all(
_attach.map(async (v) => {
const group = v.split('-').at(0);
return {
url: await employeeStore.getAttachment(_data.id, v),
group,
};
}),
)
: [],
image: null, image: null,
}; };
currentFromDataEmployee.value = structuredClone(resetEmployeeData); currentFromDataEmployee.value = structuredClone(resetEmployeeData);
const foundBranch = await customerStore.fetchListCustomeBranchById( const foundBranch = await customerStore.fetchListCustomeBranchById(
playlond.customerBranchId, payload.customerBranchId,
); );
state.value.currentEmployeeCode = playlond.code; state.value.currentEmployeeCode = payload.code;
state.value.profileUrl = profileImageUrl || ' '; state.value.profileUrl = profileImageUrl || '';
profileImageUrl profileImageUrl
? (state.value.profileSubmit = true) ? (state.value.profileSubmit = true)
@ -897,8 +910,8 @@ export const useEmployeeForm = defineStore('form-employee', () => {
state.value.formDataEmployeeOwner = { ...foundBranch }; state.value.formDataEmployeeOwner = { ...foundBranch };
if ( if (
foundBranch.address === playlond.address && foundBranch.address === payload.address &&
foundBranch.zipCode === playlond.zipCode foundBranch.zipCode === payload.zipCode
) { ) {
state.value.formDataEmployeeSameAddr = true; state.value.formDataEmployeeSameAddr = true;
} else { } else {

View file

@ -16,6 +16,7 @@ import {
import { CustomerBranch } from '../customer/types'; import { CustomerBranch } from '../customer/types';
import axios from 'axios'; import axios from 'axios';
import useFlowStore from '../flow'; import useFlowStore from '../flow';
import { baseUrl } from '../utils';
const useEmployeeStore = defineStore('api-employee', () => { const useEmployeeStore = defineStore('api-employee', () => {
const flowStore = useFlowStore(); const flowStore = useFlowStore();
@ -544,6 +545,57 @@ const useEmployeeStore = defineStore('api-employee', () => {
} }
} }
async function listAttachment(employeeId: string) {
const res = await api.get<string[]>(`/employee/${employeeId}/attachment`);
return !res || res.status >= 400 ? false : res.data;
}
async function getAttachment(
employeeId: string,
filename: string,
download = false,
) {
const url = `${baseUrl}/employee/${employeeId}/attachment/${filename}`;
const res = await api.get<string>(url);
if (download) {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
let a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
}
async function uploadAttachment(
employeeId: string,
file: File,
filename?: string,
) {
const res = await api.put(
`/employee/${employeeId}/attachment/${filename || file.name}`,
file,
{
headers: {
'X-Rtid': flowStore.rtid,
'Content-Type': file.type,
},
},
);
return !(!res || res.status >= 400);
}
async function deleteAttachment(employeeId: string, filename: string) {
const res = await api.put(
`/employee/${employeeId}/attachment/${filename}`,
{ headers: { 'X-Rtid': flowStore.rtid } },
);
return !(!res || res.status >= 400);
}
return { return {
data, data,
globalOption, globalOption,
@ -571,6 +623,11 @@ const useEmployeeStore = defineStore('api-employee', () => {
createEmployeeOtherInfo, createEmployeeOtherInfo,
editByIdEmployeeOtherInfo, editByIdEmployeeOtherInfo,
listAttachment,
getAttachment,
uploadAttachment,
deleteAttachment,
}; };
}); });

View file

@ -108,6 +108,12 @@ export type EmployeeCreate = {
employeeCheckup?: EmployeeCheckupCreate[]; employeeCheckup?: EmployeeCheckupCreate[];
employeeOtherInfo?: EmployeeOtherCreate; employeeOtherInfo?: EmployeeOtherCreate;
file?: {
group?: string;
url?: string;
file?: File;
}[];
}; };
export type EmployeeUpdate = { export type EmployeeUpdate = {