feat: user endpoints
This commit is contained in:
parent
1b0cbc606c
commit
bd2a3174ad
2 changed files with 312 additions and 0 deletions
241
src/stores/user/index.ts
Normal file
241
src/stores/user/index.ts
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
import { ref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { Pagination } from '../types';
|
||||
import { api } from 'src/boot/axios';
|
||||
import { User, UserCreate } from './types';
|
||||
import axios from 'axios';
|
||||
|
||||
const useUserStore = defineStore('api-user', () => {
|
||||
const data = ref<Pagination<User[]>>();
|
||||
|
||||
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>(`/user/${id}`, data, {
|
||||
headers: {
|
||||
'X-Session-Id': flow?.sessionId,
|
||||
'X-Rtid': flow?.refTransactionId,
|
||||
'X-Tid': flow?.transactionId,
|
||||
},
|
||||
});
|
||||
|
||||
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`,
|
||||
{ user: ([] 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 === 200) return res.data;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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: { user: ([] as string[]).concat(branchId) },
|
||||
});
|
||||
|
||||
if (!res) return false;
|
||||
if (res.status === 200) return res.data;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
fetchList,
|
||||
fetchById,
|
||||
|
||||
create,
|
||||
editById,
|
||||
deleteById,
|
||||
|
||||
getBranch,
|
||||
addBranch,
|
||||
removeBranch,
|
||||
};
|
||||
});
|
||||
|
||||
export default useUserStore;
|
||||
71
src/stores/user/types.ts
Normal file
71
src/stores/user/types.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { District, Province, SubDistrict } from '../address';
|
||||
import { Status } from '../types';
|
||||
|
||||
export type User = {
|
||||
subDistrict: SubDistrict | null;
|
||||
district: District | null;
|
||||
province: Province | null;
|
||||
updatedAt: string;
|
||||
updateBy: string;
|
||||
createdAt: string;
|
||||
createdBy: string;
|
||||
status: Status;
|
||||
trainingPlace: string;
|
||||
importNationality: string;
|
||||
sourceNationality: string;
|
||||
licenseExpireDate: string;
|
||||
licenseIssueDate: string;
|
||||
licenseNo: string;
|
||||
discountCondition: string;
|
||||
userRole: string;
|
||||
userType: string;
|
||||
retireDate: string;
|
||||
startDate: string;
|
||||
registrationNo: string;
|
||||
telephoneNo: string;
|
||||
email: string;
|
||||
zipCode: string;
|
||||
subDistrictId: string | null;
|
||||
districtId: string | null;
|
||||
provinceId: string | null;
|
||||
addressEN: string;
|
||||
addressTH: string;
|
||||
lastNameEN: string;
|
||||
lastNameTH: string;
|
||||
firstNameEN: string;
|
||||
firstNameTH: string;
|
||||
code: string;
|
||||
keycloakId: string;
|
||||
id: string;
|
||||
profileImageUrl: string;
|
||||
};
|
||||
|
||||
export type UserCreate = {
|
||||
provinceId?: string | null;
|
||||
districtId?: string | null;
|
||||
subDistrictId?: string | null;
|
||||
telephoneNo: string;
|
||||
email: string;
|
||||
zipCode: string;
|
||||
addressEN: string;
|
||||
addressTH: string;
|
||||
trainingPlace: string;
|
||||
importNationality: string;
|
||||
sourceNationality: string;
|
||||
licenseExpireDate: Date;
|
||||
licenseIssueDate: Date;
|
||||
licenseNo: string;
|
||||
discountCondition: string;
|
||||
retireDate: Date;
|
||||
startDate: Date;
|
||||
registrationNo: string;
|
||||
code: string;
|
||||
lastNameEN: string;
|
||||
lastNameTH: string;
|
||||
firstNameEN: string;
|
||||
firstNameTH: string;
|
||||
userRole: string;
|
||||
userType: string;
|
||||
keycloakId: string;
|
||||
profileImage: File;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue