增加交易记录相关接口

This commit is contained in:
zhl 2023-01-12 11:10:06 +08:00
parent bf0e6c865a
commit 9e8d14ba84
3 changed files with 121 additions and 9 deletions

View File

@ -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
}
}

View File

@ -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<T extends BaseModule>(this: ReturnModelType<AnyParamConstructor<T>>, 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<T extends BaseModule>(
this: ReturnModelType<AnyParamConstructor<T>>,
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']
}

50
src/modules/TranRecord.ts Normal file
View File

@ -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 })