diff --git a/src/stores/user/index.ts b/src/stores/user/index.ts index 06c66e6d..97171cc5 100644 --- a/src/stores/user/index.ts +++ b/src/stores/user/index.ts @@ -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>(); + async function fetchAttachment( + userId: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.get(`/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, }; }); diff --git a/src/stores/user/types.ts b/src/stores/user/types.ts index 44fbaa08..4a55fd2c 100644 --- a/src/stores/user/types.ts +++ b/src/stores/user/types.ts @@ -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[]; +};