feat: 1. 完善起草合同页面的逻辑交互,对接minio的接口操作
This commit is contained in:
@@ -80,134 +80,3 @@ export async function copyMinioFile(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建起草合同记录
|
||||
* @param request 创建请求
|
||||
* @param userId 用户ID
|
||||
* @param draftFilePath 可选:草稿文件路径(如果不提供,使用模板路径)
|
||||
* @returns 创建的记录
|
||||
*
|
||||
* 使用场景:
|
||||
* 1. 不传 draftFilePath:直接使用模板文件路径,在原模板上编辑
|
||||
* 2. 传 draftFilePath:使用复制后的文件路径(由文件复制接口提供)
|
||||
*/
|
||||
export async function createDraftContract(
|
||||
request: CreateDraftRequest,
|
||||
userId: number,
|
||||
draftFilePath?: string,
|
||||
jwt?: string
|
||||
): Promise<DraftedContract> {
|
||||
try {
|
||||
// 1. 查询模板信息
|
||||
const templateResponse = await postgrestGet('contract_templates', {
|
||||
select: 'id,file_path',
|
||||
filter: { id: `eq.${request.templateId}` },
|
||||
token: jwt
|
||||
});
|
||||
|
||||
if (!templateResponse.data || (Array.isArray(templateResponse.data) && templateResponse.data.length === 0)) {
|
||||
throw new Error('模板不存在');
|
||||
}
|
||||
|
||||
const template = Array.isArray(templateResponse.data) ? templateResponse.data[0] : templateResponse.data;
|
||||
|
||||
// 2. 确定使用的文件路径
|
||||
// 如果没有提供草稿路径,直接使用模板路径(适合直接编辑模板的场景)
|
||||
// 如果提供了草稿路径,使用复制后的文件路径
|
||||
const finalFilePath = draftFilePath || template.file_path;
|
||||
|
||||
console.log('[Draft Service] 创建草稿:', {
|
||||
templateId: request.templateId,
|
||||
templatePath: template.file_path,
|
||||
draftPath: draftFilePath,
|
||||
finalPath: finalFilePath,
|
||||
mode: draftFilePath ? '使用复制文件' : '直接使用模板文件'
|
||||
});
|
||||
|
||||
// 3. 创建草稿记录
|
||||
const insertResponse = await postgrestPost('drafted_contracts', {
|
||||
body: {
|
||||
template_id: request.templateId,
|
||||
file_path: finalFilePath,
|
||||
title: request.title,
|
||||
placeholder_values: {},
|
||||
status: 'draft',
|
||||
created_by: userId
|
||||
},
|
||||
select: '*',
|
||||
token: jwt
|
||||
});
|
||||
|
||||
if (!insertResponse.data) {
|
||||
throw new Error('创建草稿记录失败');
|
||||
}
|
||||
|
||||
const draft = Array.isArray(insertResponse.data) ? insertResponse.data[0] : insertResponse.data;
|
||||
return draft as DraftedContract;
|
||||
} catch (error) {
|
||||
console.error('[Draft Service] 创建草稿失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除草稿记录
|
||||
* @param draftId 草稿ID
|
||||
* @param userId 用户ID
|
||||
* @param jwt JWT token
|
||||
*/
|
||||
export async function deleteDraft(
|
||||
draftId: number,
|
||||
userId: number,
|
||||
jwt?: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
const response = await postgrestDelete('drafted_contracts', {
|
||||
filter: {
|
||||
id: `eq.${draftId}`,
|
||||
created_by: `eq.${userId}` // 确保只能删除自己的草稿
|
||||
},
|
||||
token: jwt
|
||||
});
|
||||
|
||||
console.log('[Draft Service] 草稿已删除:', draftId);
|
||||
} catch (error) {
|
||||
console.error('[Draft Service] 删除草稿失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取草稿详情
|
||||
* @param draftId 草稿ID
|
||||
* @param userId 用户ID
|
||||
* @returns 草稿记录
|
||||
*/
|
||||
export async function getDraftById(
|
||||
draftId: number,
|
||||
userId: number,
|
||||
jwt?: string
|
||||
): Promise<DraftedContract | null> {
|
||||
try {
|
||||
const response = await postgrestGet('drafted_contracts', {
|
||||
select: '*',
|
||||
filter: {
|
||||
id: `eq.${draftId}`,
|
||||
created_by: `eq.${userId}`
|
||||
},
|
||||
token: jwt
|
||||
});
|
||||
|
||||
if (!response.data || (Array.isArray(response.data) && response.data.length === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const draft = Array.isArray(response.data) ? response.data[0] : response.data;
|
||||
return draft as DraftedContract;
|
||||
} catch (error) {
|
||||
console.error('[Draft Service] 获取草稿失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user