refactor phase 3a api admin auth documents email targeted actions imports

This commit is contained in:
2026-03-12 09:58:39 +00:00
parent cf99d8e466
commit 7bd5c7d833
9 changed files with 65 additions and 63 deletions
+4 -6
View File
@@ -18,11 +18,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import {
azureHeadersPagedCustom,
consoleLogger,
getToken,
} from "../../../actions";
import { azureHeadersPagedCustom } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
import { number } from "prop-types";
const WORDKEY = process.env.HASHKEY;
@@ -154,7 +152,7 @@ export default async function ApiProxy(req, res) {
let flattened = data.value.map((r) => ({
...r,
ticketnumber: r.pinswg_DocumentIds?.ticketnumber || null,
publishtoweb: r.pinswg_DocumentIds?.pinswg_publishtoweb || null,
publishtoweb: r.pinswg_DocumentIds?.pinswg_publishtoweb || null
}));
data.value = flattened;
+3 -5
View File
@@ -18,11 +18,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import {
azureHeadersPagedCustom,
consoleLogger,
getToken,
} from "../../../actions";
import { azureHeadersPagedCustom } from "../../../actions/core/headers";
import { getToken } from "../../../actions/core/token";
import { consoleLogger } from "../../../actions/core/logger";
const WORDKEY = process.env.HASHKEY;
+15 -14
View File
@@ -14,7 +14,8 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
import NextAuth from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { consoleLogger, getPreferredLanguage } from "../../../actions";
import { consoleLogger } from "../../../actions/core/logger";
import { getPreferredLanguage } from "../../../actions/services/accountService";
const prisma = new PrismaClient();
@@ -43,7 +44,7 @@ const parseUrl = (url) => {
host: _url.host,
path,
base,
toString: () => base,
toString: () => base
};
};
@@ -127,7 +128,7 @@ const authOptions = (locale, req, res) => {
return res
.writeHead(200, { "Content-Type": "application/json" })
.json({ url: formURL });
},
}
},
EmailProvider({
maxAge: 2 * 60 * 60, //10 * 60, // Magic links are valid for 10 min only
@@ -191,7 +192,7 @@ const authOptions = (locale, req, res) => {
// url.split("&token")[1]
// : url,
"linkExpiry": 2 * 60 * 60,
"linkExpiry": 2 * 60 * 60
};
const reference = "PEDW-SIGNIN";
@@ -208,14 +209,14 @@ const authOptions = (locale, req, res) => {
emailAddress,
{
personalisation: personalisation,
reference: reference,
reference: reference
// emailReplyToId: emailReplyToId,
}
)
//.then((response) => console.log(response))
.catch((error) => consoleLogger(error));
},
}),
}
})
],
adapter: PrismaAdapter(prisma),
secret: process.env.NEXTAUTH_SECRET,
@@ -226,7 +227,7 @@ const authOptions = (locale, req, res) => {
theme: "dark",
debug: true,
maxAge: 30 * 60,
updateAge: 5 * 60, // 24 hours
updateAge: 5 * 60 // 24 hours
},
cookies: {
callbackUrl: {
@@ -234,16 +235,16 @@ const authOptions = (locale, req, res) => {
options: {
sameSite: "lax",
path: "/",
secure: true,
},
},
secure: true
}
}
},
pages: {
signIn: (locale == "cy" ? "/cy" : "") + "/auth/signin",
error: (locale == "cy" ? "/cy" : "") + "/auth/error", // Error code passed in query string as ?error=
verifyRequest:
(locale == "cy" ? "/cy" : "") + "/auth/verify-request", // (used for check email message)
newUser: (locale == "cy" ? "/cy" : "") + "/account/register", // New users will be directed here on first sign in (leave the property out if not of interest)
newUser: (locale == "cy" ? "/cy" : "") + "/account/register" // New users will be directed here on first sign in (leave the property out if not of interest)
},
callbacks: {
session: async (session, user) => {
@@ -275,8 +276,8 @@ const authOptions = (locale, req, res) => {
console.log("the url:", updatedUrl);
return updatedUrl;
},
},
}
}
};
};
+6 -5
View File
@@ -110,7 +110,8 @@
// }
import axios from "axios";
import { getToken, consoleLogger } from "../../../../actions";
import { getToken } from "../../../../actions/core/token";
import { consoleLogger } from "../../../../actions/core/logger";
const WEBAPI_URL =
process.env.RELAY_ROOT ||
@@ -149,9 +150,9 @@ const ApiProxy = async (req, res) => {
"Prefer":
'odata.include-annotations="*",return=representation',
"Content-Type": "application/json",
"Authorization": "Bearer " + token.access_token,
"Authorization": "Bearer " + token.access_token
},
responseType: "stream",
responseType: "stream"
});
};
@@ -196,8 +197,8 @@ const ApiProxy = async (req, res) => {
export const config = {
api: {
responseLimit: false,
},
responseLimit: false
}
};
export default ApiProxy;
+22 -26
View File
@@ -1,13 +1,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import {
getToken,
azureHeaders,
azureHeadersPaged,
consoleLogger,
sendEmail,
} from "../../../actions";
import { azureHeaders, azureHeadersPaged } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
import { formatDates } from "../../../components/utils";
var NotifyClient = require("notifications-node-client").NotifyClient;
@@ -79,14 +75,14 @@ const buildNotifyPayloads = (watchlistByEmail) => {
noRef: "No Ref",
untitled: "Untitled",
noDate: "No Date",
link: "Link",
link: "Link"
},
cy: {
noRef: "Dim Cyfeirnod",
untitled: "Heb Deitl",
noDate: "Dim Dyddiad",
link: "Dolen",
},
link: "Dolen"
}
};
const t = labels[language] || labels.en;
@@ -114,13 +110,13 @@ const buildNotifyPayloads = (watchlistByEmail) => {
en: {
unnamed: "Unnamed Event",
noDate: "No Date",
on: "on",
on: "on"
},
cy: {
unnamed: "Digwyddiad Heb Enw",
noDate: "Dim Dyddiad",
on: "ar",
},
on: "ar"
}
};
const t = labels[language] || labels.en;
@@ -144,14 +140,14 @@ const buildNotifyPayloads = (watchlistByEmail) => {
open: "Open",
to: "to",
close: "Close",
na: "N/A",
na: "N/A"
},
cy: {
open: "Agor",
to: "i",
close: "Cau",
na: "Dim ar gael",
},
na: "Dim ar gael"
}
};
const t = labels[language] || labels.en;
@@ -234,7 +230,7 @@ const buildNotifyPayloads = (watchlistByEmail) => {
docSection,
sipSection,
repsSection,
caseUnsubscribelink,
caseUnsubscribelink
]
.filter(Boolean)
.join("\n")}`;
@@ -242,7 +238,7 @@ const buildNotifyPayloads = (watchlistByEmail) => {
.join("\n\n"),
unsubscribeLink: `Unsubscribe by selecting this link if you no longer want to receive any updates ${
process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId
}`,
}`
};
const personalisationCY = {
@@ -281,7 +277,7 @@ const buildNotifyPayloads = (watchlistByEmail) => {
docSection,
sipSection,
repsSection,
caseUnsubscribelink,
caseUnsubscribelink
]
.filter(Boolean)
.join("\n")}`;
@@ -289,7 +285,7 @@ const buildNotifyPayloads = (watchlistByEmail) => {
.join("\n\n"),
unsubscribeLink: `Dad-danysgrifiwch drwy ddewis y ddolen hon os nad ydych chi eisiau derbyn unrhyw ddiweddariadau mwyach ${
process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId
}`,
}`
};
return {
@@ -305,7 +301,7 @@ const buildNotifyPayloads = (watchlistByEmail) => {
process.env.NEXTAUTH_URL +
(prefLanguage == 846040001 ? "en" : "cy") +
"/unsubscribeall/" +
contactId,
contactId
};
})
.filter(Boolean); // Remove any nulls from filtered-out users
@@ -348,7 +344,7 @@ export default async function CombinedApiProxy(req, res) {
...doc,
pinswg_hashlink: encryptDocReference(
doc.pinswg_isharedocumentreference
),
)
}));
} catch (err) {
consoleLogger(
@@ -401,7 +397,7 @@ export default async function CombinedApiProxy(req, res) {
...entry,
repsPeriods,
documents,
sipEvents,
sipEvents
};
})
);
@@ -428,7 +424,7 @@ export default async function CombinedApiProxy(req, res) {
reference: payload.reference,
template_id: payload.template_id,
language:
payload.pinswg_preferredlanguage == 846040001 ? "en" : "cy",
payload.pinswg_preferredlanguage == 846040001 ? "en" : "cy"
};
try {
@@ -437,7 +433,7 @@ export default async function CombinedApiProxy(req, res) {
payload.email_address,
{
personalisation: payload.personalisation,
reference: payload.reference,
reference: payload.reference
}
);
result.status = "success";
@@ -455,7 +451,7 @@ export default async function CombinedApiProxy(req, res) {
consoleLogger(error);
res.status(500).json({
error: "An error occurred while retrieving combined data.",
details: error.message || error.toString(),
details: error.message || error.toString()
});
}
}
+3 -1
View File
@@ -1,7 +1,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import { getToken, azureHeaders, consoleLogger } from "../../../actions";
import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
const WORDKEY = process.env.HASHKEY;
+5 -3
View File
@@ -1,7 +1,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import { azureHeadersPaged, consoleLogger, getToken } from "../../../actions";
import { azureHeadersPaged } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
const WORDKEY = process.env.HASHKEY;
const WEBAPI_URL =
@@ -84,7 +86,7 @@ export default async function ApiProxy(req, res) {
...doc,
pinswg_hashlink: encryptDocReference(
doc.pinswg_isharedocumentreference
),
)
}));
res.status(200).json({ ...data, value: resultsWithLinks });
@@ -92,7 +94,7 @@ export default async function ApiProxy(req, res) {
consoleLogger(error);
res.status(500).json({
error: "Failed to fetch document details.",
details: error.message || error.toString(),
details: error.message || error.toString()
});
}
}
+4 -2
View File
@@ -1,7 +1,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import { getToken, azureHeaders, consoleLogger } from "../../../actions";
import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
const WORDKEY = process.env.HASHKEY;
const WEBAPI_URL =
@@ -67,7 +69,7 @@ export default async function ApiProxy(req, res) {
consoleLogger(error);
res.status(500).json({
error: "An error occurred while retrieving data.",
details: error.message || error.toString(),
details: error.message || error.toString()
});
}
}
+3 -1
View File
@@ -1,7 +1,9 @@
import axios from "axios";
import CryptoJS from "crypto-js";
import _ from "lodash";
import { getToken, azureHeaders, consoleLogger } from "../../../actions";
import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
const WORDKEY = process.env.HASHKEY;