jws-frontend/src/stores/request-list/index.ts
2025-01-22 14:03:14 +07:00

302 lines
7.2 KiB
TypeScript

import { defineStore } from 'pinia';
import { ref } from 'vue';
import {
RequestData,
RequestDataStatus,
RequestWork,
RequestWorkStatus,
Step,
} from './types';
import { api } from 'src/boot/axios';
import { PaginationResult } from 'src/types';
import {
EmployeePassportPayload,
EmployeeVisaPayload,
} from 'stores/employee/types';
import { manageAttachment, manageFile, manageMeta } from '../utils';
export const useRequestList = defineStore('request-list', () => {
const data = ref<RequestData[]>([]);
const page = ref<number>(1);
const pageMax = ref<number>(1);
const pageSize = ref<number>(30);
const stats = ref<Record<RequestDataStatus, number>>({
[RequestDataStatus.Pending]: 0,
[RequestDataStatus.Ready]: 0,
[RequestDataStatus.InProgress]: 0,
[RequestDataStatus.Completed]: 0,
[RequestDataStatus.Canceled]: 0,
});
type TypeFile =
| 'passport'
| 'visa'
| 'citizen'
| 'house-registration'
| 'commercial-registration'
| 'vat-registration'
| 'power-of-attorney';
async function uploadAttachmentRequest(opt: {
id: string;
type: 'customer' | 'employee';
group: string;
file: File;
form?: EmployeePassportPayload | EmployeeVisaPayload;
name?: string;
}) {
const base = { customer: 'customer-branch', employee: 'employee' }[
opt.type
];
const attachmentManag = manageAttachment(api, base);
const metaManager = manageMeta<TypeFile>(api, base);
let res;
const group = [
'passport',
'visa',
'citizen',
'house-registration',
'commercial-registration',
'vat-registration',
'power-of-attorney',
];
console.log(opt.group);
if (group.includes(opt.group)) {
res = await metaManager.postMeta({
group: opt.group as TypeFile,
parentId: opt.id,
meta: opt.form,
file: opt.file,
});
} else {
res = await attachmentManag.putAttachment({
parentId: opt.id,
name: opt.name || '',
file: opt.file,
});
}
return res;
}
async function viewAttachmentRequest(opt: {
id: string;
name: string;
type: 'customer' | 'employee';
group: string;
download?: boolean;
}) {
const base = { customer: 'customer-branch', employee: 'employee' }[
opt.type
];
const attachmentManag = manageAttachment(api, base);
const fileManager = manageFile<TypeFile>(api, base);
let res;
const group = [
'passport',
'visa',
'citizen',
'house-registration',
'commercial-registration',
'vat-registration',
'power-of-attorney',
];
if (group.includes(opt.group)) {
res = await fileManager.getFile({
parentId: opt.id,
group: opt.group as TypeFile,
fileId: opt.name,
download: opt.download,
});
}
if (!group.includes(opt.group)) {
res = await attachmentManag.getAttachment({
parentId: opt.id,
name: opt.name,
download: opt.download,
});
}
if (res) return res;
}
async function getAttachmentRequest(
id: string,
type: 'customer' | 'employee',
) {
const base = { customer: 'customer-branch', employee: 'employee' }[type];
const attachmentManag = manageAttachment(api, base);
const fileManager = manageFile<TypeFile>(api, base);
const resFiles: Partial<Record<string, any>> = {};
if (type === 'employee') {
const resPassport = await fileManager.listFile({
group: 'passport',
parentId: id,
});
const resVisa = await fileManager.listFile({
group: 'visa',
parentId: id,
});
resFiles['passport'] = { ...resPassport };
resFiles['visa'] = { ...resVisa };
} else if (type === 'customer') {
const groups = [
'citizen',
'house-registration',
'commercial-registration',
'vat-registration',
'power-of-attorney',
] as const;
for (const group of groups) {
const res = await fileManager.listFile({
group,
parentId: id,
});
resFiles[group] = { ...res };
}
}
const resAttachment = await attachmentManag.listAttachment({
parentId: id,
});
if (resAttachment)
for (const item of resAttachment) {
const [key] = item.split('-').map((s) => s.trim());
if (key) {
if (!resFiles[key]) {
resFiles[key] = [];
}
if (!resFiles[key].includes(item)) {
resFiles[key].push(item);
}
}
}
return resFiles;
}
async function getRequestDataStats() {
const res = await api.get<typeof stats.value>('/request-data/stats');
if (res.status < 400) return res.data;
return null;
}
async function getRequestData(id: string) {
const res = await api.get<RequestData>(`/request-data/${id}`);
if (res.status < 400) return res.data;
return null;
}
async function getRequestDataList(params?: {
query?: string;
page?: number;
pageSize?: number;
requestDataStatus?: RequestDataStatus;
responsibleOnly?: boolean;
quotationId?: string;
}) {
const res = await api.get<PaginationResult<RequestData>>('/request-data', {
params,
});
if (res.status < 400) return res.data;
return null;
}
async function getRequestWorkList(params?: {
requestDataId?: string;
query?: string;
page?: number;
pageSize?: number;
workStatus?: RequestWorkStatus;
readyToTask?: boolean;
quotationId?: string;
cancelOnly?: boolean;
}) {
const res = await api.get<PaginationResult<RequestWork>>('/request-work', {
params,
});
if (res.status < 400) return res.data;
return null;
}
async function editRequestWork(body: Partial<RequestWork>) {
const res = await api.put(`/request-work/${body.id}`, {
...body,
id: undefined,
});
if (res.status < 400) return res.data;
return null;
}
async function editStatusRequestWork(body: Step, successAll?: boolean) {
const res = await api.put<Step>(
`/request-work/${body.requestWorkId}/step-status/${body.step}`,
{
customerDuty: body.customerDuty,
customerDutyCost: body.customerDutyCost,
companyDuty: body.companyDuty,
companyDutyCost: body.companyDutyCost,
individualDuty: body.individualDuty,
individualDutyCost: body.individualDutyCost,
responsibleUserLocal: body.responsibleUserLocal,
responsibleUserId: body.responsibleUserId,
attributes: body.attributes,
workStatus: body.workStatus,
requestWorkId: undefined,
step: undefined,
},
{ params: { successAll } },
);
if (res.status < 400) return res.data;
return null;
}
async function cancelRequest(id: string) {
const res = await api.post(`/request-data/${id}/cancel`);
if (res.status < 400) return true;
return false;
}
return {
data,
page,
pageMax,
pageSize,
stats,
viewAttachmentRequest,
getAttachmentRequest,
uploadAttachmentRequest,
getRequestDataStats,
getRequestData,
getRequestDataList,
getRequestWorkList,
editRequestWork,
editStatusRequestWork,
cancelRequest,
};
});
export * from './types.ts';