feat(INDA-383): daily stats.
[ProtonMail-WebClient.git] / packages / shared / lib / mail / send / generatePackages.ts
blob81ff678385f3c055b981b0902bcb93a9d9cce6e3
1 import type { PrivateKeyReference, PublicKeyReference } from '@proton/crypto';
3 import type { Attachment, Message } from '../../interfaces/mail/Message';
4 import type { AttachmentDirect, PackageDirect, SendPreferences } from '../../interfaces/mail/crypto';
5 import type { RequireOnly, SimpleMap } from '../../interfaces/utils';
6 import { encryptPackages } from './sendEncrypt';
7 import { attachSubPackages } from './sendSubPackages';
8 import { generateTopPackages } from './sendTopPackages';
10 const generatePackages = async ({
11     message,
12     emails,
13     sendPreferencesMap,
14     attachments,
15     attachmentData,
16     publicKeys,
17     privateKeys,
18 }: {
19     message: RequireOnly<Message, 'Body' | 'MIMEType'>;
20     emails: string[];
21     sendPreferencesMap: SimpleMap<SendPreferences>;
22     attachments: Attachment[];
23     attachmentData: { attachment: AttachmentDirect; data: string };
24     publicKeys: PublicKeyReference[];
25     privateKeys: PrivateKeyReference[];
26 }): Promise<SimpleMap<PackageDirect>> => {
27     // There are two packages to be generated for the payload.
28     // The Packages in the request body, called here top-level packages
29     // The Packages inside Packages.addresses, called subpackages here
30     let packages = generateTopPackages({
31         message,
32         sendPreferencesMap,
33         attachmentData,
34     });
35     packages = await attachSubPackages({
36         packages,
37         attachments,
38         emails,
39         sendPreferencesMap,
40     });
41     return encryptPackages({
42         packages,
43         attachments,
44         publicKeys,
45         privateKeys,
46         message,
47     });
50 export default generatePackages;