✨ Feat: Invoice viewing, JoFotara UBL refinements, delete functionality, and tax rate validation
This commit is contained in:
@@ -10,9 +10,13 @@ import {
|
||||
ForbiddenException,
|
||||
Inject,
|
||||
forwardRef,
|
||||
InternalServerErrorException,
|
||||
StreamableFile,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { InjectQueue } from '@nestjs/bull';
|
||||
import { Queue } from 'bull';
|
||||
import { Invoice, InvoiceStatus } from './entities/invoice.entity';
|
||||
@@ -157,4 +161,39 @@ export class InvoicesService {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* حذف الفاتورة والملف التابع لها
|
||||
*/
|
||||
async remove(tenantId: string, id: string): Promise<void> {
|
||||
const invoice = await this.findOne(tenantId, id);
|
||||
|
||||
// 1. Delete file if exists
|
||||
if (invoice.original_file_path) {
|
||||
await this.localStorageService.deleteFile(invoice.original_file_path);
|
||||
}
|
||||
|
||||
// 2. Delete from DB (lines will be deleted via CASCADE)
|
||||
await this.invoiceRepository.delete(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* الحصول على الملف كـ Stream
|
||||
*/
|
||||
async getFile(tenantId: string, id: string): Promise<StreamableFile> {
|
||||
const invoice = await this.findOne(tenantId, id);
|
||||
if (!invoice.original_file_path) {
|
||||
throw new NotFoundException('Invoice file not found');
|
||||
}
|
||||
|
||||
const storageRoot = this.localStorageService['storageRoot']; // Accessing private for path resolve
|
||||
const fullPath = path.join(storageRoot, invoice.original_file_path);
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
throw new NotFoundException('File does not exist on disk');
|
||||
}
|
||||
|
||||
const file = fs.createReadStream(fullPath);
|
||||
return new StreamableFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user