jws-frontend/src/stores/user/index.ts
2024-04-10 14:00:19 +07:00

388 lines
8.7 KiB
TypeScript

import { ref } from 'vue';
import { defineStore } from 'pinia';
import { Pagination } from '../types';
import { api } from 'src/boot/axios';
import {
User,
UserAttachment,
UserAttachmentCreate,
UserAttachmentDelete,
UserCreate,
} from './types';
import axios from 'axios';
const useUserStore = defineStore('api-user', () => {
const userStats = ref()
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;
pageSize?: number;
zipCode?: string;
query?: string;
},
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const params = new URLSearchParams();
if (opts?.pageSize && opts?.pageSize > 0) {
params.append('pageSize', `${opts.pageSize}`);
}
if (opts?.page && opts.page > 0) params.append('page', `${opts.page}`);
if (opts?.zipCode) params.append('zipCode', opts.zipCode);
if (opts?.query) params.append('query', opts.query);
const query = params.toString();
const res = await api.get<Pagination<User[]>>(
`/user${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
);
if (res && res.status === 200) {
data.value = res.data;
return data.value;
}
return false;
}
async function fetchById(
id: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.get<User>(`/user/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
if (res.status === 204) return null;
return false;
}
async function create(
data: UserCreate,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const { profileImage, ...payload } = data;
const res = await api.post<User & { profileImageUploadUrl: string }>(
'/user',
payload,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
);
await axios
.put(res.data.profileImageUploadUrl, profileImage, {
headers: { 'Content-Type': profileImage.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
if (!res) return false;
return res.data;
}
async function editById(
id: string,
data: Partial<Omit<UserCreate, 'keycloakId'>>,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.put<User & { profileImageUploadUrl: string }>(
`/user/${id}`,
data,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
);
if (data.profileImage)
await axios
.put(res.data.profileImageUploadUrl, data.profileImage, {
headers: { 'Content-Type': data.profileImage.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
if (!res) return false;
return res.data;
}
async function deleteById(
id: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.delete<User>(`/user/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
async function getBranch(
userId: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.get(`/user/${userId}/branch`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
async function addBranch(
userId: string,
branchId: string | string[],
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.post(
`/user/${userId}/branch`,
{ branch: ([] as string[]).concat(branchId) },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
if (res.status >= 400) return false;
return res.data || true;
}
async function removeBranch(
userId: string,
branchId: string | string[],
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.delete(`/user/${userId}/branch`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
data: { branch: ([] as string[]).concat(branchId) },
});
if (!res) return false;
if (res.status >= 400) return false;
return res.data || true;
}
async function typeStats(
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},) {
const res = await api.get(`/user/type-stats`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
})
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
return {
data,
fetchList,
fetchById,
create,
editById,
deleteById,
getBranch,
addBranch,
removeBranch,
fetchAttachment,
addAttachment,
deleteAttachment,
typeStats,
};
});
export default useUserStore;