gristlabs_grist-core/app/common/resetOrg.ts
George Gevoian 74ec9358da (core) Show usage banners in doc menu of free team sites
Summary:
Also fixes a minor CSS regression in UserManager where the
link to add a team member wasn't shown on a separate row.

Test Plan: Browser tests.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3444
2022-05-26 15:01:35 -07:00

47 lines
1.8 KiB
TypeScript

import {isOwner} from 'app/common/roles';
import {ManagerDelta, PermissionDelta, UserAPI} from 'app/common/UserAPI';
/**
* A utility to reset an organization into the state it would have when first
* created - no docs, one workspace called "Home", a single user. Should be
* called by a user who is both an owner of the org and a billing manager.
*/
export async function resetOrg(api: UserAPI, org: string|number) {
const session = await api.getSessionActive();
if (!isOwner(session.org)) {
throw new Error('user must be an owner of the org to be reset');
}
const billing = api.getBillingAPI();
// If billing api is not available, don't bother setting billing manager.
const account = await billing.getBillingAccount().catch(e => null);
if (account && !account.managers.some(manager => (manager.id === session.user.id))) {
throw new Error('user must be a billing manager');
}
const wss = await api.getOrgWorkspaces(org);
for (const ws of wss) {
if (!ws.isSupportWorkspace) {
await api.deleteWorkspace(ws.id);
}
}
await api.newWorkspace({name: 'Home'}, org);
const permissions: PermissionDelta = { users: {} };
for (const user of (await api.getOrgAccess(org)).users) {
if (user.id !== session.user.id) {
permissions.users![user.email] = null;
}
}
await api.updateOrgPermissions(org, permissions);
// For non-individual accounts, update billing managers (individual accounts will
// throw an error if we try to do this).
if (account && !account.individual) {
const managers: ManagerDelta = { users: {} };
for (const user of account.managers) {
if (user.id !== session.user.id) {
managers.users[user.email] = null;
}
}
await billing.updateBillingManagers(managers);
}
return api;
}