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;
}
const res = await employeeStore.fetchById(id);
const _data = await employeeStore.fetchById(id);
if (res) {
state.value.currentEmployee = res;
if (_data) {
const _attach = await employeeStore.listAttachment(_data.id);
state.value.currentEmployee = _data;
const {
createdAt,
@ -847,48 +849,59 @@ export const useEmployeeForm = defineStore('form-employee', () => {
createdBy,
updatedBy,
profileImageUrl,
...playlond
} = res;
...payload
} = _data;
resetEmployeeData = {
...playlond,
...payload,
provinceId: province?.id,
districtId: district?.id,
subDistrictId: subDistrict?.id,
employeeCheckup: structuredClone(
playlond.employeeCheckup?.length === 0
payload.employeeCheckup?.length === 0
? defaultFormData.employeeCheckup
: playlond.employeeCheckup?.map((item) => ({
: payload.employeeCheckup?.map((item) => ({
...item,
statusSave: true,
})),
),
employeeOtherInfo: structuredClone(
{
...playlond.employeeOtherInfo,
statusSave: !!playlond.employeeOtherInfo?.id ? true : false,
...payload.employeeOtherInfo,
statusSave: !!payload.employeeOtherInfo?.id ? true : false,
} || {},
),
employeeWork: structuredClone(
playlond.employeeWork?.length === 0
payload.employeeWork?.length === 0
? defaultFormData.employeeWork
: playlond.employeeWork?.map((item) => ({
: payload.employeeWork?.map((item) => ({
...item,
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,
};
currentFromDataEmployee.value = structuredClone(resetEmployeeData);
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
? (state.value.profileSubmit = true)
@ -897,8 +910,8 @@ export const useEmployeeForm = defineStore('form-employee', () => {
state.value.formDataEmployeeOwner = { ...foundBranch };
if (
foundBranch.address === playlond.address &&
foundBranch.zipCode === playlond.zipCode
foundBranch.address === payload.address &&
foundBranch.zipCode === payload.zipCode
) {
state.value.formDataEmployeeSameAddr = true;
} else {

View file

@ -16,6 +16,7 @@ import {
import { CustomerBranch } from '../customer/types';
import axios from 'axios';
import useFlowStore from '../flow';
import { baseUrl } from '../utils';
const useEmployeeStore = defineStore('api-employee', () => {
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 {
data,
globalOption,
@ -571,6 +623,11 @@ const useEmployeeStore = defineStore('api-employee', () => {
createEmployeeOtherInfo,
editByIdEmployeeOtherInfo,
listAttachment,
getAttachment,
uploadAttachment,
deleteAttachment,
};
});

View file

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