feat: เพิ่ม fetchListBranch

This commit is contained in:
Net 2024-06-11 11:25:04 +07:00
parent 17ca97165b
commit fd0cde4f42

View file

@ -11,6 +11,7 @@ import {
CustomerStats,
CustomerBranch,
CustomerBranchCreate,
CustomerType,
} from './types';
import axios from 'axios';
@ -43,12 +44,63 @@ const useCustomerStore = defineStore('api-customer', () => {
return false;
}
async function fetchListBranch<
Options extends {
zipCode?: string;
customerId?: string;
includeCustomer?: boolean;
query?: string;
page?: number;
pageSize?: number;
},
Data extends Pagination<
(CustomerBranch &
(Options['includeCustomer'] extends true
? { customer: Customer }
: unknown))[]
>,
>(
opts?: Options,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/customer-branch${(params && '?'.concat(query)) || ''}`,
{
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 fetchList<
Options extends {
page?: number;
pageSize?: number;
query?: string;
includeBranch?: boolean;
customerType?: CustomerType;
},
Data extends Pagination<
(Customer &
@ -373,6 +425,7 @@ const useCustomerStore = defineStore('api-customer', () => {
getStatsCustomer,
fetchListBranch,
fetchListById,
fetchList,
create,