diff --git a/src/stores/customer/index.ts b/src/stores/customer/index.ts index 0c9111eb..2232722f 100644 --- a/src/stores/customer/index.ts +++ b/src/stores/customer/index.ts @@ -8,6 +8,7 @@ import { CustomerUpdate, BranchAttachmentCreate, BranchAttachment, + CustomerBranch, } from './types'; import axios from 'axios'; @@ -61,12 +62,21 @@ const useCustomerStore = defineStore('api-customer', () => { transactionId: string; }, ) { - console.log(data); - const { 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 & { imageUrl: string; imageUploadUrl: string } + Customer & { + branch: CustomerBranch[]; + imageUrl: string; + imageUploadUrl: string; + } >('/customer', payload, { headers: { 'X-Session-Id': flow?.sessionId, @@ -75,12 +85,19 @@ const useCustomerStore = defineStore('api-customer', () => { }, }); - await axios - .put(res.data.imageUploadUrl, image, { - headers: { 'Content-Type': image.type }, - onUploadProgress: (e) => console.log(e), - }) - .catch((e) => console.error(e)); + await Promise.allSettled([ + ...res.data.branch.map(async (v, i) => { + const fileList = attachment?.[i]; + if (fileList) + return await addBranchAttachment(v.id, { file: fileList }); + }), + 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; @@ -97,8 +114,20 @@ const useCustomerStore = defineStore('api-customer', () => { }, ) { const { 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.put< - Customer & { imageUrl: string; imageUploadUrl: string } + Customer & { + branch: CustomerBranch[]; + imageUrl: string; + imageUploadUrl: string; + } >(`/customer/${id}`, payload, { headers: { 'X-Session-Id': flow?.sessionId, @@ -107,13 +136,20 @@ const useCustomerStore = defineStore('api-customer', () => { }, }); - if (image) - await axios - .put(res.data.imageUploadUrl, image, { - headers: { 'Content-Type': image.type }, - onUploadProgress: (e) => console.log(e), - }) - .catch((e) => console.error(e)); + 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; diff --git a/src/stores/customer/types.ts b/src/stores/customer/types.ts index 88398ff9..b6ea0de4 100644 --- a/src/stores/customer/types.ts +++ b/src/stores/customer/types.ts @@ -1,3 +1,4 @@ +import { District, Province, SubDistrict } from '../address'; import { Status } from '../types'; export type CustomerType = 'CORP' | 'PERS'; @@ -11,9 +12,47 @@ export type Customer = { customerNameEN: string; status: Status; createdBy: string | null; - createdAt: Date; + createdAt: string; updateBy: string | null; - updatedAt: Date; + updatedAt: string; +}; + +export type CustomerBranch = { + id: string; + branchNo: string; + legalPersonNo: string; + name: string; + nameEN: string; + customerId: string; + taxNo: string; + registerName: string; + registerDate: string; + authorizedCapital: string; + address: string; + addressEN: string; + provinceId: string | null; + districtId: string | null; + subDistrictId: string | null; + zipCode: string; + email: string; + telephoneNo: string; + employmentOffice: string; + bussinessType: string; + bussinessTypeEN: string; + jobPosition: string; + jobPositionEN: string; + jobDescription: string; + saleEmployee: string; + payDate: string; + wageDate: string; + status: Status; + createdBy: string | null; + createdAt: string; + updateBy: string | null; + updatedAt: string; + province: Province; + district: District; + subDistrict: SubDistrict; }; export type CustomerBranchCreate = { @@ -42,6 +81,7 @@ export type CustomerBranchCreate = { saleEmployee: string; payDate: string; wageDate: string; + file?: File[]; }; export type CustomerCreate = { @@ -54,32 +94,6 @@ export type CustomerCreate = { image: File; }; -export type CustomerBranchUpdate = { - id: string; - status?: 'ACTIVE' | 'INACTIVE'; - - legalPersonNo?: string; - - taxNo?: string; - name?: string; - nameEN?: string; - addressEN?: string; - address?: string; - zipCode?: string; - email?: string; - telephoneNo?: string; - longitude?: string; - latitude?: string; - - registerName?: string; - registerDate?: Date; - authorizedCapital?: string; - - subDistrictId?: string | null; - districtId?: string | null; - provinceId?: string | null; -}; - export type CustomerUpdate = { status?: 'ACTIVE' | 'INACTIVE'; customerType?: CustomerType;