Merge branch 'IDTEAM-1.26.0' into 'main'
[ProtonMail-WebClient.git] / packages / pass / lib / b2b / b2b.requests.ts
blob627ae5af512be8d5b7c6c27919cf16971f1fc889
1 import { MAX_BATCH_PER_REQUEST } from '@proton/pass/constants';
2 import { api } from '@proton/pass/lib/api/api';
3 import type { ItemMarkAsReadRequest } from '@proton/pass/types';
4 import { type B2BEvent, B2BEventName } from '@proton/pass/types/data/b2b';
5 import { groupByKey } from '@proton/pass/utils/array/group-by-key';
6 import type { EventBundle } from '@proton/pass/utils/event/dispatcher';
7 import chunk from '@proton/utils/chunk';
9 import { isB2BEvent } from './b2b.utils';
11 export const sendItemReadEvents = async (events: B2BEvent<B2BEventName.ItemRead>[]) =>
12     Promise.all(
13         groupByKey(events, 'shareId').map((events) => {
14             const batches = chunk(events, MAX_BATCH_PER_REQUEST);
16             return Promise.all(
17                 batches.map((batch) => {
18                     const { shareId } = batch[0];
20                     const ItemTimes = batch.map<ItemMarkAsReadRequest>((event) => ({
21                         ItemID: event.itemId,
22                         Timestamp: event.timestamp,
23                     }));
25                     return api({
26                         url: `pass/v1/share/${shareId}/item/read`,
27                         method: 'put',
28                         data: { ItemTimes },
29                     });
30                 })
31             );
32         })
33     );
35 export const sendB2BEventsBundle = async ({ events }: EventBundle<B2BEvent>): Promise<void> => {
36     await sendItemReadEvents(events.filter(isB2BEvent(B2BEventName.ItemRead)));