MCPcopy Create free account
hub / github.com/monkeytypegame/monkeytype / updateStreak

Function updateStreak

backend/src/dal/user.ts:1124–1157  ·  view source on GitHub ↗
(
  uid: string,
  timestamp: number,
)

Source from the content-addressed store, hash-verified

1122}
1123
1124export async function updateStreak(
1125 uid: string,
1126 timestamp: number,
1127): Promise<number> {
1128 const user = await getPartialUser(uid, "calculate streak", ["streak"]);
1129 const streak: UserStreak = {
1130 lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0,
1131 length: user.streak?.length ?? 0,
1132 maxLength: user.streak?.maxLength ?? 0,
1133 hourOffset: user.streak?.hourOffset,
1134 };
1135
1136 if (isYesterday(streak.lastResultTimestamp, streak.hourOffset ?? 0)) {
1137 streak.length += 1;
1138 } else if (!isToday(streak.lastResultTimestamp, streak.hourOffset ?? 0)) {
1139 void addImportantLog("streak_lost", streak, uid);
1140 streak.length = 1;
1141 }
1142
1143 if (streak.length > streak.maxLength) {
1144 streak.maxLength = streak.length;
1145 }
1146
1147 streak.lastResultTimestamp = timestamp;
1148
1149 if (user.streak?.hourOffset === 0) {
1150 // todo this needs to be removed after a while
1151 delete streak.hourOffset;
1152 }
1153
1154 await getUsersCollection().updateOne({ uid }, { $set: { streak } });
1155
1156 return streak.length;
1157}
1158
1159export async function setStreakHourOffset(
1160 uid: string,

Callers

nothing calls this directly

Calls 5

isYesterdayFunction · 0.90
isTodayFunction · 0.90
addImportantLogFunction · 0.90
getPartialUserFunction · 0.85
getUsersCollectionFunction · 0.85

Tested by

no test coverage detected