File size: 1,402 Bytes
57aa51e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import { proxyActivities, sleep } from '@temporalio/workflow';
import { IntegrationsActivity } from '@gitroom/orchestrator/activities/integrations.activity';
const { getIntegrationsById, refreshToken } =
proxyActivities<IntegrationsActivity>({
startToCloseTimeout: '10 minute',
retry: {
maximumAttempts: 3,
backoffCoefficient: 1,
initialInterval: '2 minutes',
},
});
export async function refreshTokenWorkflow({
organizationId,
integrationId,
}: {
integrationId: string;
organizationId: string;
}) {
while (true) {
let integration = await getIntegrationsById(integrationId, organizationId);
if (
!integration ||
integration.deletedAt ||
integration.inBetweenSteps ||
integration.refreshNeeded
) {
return false;
}
const today = new Date();
const endDate = new Date(integration.tokenExpiration);
const minMax = Math.max(0, endDate.getTime() - today.getTime());
if (!minMax) {
return false;
}
await sleep(minMax as number);
// while we were sleeping, the integration might have been deleted
integration = await getIntegrationsById(integrationId, organizationId);
if (
!integration ||
integration.deletedAt ||
integration.inBetweenSteps ||
integration.refreshNeeded
) {
return false;
}
await refreshToken(integration);
}
}
|