Skip to content

Commit

Permalink
Merge pull request #12 from reflash/feature/progress-on-task-completion
Browse files Browse the repository at this point in the history
  • Loading branch information
reflash authored Oct 28, 2022
2 parents d77815d + 9ab7e0a commit 1f0f075
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/endpoints/todoistQueue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// tslint:disable: no-magic-numbers
import { achievementNotionService, bot, categoryNotionService, questNotionService, questTodoistService } from '../container';
import { goalCompletePicture, goalIncompletePicture } from '../service/common';
import { isChest, isQuest, isRepeating } from '../service/quests/mappers';
import { parseTask, TaskParams } from '../service/quests/todoist';
import { Quest } from '../service/quests/types';
Expand Down Expand Up @@ -34,6 +35,25 @@ const handleRepeating = async (params: TaskParams) => {
'Times completed in a row': { number: newTimesCompletedInARow } as NumberPropertyValue,
'Max times completed in a row': { number: newMaxInARow } as NumberPropertyValue,
});

if (quest.goalId){
const goalsWithSubgoals = await questNotionService.getVaultGoals();
const goal = goalsWithSubgoals.find(g => g.isGoal && g.active && g.id === quest.goalId);

if (goal) {
const subgoals = goalsWithSubgoals.filter(g => !g.isGoal && g.active && g.goalId === quest.goalId);

const newName = `* ${goal.emoji} ${goal.progress} | **[Goal]** [${goal.name}](${goal.url})`;
const taskId = await questTodoistService.getTaskIdByName(goal.name);
await questTodoistService.updateTaskName(taskId, newName);

for (const subgoal of subgoals) {
const completed = subgoal.required <= goal.actual;
const coverUrl = completed ? goalCompletePicture : goalIncompletePicture;
await questNotionService.updatePage(subgoal.id, {}, coverUrl);
}
}
}

const name = `Task completed ${params.id}`;
await questNotionService.addToHistory(
Expand Down
3 changes: 2 additions & 1 deletion src/service/quests/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ export const mapPageToQuest = (page: Page): Quest => {
const url = page.url.replace('https', 'notion');
const categoryId = (page.properties['Category'] as RelationPropertyValue).relation?.[0]?.id;
const rewardIds = (page.properties['Rewards'] as RelationPropertyValue).relation?.map(r => r?.id!);
const goalId = (page.properties['Relation (Great Vault)'] as RelationPropertyValue).relation?.[0]?.id;

const random = mapRandomProps(page, tags);
const repeating = mapRepeatingProps(page, isRepeating);
const pictureUrl = getPagePicture(page);

return { id, name, description, emoji, pictureUrl, priority, type, url, categoryId, rewardIds, random, repeating };
return { id, name, description, emoji, pictureUrl, priority, type, url, goalId, categoryId, rewardIds, random, repeating };
};
1 change: 1 addition & 0 deletions src/service/quests/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class QuestNotionService implements IQuestNotionService {
public getVaultGoals = async () => {
const { results: pages } = await this.client.databases.query({
database_id: process.env.VAULT_NOTION_DATABASE!,
filter: { or: [{ property: 'Active', checkbox: { equals: true } }, { property: 'Active (Goal - 2)', formula: { checkbox: { equals: true } } }] },
});
return Promise.all(
pages
Expand Down
1 change: 1 addition & 0 deletions src/service/quests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type Quest = {

categoryId: string;
rewardIds: string[];
goalId: string;

random?: Random;
repeating?: Repeating;
Expand Down

0 comments on commit 1f0f075

Please sign in to comment.