Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / calendar / members.ts
blobf14ff30afe38cc453a90edeb8b471f378be21d33
1 import type { Address as AddressInterface } from '../interfaces';
2 import type { CalendarMember as MemberInterface } from '../interfaces/calendar';
4 export const getMemberAndAddress = (
5     Addresses: AddressInterface[] = [],
6     Members: MemberInterface[] = [],
7     Author = ''
8 ): [MemberInterface, AddressInterface] => {
9     if (!Members.length) {
10         throw new Error('No members');
11     }
12     if (!Addresses.length) {
13         throw new Error('No addresses');
14     }
16     // First try to find self by author to use the same address.
17     const selfAddress = Addresses.find((Address) => Address.Email === Author);
18     const selfMember = selfAddress ? Members.find((Member) => Member.Email === selfAddress.Email) : undefined;
20     if (selfMember && selfAddress) {
21         return [selfMember, selfAddress];
22     }
24     // Otherwise just use the first member. It is assumed the list of members only contain yourself.
25     const [defaultMember] = Members;
27     const Address = Addresses.find(({ Email }) => defaultMember.Email === Email);
28     if (!Address) {
29         throw new Error('Self as member not found');
30     }
31     return [defaultMember, Address];