1 import { put, select } from 'redux-saga/effects';
3 import { parseItemRevision } from '@proton/pass/lib/items/item.parser';
4 import { updateItemFlags } from '@proton/pass/lib/items/item.requests';
5 import { toggleCustomEmail, toggleProtonEmail } from '@proton/pass/lib/monitor/monitor.request';
6 import { intoAliasMonitorAddress, intoCustomMonitorAddress } from '@proton/pass/lib/monitor/monitor.utils';
7 import { AddressType, type MonitorAddress } from '@proton/pass/lib/monitor/types';
8 import { getBreaches, itemsEditSync, toggleAddressMonitor } from '@proton/pass/store/actions';
9 import { withRevalidate } from '@proton/pass/store/request/enhancers';
10 import { createRequestSaga } from '@proton/pass/store/request/sagas';
11 import { selectProtonBreaches } from '@proton/pass/store/selectors';
12 import type { BreachCustomEmailGetResponse, ItemRevision, ItemRevisionContentsResponse } from '@proton/pass/types';
13 import { partialMerge } from '@proton/pass/utils/object/merge';
15 export default createRequestSaga({
16 actions: toggleAddressMonitor,
17 call: function* ({ monitor: Monitor, ...dto }) {
19 case AddressType.ALIAS: {
20 const { shareId, itemId } = dto;
21 const data = { SkipHealthCheck: !Monitor };
22 const encryptedItem: ItemRevisionContentsResponse = yield updateItemFlags(shareId, itemId, data);
23 const item: ItemRevision<'alias'> = yield parseItemRevision(shareId, encryptedItem);
24 yield put(itemsEditSync([item]));
25 yield put(withRevalidate(getBreaches.intent()));
26 return intoAliasMonitorAddress(item);
29 case AddressType.CUSTOM: {
30 const { addressId } = dto;
31 const response: BreachCustomEmailGetResponse = yield toggleCustomEmail(addressId, { Monitor });
32 yield put(withRevalidate(getBreaches.intent()));
33 return intoCustomMonitorAddress(response);
36 case AddressType.PROTON: {
37 const { addressId } = dto;
38 const addresses: MonitorAddress<AddressType.PROTON>[] = (yield select(selectProtonBreaches)) ?? [];
39 const current = addresses.find((address) => address.addressId === addressId);
40 if (!current) throw new Error('Unknown proton address');
41 yield toggleProtonEmail(addressId, { Monitor });
42 yield put(withRevalidate(getBreaches.intent()));
43 return partialMerge(current, { monitored: Monitor });