jws-frontend/src/stores/customer/index.ts
puriphatt 62fc7055dd Squashed commit of the following:
commit eb6c7b164a9f182f8d1ce73cc5354866c6d6b10e
Author: puriphatt <puriphat@frappet.com>
Date:   Wed Sep 11 11:29:44 2024 +0700

    refactor: no img close to default on create

commit eae9eb26071cc2985624bb1c6ce551bf5eb6eb8b
Author: puriphatt <puriphat@frappet.com>
Date:   Wed Sep 11 11:04:04 2024 +0700

    refactor/feat: save => apply, disabled selected img, no img close to default

commit ccbf80fc53db3144873c049bd6dbd37b4e2e9ff3
Author: puriphatt <puriphat@frappet.com>
Date:   Wed Sep 11 09:31:32 2024 +0700

    fix(01): use submit function

commit 36b4f6ca15e5966f37dfefc9fdb744feec60dd27
Author: puriphatt <puriphat@frappet.com>
Date:   Tue Sep 10 17:45:19 2024 +0700

    fix: imgList error

commit bac0eaf3ab955672ae0c78d3295b4a839827c5f2
Author: puriphatt <puriphat@frappet.com>
Date:   Tue Sep 10 17:18:03 2024 +0700

    refactor(03): customer new upload img dialog

commit 9d7398e9613a738c33e265482cdb7d7bb250ea9f
Author: puriphatt <puriphat@frappet.com>
Date:   Tue Sep 10 15:40:39 2024 +0700

    refactor(02): new upload dialog

commit 8b91d43f41eae3ba2442f6c742d617c25ee180cb
Author: puriphatt <puriphat@frappet.com>
Date:   Tue Sep 10 15:25:21 2024 +0700

    refactor(01): new upload dialog, confirm remove, individual action

commit 61caf1919168bc5635568d7ca246574fdc43cd04
Author: puriphatt <puriphat@frappet.com>
Date:   Mon Sep 9 17:08:42 2024 +0700

    refactor(01): branch new img upload

commit e791b7316d001d839c8afb1950f7331c62d9e81a
Author: puriphatt <puriphat@frappet.com>
Date:   Mon Sep 9 17:08:42 2024 +0700

    refactor(02): personnel new img upload

commit af4d11312b9cb666338901efa9971117cb7738c4
Author: puriphatt <puriphat@frappet.com>
Date:   Mon Sep 9 17:08:42 2024 +0700

    feat(02): new image upload

commit e4d7afdb8c74d65a550644f2c60f70909d51d4a8
Author: puriphatt <puriphat@frappet.com>
Date:   Mon Sep 9 17:08:41 2024 +0700

    refactor: mock select image function

commit 5ab3f045b9c7d2c821920c12114da15eed09655a
Author: puriphatt <puriphat@frappet.com>
Date:   Mon Sep 9 17:08:41 2024 +0700

    refactor: mock new image preview
2024-09-11 16:43:41 +07:00

659 lines
16 KiB
TypeScript

import { ref } from 'vue';
import { defineStore } from 'pinia';
import { Pagination } from '../types';
import { api } from 'src/boot/axios';
import {
Customer,
CustomerCreate,
CustomerUpdate,
BranchAttachmentCreate,
BranchAttachment,
CustomerStats,
CustomerBranch,
CustomerBranchCreate,
CustomerType,
} from './types';
import axios from 'axios';
import useFlowStore from '../flow';
import { Employee } from '../employee/types';
import { baseUrl } from '../utils';
const useCustomerStore = defineStore('api-customer', () => {
const flowStore = useFlowStore();
const data = ref<Pagination<Customer[]>>();
async function setImage(id: string, image: File) {
await api.put(`/customer/${id}/image`, image, {
headers: { 'Content-Type': image?.type },
onUploadProgress: (e) => console.log(e),
});
}
async function fetchById(
customerId: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get<
Customer & { branch: CustomerBranch[]; registeredBranchId: string }
>(`/customer/${customerId}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (res && res.status === 200) {
return res.data;
}
return false;
}
async function fetchBranchEmployee(
idBranch: string,
opts?: {
zipCode?: string;
query?: string;
page?: number;
pageSize?: number;
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Pagination<Employee[]>>(
`/customer-branch/${idBranch}/employee${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (res.status === 200) {
return res;
}
}
async function fetchListCustomeBranch<
Options extends {
zipCode?: string;
customerId?: string;
includeCustomer?: boolean;
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
query?: string;
page?: number;
pageSize?: number;
},
Data extends Pagination<
(CustomerBranch &
(Options['includeCustomer'] extends true
? { customer: Customer }
: unknown))[]
>,
>(
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/customer-branch${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function fetchList<
Options extends {
page?: number;
pageSize?: number;
query?: string;
includeBranch?: boolean;
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
customerType?: CustomerType;
},
Data extends Pagination<
(Customer &
(Options['includeBranch'] extends true
? { branch: CustomerBranch[] }
: unknown))[]
>,
>(
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/customer${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function getStatsCustomer(flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
}) {
const res = await api.get<CustomerStats>('/customer/type-stats', {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
return res.data;
}
async function putAttachment(opts: {
branchId: string;
file: File;
filename?: string;
}) {
const res = await api.put(
`/customer-branch/${opts.branchId}/attachment/${opts.filename || opts.file.name}`,
opts.file,
{
headers: { 'X-Rtid': flowStore.rtid, 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
if (res.status >= 400) return false;
return true;
}
async function deleteAttachment(id: string, filename: string) {
const res = await api.delete(
`/customer-branch/${id}/attachment/${filename}`,
);
if (res.status >= 400) return false;
return true;
}
async function listAttachment(id: string) {
const res = await api.get<string[]>(`/customer-branch/${id}/attachment`, {
headers: { 'X-Rtid': flowStore.rtid },
});
if (res.status >= 400) return false;
return res.data;
}
async function getAttachment(id: string, filename: string, download = false) {
const url = `${baseUrl}/customer-branch/${id}/attachment/${filename}`;
const res = await api.get<string>(url);
if (download) {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
const a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
}
async function fetchImageListById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/customer/${id}/image`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'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 addImageList(file: File, customerId: string, name: string) {
await api
.put(`/customer/${customerId}/image/${name}`, file, {
headers: { 'Content-Type': file.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
return name;
}
async function deleteImageByName(
id: string,
name: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete(`/customer/${id}/image/${name}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
}
async function create(
data: CustomerCreate,
imgList: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const { code, customerBranch, image, ...payload } = data;
// const attachment = payload.customerBranch?.map((v) => v.file);
// if (payload.customerBranch?.length) {
// for (let i = 0; i < payload.customerBranch?.length; i++) {
// delete payload.customerBranch[i].file;
// }
// }
const res = await api.post<
Customer & {
branch: CustomerBranch[];
imageUrl: string;
imageUploadUrl: string;
}
>(
'/customer',
{ ...payload, selectedImage: imgList.selectedImage },
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
// await Promise.allSettled([
// ...res.data.branch.map(async (v, i) => {
// const fileList = attachment?.[i];
// if (fileList)
// return await addBranchAttachment(v.id, { file: fileList });
// }),
// image &&
// (await api
// .put(`/customer/${res.data.id}/image`, image, {
// headers: { 'Content-Type': image?.type },
// onUploadProgress: (e) => console.log(e),
// })
// .catch((e) => console.error(e)));
// ]);
if (imgList.list.length > 0 && res.data.id) {
for (let index = 0; index < imgList.list.length; index++) {
const imgFile = imgList.list[index].imgFile;
if (imgFile)
await addImageList(imgFile, res.data.id, imgList.list[index].name);
}
}
if (!res) return false;
return res.data;
}
async function editById(
id: string,
data: Partial<CustomerUpdate>,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const { customerBranch, image, ...payload } = data;
// const attachment = payload.customerBranch?.map((v) => v.file);
// payload.customerBranch = payload.customerBranch?.map((v) => {
// const { code: _code, ...rest } = v;
// return { ...rest };
// });
// if (payload.customerBranch?.length) {
// for (let i = 0; i < payload.customerBranch?.length; i++) {
// delete payload.customerBranch[i].file;
// }
// }
const res = await api.put<
Customer & {
branch: CustomerBranch[];
imageUrl: string;
imageUploadUrl: string;
}
>(`/customer/${id}`, payload, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
// await Promise.allSettled([
// ...res.data.branch.map(async (v, i) => {
// const fileList = attachment?.[i];
// if (fileList)
// return await addBranchAttachment(v.id, { file: fileList });
// }),
// 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 || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
async function getBranchById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get<CustomerBranch>(`/customer-branch/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
return res.data;
}
async function createBranch(
data: CustomerBranchCreate & { id?: string; customerId: string },
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const {
customerCode,
registerCompanyName,
statusSave,
code,
file,
...payload
} = data;
if (!!payload.citizenId) {
delete payload['registerDate'];
delete payload['registerName'];
delete payload['registerNameEN'];
delete payload['authorizedCapital'];
delete payload['legalPersonNo'];
} else {
delete payload['citizenId'];
}
const res = await api.post<CustomerBranch>('/customer-branch', payload, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
// if (file) await addBranchAttachment(res.data.id, { file });
return res.data;
}
async function editBranchById(
id: string,
data: Partial<CustomerBranchCreate & { id?: string; customerId: string }>,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const {
customerCode,
registerCompanyName,
statusSave,
code,
status,
file,
...payload
} = data;
console.log(payload);
if (!!payload.citizenId) {
delete payload['registerDate'];
delete payload['registerName'];
delete payload['registerNameEN'];
delete payload['authorizedCapital'];
delete payload['legalPersonNo'];
} else {
delete payload['citizenId'];
}
const res = await api.put<CustomerBranch>(
`/customer-branch/${id}`,
payload,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
// if (file) await addBranchAttachment(id, { file });
if (!res) return false;
return res.data;
}
async function deleteBranchById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete<Customer>(`/customer-branch/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
async function fetchListCustomeBranchById(
branchId: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/customer-branch/${branchId}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (res && res.status === 200) {
return res.data;
}
return false;
}
return {
data,
getStatsCustomer,
setImage,
fetchListCustomeBranch,
fetchById,
fetchList,
fetchImageListById,
addImageList,
deleteImageByName,
create,
editById,
deleteById,
createBranch,
getBranchById,
editBranchById,
deleteBranchById,
fetchListCustomeBranchById,
fetchBranchEmployee,
listAttachment,
getAttachment,
putAttachment,
deleteAttachment,
};
});
export default useCustomerStore;