core: contest: simplify

This commit is contained in:
undefined committed 2022-12-22 23:38:32 +08:00
1 parent 534fc4bb3b
commit 8024122e8f
9 files changed
+32 -62

No files matched your search

+4 -1
View File
@@ -350,12 +350,15 @@ export async function setStatusIfNotCondition<T extends keyof DocStatusType>(
export async function cappedIncStatus<T extends keyof DocStatusType>(
domainId: string, docType: T, docId: DocStatusType[T]['docId'], uid: number,
key: NumberKeys<DocStatusType[T]>, value: number, minValue = -1, maxValue = 1,
setPayload: Partial<DocStatusType[T]> = {},
): Promise<DocStatusType[T]> {
assert(value !== 0);
const $not = value > 0 ? { $gte: maxValue } : { $lte: minValue };
const operation = { $inc: { [key]: value } } as UpdateQuery<DocStatusType[T]>;
if (Object.keys(setPayload).length) operation.$set = setPayload;
const res = await collStatus.findOneAndUpdate(
{ domainId, docType, docId, uid, [key]: { $not } },
{ $inc: { [key]: value } },
operation,
{ upsert: true, returnDocument: 'after' },
);
return res.value;