Skip to content

Commit

Permalink
LF-4625 Update registerOrganizationWebhook and bulkSensorClaim for re…
Browse files Browse the repository at this point in the history
…adability

Remove unused returned values from registerOrganizationWebhook; un-nest code run when function does not return early; add comments to make more explicit how bulkSensorClaim response is defined from Ensemble API response
  • Loading branch information
kathyavini committed Jan 23, 2025
1 parent 95ac33c commit 1172d83
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions packages/api/src/util/ensemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async function bulkSensorClaim(accessToken, organizationId, esids) {
data: { esids },
};

// partial or complete failures (at least some esids failed to claim)
const onError = (error) => {
if (error.response?.data && error.response?.status) {
return { ...error.response.data, status: error.response.status };
Expand All @@ -104,6 +105,7 @@ async function bulkSensorClaim(accessToken, organizationId, esids) {
}
};

// full success (all esids successfully claimed)
const onResponse = (response) => {
return {
success: esids,
Expand Down Expand Up @@ -131,27 +133,27 @@ async function registerOrganizationWebhook(farmId, organizationId, accessToken)
.where({ farm_id: farmId, partner_id: 1 })
.first();
if (existingIntegration?.webhook_id) {
return existingIntegration.webhook_id;
} else {
const axiosObject = {
method: 'post',
url: `${ensembleAPI}/organizations/${organizationId}/webhooks/`,
data: {
url: `${baseUrl}/sensor/reading/partner/1/farm/${farmId}`,
authorization_header: authHeader,
frequency: 15,
},
};
const onError = (error) => {
console.log(error);
throw new Error('Failed to register webhook with ESCI');
};
const onResponse = async (response) => {
await FarmExternalIntegrationsModel.updateWebhookId(farmId, response.data.id);
return { ...response.data, status: response.status };
};
return await ensembleAPICall(accessToken, axiosObject, onError, onResponse);
return;
}

const axiosObject = {
method: 'post',
url: `${ensembleAPI}/organizations/${organizationId}/webhooks/`,
data: {
url: `${baseUrl}/sensor/reading/partner/1/farm/${farmId}`,
authorization_header: authHeader,
frequency: 15,
},
};
const onError = (error) => {
console.log(error);
throw new Error('Failed to register webhook with ESCI');
};
const onResponse = async (response) => {
await FarmExternalIntegrationsModel.updateWebhookId(farmId, response.data.id);
return { ...response.data, status: response.status };
};
await ensembleAPICall(accessToken, axiosObject, onError, onResponse);
}

/**
Expand Down

0 comments on commit 1172d83

Please sign in to comment.