Dr-Swopt před 4 dny
rodič
revize
95bec7a417

+ 11 - 1
src/palm-oil/palm-oil.controller.ts

@@ -9,7 +9,7 @@
  * the Angular history tab and do not conflict with the Hoarding architecture.
  * the Angular history tab and do not conflict with the Hoarding architecture.
  */
  */
 
 
-import { Controller, Get, Res, Param } from '@nestjs/common';
+import { Controller, Get, Delete, Res, Param } from '@nestjs/common';
 import { PalmOilService } from './palm-oil.service';
 import { PalmOilService } from './palm-oil.service';
 import { Response } from 'express';
 import { Response } from 'express';
 import * as fs from 'fs';
 import * as fs from 'fs';
@@ -23,6 +23,16 @@ export class PalmOilController {
     return this.palmOilService.getHistory();
     return this.palmOilService.getHistory();
   }
   }
 
 
+  @Delete('history/:archiveId')
+  async deleteRecord(@Param('archiveId') archiveId: string) {
+    return this.palmOilService.deleteRecord(archiveId);
+  }
+
+  @Delete('history')
+  async clearAllHistory() {
+    return this.palmOilService.clearAllHistory();
+  }
+
   @Get('archive/:id')
   @Get('archive/:id')
   async getArchivedImage(@Param('id') id: string, @Res() res: Response) {
   async getArchivedImage(@Param('id') id: string, @Res() res: Response) {
     const record = await this.palmOilService.getRecordByArchiveId(id);
     const record = await this.palmOilService.getRecordByArchiveId(id);

+ 27 - 0
src/palm-oil/palm-oil.service.ts

@@ -114,4 +114,31 @@ export class PalmOilService {
   async getRecordByArchiveId(archiveId: string): Promise<History | null> {
   async getRecordByArchiveId(archiveId: string): Promise<History | null> {
     return this.historyRepository.findOne({ where: { archive_id: archiveId } });
     return this.historyRepository.findOne({ where: { archive_id: archiveId } });
   }
   }
+
+  async deleteRecord(archiveId: string): Promise<{ deleted: boolean }> {
+    const record = await this.historyRepository.findOne({ where: { archive_id: archiveId } });
+    if (!record) return { deleted: false };
+
+    // Remove archived image from disk
+    if (record.image_path && fs.existsSync(record.image_path)) {
+      fs.unlinkSync(record.image_path);
+    }
+
+    await this.historyRepository.delete({ archive_id: archiveId });
+    return { deleted: true };
+  }
+
+  async clearAllHistory(): Promise<{ deleted: number }> {
+    const records = await this.historyRepository.find();
+
+    // Remove every archived image from disk
+    for (const record of records) {
+      if (record.image_path && fs.existsSync(record.image_path)) {
+        fs.unlinkSync(record.image_path);
+      }
+    }
+
+    await this.historyRepository.clear();
+    return { deleted: records.length };
+  }
 }
 }

+ 1 - 1
src/surveillance/surveillance.gateway.ts

@@ -27,7 +27,7 @@ export class SurveillanceGateway
   implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, OnModuleInit
   implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect, OnModuleInit
 {
 {
   @WebSocketServer()
   @WebSocketServer()
-  private server: Server;
+  private server!: Server;
 
 
   private readonly logger = new Logger(SurveillanceGateway.name);
   private readonly logger = new Logger(SurveillanceGateway.name);