feat: api function customer endpoints
This commit is contained in:
parent
65781eb480
commit
e6052e087b
2 changed files with 180 additions and 0 deletions
147
src/stores/customer/index.ts
Normal file
147
src/stores/customer/index.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import { ref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { Pagination } from '../types';
|
||||
import { api } from 'src/boot/axios';
|
||||
import { Customer, CustomerCreate, CustomerUpdate } from './types';
|
||||
import axios from 'axios';
|
||||
|
||||
const useCustomerStore = defineStore('api-customer', () => {
|
||||
const data = ref<Pagination<Customer[]>>();
|
||||
async function fetchList(
|
||||
opts?: {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
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?.query) params.append('query', opts.query);
|
||||
|
||||
const query = params.toString();
|
||||
|
||||
const res = await api.get<Pagination<Customer[]>>(
|
||||
`/customer${(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 create(
|
||||
data: CustomerCreate,
|
||||
flow?: {
|
||||
sessionId: string;
|
||||
refTransactionId: string;
|
||||
transactionId: string;
|
||||
},
|
||||
) {
|
||||
const { image, ...payload } = data;
|
||||
|
||||
const res = await api.post<
|
||||
Customer & { imageUrl: string; imageUploadUrl: string }
|
||||
>('/customer', payload, {
|
||||
headers: {
|
||||
'X-Session-Id': flow?.sessionId,
|
||||
'X-Rtid': flow?.refTransactionId,
|
||||
'X-Tid': flow?.transactionId,
|
||||
},
|
||||
});
|
||||
|
||||
await axios
|
||||
.put(res.data.imageUploadUrl, image, {
|
||||
headers: { 'Content-Type': image.type },
|
||||
onUploadProgress: (e) => console.log(e),
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function editById(
|
||||
id: string,
|
||||
data: CustomerUpdate,
|
||||
flow?: {
|
||||
sessionId: string;
|
||||
refTransactionId: string;
|
||||
transactionId: string;
|
||||
},
|
||||
) {
|
||||
const { image, ...payload } = data;
|
||||
const res = await api.put<
|
||||
Customer & { imageUrl: string; imageUploadUrl: string }
|
||||
>(`/customer/${id}`, payload, {
|
||||
headers: {
|
||||
'X-Session-Id': flow?.sessionId,
|
||||
'X-Rtid': flow?.refTransactionId,
|
||||
'X-Tid': flow?.transactionId,
|
||||
},
|
||||
});
|
||||
|
||||
if (image)
|
||||
await axios
|
||||
.put(res.data.imageUploadUrl, image, {
|
||||
headers: { 'Content-Type': image.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<Customer>(`/customer/${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;
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
|
||||
fetchList,
|
||||
create,
|
||||
editById,
|
||||
deleteById,
|
||||
};
|
||||
});
|
||||
|
||||
export default useCustomerStore;
|
||||
33
src/stores/customer/types.ts
Normal file
33
src/stores/customer/types.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Status } from '../types';
|
||||
|
||||
export type CustomerType = 'CORP' | 'PERS';
|
||||
|
||||
export type Customer = {
|
||||
imageUrl: string;
|
||||
id: string;
|
||||
code: string;
|
||||
customerType: CustomerType;
|
||||
customerName: string;
|
||||
customerNameEN: string;
|
||||
status: Status;
|
||||
createdBy: string | null;
|
||||
createdAt: Date;
|
||||
updateBy: string | null;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type CustomerCreate = {
|
||||
status?: Status;
|
||||
customerType: CustomerType;
|
||||
customerName: string;
|
||||
customerNameEN: string;
|
||||
image: File;
|
||||
};
|
||||
|
||||
export type CustomerUpdate = {
|
||||
status?: 'ACTIVE' | 'INACTIVE';
|
||||
customerType?: CustomerType;
|
||||
customerName?: string;
|
||||
customerNameEN?: string;
|
||||
image?: File;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue