-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
result.ts
129 lines (117 loc) · 3.38 KB
/
result.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import _ from "lodash";
import {
Collection,
type DeleteResult,
ObjectId,
type UpdateResult,
} from "mongodb";
import MonkeyError from "../utils/error";
import * as db from "../init/db";
import { DBResult as SharedDBResult } from "@monkeytype/shared-types";
import { getUser, getTags } from "./user";
import { Mode } from "@monkeytype/contracts/schemas/shared";
type DBResult = MonkeyTypes.WithObjectId<SharedDBResult<Mode>>;
export const getResultCollection = (): Collection<DBResult> =>
db.collection<DBResult>("results");
export async function addResult(
uid: string,
result: MonkeyTypes.DBResult
): Promise<{ insertedId: ObjectId }> {
let user: MonkeyTypes.DBUser | null = null;
try {
user = await getUser(uid, "add result");
} catch (e) {
user = null;
}
if (!user) throw new MonkeyError(404, "User not found", "add result");
if (result.uid === undefined) result.uid = uid;
// result.ir = true;
const res = await getResultCollection().insertOne(result);
return {
insertedId: res.insertedId,
};
}
export async function deleteAll(uid: string): Promise<DeleteResult> {
return await getResultCollection().deleteMany({ uid });
}
export async function updateTags(
uid: string,
resultId: string,
tags: string[]
): Promise<UpdateResult> {
const result = await getResultCollection().findOne({
_id: new ObjectId(resultId),
uid,
});
if (!result) throw new MonkeyError(404, "Result not found");
const userTags = await getTags(uid);
const userTagIds = userTags.map((tag) => tag._id.toString());
let validTags = true;
tags.forEach((tagId) => {
if (!userTagIds.includes(tagId)) validTags = false;
});
if (!validTags) {
throw new MonkeyError(422, "One of the tag id's is not valid");
}
return await getResultCollection().updateOne(
{ _id: new ObjectId(resultId), uid },
{ $set: { tags } }
);
}
export async function getResult(
uid: string,
id: string
): Promise<MonkeyTypes.DBResult> {
const result = await getResultCollection().findOne({
_id: new ObjectId(id),
uid,
});
if (!result) throw new MonkeyError(404, "Result not found");
return result;
}
export async function getLastResult(
uid: string
): Promise<Omit<MonkeyTypes.DBResult, "uid">> {
const [lastResult] = await getResultCollection()
.find({ uid })
.sort({ timestamp: -1 })
.limit(1)
.toArray();
if (!lastResult) throw new MonkeyError(404, "No results found");
return _.omit(lastResult, "uid");
}
export async function getResultByTimestamp(
uid: string,
timestamp
): Promise<MonkeyTypes.DBResult | null> {
return await getResultCollection().findOne({ uid, timestamp });
}
type GetResultsOpts = {
onOrAfterTimestamp?: number;
limit?: number;
offset?: number;
};
export async function getResults(
uid: string,
opts?: GetResultsOpts
): Promise<MonkeyTypes.DBResult[]> {
const { onOrAfterTimestamp, offset, limit } = opts ?? {};
let query = getResultCollection()
.find({
uid,
...(!_.isNil(onOrAfterTimestamp) &&
!_.isNaN(onOrAfterTimestamp) && {
timestamp: { $gte: onOrAfterTimestamp },
}),
})
.sort({ timestamp: -1 });
if (limit !== undefined) {
query = query.limit(limit);
}
if (offset !== undefined) {
query = query.skip(offset);
}
const results = await query.toArray();
if (results === undefined) throw new MonkeyError(404, "Result not found");
return results;
}