From 9e8d14ba842e170f407670af6c8abb949b2ba181 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 12 Jan 2023 11:10:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=A4=E6=98=93=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/record.controller.ts | 47 ++++++++++++++++++++++++++ src/modules/Base.ts | 33 +++++++++++++----- src/modules/TranRecord.ts | 50 ++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 src/controllers/record.controller.ts create mode 100644 src/modules/TranRecord.ts diff --git a/src/controllers/record.controller.ts b/src/controllers/record.controller.ts new file mode 100644 index 0000000..3296987 --- /dev/null +++ b/src/controllers/record.controller.ts @@ -0,0 +1,47 @@ +import BaseController from 'common/base.controller' +import { ZError } from 'common/ZError' +import { router } from 'decorators/router' +import { TranRecord } from 'modules/TranRecord' + +class RecordController extends BaseController { + @router('post /trans/record') + async save(req) { + let { _id } = req.params + let user = req.user + let record + if (!_id) { + record = new TranRecord(req.params) + record.account = user.id + } else { + record = await TranRecord.findById(_id) + record.account = user.id + } + await record.save() + return record.toJson() + } + + @router('post /trans/records') + async list(req) { + const user = req.user + let defaultParams: any = { account: user.id } + let { params } = req + Object.assign(params, defaultParams) + const records = await TranRecord.pageQuery(params) + return records + } + + @router('delete /trans/record') + async delete(req) { + let { ids } = req.params + const user = req.user + if (!ids) { + throw new ZError(11, 'params mismatch') + } + const params: any = { + _id: { $in: ids }, + account: user.id, + } + let result = await TranRecord.deleteRecords(params) + return result + } +} diff --git a/src/modules/Base.ts b/src/modules/Base.ts index 71aee4d..e6e9b45 100644 --- a/src/modules/Base.ts +++ b/src/modules/Base.ts @@ -1,6 +1,6 @@ import { FindOrCreate } from '@typegoose/typegoose/lib/defaultClasses' -import { checkJson } from '../decorators/nojson' -import { plugin, ReturnModelType } from '@typegoose/typegoose' +import { checkJson, noJson } from '../decorators/nojson' +import { plugin, prop, ReturnModelType } from '@typegoose/typegoose' // @ts-ignore import findOrCreate from 'mongoose-findorcreate' @@ -56,6 +56,15 @@ export abstract class BaseModule extends FindOrCreate { ) } + public static deleteRecords(this: ReturnModelType>, params) { + return this.updateMany(params, { + $set: { + deleted: true, + deleteTime: new Date(), + }, + }) + } + /** * 自定义分页查询 * @param data @@ -64,16 +73,16 @@ export abstract class BaseModule extends FindOrCreate { public static async pageQuery( this: ReturnModelType>, data: any, - json: boolean = false, + options?: any, ) { let { start, limit, page } = data - limit = +limit || 10 + limit = +limit || 20 start = +start || (+page - 1) * limit || 0 // @ts-ignore - let { opt, sort } = this.parseQueryParam(data) + let { opt, sort } = this.parseQueryParam(data, options) let records = await this.find(opt).sort(sort).skip(start).limit(limit) let total = await this.countDocuments(opt) - if (json) { + if (options?.json) { records.map((o: T) => o.toJson()) } return { records, total, start, limit } @@ -102,14 +111,18 @@ export abstract class BaseModule extends FindOrCreate { * @return {{opt: any, sort: {_id: number}}} */ public static parseQueryParam(params: {}, options?: any) { - const opt: any = { deleted: false } + const opt: any = { deleted: { $ne: true } } // @ts-ignore let obj = this.schema.paths for (let key in params) { if (key !== 'sort' && obj.hasOwnProperty(key)) { switch (obj[key].instance) { case 'String': - opt[key] = { $regex: params[key], $options: 'i' } + if (typeof params[key] === 'object') { + opt[key] = params[key] + } else { + opt[key] = { $regex: params[key], $options: 'i' } + } break case 'Number': opt[key] = params[key] @@ -133,6 +146,8 @@ export abstract class BaseModule extends FindOrCreate { } break } + } else if (key === '$or' || key == '$and') { + opt[key] = params[key] } } if (params.hasOwnProperty('key') && params['key']) { @@ -192,7 +207,7 @@ export abstract class BaseModule extends FindOrCreate { if (options?.opt) { Object.assign(opt, options.opt) } - let sort = { _id: 1 } + let sort = { _id: -1 } if (params.hasOwnProperty('sort')) { sort = params['sort'] } diff --git a/src/modules/TranRecord.ts b/src/modules/TranRecord.ts new file mode 100644 index 0000000..d5884f5 --- /dev/null +++ b/src/modules/TranRecord.ts @@ -0,0 +1,50 @@ +import { getModelForClass, index, modelOptions, mongoose, prop, Severity } from '@typegoose/typegoose' +import { dbconn } from 'decorators/dbconn' +import { BaseModule } from './Base' + +@dbconn() +@index({ transactionHash: 1 }, { unique: true }) +@index({ contract: 1, account: 1, tokenId: 1 }) +@modelOptions({ + schemaOptions: { collection: 'transaction_record', timestamps: true }, +}) +class TranRecordClass extends BaseModule { + @prop({ required: true }) + public transactionHash!: string + @prop({ required: true }) + public contract!: string + @prop({ required: true }) + public account!: string + @prop() + public blockNumber: number + @prop() + public blockHash: string + @prop() + public from: string + @prop() + public to: string + // 对于ERC20, 该值永远为'0' + @prop({ default: '0' }) + public tokenId: string + // 对于ERC721, 该值永远为'1' + @prop({ default: '1' }) + public amount: string + + @prop({ default: 0 }) + public status: number + + public toJson() { + return { + transactionHash: this.transactionHash, + contract: this.contract, + blockNumber: this.blockNumber, + blockHash: this.blockHash, + from: this.from, + to: this.to, + amount: this.amount, + tokenId: this.tokenId, + } + } +} + +export const TranRecord = getModelForClass(TranRecordClass, { existingConnection: TranRecordClass.db })