Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / store / sagas / organization / organization-settings.saga.ts
blobf3c62025391219e75c5b968bc34b79ca53e3af35
1 import { call, put, select, takeLeading } from 'redux-saga/effects';
3 import { LockMode } from '@proton/pass/lib/auth/lock/types';
4 import { getOrganizationSettings as fetchOrganizationSettings } from '@proton/pass/lib/organization/organization.requests';
5 import { lockSync } from '@proton/pass/store/actions';
6 import { getOrganizationSettings } from '@proton/pass/store/actions/creators/organization';
7 import { selectOrganization } from '@proton/pass/store/selectors';
8 import type { RootSagaOptions } from '@proton/pass/store/types';
9 import { type MaybeNull, type OrganizationGetResponse } from '@proton/pass/types';
10 import { logger } from '@proton/pass/utils/logger';
11 import type { Organization } from '@proton/shared/lib/interfaces';
12 import noop from '@proton/utils/noop';
14 function* getOrganizationSettingsWorker(
15     options: RootSagaOptions,
16     { meta }: ReturnType<typeof getOrganizationSettings.intent>
17 ) {
18     try {
19         const organization: MaybeNull<Organization> = yield select(selectOrganization);
20         if (!organization) throw new Error('User not in organization');
22         const data: OrganizationGetResponse = yield call(fetchOrganizationSettings);
23         const orgLockTTL = data?.Settings?.ForceLockSeconds;
25         yield put(getOrganizationSettings.success(meta.request.id, data));
27         try {
28             if (orgLockTTL) {
29                 const authStore = options.getAuthStore();
30                 const auth = options.getAuthService();
31                 const lockMode = authStore.getLockMode();
32                 const lockTTL = authStore.getLockTTL();
34                 /** If the user's organization has a `ForceLockSeconds` setting, ensure
35                  * the user's local lock TTL matches and re-persist the session. If no lock
36                  * is setup, user will be brought to the "lock-setup" screen (`AppGuard.tsx`) */
37                 if (lockMode !== LockMode.NONE && lockTTL !== orgLockTTL) {
38                     authStore.setLockTTL(orgLockTTL);
39                     yield put(lockSync({ mode: lockMode, locked: false, ttl: orgLockTTL }));
40                     yield auth.persistSession().catch(noop);
41                 }
42             }
43         } catch (error) {
44             logger.error(`[Saga::Org] Error updating lock TTL from organization's ForceLockSeconds: ${error}`);
45         }
46     } catch (error) {
47         yield put(getOrganizationSettings.failure(meta.request.id, {}, error));
48     }
51 export default function* watcher(options: RootSagaOptions) {
52     yield takeLeading(getOrganizationSettings.intent.match, getOrganizationSettingsWorker, options);