chore: clean branch module

This commit is contained in:
Methapon Metanipat 2024-08-22 14:30:47 +07:00
parent a4afc54f2e
commit f62a252f5c
2 changed files with 40 additions and 186 deletions

View file

@ -15,7 +15,6 @@ import {
Branch,
BankBook,
} from 'stores/branch/types';
import { Status } from 'stores/types';
import useUtilsStore, { dialog, baseUrl } from 'stores/utils';
import EmptyAddButton from 'components/AddButton.vue';
@ -90,7 +89,6 @@ const columns = [
const modal = ref<boolean>(false);
const hideStat = ref(false);
const currentId = ref<string>('');
const currentStatus = ref<Status | 'All'>('All');
const expandedTree = ref<string[]>([]);
const formMenuIcon = ref<{ icon: string; color: string; bgColor: string }[]>([
{
@ -224,7 +222,6 @@ const beforeBranch = ref<{ id: string; code: string }>({
});
const inputSearch = ref<string>('');
const fieldBranch = ref(['all', 'branchHQLabel', 'branchLabel']);
const fieldDisplay = ref<
(
| 'branchLabelName'
@ -638,24 +635,12 @@ function changeTitle(
function handleHold(node: BranchWithChildren) {
if ($q.screen.gt.xs) return;
return function (props: unknown) {
return function () {
holdDialog.value = true;
currentNode.value = node;
};
}
async function handleImageUpload(file: File | null, url: string | null) {
if (formType.value === 'view') {
formType.value = 'edit';
await onSubmit();
formType.value = 'view';
}
if (formType.value === 'edit') {
await onSubmit();
}
imageDialog.value = false;
}
watch(
() => profileFileImg.value,
() => {

View file

@ -1,11 +1,11 @@
import { ref, watch } from 'vue';
import { defineStore } from 'pinia';
import { Pagination } from '../types';
import { api } from 'src/boot/axios';
import useFlowStore from '../flow';
import { Pagination } from '../types';
import { BankBook, Branch, BranchCreate } from './types';
import { BranchContact } from '../branch-contact/types';
import axios from 'axios';
import useFlowStore from '../flow';
import { User } from '../user/types';
type BranchId = string;
@ -37,31 +37,18 @@ const useBranchStore = defineStore('api-branch', () => {
filter?: 'head' | 'sub';
},
Data extends Pagination<Branch[]>,
>(
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
>(opts?: Options): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
if (v !== undefined) params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/branch${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
{ headers: { 'X-Rtid': flowStore.rtid } },
);
if (res && res.status === 200) {
@ -80,32 +67,18 @@ const useBranchStore = defineStore('api-branch', () => {
(Options['includeContact'] extends true
? { contact: BranchContact[] }
: unknown),
>(
id: string,
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
>(id: string, opts?: Options): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
if (v !== undefined) params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/branch/${id}${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
{ headers: { 'X-Rtid': flowStore.rtid } },
);
if (!res) return false;
@ -114,15 +87,7 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function create(
branch: BranchCreate,
bank?: BankBook[],
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function create(branch: BranchCreate, bank?: BankBook[]) {
const { qrCodeImage, imageUrl, ...payload } = branch;
const res = await api.post<
@ -134,26 +99,21 @@ const useBranchStore = defineStore('api-branch', () => {
>(
'/branch',
{ ...payload, bank: bank },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
{ headers: { 'X-Rtid': flowStore.rtid } },
);
qrCodeImage &&
(await axios
.put(res.data.qrCodeImageUploadUrl, qrCodeImage, {
if (qrCodeImage) {
await api
.put(`/branch/${res.data.id}/line-image`, qrCodeImage, {
headers: { 'Content-Type': qrCodeImage.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
.catch((e) => console.error(e));
}
if (imageUrl) {
await axios
.put(res.data.imageUploadUrl, imageUrl, {
await api
.put(`/branch/${res.data.id}/branch-image`, qrCodeImage, {
headers: { 'Content-Type': imageUrl.type },
onUploadProgress: (e) => console.log(e),
})
@ -171,11 +131,6 @@ const useBranchStore = defineStore('api-branch', () => {
qrCodeImage?: File | undefined,
imageHq?: File | undefined,
bank?: BankBook[],
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const { ...payload } = data;
const res = await api.put<
@ -184,17 +139,13 @@ const useBranchStore = defineStore('api-branch', () => {
`/branch/${id}`,
{ ...payload, bank: bank },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
},
);
if (qrCodeImage) {
await axios
.put(res.data.qrCodeImageUploadUrl, qrCodeImage, {
await api
.put(`/branch/${res.data.id}/line-image`, qrCodeImage, {
headers: { 'Content-Type': qrCodeImage.type },
onUploadProgress: (e) => console.log(e),
})
@ -202,8 +153,8 @@ const useBranchStore = defineStore('api-branch', () => {
}
if (imageHq) {
await axios
.put(res.data.imageUploadUrl, imageHq, {
await api
.put(`/branch/${res.data.id}/branch-image`, imageHq, {
headers: { 'Content-Type': imageHq.type },
onUploadProgress: (e) => console.log(e),
})
@ -215,19 +166,10 @@ const useBranchStore = defineStore('api-branch', () => {
return res.data;
}
async function deleteById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function deleteById(id: string) {
const res = await api.delete<Branch>(`/branch/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
});
@ -237,20 +179,9 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function getUser(
branchId: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function getUser(branchId: string) {
const res = await api.get(`/branch/${branchId}/user`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
});
if (!res) return false;
@ -259,20 +190,9 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function getAdmin(
branchId: string | string[],
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function getAdmin(branchId: string | string[]) {
const res = await api.get<User>(`/branch/${branchId}/admin`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
});
if (!res) return false;
@ -282,25 +202,11 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function addUser(
branchId: string,
userId: string | string[],
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function addUser(branchId: string, userId: string | string[]) {
const res = await api.post(
`/branch/${branchId}/user`,
{ user: ([] as string[]).concat(userId) },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
{ headers: { 'X-Rtid': flowStore.rtid } },
);
if (!res) return false;
@ -309,21 +215,9 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function removeUser(
branchId: string,
userId: string | string[],
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function removeUser(branchId: string, userId: string | string[]) {
const res = await api.delete(`/branch/${branchId}/user`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
data: { user: ([] as string[]).concat(userId) },
});
@ -333,19 +227,13 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function stats(flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
}) {
async function stats() {
const res = await api.get<{
hq: number;
br: number;
}>('/branch/stats', {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
});
@ -355,22 +243,11 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function userStats(
userType: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function userStats(userType: string) {
const res = await api.get(
`/branch/user-stats${userType ? '?'.concat(userType) : ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
},
);
@ -380,18 +257,10 @@ const useBranchStore = defineStore('api-branch', () => {
return false;
}
async function getContact(
branchId: BranchId,
force = false,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function getContact(branchId: BranchId, force = false) {
if (!force && contact[branchId]) return contact[branchId];
const res = await fetchById(branchId, { includeContact: true }, flow);
const res = await fetchById(branchId, { includeContact: true });
if (res) {
contact[branchId] = res.contact;