From 8bbe6ee1b3f4e13aa39fd9291e4cf7fea0345918 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Tue, 1 Apr 2025 19:20:08 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20refact=20quarter=20f?= =?UTF-8?q?unc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/date_notes.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/services/date_notes.ts b/src/services/date_notes.ts index 31f2cf815..657f4030c 100644 --- a/src/services/date_notes.ts +++ b/src/services/date_notes.ts @@ -166,11 +166,10 @@ function getQuarterNumberStr(date: Dayjs) { return `${date.year()}-Q${Math.floor(date.month() / 3) + 1}`; } -function getQuarterNote(dateStr: string, _rootNote: BNote | null = null): BNote { +function getQuarterNote(quarterStr: string, _rootNote: BNote | null = null): BNote { const rootNote = _rootNote || getRootCalendarNote(); - const quarterStr = getQuarterNumberStr(dayjs(dateStr)); - const quarterNumber = parseInt(quarterStr.substring(6, 7)); + quarterStr = quarterStr.trim().substring(0, 7); let quarterNote = searchService.findFirstNoteWithQuery(`#${QUARTER_LABEL}="${quarterStr}"`, new SearchContext({ ancestorNoteId: rootNote.noteId })); @@ -178,8 +177,13 @@ function getQuarterNote(dateStr: string, _rootNote: BNote | null = null): BNote return quarterNote; } - const yearNote = getYearNote(dateStr, rootNote); - const noteTitle = getJournalNoteTitle(rootNote, 'quarter', dayjs(dateStr), quarterNumber); + const [yearStr, quarterNumberStr] = quarterStr.trim().split('-Q'); + const quarterNumber = parseInt(quarterNumberStr); + const firstMonth = (quarterNumber - 1) * 3; + const quarterStartDate = dayjs().year(parseInt(yearStr)).month(firstMonth).date(1); + + const yearNote = getYearNote(yearStr, rootNote); + const noteTitle = getJournalNoteTitle(rootNote, 'quarter', quarterStartDate, quarterNumber); sql.transactional(() => { quarterNote = createNote(yearNote, noteTitle); @@ -212,7 +216,7 @@ function getMonthNote(dateStr: string, _rootNote: BNote | null = null): BNote { let monthParentNote; if (rootNote.hasLabel('enableQuarterNote')) { - monthParentNote = getQuarterNote(dateStr, rootNote); + monthParentNote = getQuarterNote(getQuarterNumberStr(dayjs(dateStr)), rootNote); } else { monthParentNote = getYearNote(dateStr, rootNote); }