From 235cccc51d3a231cf263b3d958a99885c9fb9c3a Mon Sep 17 00:00:00 2001
From: Maximilian Moser <maximilian.moser@tuwien.ac.at>
Date: Sat, 22 Jun 2024 21:08:58 +0200
Subject: [PATCH] Override some search mappings

* because I am impatient and don't want to wait until my upstream PR
  gets merged and released
* also, it's interesting to see how to override mappings
---
 MANIFEST.in                                   |   1 +
 invenio_config_tuw/mappings/__init__.py       |  25 +++
 .../mappings/archivedinvitation-v1.0.0.json   | 163 ++++++++++++++++++
 .../mappings/member-v1.0.0.json               | 163 ++++++++++++++++++
 setup.cfg                                     |   2 +
 5 files changed, 354 insertions(+)
 create mode 100644 invenio_config_tuw/mappings/__init__.py
 create mode 100644 invenio_config_tuw/mappings/archivedinvitation-v1.0.0.json
 create mode 100644 invenio_config_tuw/mappings/member-v1.0.0.json

diff --git a/MANIFEST.in b/MANIFEST.in
index 609600b..3d433f9 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -37,4 +37,5 @@ recursive-include docs *.rst
 recursive-include docs *.txt
 recursive-include docs Makefile
 recursive-include invenio_config_tuw *.cfg
+recursive-include invenio_config_tuw *.json
 recursive-include tests *.py
diff --git a/invenio_config_tuw/mappings/__init__.py b/invenio_config_tuw/mappings/__init__.py
new file mode 100644
index 0000000..3050ba7
--- /dev/null
+++ b/invenio_config_tuw/mappings/__init__.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2024 TU Wien.
+#
+# Invenio-Config-TUW is free software; you can redistribute it and/or modify
+# it under the terms of the MIT License; see LICENSE file for more details.
+
+"""Custom overrides for search mappings."""
+
+from pathlib import Path
+
+from invenio_search import current_search
+
+
+def override_mappings(app):
+    """Override specific search mappings."""
+    path = Path(__file__).parent
+
+    # community members
+    prefix, name = "communitymembers-members", "member-v1.0.0"
+    current_search.mappings[f"{prefix}-{name}"] = str(path / f"{name}.json")
+
+    # member invitations
+    prefix, name = "communitymembers-archivedinvitations", "archivedinvitation-v1.0.0"
+    current_search.mappings[f"{prefix}-{name}"] = str(path / f"{name}.json")
diff --git a/invenio_config_tuw/mappings/archivedinvitation-v1.0.0.json b/invenio_config_tuw/mappings/archivedinvitation-v1.0.0.json
new file mode 100644
index 0000000..f261cb7
--- /dev/null
+++ b/invenio_config_tuw/mappings/archivedinvitation-v1.0.0.json
@@ -0,0 +1,163 @@
+{
+  "mappings": {
+    "dynamic": "strict",
+    "dynamic_templates": [
+      {
+        "user_profile": {
+          "path_match": "user.profile.*",
+          "mapping": {
+            "type": "keyword"
+          }
+        }
+      },
+      {
+        "user_preferences": {
+          "path_match": "user.preferences.*",
+          "mapping": {
+            "type": "keyword"
+          }
+        }
+      }
+    ],
+    "properties": {
+      "created": {
+        "type": "date"
+      },
+      "updated": {
+        "type": "date"
+      },
+      "indexed_at": {
+        "type": "date"
+      },
+      "id": {
+        "type": "keyword"
+      },
+      "uuid": {
+        "type": "keyword"
+      },
+      "version_id": {
+        "type": "integer"
+      },
+      "community_id": {
+        "type": "keyword"
+      },
+      "user_id": {
+        "type": "keyword"
+      },
+      "user": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "username": {
+            "type": "keyword"
+          },
+          "email": {
+            "type": "keyword"
+          },
+          "active": {
+            "type": "boolean"
+          },
+          "confirmed": {
+            "type": "boolean"
+          },
+          "verified_at": {
+            "type": "date"
+          },
+          "profile": {
+            "properties": {
+              "full_name": {
+                "type": "text",
+                "fields": {
+                  "keyword": {
+                    "type": "keyword"
+                  }
+                }
+              },
+              "affiliations": {
+                "type": "text"
+              }
+            },
+            "dynamic": true
+          },
+          "preferences": {
+            "properties": {
+              "visibility": {
+                "type": "keyword"
+              },
+              "email_visibility": {
+                "type": "keyword"
+              },
+              "locale": {
+                "type": "keyword",
+                "index": "false"
+              },
+              "timezone": {
+                "type": "keyword",
+                "index": "false"
+              },
+              "notifications": {
+                "properties": {
+                  "enabled": {
+                    "type": "boolean"
+                  }
+                }
+              }
+            },
+            "dynamic": true
+          }
+        }
+      },
+      "group_id": {
+        "type": "keyword"
+      },
+      "group": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "name": {
+            "type": "keyword"
+          }
+        }
+      },
+      "request_id": {
+        "type": "keyword"
+      },
+      "request": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "status": {
+            "type": "keyword"
+          },
+          "expires_at": {
+            "type": "date"
+          },
+          "is_open": {
+            "type": "boolean"
+          }
+        }
+      },
+      "role": {
+        "type": "keyword"
+      },
+      "visible": {
+        "type": "boolean"
+      },
+      "active": {
+        "type": "boolean"
+      }
+    }
+  }
+}
diff --git a/invenio_config_tuw/mappings/member-v1.0.0.json b/invenio_config_tuw/mappings/member-v1.0.0.json
new file mode 100644
index 0000000..f261cb7
--- /dev/null
+++ b/invenio_config_tuw/mappings/member-v1.0.0.json
@@ -0,0 +1,163 @@
+{
+  "mappings": {
+    "dynamic": "strict",
+    "dynamic_templates": [
+      {
+        "user_profile": {
+          "path_match": "user.profile.*",
+          "mapping": {
+            "type": "keyword"
+          }
+        }
+      },
+      {
+        "user_preferences": {
+          "path_match": "user.preferences.*",
+          "mapping": {
+            "type": "keyword"
+          }
+        }
+      }
+    ],
+    "properties": {
+      "created": {
+        "type": "date"
+      },
+      "updated": {
+        "type": "date"
+      },
+      "indexed_at": {
+        "type": "date"
+      },
+      "id": {
+        "type": "keyword"
+      },
+      "uuid": {
+        "type": "keyword"
+      },
+      "version_id": {
+        "type": "integer"
+      },
+      "community_id": {
+        "type": "keyword"
+      },
+      "user_id": {
+        "type": "keyword"
+      },
+      "user": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "username": {
+            "type": "keyword"
+          },
+          "email": {
+            "type": "keyword"
+          },
+          "active": {
+            "type": "boolean"
+          },
+          "confirmed": {
+            "type": "boolean"
+          },
+          "verified_at": {
+            "type": "date"
+          },
+          "profile": {
+            "properties": {
+              "full_name": {
+                "type": "text",
+                "fields": {
+                  "keyword": {
+                    "type": "keyword"
+                  }
+                }
+              },
+              "affiliations": {
+                "type": "text"
+              }
+            },
+            "dynamic": true
+          },
+          "preferences": {
+            "properties": {
+              "visibility": {
+                "type": "keyword"
+              },
+              "email_visibility": {
+                "type": "keyword"
+              },
+              "locale": {
+                "type": "keyword",
+                "index": "false"
+              },
+              "timezone": {
+                "type": "keyword",
+                "index": "false"
+              },
+              "notifications": {
+                "properties": {
+                  "enabled": {
+                    "type": "boolean"
+                  }
+                }
+              }
+            },
+            "dynamic": true
+          }
+        }
+      },
+      "group_id": {
+        "type": "keyword"
+      },
+      "group": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "name": {
+            "type": "keyword"
+          }
+        }
+      },
+      "request_id": {
+        "type": "keyword"
+      },
+      "request": {
+        "properties": {
+          "@v": {
+            "type": "keyword"
+          },
+          "id": {
+            "type": "keyword"
+          },
+          "status": {
+            "type": "keyword"
+          },
+          "expires_at": {
+            "type": "date"
+          },
+          "is_open": {
+            "type": "boolean"
+          }
+        }
+      },
+      "role": {
+        "type": "keyword"
+      },
+      "visible": {
+        "type": "boolean"
+      },
+      "active": {
+        "type": "boolean"
+      }
+    }
+  }
+}
diff --git a/setup.cfg b/setup.cfg
index c4978ae..8856b9a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -55,6 +55,8 @@ invenio_base.apps =
     invenio_config_tuw = invenio_config_tuw:InvenioConfigTUW
 invenio_base.api_apps =
     invenio_config_tuw = invenio_config_tuw:InvenioConfigTUW
+invenio_base.finalize_app =
+    invenio_config_tuw = invenio_config_tuw.mappings:override_mappings
 invenio_base.blueprints =
     invenio_config_tuw_hacks = invenio_config_tuw.startup:blueprint
     invenio_config_tuw_settings = invenio_config_tuw.startup:create_curation_settings_blueprint
-- 
GitLab