2023-03-31 08:34:35 +02:00
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
import { Client } from "@opensearch-project/opensearch";
|
|
|
|
|
import { v4 as uuid } from "uuid";
|
2023-06-26 10:07:12 +00:00
|
|
|
import taxonomy from "app/_config/taxonomy.json";
|
|
|
|
|
import unRegions from "app/_config/unRegions.json";
|
2023-03-31 08:34:35 +02:00
|
|
|
|
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
const { headers: { authorization }, body: { tickets } } = req;
|
|
|
|
|
const baseURL = `https://${process.env.OPENSEARCH_URL}`;
|
|
|
|
|
const client = new Client({
|
|
|
|
|
node: baseURL,
|
|
|
|
|
ssl: {
|
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
authorization
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const succeeded = [];
|
|
|
|
|
const failed = [];
|
|
|
|
|
|
|
|
|
|
for await (const ticket of tickets) {
|
|
|
|
|
const { id } = ticket;
|
|
|
|
|
try {
|
|
|
|
|
const country = ticket.country[0] ?? "none";
|
2023-05-24 20:27:57 +00:00
|
|
|
// @ts-expect-error
|
2023-03-31 08:34:35 +02:00
|
|
|
const translatedCountry = taxonomy.country[country]?.display ?? "none";
|
2023-05-24 20:27:57 +00:00
|
|
|
const countryDetails: any = unRegions.find((c) => c.name === translatedCountry);
|
2023-03-31 08:34:35 +02:00
|
|
|
const augmentedTicket = {
|
|
|
|
|
...ticket,
|
|
|
|
|
region: countryDetails['sub-region']?.toLowerCase().replace(" ", "-") ?? null,
|
|
|
|
|
continent: countryDetails.region?.toLowerCase().replace(" ", "-") ?? null,
|
2023-05-24 20:27:57 +00:00
|
|
|
};
|
2023-03-31 08:34:35 +02:00
|
|
|
const out = await client.create({
|
|
|
|
|
id: uuid(),
|
|
|
|
|
index: "sample_tagged_tickets",
|
|
|
|
|
refresh: true,
|
|
|
|
|
body: augmentedTicket,
|
|
|
|
|
});
|
|
|
|
|
console.log(out);
|
|
|
|
|
succeeded.push(id);
|
|
|
|
|
} catch (e) {
|
2023-05-24 20:27:57 +00:00
|
|
|
console.log(e);
|
2023-03-31 08:34:35 +02:00
|
|
|
failed.push(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results = { succeeded, failed };
|
2023-05-24 20:27:57 +00:00
|
|
|
return res.json(results);
|
2023-03-31 08:34:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default handler;
|