refactor: using stream instead of temp files in chatwoot and repository services

This commit refactors the code in the `chatwoot.service.ts` and `repository.service.ts` files to use streams instead of temporary files. This change reduces the number of disk operations, making the application faster and more efficient.

In the `chatwoot.service.ts` file, the `createReadStream`, `unlinkSync`, and `writeFileSync` functions have been replaced with the `Readable` stream. The `sendData` method has been updated to accept a `fileStream` and `fileName` instead of a file path. The `processImage` method has been refactored to use a stream instead of writing the image to a file.

In the `repository.service.ts` file, the `initStoreFolders` method has been removed, as it is no longer needed.

These changes improve the maintainability of the codebase by reducing the number of disk operations and simplifying the code. This refactoring also makes the application more efficient and faster, as it reduces the number of disk operations.

Motivation:
The motivation behind this refactoring is to improve the performance and efficiency of the application by reducing the number of disk operations.

Impact:
This refactoring reduces the number of disk operations, making the application faster and more efficient. It also simplifies the codebase and improves its maintainability.
This commit is contained in:
Davidson Gomes
2024-07-13 08:10:36 -03:00
parent d3ab402d94
commit e3a97d0071
2 changed files with 39 additions and 50 deletions

View File

@@ -1,6 +1,4 @@
import { PrismaClient } from '@prisma/client';
import fs from 'fs';
import { join } from 'path';
import { ConfigService } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
@@ -15,29 +13,10 @@ export class Query<T> {
export class PrismaRepository extends PrismaClient {
constructor(private readonly configService: ConfigService) {
super();
this.initStoreFolders();
}
private readonly logger = new Logger(PrismaRepository.name);
private async initStoreFolders() {
try {
const storePath = join(process.cwd(), 'store');
this.logger.verbose('creating store path: ' + storePath);
const tempDir = join(storePath, 'temp');
if (!fs.existsSync(tempDir)) {
this.logger.verbose('creating temp dir: ' + tempDir);
fs.mkdirSync(tempDir, { recursive: true });
}
} catch (error) {
this.logger.error(error);
}
}
public async onModuleInit() {
await this.$connect();
this.logger.info('Repository:Prisma - ON');