mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
19 changed files with 308 additions and 71 deletions
@ -0,0 +1,74 @@ |
|||
import { |
|||
NON_INVESTMENT_ACTIVITY_TYPES, |
|||
TAG_ID_DRAFT |
|||
} from '@ghostfolio/common/config'; |
|||
|
|||
import { Prisma, Type as ActivityType } from '@prisma/client'; |
|||
import { endOfToday, isAfter } from 'date-fns'; |
|||
import { uniqBy } from 'lodash'; |
|||
|
|||
export const WHERE_ACTIVITY_NOT_DRAFT: Prisma.OrderWhereInput = { |
|||
tags: { |
|||
none: { |
|||
id: TAG_ID_DRAFT |
|||
} |
|||
} |
|||
}; |
|||
|
|||
export function getTagsWithDraftTag<T extends { id: string }>({ |
|||
date, |
|||
draftTag, |
|||
endOfTodayDate = endOfToday(), |
|||
originalDate, |
|||
tags, |
|||
type |
|||
}: { |
|||
date: Date; |
|||
draftTag: T; |
|||
endOfTodayDate?: Date; |
|||
originalDate?: Date; |
|||
tags: T[]; |
|||
type: ActivityType; |
|||
}) { |
|||
if (!isDraftTagToBeAssigned({ date, endOfTodayDate, originalDate, type })) { |
|||
return tags; |
|||
} |
|||
|
|||
return uniqBy([...tags, draftTag], 'id'); |
|||
} |
|||
|
|||
export function isActivityInFuture({ |
|||
date, |
|||
endOfTodayDate = endOfToday() |
|||
}: { |
|||
date: Date; |
|||
endOfTodayDate?: Date; |
|||
}) { |
|||
return isAfter(date, endOfTodayDate); |
|||
} |
|||
|
|||
export function isDraftTagToBeAssigned({ |
|||
date, |
|||
endOfTodayDate = endOfToday(), |
|||
originalDate, |
|||
type |
|||
}: { |
|||
date: Date; |
|||
endOfTodayDate?: Date; |
|||
originalDate?: Date; |
|||
type: ActivityType; |
|||
}) { |
|||
if (NON_INVESTMENT_ACTIVITY_TYPES.includes(type)) { |
|||
return false; |
|||
} |
|||
|
|||
if (!isActivityInFuture({ date, endOfTodayDate })) { |
|||
return false; |
|||
} |
|||
|
|||
// Assign only when the date newly moves into the future, so that a tag the
|
|||
// user has removed is not restored by an unrelated change
|
|||
return originalDate |
|||
? !isActivityInFuture({ endOfTodayDate, date: originalDate }) |
|||
: true; |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
-- Create the "DRAFT" tag if it does not exist yet |
|||
INSERT INTO "Tag" ("id", "name") |
|||
VALUES ('0c077abd-eca2-4cbb-818c-6cefbf2d169a', 'DRAFT') |
|||
ON CONFLICT DO NOTHING; |
|||
|
|||
-- Migrate activities with "isDraft" to the "DRAFT" tag |
|||
INSERT INTO "_OrderToTag" ("A", "B") |
|||
SELECT |
|||
"id", |
|||
'0c077abd-eca2-4cbb-818c-6cefbf2d169a' |
|||
FROM "Order" |
|||
WHERE "isDraft" = true |
|||
ON CONFLICT DO NOTHING; |
|||
Loading…
Reference in new issue