From c6dda916c532433c873e118092f32824a25a87c4 Mon Sep 17 00:00:00 2001
From: zetavg <mail@zeta.vg>
Date: Wed, 10 Jan 2024 20:22:12 +0800
Subject: [PATCH] sort untagged items by updated time

---
 .../screens/RFIDUntaggedItemsScreen.tsx       |   6 +-
 packages/data-storage-couchdb/lib/views.ts    | 146 ++++++++++++++++++
 2 files changed, 150 insertions(+), 2 deletions(-)

diff --git a/App/app/features/inventory/screens/RFIDUntaggedItemsScreen.tsx b/App/app/features/inventory/screens/RFIDUntaggedItemsScreen.tsx
index 5aaa1aab..61a3109d 100644
--- a/App/app/features/inventory/screens/RFIDUntaggedItemsScreen.tsx
+++ b/App/app/features/inventory/screens/RFIDUntaggedItemsScreen.tsx
@@ -22,8 +22,9 @@ function RFIDUntaggedItemsScreen({
     loading: rfidUntaggedItemsDataLoading,
     refresh: refreshRfidUntaggedItems,
     refreshing: rfidUntaggedItemsRefreshing,
-  } = useView('rfid_untagged_items', {
+  } = useView('rfid_untagged_items_by_updated_time', {
     includeDocs: true,
+    descending: true,
   });
   const rfidUntaggedItems = rfidUntaggedItemsData
     ? rfidUntaggedItemsData
@@ -36,8 +37,9 @@ function RFIDUntaggedItemsScreen({
     loading: rfidTagOutdatedItemsDataLoading,
     refresh: refreshRfidTagOutdatedItems,
     refreshing: rfidTagOutdatedItemsRefreshing,
-  } = useView('rfid_tag_outdated_items', {
+  } = useView('rfid_tag_outdated_items_by_updated_time', {
     includeDocs: true,
+    descending: true,
   });
   const rfidTagOutdatedItems = rfidTagOutdatedItemsData
     ? rfidTagOutdatedItemsData
diff --git a/packages/data-storage-couchdb/lib/views.ts b/packages/data-storage-couchdb/lib/views.ts
index 3d30b69b..3160eec5 100644
--- a/packages/data-storage-couchdb/lib/views.ts
+++ b/packages/data-storage-couchdb/lib/views.ts
@@ -303,6 +303,78 @@ export const VIEWS = {
       );
     },
   }),
+  rfid_untagged_items_by_updated_time: view_defn({
+    version: 1,
+    map: `
+      function (doc) {
+        if (
+          doc.type === 'item' &&
+          !!doc.data.rfid_tag_epc_memory_bank_contents &&
+          !doc.data.actual_rfid_tag_epc_memory_bank_contents
+        ) {
+          emit([doc.updated_at], { _id: doc._id });
+        }
+      }
+    `,
+    dataParser: (data: unknown) => {
+      if (!data) return null;
+      if (typeof data !== 'object') return null;
+      const rows = (data as any).rows;
+      if (!Array.isArray(rows)) return null;
+      return rows.map(row =>
+        row.doc
+          ? {
+              __data_included: true,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+              data: getDatumFromDoc('item', row.doc),
+            }
+          : {
+              __data_included: false,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+            },
+      );
+    },
+  }),
+  rfid_untagged_items_by_created_time: view_defn({
+    version: 1,
+    map: `
+      function (doc) {
+        if (
+          doc.type === 'item' &&
+          !!doc.data.rfid_tag_epc_memory_bank_contents &&
+          !doc.data.actual_rfid_tag_epc_memory_bank_contents
+        ) {
+          emit([doc.created_at], { _id: doc._id });
+        }
+      }
+    `,
+    dataParser: (data: unknown) => {
+      if (!data) return null;
+      if (typeof data !== 'object') return null;
+      const rows = (data as any).rows;
+      if (!Array.isArray(rows)) return null;
+      return rows.map(row =>
+        row.doc
+          ? {
+              __data_included: true,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+              data: getDatumFromDoc('item', row.doc),
+            }
+          : {
+              __data_included: false,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+            },
+      );
+    },
+  }),
   rfid_tag_outdated_items_count: view_defn({
     version: 1,
     map: `
@@ -366,6 +438,80 @@ export const VIEWS = {
       );
     },
   }),
+  rfid_tag_outdated_items_by_updated_time: view_defn({
+    version: 1,
+    map: `
+      function (doc) {
+        if (
+          doc.type === 'item' &&
+          !!doc.data.rfid_tag_epc_memory_bank_contents &&
+          !!doc.data.actual_rfid_tag_epc_memory_bank_contents &&
+          doc.data.rfid_tag_epc_memory_bank_contents !== doc.data.actual_rfid_tag_epc_memory_bank_contents
+        ) {
+          emit([doc.updated_at], { _id: doc._id });
+        }
+      }
+    `,
+    dataParser: (data: unknown) => {
+      if (!data) return null;
+      if (typeof data !== 'object') return null;
+      const rows = (data as any).rows;
+      if (!Array.isArray(rows)) return null;
+      return rows.map(row =>
+        row.doc
+          ? {
+              __data_included: true,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+              data: getDatumFromDoc('item', row.doc),
+            }
+          : {
+              __data_included: false,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+            },
+      );
+    },
+  }),
+  rfid_tag_outdated_items_by_created_time: view_defn({
+    version: 1,
+    map: `
+      function (doc) {
+        if (
+          doc.type === 'item' &&
+          !!doc.data.rfid_tag_epc_memory_bank_contents &&
+          !!doc.data.actual_rfid_tag_epc_memory_bank_contents &&
+          doc.data.rfid_tag_epc_memory_bank_contents !== doc.data.actual_rfid_tag_epc_memory_bank_contents
+        ) {
+          emit([doc.created_at], { _id: doc._id });
+        }
+      }
+    `,
+    dataParser: (data: unknown) => {
+      if (!data) return null;
+      if (typeof data !== 'object') return null;
+      const rows = (data as any).rows;
+      if (!Array.isArray(rows)) return null;
+      return rows.map(row =>
+        row.doc
+          ? {
+              __data_included: true,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+              data: getDatumFromDoc('item', row.doc),
+            }
+          : {
+              __data_included: false,
+              key: row.key,
+              id: row.id,
+              value: row.value,
+            },
+      );
+    },
+  }),
   purchase_price_sums: view_defn({
     version: 1,
     map: `