From 4772a6c90b2bfa4abec7c8549b5f0f3f14997889 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 6 Jun 2024 14:23:30 +0700 Subject: [PATCH] feat: api endpoints employee --- src/stores/employee/index.ts | 147 +++++++++++++++++++++++++++++++++++ src/stores/employee/types.ts | 91 ++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 src/stores/employee/index.ts create mode 100644 src/stores/employee/types.ts diff --git a/src/stores/employee/index.ts b/src/stores/employee/index.ts new file mode 100644 index 00000000..e9647e11 --- /dev/null +++ b/src/stores/employee/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 { Employee, EmployeeCreate, EmployeeUpdate } from './types'; +import axios from 'axios'; + +const useCustomerStore = defineStore('api-employee', () => { + 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: EmployeeCreate, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const { image, ...payload } = data; + + const res = await api.post< + Employee & { 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: EmployeeUpdate, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const { image, ...payload } = data; + const res = await api.put< + Employee & { 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/employee/types.ts b/src/stores/employee/types.ts new file mode 100644 index 00000000..b594e7da --- /dev/null +++ b/src/stores/employee/types.ts @@ -0,0 +1,91 @@ +import { District, Province, SubDistrict } from '../address'; +import { Status } from '../types'; + +export type Employee = { + provinceId: string | null; + districtId: string | null; + subDistrictId: string | null; + subDistrict: SubDistrict | null; + district: District | null; + province: Province | null; + arrivalCardNo: string; + arrivalBarricade: string; + telephoneNo: string; + email: string; + zipCode: string; + address: string; + addressEN: string; + lastNameEN: string; + lastName: string; + firstNameEN: string; + firstName: string; + nationality: string; + gender: string; + dateOfBirth: string; + nrcNo: string; + code: string; + status: Status; +}; + +export type EmployeeCreate = { + customerBranchId: string; + + status?: Status; + + code: string; + nrcNo: string; + + dateOfBirth: Date; + gender: string; + nationality: string; + + firstName: string; + firstNameEN: string; + lastName: string; + lastNameEN: string; + + addressEN: string; + address: string; + zipCode: string; + email: string; + telephoneNo: string; + + arrivalBarricade: string; + arrivalCardNo: string; + + subDistrictId?: string | null; + districtId?: string | null; + provinceId?: string | null; + image: File; +}; + +export type EmployeeUpdate = { + customerBranchId?: string; + status?: 'ACTIVE' | 'INACTIVE'; + + code?: string; + nrcNo?: string; + + dateOfBirth?: Date; + gender?: string; + nationality?: string; + + firstName?: string; + firstNameEN?: string; + lastName?: string; + lastNameEN?: string; + + addressEN?: string; + address?: string; + zipCode?: string; + email?: string; + telephoneNo?: string; + + arrivalBarricade?: string; + arrivalCardNo?: string; + + subDistrictId?: string | null; + districtId?: string | null; + provinceId?: string | null; + image?: File; +};