增加获取签到列表接口天数各种情况的处理
This commit is contained in:
parent
0af44deba0
commit
c2bad5d5e3
@ -1,9 +1,8 @@
|
|||||||
import { ZError } from 'common/ZError'
|
import { ZError } from 'common/ZError'
|
||||||
import BaseController from 'common/base.controller'
|
import BaseController from 'common/base.controller'
|
||||||
import { role, router } from 'decorators/router'
|
import { role, router } from 'decorators/router'
|
||||||
import { ChainTask, ChainTaskClass } from 'models/ChainTask'
|
|
||||||
import { CheckIn } from 'models/CheckIn'
|
import { CheckIn } from 'models/CheckIn'
|
||||||
import { RequestTask } from 'models/RequestTask'
|
import { getMonthBegin, getNDayAgo } from 'utils/date.util'
|
||||||
|
|
||||||
|
|
||||||
class TaskController extends BaseController {
|
class TaskController extends BaseController {
|
||||||
@ -14,6 +13,21 @@ class TaskController extends BaseController {
|
|||||||
if (!address || !days) {
|
if (!address || !days) {
|
||||||
throw new ZError(10, 'address is required')
|
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 records = await CheckIn.find({ from: address, dateTag: {$in: days}})
|
||||||
let result = []
|
let result = []
|
||||||
for (let record of records) {
|
for (let record of records) {
|
||||||
|
@ -5,6 +5,7 @@ import { BaseModule } from './Base'
|
|||||||
@dbconn()
|
@dbconn()
|
||||||
@index({ from: 1 }, { unique: false })
|
@index({ from: 1 }, { unique: false })
|
||||||
@index({ from: 1, dateTag: 1}, { unique: true })
|
@index({ from: 1, dateTag: 1}, { unique: true })
|
||||||
|
@index({ from: 1, blockTime: 1}, { unique: false })
|
||||||
@modelOptions({
|
@modelOptions({
|
||||||
schemaOptions: { collection: 'check_in_event', timestamps: true },
|
schemaOptions: { collection: 'check_in_event', timestamps: true },
|
||||||
})
|
})
|
||||||
|
@ -6,6 +6,31 @@ export const formatDate = (date: Date): string => {
|
|||||||
return `${year}${month}${day}`;
|
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
|
// get formated datestring of yesterday
|
||||||
export const yesterday = () => {
|
export const yesterday = () => {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user