-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
08e989d
commit d5894d4
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |