fix: branch file

This commit is contained in:
Thanaphon Frappet 2024-12-06 09:30:01 +07:00
parent a0013bdbe0
commit 57aabf1deb
2 changed files with 32 additions and 32 deletions

View file

@ -7,9 +7,10 @@ import { Pagination } from '../types';
import { BankBook, Branch, BranchCreate } from './types';
import { BranchContact } from '../branch-contact/types';
import { User } from '../user/types';
type BranchId = string;
import { manageAttachment } from '../utils';
const useBranchStore = defineStore('api-branch', () => {
const flowStore = useFlowStore();
const data = ref<Pagination<(Branch & { branch?: Branch[] })[]>>({
@ -20,7 +21,7 @@ const useBranchStore = defineStore('api-branch', () => {
});
const map = ref<Record<BranchId, Branch & { contact?: BranchContact[] }>>({});
const contact: Record<BranchId, BranchContact[]> = {};
const attachmentManager = manageAttachment(api, 'branch');
watch(data, () => {
data.value.result.forEach((v) => {
map.value[v.id] = v;
@ -170,7 +171,11 @@ const useBranchStore = defineStore('api-branch', () => {
fileItem.forEach(async (v) => {
if (v.file === undefined) return;
await putAttachment(res.data.id, v.file);
await attachmentManager.putAttachment({
parentId: res.data.id,
name: v.file.name,
file: v.file,
});
});
}
@ -398,34 +403,40 @@ const useBranchStore = defineStore('api-branch', () => {
}
async function fetchListAttachment(branchId: string) {
const res = await api.get<string[]>(`branch/${branchId}/attachment`);
const res = await attachmentManager.listAttachment({ parentId: branchId });
if (res.status === 200) {
return res.data;
if (res) {
return res;
}
}
async function fetchByIdAttachment(branchId: string, name: string) {
const res = await api.get<string>(`branch/${branchId}/attachment/${name}`);
const res = await attachmentManager.getAttachment({
parentId: branchId,
name: name,
});
if (res.status === 200) {
return res.data;
if (res) {
console.log(res);
return res;
}
}
async function putAttachment(branchId: string, file: File) {
await api
.put(`branch/${branchId}/attachment/${file.name}`, file, {
headers: { 'Content-Type': file.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
await attachmentManager.putAttachment({
parentId: branchId,
name: file.name,
file: file,
});
}
async function deleteByIdAttachment(branchId: string, name: string) {
const res = await api.delete(`branch/${branchId}/attachment/${name}`);
const res = await attachmentManager.delAttachment({
parentId: branchId,
name: name,
});
if (res.status === 204) {
if (res) {
return true;
}