feat: user attachment

This commit is contained in:
Methapon2001 2024-04-10 13:43:02 +07:00
parent ee1f8e0bce
commit 542eb78ea5
2 changed files with 127 additions and 1 deletions

View file

@ -2,12 +2,121 @@ import { ref } from 'vue';
import { defineStore } from 'pinia';
import { Pagination } from '../types';
import { api } from 'src/boot/axios';
import { User, UserCreate } from './types';
import {
User,
UserAttachment,
UserAttachmentCreate,
UserAttachmentDelete,
UserCreate,
} from './types';
import axios from 'axios';
const useUserStore = defineStore('api-user', () => {
const data = ref<Pagination<User[]>>();
async function fetchAttachment(
userId: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.get<UserAttachment[]>(`/user/${userId}/attachment`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
if (res && res.status === 200) {
return res.data;
}
return false;
}
async function addAttachment(
userId: string,
payload: UserAttachmentCreate,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const list: { name: string; file: File }[] = [];
payload.file.forEach((v) => {
let filename = v.name;
if (list.some((v) => v.name === filename)) {
const dotIndex = filename.lastIndexOf('.');
const originalName =
dotIndex !== -1 && !filename.startsWith('.')
? filename.slice(0, dotIndex)
: filename;
const extension =
dotIndex !== -1 && !filename.startsWith('.')
? filename.slice(dotIndex)
: '';
let i = 0;
while (list.some((v) => v.name === filename)) {
filename = `${originalName} (${++i})`;
if (dotIndex !== -1) filename += extension;
}
}
list.push({ name: filename, file: v });
});
const res = await api.post<(UserAttachment & { uploadUrl: string })[]>(
`/user/${userId}/attachment`,
{ file: list.map((v) => v.name) },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
);
await Promise.all(
res.data.map(async (a) => {
const found = list.find((b) => b.name === a.name)!;
await axios
.put(a.uploadUrl, found.file, {
headers: { 'Content-Type': found.file.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
}),
);
}
async function deleteAttachment(
userId: string,
payload: UserAttachmentDelete,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
await api.delete(`/user/${userId}/attachment`, {
data: payload,
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
}
async function fetchList(
opts?: {
page?: number;
@ -247,6 +356,10 @@ const useUserStore = defineStore('api-user', () => {
getBranch,
addBranch,
removeBranch,
fetchAttachment,
addAttachment,
deleteAttachment,
};
});

View file

@ -70,3 +70,16 @@ export type UserCreate = {
keycloakId: string;
profileImage: File;
};
export type UserAttachment = {
name: string;
url: string;
};
export type UserAttachmentCreate = {
file: File[];
};
export type UserAttachmentDelete = {
file: string[];
};