From e6052e087bec386fc55e6de25623203fda3946ec Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:59:30 +0700 Subject: [PATCH] feat: api function customer endpoints --- src/stores/customer/index.ts | 147 +++++++++++++++++++++++++++++++++++ src/stores/customer/types.ts | 33 ++++++++ 2 files changed, 180 insertions(+) create mode 100644 src/stores/customer/index.ts create mode 100644 src/stores/customer/types.ts diff --git a/src/stores/customer/index.ts b/src/stores/customer/index.ts new file mode 100644 index 00000000..4d619b60 --- /dev/null +++ b/src/stores/customer/index.ts @@ -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>(); + 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>( + `/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/${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; diff --git a/src/stores/customer/types.ts b/src/stores/customer/types.ts new file mode 100644 index 00000000..5b1493e5 --- /dev/null +++ b/src/stores/customer/types.ts @@ -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; +};