Skip to content

Commit

Permalink
Cronjob: team join request cleanup
Browse files Browse the repository at this point in the history
For the portal's native "join team" requests, formerly linked
people's requests were remaining active. This cronjob denies
and reactivates such requests.
  • Loading branch information
jeffwilcox committed Dec 20, 2023
1 parent 08e989d commit d5894d4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
"MORE_DEBUG": "appinsights,cache,restapi,pg,querycache,user,redis-cross-org"
}
},
{
"type": "node",
"request": "launch",
"name": "Launch site :4000 as-is",
"program": "${workspaceFolder}/dist/bin/www.js",
"cwd": "${workspaceFolder}/dist",
"preLaunchTask": "tsbuild",
"sourceMaps": true,
"console": "integratedTerminal",
"env": {
"NODE_ENV": "development",
"PORT": "4000",
"DEBUG": "startup,cosmosdb,g:server,context,*simple-oauth2*,appinsights,insights,appinsights,cache"
}
},
{
"type": "node",
"request": "launch",
Expand Down Expand Up @@ -342,6 +357,20 @@
"DEBUG": "startup"
}
},
{
"type": "node",
"request": "launch",
"name": "Job: Cleanup team requests (18)",
"program": "${workspaceRoot}/dist/jobs/cleanupTeamRequests.js",
"cwd": "${workspaceRoot}/dist",
"preLaunchTask": "tsbuild",
"sourceMaps": true,
"console": "integratedTerminal",
"env": {
"NODE_ENV": "development",
"DEBUG": "startup"
}
},
{
"type": "node",
"request": "launch",
Expand Down
65 changes: 65 additions & 0 deletions jobs/cleanupTeamRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright (c) Microsoft.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

// Job 18: cleanup team requests

// Remove any team requests made by people who are no longer linked.

import { IProviders } from '../interfaces';
import job from '../job';
import { TeamApprovalDecision } from '../routes/org/team/approval';
import { CreateError } from '../transitional';

job.runBackgroundJob(cleanup, {
timeoutMinutes: 5,
insightsPrefix: 'JobTeamRequestsCleanup',
});

async function cleanup(providers: IProviders) {
const { approvalProvider, insights, linkProvider } = providers;
if (!approvalProvider) {
throw CreateError.InvalidParameters('No approval provider instance available');
}

const linkedCorporateIds = await linkProvider.getAllCorporateIds();

let approvals = await approvalProvider.queryAllApprovals();
approvals = approvals.filter((approval) => approval.active === true && approval.corporateId);

const orphanApprovals = approvals.filter((approval) => !linkedCorporateIds.includes(approval.corporateId));

let removedRequests = 0;
let i = 0;
for (const approval of orphanApprovals) {
++i;
try {
approval.active = false;
approval.decision = TeamApprovalDecision.Deny;
approval.decisionMessage = 'Requestor not linked';
await approvalProvider.updateTeamApprovalEntity(approval);
insights?.trackEvent({
name: 'JobTeamRequestsCleanupApprovalUpdate',
properties: {
approvalId: approval.approvalId,
},
});
console.log(
`Denied former linked user request ${approval.approvalId} (${i} of ${orphanApprovals.length})`
);
++removedRequests;
} catch (error) {
insights?.trackException({
exception: error,
properties: {
eventName: 'JobTeamRequestsCleanupApprovalUpdateFailed',
approvalId: approval.approvalId,
},
});
console.warn(`Error ${error.message} updating approval ${approval.approvalId}`);
}
}
console.log(`Job finishing. Removed ${removedRequests} requests from former linked users.`);
insights?.trackMetric({ name: 'JobFormerRequestsDenied', value: removedRequests });
}

0 comments on commit d5894d4

Please sign in to comment.