diff --git a/src/controllers/task.controllers.ts b/src/controllers/task.controllers.ts index 9d432c3..fbc5474 100644 --- a/src/controllers/task.controllers.ts +++ b/src/controllers/task.controllers.ts @@ -1,9 +1,8 @@ import { ZError } from 'common/ZError' import BaseController from 'common/base.controller' import { role, router } from 'decorators/router' -import { ChainTask, ChainTaskClass } from 'models/ChainTask' import { CheckIn } from 'models/CheckIn' -import { RequestTask } from 'models/RequestTask' +import { getMonthBegin, getNDayAgo } from 'utils/date.util' class TaskController extends BaseController { @@ -14,6 +13,21 @@ class TaskController extends BaseController { if (!address || !days) { throw new ZError(10, 'address is required') } + let query: any = { from: address } + if (typeof days === 'number') { + let begin = getNDayAgo(days, true) + query.blockTime = {$gt: begin.getTime() / 1000 | 0} + } else if (typeof days === 'string') { + if (days === '1month') { + let date = getMonthBegin(new Date()) + query.blockTime = {$gt: date.getTime() / 1000 | 0} + } else { + query.dateTag = days + } + + } else if (Array.isArray(days)) { + query.dateTag = {$in: days} + } let records = await CheckIn.find({ from: address, dateTag: {$in: days}}) let result = [] for (let record of records) { diff --git a/src/models/CheckIn.ts b/src/models/CheckIn.ts index 30690f3..01b2325 100644 --- a/src/models/CheckIn.ts +++ b/src/models/CheckIn.ts @@ -5,6 +5,7 @@ import { BaseModule } from './Base' @dbconn() @index({ from: 1 }, { unique: false }) @index({ from: 1, dateTag: 1}, { unique: true }) +@index({ from: 1, blockTime: 1}, { unique: false }) @modelOptions({ schemaOptions: { collection: 'check_in_event', timestamps: true }, }) diff --git a/src/utils/date.util.ts b/src/utils/date.util.ts index 76b98ee..0ce761c 100644 --- a/src/utils/date.util.ts +++ b/src/utils/date.util.ts @@ -6,6 +6,31 @@ export const formatDate = (date: Date): string => { return `${year}${month}${day}`; }; +// get begin of one day +export const getDayBegin = (date: Date): Date => { + const year = date.getFullYear(); + const month = date.getMonth(); + const day = date.getDate(); + return new Date(year, month, day); +}; + +// get begin of n day ago +export const getNDayAgo = (n: number, begin: boolean): Date => { + const date = new Date(Date.now() - n * 24 * 60 * 60 * 1000) + if (begin) { + return getDayBegin(date); + } else { + return date; + } +}; + +// get begin of this month +export const getMonthBegin = (date: Date): Date => { + const year = date.getFullYear(); + const month = date.getMonth(); + return new Date(year, month, 1); +} + // get formated datestring of yesterday export const yesterday = () => { const date = new Date();