refactor: 💡 refact quarter func

This commit is contained in:
Jin 2025-04-01 19:20:08 +02:00
parent 50009bfb6e
commit 8bbe6ee1b3

View File

@ -166,11 +166,10 @@ function getQuarterNumberStr(date: Dayjs) {
return `${date.year()}-Q${Math.floor(date.month() / 3) + 1}`; 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 rootNote = _rootNote || getRootCalendarNote();
const quarterStr = getQuarterNumberStr(dayjs(dateStr)); quarterStr = quarterStr.trim().substring(0, 7);
const quarterNumber = parseInt(quarterStr.substring(6, 7));
let quarterNote = searchService.findFirstNoteWithQuery(`#${QUARTER_LABEL}="${quarterStr}"`, new SearchContext({ ancestorNoteId: rootNote.noteId })); 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; return quarterNote;
} }
const yearNote = getYearNote(dateStr, rootNote); const [yearStr, quarterNumberStr] = quarterStr.trim().split('-Q');
const noteTitle = getJournalNoteTitle(rootNote, 'quarter', dayjs(dateStr), quarterNumber); 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(() => { sql.transactional(() => {
quarterNote = createNote(yearNote, noteTitle); quarterNote = createNote(yearNote, noteTitle);
@ -212,7 +216,7 @@ function getMonthNote(dateStr: string, _rootNote: BNote | null = null): BNote {
let monthParentNote; let monthParentNote;
if (rootNote.hasLabel('enableQuarterNote')) { if (rootNote.hasLabel('enableQuarterNote')) {
monthParentNote = getQuarterNote(dateStr, rootNote); monthParentNote = getQuarterNote(getQuarterNumberStr(dayjs(dateStr)), rootNote);
} else { } else {
monthParentNote = getYearNote(dateStr, rootNote); monthParentNote = getYearNote(dateStr, rootNote);
} }