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 1/3] 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; +}; From 1b0cbc606c288413330dfe705dcbe79ea434e73a Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:46:25 +0700 Subject: [PATCH 2/3] refactor: extract type --- src/stores/branch/index.ts | 92 +++++++------------------------------- src/stores/branch/types.ts | 48 ++++++++++++++++++++ 2 files changed, 65 insertions(+), 75 deletions(-) create mode 100644 src/stores/branch/types.ts diff --git a/src/stores/branch/index.ts b/src/stores/branch/index.ts index 286564e3..d270d850 100644 --- a/src/stores/branch/index.ts +++ b/src/stores/branch/index.ts @@ -1,41 +1,13 @@ import { ref } from 'vue'; import { defineStore } from 'pinia'; -import { District, Province, SubDistrict } from '../address'; -import { Pagination, Status } from '../types'; +import { Pagination } from '../types'; import { api } from 'src/boot/axios'; - -export type Branch = { - subDistrict: SubDistrict | null; - district: District | null; - province: Province | null; - updatedAt: string; - updateBy: string; - createdAt: string; - createdBy: string; - status: Status; - headOfficeId: string | null; - isHeadOffice: boolean; - longitude: string; - latitude: string; - telephoneNo: string; - email: string; - zipCode: string; - subDistrictId: string; - districtId: string; - provinceId: string; - addressEN: string; - addressTH: string; - nameEN: string; - nameTH: string; - taxNo: string; - code: string; - id: string; -}; +import { Branch, BranchCreate } from './types'; const useBranchStore = defineStore('api-branch', () => { const data = ref>(); - async function fetchBranch( + async function fetchList( opts?: { page?: number; pageSize?: number; @@ -77,7 +49,7 @@ const useBranchStore = defineStore('api-branch', () => { return false; } - async function fetchBranchById( + async function fetchById( id: string, flow?: { sessionId: string; @@ -100,31 +72,17 @@ const useBranchStore = defineStore('api-branch', () => { return false; } - async function createBranch( - branch: { - code: string; - taxNo: string; - nameEN: string; - nameTH: string; - addressEN: string; - addressTH: string; - zipCode: string; - email: string; - telephoneNo: string; - longitude: string; - latitude: string; - subDistrictId?: string | null; - districtId?: string | null; - provinceId?: string | null; - headOfficeId?: string | null; - }, + async function create( + branch: BranchCreate, flow?: { sessionId: string; refTransactionId: string; transactionId: string; }, ) { - const res = await api.post('/branch', branch, { + const res = await api.post< + Branch & { qrCodeImageUrl: string; qrCodeImageUploadUrl: string } + >('/branch', branch, { headers: { 'X-Session-Id': flow?.sessionId, 'X-Rtid': flow?.refTransactionId, @@ -137,25 +95,9 @@ const useBranchStore = defineStore('api-branch', () => { return res.data; } - async function editBranchById( + async function editById( id: string, - branch: { - code?: string; - taxNo?: string; - nameEN?: string; - nameTH?: string; - addressEN?: string; - addressTH?: string; - zipCode?: string; - email?: string; - telephoneNo?: string; - longitude?: string; - latitude?: string; - subDistrictId?: string | null; - districtId?: string | null; - provinceId?: string | null; - headOfficeId?: string | null; - }, + branch: Partial, flow?: { sessionId: string; refTransactionId: string; @@ -175,7 +117,7 @@ const useBranchStore = defineStore('api-branch', () => { return res.data; } - async function deleteBranchById( + async function deleteById( id: string, flow?: { sessionId: string; @@ -272,12 +214,12 @@ const useBranchStore = defineStore('api-branch', () => { return { data, - fetchBranch, - fetchBranchById, + fetchList, + fetchById, - createBranch, - editBranchById, - deleteBranchById, + create, + editById, + deleteById, getUser, addUser, diff --git a/src/stores/branch/types.ts b/src/stores/branch/types.ts new file mode 100644 index 00000000..93359487 --- /dev/null +++ b/src/stores/branch/types.ts @@ -0,0 +1,48 @@ +import { District, Province, SubDistrict } from '../address'; +import { Status } from '../types'; + +export type Branch = { + subDistrict: SubDistrict | null; + district: District | null; + province: Province | null; + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; + status: Status; + headOfficeId: string | null; + isHeadOffice: boolean; + longitude: string; + latitude: string; + telephoneNo: string; + email: string; + zipCode: string; + subDistrictId: string | null; + districtId: string | null; + provinceId: string | null; + addressEN: string; + addressTH: string; + nameEN: string; + nameTH: string; + taxNo: string; + code: string; + id: string; +}; + +export type BranchCreate = { + code: string; + taxNo: string; + nameEN: string; + nameTH: string; + addressEN: string; + addressTH: string; + zipCode: string; + email: string; + telephoneNo: string; + longitude: string; + latitude: string; + subDistrictId?: string | null; + districtId?: string | null; + provinceId?: string | null; + headOfficeId?: string | null; +}; From bd2a3174ad7a14c477c7d0871548c3aaeff7172f Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 4 Apr 2024 13:23:23 +0700 Subject: [PATCH 3/3] feat: user endpoints --- src/stores/user/index.ts | 241 +++++++++++++++++++++++++++++++++++++++ src/stores/user/types.ts | 71 ++++++++++++ 2 files changed, 312 insertions(+) create mode 100644 src/stores/user/index.ts create mode 100644 src/stores/user/types.ts diff --git a/src/stores/user/index.ts b/src/stores/user/index.ts new file mode 100644 index 00000000..df626d7a --- /dev/null +++ b/src/stores/user/index.ts @@ -0,0 +1,241 @@ +import { ref } from 'vue'; +import { defineStore } from 'pinia'; +import { Pagination } from '../types'; +import { api } from 'src/boot/axios'; +import { User, UserCreate } from './types'; +import axios from 'axios'; + +const useUserStore = defineStore('api-user', () => { + const data = ref>(); + + async function fetchList( + opts?: { + page?: number; + pageSize?: number; + zipCode?: string; + 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?.zipCode) params.append('zipCode', opts.zipCode); + if (opts?.query) params.append('query', opts.query); + + const query = params.toString(); + + const res = await api.get>( + `/user${(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 fetchById( + id: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.get(`/user/${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; + if (res.status === 204) return null; + + return false; + } + + async function create( + data: UserCreate, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const { profileImage, ...payload } = data; + const res = await api.post( + '/user', + payload, + { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }, + ); + + await axios + .put(res.data.profileImageUploadUrl, profileImage, { + headers: { 'Content-Type': profileImage.type }, + onUploadProgress: (e) => console.log(e), + }) + .catch((e) => console.error(e)); + + if (!res) return false; + + return res.data; + } + + async function editById( + id: string, + data: Partial>, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.put(`/user/${id}`, data, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }); + + if (!res) return false; + + return res.data; + } + + async function deleteById( + id: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.delete(`/user/${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; + } + + async function getBranch( + userId: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.get(`/user/${userId}/branch`, { + 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 addBranch( + userId: string, + branchId: string | string[], + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.post( + `/user/${userId}/branch`, + { user: ([] as string[]).concat(branchId) }, + { + 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 removeBranch( + userId: string, + branchId: string | string[], + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.delete(`/user/${userId}/branch`, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + data: { user: ([] as string[]).concat(branchId) }, + }); + + if (!res) return false; + if (res.status === 200) return res.data; + + return false; + } + + return { + data, + fetchList, + fetchById, + + create, + editById, + deleteById, + + getBranch, + addBranch, + removeBranch, + }; +}); + +export default useUserStore; diff --git a/src/stores/user/types.ts b/src/stores/user/types.ts new file mode 100644 index 00000000..3b4efcac --- /dev/null +++ b/src/stores/user/types.ts @@ -0,0 +1,71 @@ +import { District, Province, SubDistrict } from '../address'; +import { Status } from '../types'; + +export type User = { + subDistrict: SubDistrict | null; + district: District | null; + province: Province | null; + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; + status: Status; + trainingPlace: string; + importNationality: string; + sourceNationality: string; + licenseExpireDate: string; + licenseIssueDate: string; + licenseNo: string; + discountCondition: string; + userRole: string; + userType: string; + retireDate: string; + startDate: string; + registrationNo: string; + telephoneNo: string; + email: string; + zipCode: string; + subDistrictId: string | null; + districtId: string | null; + provinceId: string | null; + addressEN: string; + addressTH: string; + lastNameEN: string; + lastNameTH: string; + firstNameEN: string; + firstNameTH: string; + code: string; + keycloakId: string; + id: string; + profileImageUrl: string; +}; + +export type UserCreate = { + provinceId?: string | null; + districtId?: string | null; + subDistrictId?: string | null; + telephoneNo: string; + email: string; + zipCode: string; + addressEN: string; + addressTH: string; + trainingPlace: string; + importNationality: string; + sourceNationality: string; + licenseExpireDate: Date; + licenseIssueDate: Date; + licenseNo: string; + discountCondition: string; + retireDate: Date; + startDate: Date; + registrationNo: string; + code: string; + lastNameEN: string; + lastNameTH: string; + firstNameEN: string; + firstNameTH: string; + userRole: string; + userType: string; + keycloakId: string; + profileImage: File; +};