502 lines
14 KiB
TypeScript
502 lines
14 KiB
TypeScript
import { ref } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { Pagination } from '../types';
|
|
import { api } from 'src/boot/axios';
|
|
import {
|
|
RoleData,
|
|
User,
|
|
UserAttachment,
|
|
UserAttachmentCreate,
|
|
UserAttachmentDelete,
|
|
UserCreate,
|
|
UserOption,
|
|
} from './types';
|
|
import axios from 'axios';
|
|
import useBranchStore from '../branch';
|
|
import { Branch } from '../branch/types';
|
|
|
|
const branchStore = useBranchStore()
|
|
|
|
const useUserStore = defineStore('api-user', () => {
|
|
const userOption = ref<UserOption>({
|
|
hqOpts: [],
|
|
brOpts: [],
|
|
roleOpts: [],
|
|
userTypeOpts: [
|
|
{ label: 'พนักงาน', value: 'USER' },
|
|
{ label: 'พนักงานส่งเอกสาร', value: 'MESSENGER' },
|
|
{ label: 'ตัวแทน', value: 'DELEGATE' },
|
|
{ label: 'เอเจนซี่', value: 'AGENCY' },
|
|
],
|
|
genderOpts: [
|
|
{ label: 'ชาย', value: 'male' },
|
|
{ label: 'หญิง', value: 'female' },
|
|
],
|
|
responsibleAreaOpts: [
|
|
{ label: 'เขตพื้นที่ 1 บางรัก ปทุมวัน ยานนาวสาทร และบางคอแหลม' },
|
|
{ label: 'เขตพื้นที่ 2 จอมทอง ทุ่งครุ บางขุนเทียน บางบอน และราษฎร์บูรณะ' },
|
|
{ label: 'เขตพื้นที่ 3 คลองเตย บางนา ประเวศ พระโขนง วัฒนา และสวนหลวง' },
|
|
{ label: 'เขตพื้นที่ 4 คันนายาว บางกะปิ ลาดพร้าว บึงกุ่ม และวังทางหลาง' },
|
|
{ label: 'เขตพื้นที่ 5 คลองสามวา มีนบุรี ลาดกระบัง สะพานสูง หนองจอก และสายไหม' },
|
|
{ label: 'เขตพื้นที่ 6 คลองสาน ธนบุรี บางกอกน้อย บางกอกใหญ่ และบางพลัด' },
|
|
{ label: 'เขตพื้นที่ 7 ตลิ่งชัน ทวีวัฒนา บางแค ภาษีเจริญ และหนองแขม' },
|
|
{ label: 'เขตพื้นที่ 8 ดุสิต พระนครป้อมปราบศัตรูพ่าย และสัมพันธวงศ์' },
|
|
{ label: 'เขตพื้นที่ 9 จตุจักร ดอนเมือง บางชื่อ บางเขน และหลักส' },
|
|
{ label: 'เขตพื้นที่ 10 ดินแดง พญาไท ราชเทวี และห้วยขวาง' },
|
|
],
|
|
nationalityOpts: [
|
|
{ label: 'ไทย', value: 'th' },
|
|
{ label: 'เมียนมา', value: 'mm' },
|
|
{ label: 'ลาว', value: 'lo' },
|
|
{ label: 'กัมพูชา', value: 'kh' },
|
|
{ label: 'เวียดนาม', value: 'vn' },
|
|
],
|
|
trainingPlaceOpts: [
|
|
{ label: 'สถานที่อบรมแรงงานเมียนมา-แม่สอด จ.ตาก' },
|
|
{ label: 'สถานที่อบรมแรงงานเมียนมา- เกาะสอง จ.ระนอง' },
|
|
{ label: 'สถานที่อบรมแรงงานลาว-หนองคาย จ.หนองคาย' },
|
|
{ label: 'สถานที่อบรมแรงงานกัมพูชา- อรัญประเทศ จ.สระแก้ว' },
|
|
{ label: 'สถานที่อบรมแรงงานกัมพูชา-บ้านแหลม จ.จันทบุร' },
|
|
],
|
|
});
|
|
|
|
const data = ref<Pagination<User[]>>();
|
|
|
|
function calculateAge(birthDate: Date | null): string {
|
|
if (!birthDate) return '';
|
|
const birthDateTimeStamp = new Date(birthDate).getTime();
|
|
const now = new Date();
|
|
const diff = now.getTime() - birthDateTimeStamp;
|
|
|
|
const ageDate = new Date(diff);
|
|
const years = ageDate.getUTCFullYear() - 1970;
|
|
const result = `${years} ปี`
|
|
return result;
|
|
}
|
|
|
|
async function fetchHqOption() {
|
|
if (userOption.value.hqOpts.length === 0) {
|
|
const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' });
|
|
if (res) {
|
|
res.result.map((item) => {
|
|
userOption.value.hqOpts.push({
|
|
label: item.code,
|
|
value: item.id,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async function fetchBrOption(id: string) {
|
|
const res = await branchStore.fetchById(id, {
|
|
includeSubBranch: true,
|
|
});
|
|
if (res && res?.branch) {
|
|
res.branch.map((item) => {
|
|
userOption.value.brOpts.push({
|
|
label: item.code,
|
|
value: item.id,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
async function fetchRoleOption() {
|
|
const res = await api.get<RoleData[]>('/keycloak/role');
|
|
res.data.map((item) => {
|
|
const formattedName = item.name
|
|
.replace(/_/g, ' ')
|
|
.toLowerCase()
|
|
.split(' ')
|
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
.join(' ');
|
|
userOption.value.roleOpts.push({
|
|
label: formattedName,
|
|
value: item.id,
|
|
});
|
|
});
|
|
}
|
|
|
|
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;
|
|
includeBranch?: boolean;
|
|
},
|
|
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);
|
|
if (opts?.includeBranch) params.append('includeBranch', 'true');
|
|
|
|
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,
|
|
},
|
|
},
|
|
);
|
|
|
|
profileImage &&
|
|
(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 { profileImage, ...payload } = data
|
|
const res = await api.put<User & { profileImageUploadUrl: string }>(
|
|
`/user/${id}`,
|
|
payload,
|
|
{
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
},
|
|
);
|
|
|
|
if (profileImage)
|
|
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 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<Pagination<Branch[]>>(`/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,
|
|
userOption,
|
|
calculateAge,
|
|
|
|
fetchHqOption,
|
|
fetchBrOption,
|
|
fetchRoleOption,
|
|
|
|
fetchList,
|
|
fetchById,
|
|
|
|
create,
|
|
editById,
|
|
deleteById,
|
|
|
|
getBranch,
|
|
addBranch,
|
|
removeBranch,
|
|
|
|
fetchAttachment,
|
|
addAttachment,
|
|
deleteAttachment,
|
|
|
|
typeStats,
|
|
};
|
|
});
|
|
|
|
export default useUserStore;
|