add interface for psdoc

This commit is contained in:
undefined committed 2020-09-11 23:27:15 +08:00
1 parent f723e2815f
commit 084688f6c1
3 files changed
+69 -89

No files matched your search

+19 -14
View File
@@ -3,6 +3,8 @@ import { ObjectID, GridFSBucket } from 'mongodb';
import fs from 'fs'; import fs from 'fs';
import { Dictionary, NumericDictionary } from 'lodash'; import { Dictionary, NumericDictionary } from 'lodash';
type document = typeof import('./model/document');
export type NumberKeys<O> = { export type NumberKeys<O> = {
[K in keyof O]: number extends O[K] ? K : never [K in keyof O]: number extends O[K] ? K : never
}[keyof O]; }[keyof O];
@@ -193,6 +195,14 @@ export interface StatusDoc {
uid: number, uid: number,
} }
export interface Document {
_id: ObjectID,
docId: any,
docType: number,
domainId: string,
owner: number,
}
export interface ProblemStatusDoc extends StatusDoc { export interface ProblemStatusDoc extends StatusDoc {
docId: number, docId: number,
docType: 10, docType: 10,
@@ -265,12 +275,9 @@ export interface TrainingNode {
pids: number[], pids: number[],
} }
export interface Tdoc { export interface Tdoc extends Document {
_id: ObjectID,
domainId: string,
docId: ObjectID, docId: ObjectID,
docType: number, docType: document['TYPE_CONTEST'] | document['TYPE_HOMEWORK'] | document['TYPE_TRAINING'],
owner: number,
beginAt: Date, beginAt: Date,
endAt: Date, endAt: Date,
attend: number, attend: number,
@@ -278,6 +285,7 @@ export interface Tdoc {
content: string, content: string,
rule: string, rule: string,
pids: number[], pids: number[],
rated?: boolean,
// For homework // For homework
penaltySince?: Date, penaltySince?: Date,
@@ -329,13 +337,11 @@ export interface Bdoc {
} }
// Discussion // Discussion
export interface Ddoc { export interface Ddoc extends Document {
_id: ObjectID, docType: document['TYPE_DISCUSSION'],
docType: number,
docId: ObjectID, docId: ObjectID,
parentType: number, parentType: number,
parentId: ObjectID | number | string, parentId: ObjectID | number | string,
owner: number,
title: string, title: string,
content: string, content: string,
ip: string, ip: string,
@@ -343,16 +349,15 @@ export interface Ddoc {
highlight: boolean, highlight: boolean,
updateAt: Date, updateAt: Date,
nReply: number, nReply: number,
views: number,
} }
// Discussion reply // Discussion reply
export interface Drdoc { export interface Drdoc extends Document {
_id: ObjectID, docType: document['TYPE_DISCUSSION_REPLY'],
docType: number,
docId: ObjectID, docId: ObjectID,
parentType: number, parentType: document['TYPE_DISCUSSION'],
parentId: ObjectID, parentId: ObjectID,
owner: number,
ip: string, ip: string,
content: string, content: string,
reply: Drrdoc[], reply: Drrdoc[],
+1 -1
View File
@@ -557,7 +557,7 @@ export async function recalcStatus(domainId: string, tid: ObjectID, type: Type =
tasks.push( tasks.push(
document.revSetStatus( document.revSetStatus(
domainId, type, tid, domainId, type, tid,
tsdoc.uid, tsdoc.rev, { journal, ...stats }, false, tsdoc.uid, tsdoc.rev, { journal, ...stats },
), ),
); );
} }
+49 -74
View File
@@ -1,3 +1,4 @@
/* eslint-disable object-curly-newline */
import assert from 'assert'; import assert from 'assert';
import { import {
ObjectID, Cursor, FilterQuery, UpdateQuery, ObjectID, Cursor, FilterQuery, UpdateQuery,
@@ -5,7 +6,7 @@ import {
import * as db from '../service/db'; import * as db from '../service/db';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import { import {
Pdoc, Ddoc, Ufdoc, Drdoc, Tdoc, TrainingDoc, NumberKeys, ArrayKeys, ProblemStatusDoc, Pdoc, Ddoc, Drdoc, Tdoc, TrainingDoc, NumberKeys, ArrayKeys, ProblemStatusDoc,
} from '../interface'; } from '../interface';
type DocID = ObjectID | string | number; type DocID = ObjectID | string | number;
@@ -33,7 +34,7 @@ export interface DocType {
[TYPE_DISCUSSION_REPLY]: Drdoc, [TYPE_DISCUSSION_REPLY]: Drdoc,
[TYPE_CONTEST]: Tdoc, [TYPE_CONTEST]: Tdoc,
[TYPE_TRAINING]: TrainingDoc, [TYPE_TRAINING]: TrainingDoc,
[TYPE_FILE]: Ufdoc, [TYPE_FILE]: any,
[TYPE_HOMEWORK]: Tdoc, [TYPE_HOMEWORK]: Tdoc,
} }
@@ -42,17 +43,17 @@ export interface DocStatusType {
[key: number]: any [key: number]: any
} }
export async function add<T extends DocID, K extends keyof DocType>( export async function add<T extends keyof DocType, K extends DocType[T]['docId']>(
domainId: string, content: string, owner: number, domainId: string, content: string, owner: number,
docType: K, docId: DocType[K]['docId'], docType: T, docId: K,
parentType?: number | null, parentId?: DocID, parentType?: DocType[T]['parentType'], parentId?: DocType[T]['parentId'],
args?: any, args?: Partial<DocType[T]>,
): Promise<T> ): Promise<K>
export async function add<K extends keyof DocType>( export async function add<T extends keyof DocType>(
domainId: string, content: string, owner: number, domainId: string, content: string, owner: number,
docType: K, docId: null, docType: T, docId: null,
parentType?: number, parentId?: DocID, parentType?: DocType[T]['parentType'], parentId?: DocType[T]['parentId'],
args?: any, args?: Partial<DocType[T]>,
): Promise<ObjectID> ): Promise<ObjectID>
export async function add( export async function add(
domainId: string, content: string, owner: number, domainId: string, content: string, owner: number,
@@ -160,13 +161,7 @@ export async function push<K extends keyof DocType, T extends keyof DocType[K]>(
const _id = new ObjectID(); const _id = new ObjectID();
const doc = await coll.findOneAndUpdate( const doc = await coll.findOneAndUpdate(
{ domainId, docType, docId }, { domainId, docType, docId },
{ { $push: { [key]: { ...args, content, owner, _id } } },
$push: {
[key]: {
...args, content, owner, _id,
},
},
},
{ returnOriginal: false }, { returnOriginal: false },
); );
return [doc.value, _id]; return [doc.value, _id];
@@ -215,7 +210,7 @@ export async function getSub<T extends keyof DocType, K extends ArrayKeys<DocTyp
export async function setSub<T extends keyof DocType, K extends ArrayKeys<DocType[T]>>( export async function setSub<T extends keyof DocType, K extends ArrayKeys<DocType[T]>>(
domainId: string, docType: T, docId: DocType[T]['docId'], domainId: string, docType: T, docId: DocType[T]['docId'],
key: K, subId: ObjectID, args: UpdateQuery<DocType[T][K][0]>['$set'], key: K, subId: DocType[T][K][0]['_id'], args: Partial<DocType[T][K][0]>,
): Promise<DocType[T]> { ): Promise<DocType[T]> {
const $set = {}; const $set = {};
for (const k in args) { for (const k in args) {
@@ -246,12 +241,10 @@ export async function addToSet<T extends keyof DocType, K extends ArrayKeys<DocT
return res.value; return res.value;
} }
export function getStatus<K extends keyof DocStatusType>( export async function getStatus<K extends keyof DocStatusType>(
domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number,
): Promise<DocStatusType[K]> { ): Promise<DocStatusType[K]> {
return collStatus.findOne({ return await collStatus.findOne({ domainId, docType, docId, uid });
domainId, docType, docId, uid,
});
} }
export function getMultiStatus<K extends keyof DocStatusType>( export function getMultiStatus<K extends keyof DocStatusType>(
@@ -270,111 +263,93 @@ export async function setStatus<K extends keyof DocStatusType>(
domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, args: UpdateQuery<DocStatusType[K]>['$set'], domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, args: UpdateQuery<DocStatusType[K]>['$set'],
): Promise<DocStatusType[K]> { ): Promise<DocStatusType[K]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid },
domainId, docType, docId, uid,
},
{ $set: args }, { $set: args },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export function setMultiStatus<K extends keyof DocStatusType>( export async function setMultiStatus<K extends keyof DocStatusType>(
domainId: string, docType: K, query: FilterQuery<DocStatusType[K]>, args: UpdateQuery<DocStatusType[K]>['$set'], domainId: string, docType: K, query: FilterQuery<DocStatusType[K]>, args: Partial<DocStatusType[K]>,
) { ) {
return collStatus.updateMany( return await collStatus.updateMany(
{ domainId, docType, ...query }, { domainId, docType, ...query },
{ $set: args }, { $set: args },
); );
} }
export async function setIfNotStatus( export async function setIfNotStatus<T extends keyof DocStatusType, K extends keyof DocStatusType[T]>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: string, value: number, ifNot: any, args: any, key: K, value: number, ifNot: DocStatusType[T][K], args: Partial<DocStatusType[T]>,
) { ): Promise<DocStatusType[T]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid, [key]: { $not: { $eq: ifNot } } },
domainId, docType, docId, uid, key: { $not: { $eq: ifNot } },
},
{ $set: { [key]: value, ...args } }, { $set: { [key]: value, ...args } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export async function cappedIncStatus( export async function cappedIncStatus<T extends keyof DocStatusType>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: string, value: number, minValue = -1, maxValue = 1, key: NumberKeys<DocStatusType[T]>, value: number, minValue = -1, maxValue = 1,
) { ): Promise<DocStatusType[T]> {
assert(value !== 0); assert(value !== 0);
const $not = value > 0 ? { $gte: maxValue } : { $lte: minValue }; const $not = value > 0 ? { $gte: maxValue } : { $lte: minValue };
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid, [key]: { $not } },
domainId, docType, docId, uid, [key]: { $not },
},
{ $inc: { [key]: value } }, { $inc: { [key]: value } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export async function incStatus( export async function incStatus<T extends keyof DocStatusType>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: string, value: number, key: NumberKeys<DocStatusType[T]>, value: number,
) { ): Promise<DocStatusType[T]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid },
domainId, docType, docId, uid,
},
{ $inc: { [key]: value } }, { $inc: { [key]: value } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export async function revPushStatus( export async function revPushStatus<T extends keyof DocStatusType>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: string, value: any, key: ArrayKeys<DocStatusType[T]>, value: any,
) { ): Promise<DocStatusType[T]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid },
domainId, docType, docId, uid,
},
{ $push: { [key]: value }, $inc: { rev: 1 } }, { $push: { [key]: value }, $inc: { rev: 1 } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export async function revInitStatus( export async function revInitStatus<T extends keyof DocStatusType>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
) { ): Promise<DocStatusType[T]> {
const res = await collStatus.findOneAndUpdate( const res = await collStatus.findOneAndUpdate(
{ { domainId, docType, docId, uid },
domainId, docType, docId, uid,
},
{ $inc: { rev: 1 } }, { $inc: { rev: 1 } },
{ upsert: true, returnOriginal: false }, { upsert: true, returnOriginal: false },
); );
return res.value; return res.value;
} }
export async function revSetStatus( export async function revSetStatus<T extends keyof DocStatusType>(
domainId: string, docType: number, docId: DocID, uid: number, domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
rev: number, args: any, returnDoc = true, rev: number, args: Partial<DocStatusType[T]>,
) { ): Promise<any> {
const filter = { const filter = { domainId, docType, docId, uid, rev };
domainId, docType, docId, uid, rev,
};
const update = { $set: args, $inc: { rev: 1 } }; const update = { $set: args, $inc: { rev: 1 } };
if (returnDoc) {
const res = await collStatus.findOneAndUpdate(filter, update, { returnOriginal: false }); const res = await collStatus.findOneAndUpdate(filter, update, { returnOriginal: false });
return res.value; return res.value;
} }
return await collStatus.updateOne(filter, update);
}
/* eslint-disable object-curly-newline */
async function ensureIndexes() { async function ensureIndexes() {
const ic = coll.createIndex.bind(coll); const ic = coll.createIndex.bind(coll);
const is = collStatus.createIndex.bind(coll); const is = collStatus.createIndex.bind(coll);