From aa30b1e7219459e6e70c20704ad8b6d49c4ab908 Mon Sep 17 00:00:00 2001
From: Mat Jordan <mat@northwestern.edu>
Date: Wed, 8 Mar 2023 13:04:12 -0500
Subject: [PATCH] Add permanent legacy-pid redirects.

---
 pages/legacy-pid/[pid].tsx | 47 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 pages/legacy-pid/[pid].tsx

diff --git a/pages/legacy-pid/[pid].tsx b/pages/legacy-pid/[pid].tsx
new file mode 100644
index 00000000..c6ddd5f2
--- /dev/null
+++ b/pages/legacy-pid/[pid].tsx
@@ -0,0 +1,47 @@
+import { ApiSearchResponse } from "@/types/api/response";
+import { DC_API_SEARCH_URL } from "@/lib/constants/endpoints";
+import { GetServerSideProps } from "next";
+import { ParsedUrlQuery } from "querystring";
+import { apiPostRequest } from "@/lib/dc-api";
+
+const LegacyPid = () => null;
+
+interface Params extends ParsedUrlQuery {
+  pid: string;
+}
+
+export const getServerSideProps: GetServerSideProps = async ({ params }) => {
+  const { pid } = params as Params;
+  const response = await apiPostRequest<ApiSearchResponse>({
+    body: {
+      _source: ["id"],
+      query: {
+        bool: {
+          must: [
+            {
+              match: {
+                legacy_identifier: pid,
+              },
+            },
+          ],
+        },
+      },
+      size: 1,
+    },
+    url: DC_API_SEARCH_URL,
+  });
+
+  if (!response?.data.length)
+    return {
+      notFound: true,
+    };
+
+  return {
+    redirect: {
+      destination: `/items/${response.data[0].id}`,
+      permanent: true,
+    },
+  };
+};
+
+export default LegacyPid;