From c2bad5d5e342bffbaf315707298a05b7a6a0a52e Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Fri, 5 Jan 2024 19:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E7=AD=BE?= =?UTF-8?q?=E5=88=B0=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E5=A4=A9=E6=95=B0?= =?UTF-8?q?=E5=90=84=E7=A7=8D=E6=83=85=E5=86=B5=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/task.controllers.ts | 18 ++++++++++++++++-- src/models/CheckIn.ts | 1 + src/utils/date.util.ts | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) 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();