form 16 renaming logs added

This commit is contained in:
Aaditya Jaiswal 2026-04-16 12:58:40 +05:30
parent 44a19bbfea
commit 1a02781731
3 changed files with 71 additions and 1 deletions

Binary file not shown.

View File

@ -745,6 +745,13 @@ async function renameForm16SubmissionPdfAfterCreditNote(params: {
oldRelativePath: string;
}): Promise<void> {
const { submissionId, requestId, oldRelativePath } = params;
logger.info('[Form16] PDF rename flow start', {
submissionId,
requestId,
oldRelativePath,
nodeEnv: process.env.NODE_ENV,
gcpBucket: process.env.GCP_BUCKET_NAME || null,
});
const oldPathNorm = String(oldRelativePath || '').replace(/\\/g, '/').trim();
if (!oldPathNorm || oldPathNorm.includes('..') || !oldPathNorm.startsWith('requests/')) {
logger.warn('[Form16] Skip PDF rename: invalid storage path', { oldPathNorm });
@ -752,16 +759,39 @@ async function renameForm16SubmissionPdfAfterCreditNote(params: {
}
const sub = await Form16aSubmission.findByPk(submissionId);
if (!sub) return;
if (!sub) {
logger.warn('[Form16] Skip PDF rename: submission not found', { submissionId, requestId });
return;
}
const newFileName = buildForm16CreditNoteSuccessPdfFileName(sub);
logger.info('[Form16] PDF rename target name computed', {
submissionId,
requestId,
newFileName,
tanNumber: (sub as any).tanNumber || null,
financialYear: (sub as any).financialYear || null,
quarter: (sub as any).quarter || null,
form16aNumber: (sub as any).form16aNumber || null,
});
try {
const result = await gcsStorageService.renameRequestDocumentFile({
oldRelativePath: oldPathNorm,
newFileName,
});
logger.info('[Form16] Storage rename success', {
submissionId,
requestId,
oldPathNorm,
renamedFilePath: result.filePath,
renamedStorageUrlPrefix: String(result.storageUrl || '').slice(0, 120),
});
await sub.update({ documentUrl: result.storageUrl });
logger.info('[Form16] Submission documentUrl updated after rename', {
submissionId,
requestId,
});
const doc = await Document.findOne({
where: { requestId, filePath: oldPathNorm },
@ -776,6 +806,13 @@ async function renameForm16SubmissionPdfAfterCreditNote(params: {
filePath: fp,
storageUrl: su,
});
logger.info('[Form16] Document metadata updated after rename', {
requestId,
submissionId,
documentId: (doc as any).id || null,
oldPathNorm,
newPath: fp,
});
} else {
logger.warn('[Form16] PDF renamed; documents row not found for path', { requestId, oldPathNorm });
}
@ -1331,6 +1368,12 @@ export async function createSubmission(
);
// When credit note is issued (completed), set workflow status to CLOSED so the request appears on Closed requests page
if (validationStatus === 'success' && creditNoteNumber) {
logger.info('[Form16] Success path reached; triggering PDF rename', {
requestId,
submissionId: submission.id,
creditNoteNumber,
uploadFilePath,
});
const workflow = await WorkflowRequest.findOne({ where: { requestId }, attributes: ['requestId', 'status'] });
if (workflow && (workflow as any).status !== WorkflowStatus.CLOSED) {
await workflow.update({ status: WorkflowStatus.CLOSED });
@ -1341,6 +1384,17 @@ export async function createSubmission(
requestId,
oldRelativePath: uploadFilePath.replace(/\\/g, '/'),
});
logger.info('[Form16] PDF rename call completed', {
requestId,
submissionId: submission.id,
});
} else {
logger.info('[Form16] PDF rename not triggered (submission not successful)', {
requestId,
submissionId: submission.id,
validationStatus,
creditNoteNumber: creditNoteNumber || null,
});
}
} catch (err: any) {
logger.error(

View File

@ -355,6 +355,14 @@ class GCSStorageService {
}): Promise<{ storageUrl: string; filePath: string; fileName: string }> {
const { oldRelativePath } = options;
let newFileName = path.basename(String(options.newFileName || '').trim());
logger.info('[GCS] renameRequestDocumentFile called', {
oldRelativePath,
requestedNewFileName: options.newFileName,
sanitizedNewFileName: newFileName,
nodeEnv: process.env.NODE_ENV,
bucket: this.bucketName || null,
useGoogleSecretManager: process.env.USE_GOOGLE_SECRET_MANAGER || null,
});
if (!newFileName || newFileName === '.' || newFileName === '..') {
throw new Error('Invalid new file name');
}
@ -367,6 +375,7 @@ class GCSStorageService {
const newRelativePath = `${dir}/${newFileName}`;
if (!this.isConfigured()) {
logger.info('[GCS] renameRequestDocumentFile using local storage mode');
const fullOld = path.join(UPLOAD_DIR, ...oldNorm.split('/').filter(Boolean));
const fullNew = path.join(UPLOAD_DIR, ...newRelativePath.split('/').filter(Boolean));
if (!fs.existsSync(fullOld)) {
@ -387,12 +396,19 @@ class GCSStorageService {
const bucket = this.storage.bucket(this.bucketName);
const oldFile = bucket.file(oldNorm);
const [exists] = await oldFile.exists();
logger.info('[GCS] renameRequestDocumentFile GCS existence check', {
oldNorm,
exists,
newRelativePath,
});
if (!exists) {
throw new Error(`GCS file not found: ${oldNorm}`);
}
const newFile = bucket.file(newRelativePath);
await oldFile.copy(newFile);
logger.info('[GCS] renameRequestDocumentFile copy success', { from: oldNorm, to: newRelativePath });
await oldFile.delete();
logger.info('[GCS] renameRequestDocumentFile delete old success', { oldNorm });
let publicUrl: string;
try {