implement contract booking functionality and MinIO setup

This commit is contained in:
marshal
2026-05-23 09:31:52 +03:00
parent f9a9d9923c
commit 82c6b1284e
16 changed files with 1231 additions and 40 deletions

View File

@@ -0,0 +1,2 @@
export * from "./minio.module";
export * from "./minio.service";

View File

@@ -0,0 +1,10 @@
import { registerAs } from "@nestjs/config";
export const minioConfig = registerAs("minio", () => ({
endPoint: process.env.MINIO_ENDPOINT || "minio-dev.smart.aaca.gov.et",
port: parseInt(process.env.MINIO_PORT || "443", 10),
useSSL: process.env.MINIO_USE_SSL !== "false",
accessKey: process.env.MINIO_ACCESS_KEY || "",
secretKey: process.env.MINIO_SECRET_KEY || "",
bucket: process.env.MINIO_BUCKET || "fhc",
}));

View File

@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { minioConfig } from "./minio.config";
import { MinioService } from "./minio.service";
@Module({
imports: [ConfigModule.forFeature(minioConfig)],
providers: [MinioService],
exports: [MinioService],
})
export class MinioModule {}

View File

@@ -0,0 +1,65 @@
import { Inject, Injectable, Logger } from "@nestjs/common";
import { ConfigType } from "@nestjs/config";
import { Client } from "minio";
import { minioConfig } from "./minio.config";
@Injectable()
export class MinioService {
private readonly client: Client;
private readonly logger = new Logger(MinioService.name);
private readonly bucket: string;
constructor(
@Inject(minioConfig.KEY)
private readonly config: ConfigType<typeof minioConfig>,
) {
console.log('[MinioService] Configuration loaded:', {
endPoint: config.endPoint,
port: config.port,
useSSL: config.useSSL,
accessKey: config.accessKey,
secretKey: config.secretKey ? '***HIDDEN***' : 'EMPTY',
bucket: config.bucket,
});
this.bucket = config.bucket;
this.client = new Client({
endPoint: config.endPoint,
port: config.port,
useSSL: config.useSSL,
accessKey: config.accessKey,
secretKey: config.secretKey,
});
}
async uploadFile(
objectName: string,
buffer: Buffer,
contentType: string,
): Promise<string> {
try {
await this.client.putObject(this.bucket, objectName, buffer, buffer.length, {
"Content-Type": contentType,
});
this.logger.log(`File uploaded successfully: ${objectName}`);
return this.getPublicUrl(objectName);
} catch (error) {
this.logger.error(`Failed to upload file ${objectName}:`, error);
throw error;
}
}
getPublicUrl(objectName: string): string {
const protocol = this.config.useSSL ? "https" : "http";
return `${protocol}://${this.config.endPoint}:${this.config.port}/${this.bucket}/${objectName}`;
}
async deleteFile(objectName: string): Promise<void> {
try {
await this.client.removeObject(this.bucket, objectName);
this.logger.log(`File deleted successfully: ${objectName}`);
} catch (error) {
this.logger.error(`Failed to delete file ${objectName}:`, error);
throw error;
}
}
}