fares 9 сар өмнө
parent
commit
670f961023

+ 30 - 3
lib/handlers/file_upload_api.dart

@@ -1,12 +1,12 @@
 import 'dart:io';
 import 'package:file_upload_processor/handlers/base_api.dart';
-import 'package:intl/intl.dart';
 import 'package:mime/mime.dart';
 import 'package:shelf/shelf.dart' as shelf;
 import 'package:path/path.dart' as path;
 import 'package:http_parser/http_parser.dart';
 import 'package:archive/archive.dart';
 import 'package:supabase/supabase.dart';
+import 'package:intl/intl.dart';
 
 class FileUploadApi extends BaseApi {
   FileUploadApi(shelf.Request request) : super(request);
@@ -450,7 +450,11 @@ class FileProcess {
     final allmapsToInsert = await parseCsv();
     final scopeName = scopes[filename] ?? "";
     final scopesInNew = allmapsToInsert
-        .fold(<String>{}, (t, e) => t..add(e[scopeName] ?? "")).toList();
+        .fold(<String>{}, (t, e) => t..add(e[scopeName] ?? "")).toList()
+      ..sort((a, b) => _parseDate(a).compareTo(_parseDate(b)));
+
+    //special export prgpn list of tlcs imported in new file
+    Set<String> tlcs = {};
 
     for (var scopeInNew in scopesInNew) {
       final mapsToInsert =
@@ -475,7 +479,17 @@ class FileProcess {
 
       //special export prgpn
       if (filename == "exportPGRGPN.txt" ||
-          filename == "ExportPGRGPNmois.txt") {}
+          filename == "ExportPGRGPNmois.txt") {
+        //if there is line insert with tlc and date
+        for (final data in mapsToInsert) {
+          tlcs..add(data["tlc"] ?? "");
+        }
+        final indexRemovedToMaintain = indexToRemove
+            .where((e) => !tlcs.contains(oldComparable[e]["tlc"]))
+            .toList();
+        indexToMaintain.addAll(indexRemovedToMaintain);
+        indexToRemove.removeWhere((e) => indexRemovedToMaintain.contains(e));
+      }
 
       try {
         if (!donttouchdb)
@@ -619,6 +633,19 @@ class FileProcess {
   }
 }
 
+dynamic _parseDate(String date) {
+  final parts = date.split('/');
+  if (parts.length == 3) {
+    return DateTime(
+      int.parse(parts[2]), // year
+      int.parse(parts[1]), // month
+      int.parse(parts[0]), // day
+    );
+  } else {
+    return date;
+  }
+}
+
 extension CompareIterables<T> on Iterable<T> {
   /// Compares this iterable with another iterable and returns a map containing:
   /// - 'added': Items that are in the other iterable but not in this one.