* Link an existing note to an ICS event
(notePath: string, icsEvent: ICSEvent)
| 601 | * Link an existing note to an ICS event |
| 602 | */ |
| 603 | async linkNoteToICS(notePath: string, icsEvent: ICSEvent): Promise<void> { |
| 604 | try { |
| 605 | const file = this.plugin.app.vault.getAbstractFileByPath(notePath); |
| 606 | if (!(file instanceof TFile)) { |
| 607 | throw new Error(`Cannot find note file: ${notePath}`); |
| 608 | } |
| 609 | |
| 610 | // Update the note's frontmatter to include the ICS event ID |
| 611 | await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => { |
| 612 | const icsEventIdField = this.plugin.fieldMapper.toUserField("icsEventId"); |
| 613 | |
| 614 | // Get existing ICS event IDs or create new array |
| 615 | let existingIds = frontmatter[icsEventIdField]; |
| 616 | if (!existingIds) { |
| 617 | existingIds = []; |
| 618 | } else if (!Array.isArray(existingIds)) { |
| 619 | // Convert single value to array for backwards compatibility |
| 620 | existingIds = [existingIds]; |
| 621 | } |
| 622 | |
| 623 | // Add new event ID if not already present |
| 624 | if (!existingIds.includes(icsEvent.id)) { |
| 625 | existingIds.push(icsEvent.id); |
| 626 | } |
| 627 | |
| 628 | frontmatter[icsEventIdField] = existingIds; |
| 629 | const dateModifiedField = this.plugin.fieldMapper.toUserField("dateModified"); |
| 630 | frontmatter[dateModifiedField] = getCurrentTimestamp(); |
| 631 | }); |
| 632 | |
| 633 | publishUserNotice(this.plugin.emitter, |
| 634 | this.translate("services.icsNote.notices.linkedToEvent", { title: icsEvent.title }) |
| 635 | ); |
| 636 | } catch (error) { |
| 637 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 638 | tasknotesLogger.error("Error linking note to ICS event:", { |
| 639 | category: "provider", |
| 640 | operation: "linking-note-ics-event", |
| 641 | details: { notePath, icsEventId: icsEvent.id }, |
| 642 | error: errorMessage, |
| 643 | }); |
| 644 | throw new Error(`Failed to link note to ICS event: ${errorMessage}`); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Build default event details from ICS event data |
no test coverage detected