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

added migration script #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ public/*
*.DS_Store

package-lock.json

keycloak-public-keys/*

script/.env
Binary file added script/README.md
Binary file not shown.
85 changes: 85 additions & 0 deletions script/api-list/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const { default: axios } = require("axios");
const { CONFIG } = require("../constant/config");
const querystring = require("querystring");
const jwt = require("jsonwebtoken");

const genToken = async (url, body, type) => {
const isValid = await validateToken(type);

const headers = {
"Content-Type": "application/x-www-form-urlencoded",
};

if (!isValid) {
const res = await axios.post(url, body, headers).catch((err) => {
console.log("Error while generateToken", err.response.data);
return err;
});
return res ? res.data.access_token : "";
} else {
const token = type === "dev" ? this.dev_token : this.dock_token;

return token;
}
};

const validateToken = (type) => {
const token = type === "dev" ? this.dev_token : this.dock_token;

try {
jwt.verify(token, "shhhhh");
return true;
} catch (err) {
return false;
}
};

const generateToken = async (type) => {
let url = "";
let body = {};

switch (type) {
case "dev":
url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.token;
body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dev.query });
this.dev_token = await genToken(url, body, "dev");
return this.dev_token;
case "dock":
url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.token;
body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dock.query });
this.dock_token = await genToken(url, body, "dock");
return this.dock_token;
}
};

const getHeaders = async (isTokenReq, type) => {
let headers = {};

switch (type) {
case "dev":
headers = {
"Content-Type": "application/json",
Authorization: CONFIG.SUNBIRD.config.dev.authorization,
};
if (isTokenReq) {
headers["x-authenticated-user-token"] = await generateToken("dev");
}
break;

case "dock":
headers = {
"Content-Type": "application/json",
Authorization: CONFIG.SUNBIRD.config.dock.authorization,
};
if (isTokenReq) {
headers["x-authenticated-user-token"] = await generateToken("dock");
}
break;
}

return headers;
};

module.exports = {
getHeaders,
};
136 changes: 136 additions & 0 deletions script/api-list/program.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const { default: axios } = require("axios");
const { CONFIG } = require("../constant/config");
const { getHeaders } = require("./headers");

const nominateProgram = async (program_id, author) => {
const url =
CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.add_program_nomination;
const data = {
request: {
program_id: program_id,
status: "Pending",
collection_ids: [],
targetprimarycategories: [
{
name: "Observation",
identifier: "obj-cat:observation_questionset_all",
targetObjectType: "QuestionSet",
},
{
name: "Survey",
identifier: "obj-cat:survey_questionset_all",
targetObjectType: "QuestionSet",
}
],
content_types: [],
organisation_id: "937dd865-b256-4c1a-9830-a9b5b89f0913",
user_id: "bb551fff-121e-4a18-b969-984ac62bd572",
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: data,
};


const res = await axios(config).catch((err) => {
console.log("Error while nominating Program", err.response.data);
});
return res.data;
};

const updateContributorToProgram = async (reqData) => {

const url =
CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program_nomination;
const data = {
request: {
...reqData
},
};

const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: data,
};


const res = await axios(config).catch((err) => {
console.log(
"Error while updating contributor to the Program",
err.response.data
);
});
return res.data;
};

const createProgram = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_program;
const data = {
request: {
...templateData,
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(false, "dock"),
data: data,
};

const res = await axios(config).catch((err) => {
console.log("Error while creating the Program", err.response.data);
});
return res.data.result.program_id;
};

const updateProgram = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program;
const data = {
request: {
...templateData,
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(false, "dock"),
data: data,
};
const res = await axios(config).catch((err) => {
console.log("Error while updateProgram", err.response.data);
});

return res.data;
};

const publishProgram = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.publish_program;
const data = {
request: {
...templateData,
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(false, "dock"),
data: data,
};
const res = await axios(config).catch((err) => {
console.log("Error while publishProgram", err.response.data);
});
return res.data;
};

module.exports = {
createProgram,
updateProgram,
publishProgram,
nominateProgram,
updateContributorToProgram,
};
110 changes: 110 additions & 0 deletions script/api-list/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const { default: axios } = require("axios");
const { CONFIG } = require("../constant/config");
const { getHeaders } = require("./headers");


// Questionset

const createQuestionSet = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_questionset;
const data = {
request: {
questionset: { ...templateData },
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: data,
};
const res = await axios(config).catch((err) => {
console.log("Error while creating Questionset", err.response.data);
});
return res.data.result.identifier;
};

const updateQuestionSetHierarchy = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_hierarchy;

const config = {
method: "patch",
url: url,
headers: await getHeaders(true, "dock"),
data: templateData,
};

const res = await axios(config).catch((err) => {
console.log("Error while updating the questionset", err.response.data);
});
return res.data.result.identifiers;
};

const publishQuestionSet = async (questionsetId) => {
const url =
CONFIG.SUNBIRD.HOST.dock +
CONFIG.SUNBIRD.APIS.publish_questionset +
"/" +
questionsetId;
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: {},
};

const res = await axios(config).catch((err) => {
console.log("Error while publishing the questionset", err.response.data);
});
};

// Questions
const createQuestions = async (templateData) => {
const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_question;
const data = {
request: {
question: { ...templateData },
},
};
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: data,
};
const res = await axios(config).catch((err) => {
console.log("Error while creating the question", err.response.data);
});
return res.data.result.identifier;
};

const publishQuestion = async (questionId) => {
const url =
CONFIG.SUNBIRD.HOST.dock +
CONFIG.SUNBIRD.APIS.publish_question +
"/" +
questionId;
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dock"),
data: {},
};

const res = await axios(config).catch((err) => {
console.log(
"Error while publishing the question",
questionId,
err.response.data
);
});
return res.data.result.identifier;
};

module.exports = {
createQuestionSet,
updateQuestionSetHierarchy,
publishQuestionSet,
createQuestions,
publishQuestion
};
41 changes: 41 additions & 0 deletions script/api-list/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { default: axios } = require("axios");
const { CONFIG } = require("../constant/config");
const { getHeaders } = require("./headers");

const readUser = async (userId) => {
const params = "organisations,roles,locations,declarations,externalIds";
const url =
CONFIG.SUNBIRD.HOST.dev +
CONFIG.SUNBIRD.APIS.read_user +
userId +
"?fields=" +
params;

const res = await axios
.get(url, await getHeaders(true, "dev"))
.catch((err) => {
console.log("Error while reading User", err.response.data);
});
};

const searchUser = async (userId) => {
const url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.search_user;
const config = {
method: "post",
url: url,
headers: await getHeaders(true, "dev"),
data: {
request: { filters: { id: userId } },
},
};

const res = await axios(config).catch((err) => {
console.log("Error while searching User", err.response.data);
});
return res.data?.result?.response?.content;
};

module.exports = {
readUser,
searchUser,
};
Loading