Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes open search url #1

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contentful from "contentful-management";
import fetch from "node-fetch";
import contentful from 'contentful-management';
import fetch from 'node-fetch';

const openSearchConfig = {
username: process.env.OPEN_SEARCH_USERNAME,
Expand All @@ -19,7 +19,7 @@ const client = contentful.createClient(
accessToken: contentfulConfig.accessToken,
},
{
type: "plain",
type: 'plain',
defaults: {
spaceId: contentfulConfig.spaceId,
environmentId: contentfulConfig.environmentId,
Expand All @@ -30,8 +30,8 @@ const client = contentful.createClient(
const getGrantById = async (contentfulEntryId) => {
const { items } = await client.entry.getPublished({
query: {
"sys.id": contentfulEntryId,
content_type: "grantDetails",
'sys.id': contentfulEntryId,
content_type: 'grantDetails',
},
});

Expand All @@ -47,13 +47,13 @@ const getGrantById = async (contentfulEntryId) => {
};

const updateElasticIndex = async (contentfulEntry, action) => {
const auth = openSearchConfig.username + ":" + openSearchConfig.password;
const authHeader = "Basic " + btoa(auth);
const url = `${openSearchConfig.url}${openSearchConfig.domain}/_doc/${contentfulEntry.sys.id}`;
const auth = openSearchConfig.username + ':' + openSearchConfig.password;
const authHeader = 'Basic ' + btoa(auth);
const url = `${openSearchConfig.url}/${openSearchConfig.domain}/_doc/${contentfulEntry.sys.id}`;

const ACTIONS = {
ADD: "PUT",
REMOVE: "DELETE",
ADD: 'PUT',
REMOVE: 'DELETE',
};

const method = ACTIONS[action];
Expand All @@ -63,12 +63,12 @@ const updateElasticIndex = async (contentfulEntry, action) => {
const body = JSON.stringify(contentfulEntry);

console.log(
`Updating elastic index for grant ${contentfulEntry.fields.grantName["en-US"]}, with contentful entry: \n ${body}`
`Updating elastic index for grant ${contentfulEntry.fields.grantName['en-US']}, with contentful entry: \n ${body}`
);
const response = await fetch(url, {
method: method,
headers: {
"Content-Type": "application/json; charset=utf-8",
'Content-Type': 'application/json; charset=utf-8',
Authorization: authHeader,
},
body: body,
Expand All @@ -81,7 +81,7 @@ const updateElasticIndex = async (contentfulEntry, action) => {
);
} else {
console.log(
`Successfully updated elastic index for grant ${contentfulEntry.fields.grantName["en-US"]}`
`Successfully updated elastic index for grant ${contentfulEntry.fields.grantName['en-US']}`
);
}
};
Expand Down