-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(be): add repeat quest every day
- Loading branch information
Showing
5 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
import { RepeatQuestsGraphql } from './repeat-quests.graphql'; | ||
|
||
@Injectable() | ||
export class RepeatQuestTaskService { | ||
private readonly logger = new Logger(RepeatQuestTaskService.name); | ||
constructor(private repeatQuestsGraphql: RepeatQuestsGraphql) {} | ||
|
||
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) | ||
async createRepeatQuests() { | ||
// get all repeatable quest | ||
const quests = await this.repeatQuestsGraphql.getRepeatableQuests(); | ||
const insertObjects = []; | ||
quests.forEach((quest) => { | ||
if (this.inDurationCondition(quest.condition)) { | ||
// create repeat quest | ||
insertObjects.push({ | ||
quest_id: quest.id, | ||
}); | ||
} | ||
|
||
if (quest.condition.level) { | ||
insertObjects.push({ | ||
quest_id: quest.id, | ||
}); | ||
} | ||
}); | ||
|
||
const result = await this.repeatQuestsGraphql.insertRepeatQuests({ | ||
objects: insertObjects, | ||
}); | ||
|
||
this.logger.debug('Insert repeat quests result:'); | ||
this.logger.debug(JSON.stringify(result)); | ||
} | ||
|
||
inDurationCondition(condition: any) { | ||
const inDuration = []; | ||
|
||
const now = new Date(); | ||
if (condition.after) { | ||
const after = new Date(condition.after); | ||
inDuration.push(after < now); | ||
} | ||
|
||
if (condition.before) { | ||
const before = new Date(condition.before); | ||
inDuration.push(now < before); | ||
} | ||
|
||
return inDuration.includes(false) || inDuration.length === 0 ? false : true; | ||
} | ||
} |