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

Fix job fetching logic #262

Merged
merged 7 commits into from
Oct 6, 2020
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: 18 additions & 8 deletions src/rules/vouched-verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,31 @@ async function vouchedVerification(user, context, callback) {
const defaultUiUrl = 'https://i.vouched.id';
const idTokenClaim = 'https://vouched.id/is_verified';

const getJob = async (apiKey, jobToken, apiUrl) => {
const getJobByToken = async (apiKey, jobToken, apiUrl) => {
return getJob(apiKey, { token: jobToken }, apiUrl);
};

const getJobById = async (apiKey, jobId, apiUrl) => {
return getJob(apiKey, { id: jobId }, apiUrl);
};

const getJob = async (apiKey, params, apiUrl) => {
const response = await axios({
headers: {
'X-Api-Key': apiKey,
'Content-Type': 'application/json'
},
baseURL: apiUrl,
url: '/jobs',
params: {
token: jobToken
}
params: params
});
const items = response.data.items;
if (items.length === 0) {
throw new Error(`Unable to find Job with the following id: ${jobToken}`);
throw new Error(
`Unable to find Job with the following params: ${JSON.stringify(
params
)}`
);
}
return items[0];
};
Expand Down Expand Up @@ -137,7 +147,7 @@ async function vouchedVerification(user, context, callback) {
const jobToken = ruleUtils.queryParams.jobToken;
if (ruleUtils.isRedirectCallback && jobToken) {
// get job from API
const job = await getJob(
const job = await getJobByToken(
configuration.VOUCHED_API_KEY,
jobToken,
vouchedApiUrl
Expand All @@ -159,9 +169,9 @@ async function vouchedVerification(user, context, callback) {
if (vouchedResults) {
if (!isJobVerified(vouchedResults)) {
// user failed id verification
const mostRecentJob = await getJob(
const mostRecentJob = await getJobById(
configuration.VOUCHED_API_KEY,
jobToken,
vouchedResults.id,
vouchedApiUrl
);

Expand Down