({ request, user, match, session, isRunningLocally }: RequestHandlerParams)
| 9 | const titlePrefix = 'Contacts'; |
| 10 | |
| 11 | async function get({ request, user, match, session, isRunningLocally }: RequestHandlerParams) { |
| 12 | const baseUrl = (await AppConfig.getConfig()).auth.baseUrl; |
| 13 | const contactsConfig = await AppConfig.getContactsConfig(); |
| 14 | |
| 15 | if (!contactsConfig.enableCardDavServer) { |
| 16 | throw new Error('CardDAV server is not enabled'); |
| 17 | } |
| 18 | |
| 19 | if (!(await AppConfig.isAppEnabled('contacts'))) { |
| 20 | throw new Error('Contacts app is not enabled'); |
| 21 | } |
| 22 | |
| 23 | const userId = user!.id; |
| 24 | |
| 25 | const searchParams = new URL(request.url).searchParams; |
| 26 | |
| 27 | const page = parseInt(searchParams.get('page') || '1', 10); |
| 28 | const search = searchParams.get('search') || undefined; |
| 29 | let addressBookId = searchParams.get('addressBookId') || undefined; |
| 30 | |
| 31 | let userAddressBooks = await ContactModel.listAddressBooks(userId); |
| 32 | |
| 33 | // Create default address book if none exists |
| 34 | if (userAddressBooks.length === 0) { |
| 35 | await ContactModel.createAddressBook(userId, 'Contacts'); |
| 36 | |
| 37 | userAddressBooks = await ContactModel.listAddressBooks(userId); |
| 38 | } |
| 39 | |
| 40 | if (!addressBookId) { |
| 41 | addressBookId = userAddressBooks[0].uid!; |
| 42 | } |
| 43 | |
| 44 | if (!addressBookId) { |
| 45 | throw new Error('Invalid address book ID'); |
| 46 | } |
| 47 | |
| 48 | const userContacts = await ContactModel.list(userId, addressBookId); |
| 49 | |
| 50 | const lowerCaseSearch = search?.toLowerCase(); |
| 51 | |
| 52 | const filteredContacts = lowerCaseSearch |
| 53 | ? userContacts.filter((contact) => |
| 54 | contact.firstName!.toLowerCase().includes(lowerCaseSearch) || |
| 55 | contact.lastName?.toLowerCase().includes(lowerCaseSearch) |
| 56 | ) |
| 57 | : userContacts; |
| 58 | |
| 59 | const contactsCount = filteredContacts.length; |
| 60 | |
| 61 | const htmlContent = defaultHtmlContent({ |
| 62 | addressBookId, |
| 63 | userContacts: filteredContacts, |
| 64 | userAddressBooks, |
| 65 | page, |
| 66 | contactsCount, |
| 67 | baseUrl, |
| 68 | search, |
nothing calls this directly
no test coverage detected