From be2d232fab1249fe1aee19f8fbd9da17a80223d3 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:45:14 +0700 Subject: [PATCH] feat: branch-contact endpoints fn --- src/stores/branch-contact/index.ts | 153 +++++++++++++++++++++++++++++ src/stores/branch-contact/types.ts | 16 +++ 2 files changed, 169 insertions(+) create mode 100644 src/stores/branch-contact/index.ts create mode 100644 src/stores/branch-contact/types.ts diff --git a/src/stores/branch-contact/index.ts b/src/stores/branch-contact/index.ts new file mode 100644 index 00000000..83877278 --- /dev/null +++ b/src/stores/branch-contact/index.ts @@ -0,0 +1,153 @@ +import axios from 'axios'; +import { ref } from 'vue'; +import { defineStore } from 'pinia'; +import { BranchContact, BranchContactCreate } from './types'; +import { Pagination } from '../types'; +import { api } from 'src/boot/axios'; + +const useBranchContactStore = defineStore('api-branch-contact', () => { + const data = ref>(); + + async function fetchList( + branchId: string, + opts?: { + page?: number; + pageSize?: number; + }, + 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}`); + + const query = params.toString(); + + const res = await api.get>( + `/branch/${branchId}/contact${(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( + branchId: string, + data: BranchContactCreate, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const { qrCodeImage, ...payload } = data; + + const res = await api.post< + BranchContact & { qrCodeImageUploadUrl: string } + >(`/branch/${branchId}/contact`, payload, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }); + + await axios + .put(res.data.qrCodeImageUploadUrl, qrCodeImage, { + headers: { 'Content-Type': qrCodeImage.type }, + onUploadProgress: (e) => console.log(e), + }) + .catch((e) => console.error(e)); + + if (!res) return false; + + return res.data; + } + + async function editById( + branchId: string, + contactId: string, + data: Partial, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const { qrCodeImage, ...payload } = data; + + const res = await api.put( + `/branch/${branchId}/contact/${contactId}`, + payload, + { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }, + ); + + if (qrCodeImage) { + await axios + .put(res.data.qrCodeImageUploadUrl, qrCodeImage, { + headers: { 'Content-Type': qrCodeImage.type }, + onUploadProgress: (e) => console.log(e), + }) + .catch((e) => console.error(e)); + } + + if (!res) return false; + + return res.data; + } + + async function deleteById( + branchId: string, + contactId: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.delete< + BranchContact & { qrCodeImageUploadUrl: string } + >(`/branch/${branchId}/contact/${contactId}`, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }); + + if (!res) return false; + + return res.data; + } + + return { + fetchList, + create, + editById, + deleteById, + }; +}); + +export default useBranchContactStore; diff --git a/src/stores/branch-contact/types.ts b/src/stores/branch-contact/types.ts new file mode 100644 index 00000000..da7c2ab1 --- /dev/null +++ b/src/stores/branch-contact/types.ts @@ -0,0 +1,16 @@ +export type BranchContact = { + id: string; + lineId: string; + telephoneNo: string; + qrCodeImageUrl: string; + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; +}; + +export type BranchContactCreate = { + lineId: string; + telephoneNo: string; + qrCodeImage: File; +};