From 1b040210b7ca6b2d0f687b90b0360b993293128d Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Wed, 28 Aug 2024 09:54:44 +0700 Subject: [PATCH] feat: update emploee store for attachment function --- src/pages/03_customer-management/form.ts | 49 ++++++++++++-------- src/stores/employee/index.ts | 57 ++++++++++++++++++++++++ src/stores/employee/types.ts | 6 +++ 3 files changed, 94 insertions(+), 18 deletions(-) diff --git a/src/pages/03_customer-management/form.ts b/src/pages/03_customer-management/form.ts index 37ac8ae6..4e50f0a7 100644 --- a/src/pages/03_customer-management/form.ts +++ b/src/pages/03_customer-management/form.ts @@ -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 { diff --git a/src/stores/employee/index.ts b/src/stores/employee/index.ts index 858b12d7..d2fc5836 100644 --- a/src/stores/employee/index.ts +++ b/src/stores/employee/index.ts @@ -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(`/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(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, }; }); diff --git a/src/stores/employee/types.ts b/src/stores/employee/types.ts index 53245a61..2e32368c 100644 --- a/src/stores/employee/types.ts +++ b/src/stores/employee/types.ts @@ -108,6 +108,12 @@ export type EmployeeCreate = { employeeCheckup?: EmployeeCheckupCreate[]; employeeOtherInfo?: EmployeeOtherCreate; + + file?: { + group?: string; + url?: string; + file?: File; + }[]; }; export type EmployeeUpdate = {