mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-06 13:58:05 +03:00
Finish up everything.
This commit is contained in:
@@ -2,6 +2,7 @@ import db from "external/mongo/db";
|
||||
import { SubscribeFailReasons } from "lib/constants/err-codes";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import {
|
||||
GoalDocument,
|
||||
GoalSubscriptionDocument,
|
||||
integer,
|
||||
MilestoneDocument,
|
||||
@@ -167,6 +168,12 @@ export async function EvaluateMilestoneProgress(userID: integer, milestone: Mile
|
||||
};
|
||||
}
|
||||
|
||||
interface MilestoneSubscriptionReturns {
|
||||
milestoneSub: MilestoneSubscriptionDocument;
|
||||
goals: GoalDocument[];
|
||||
goalResults: EvaluatedGoalResult[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes the given user to a provided milestone. If the user is already subscribed,
|
||||
* null is returned.
|
||||
@@ -174,11 +181,20 @@ export async function EvaluateMilestoneProgress(userID: integer, milestone: Mile
|
||||
* @param cancelIfAchieved - Don't subscribe to the milestone if subscribing would cause
|
||||
* the user to immediately achieve it.
|
||||
*/
|
||||
export async function SubscribeToMilestone(
|
||||
userID: integer,
|
||||
milestone: MilestoneDocument,
|
||||
cancelIfAchieved: false
|
||||
): Promise<MilestoneSubscriptionReturns | SubscribeFailReasons.ALREADY_SUBSCRIBED>;
|
||||
export async function SubscribeToMilestone(
|
||||
userID: integer,
|
||||
milestone: MilestoneDocument,
|
||||
cancelIfAchieved = true
|
||||
) {
|
||||
): Promise<
|
||||
| MilestoneSubscriptionReturns
|
||||
| SubscribeFailReasons.ALREADY_SUBSCRIBED
|
||||
| SubscribeFailReasons.ALREADY_ACHIEVED
|
||||
> {
|
||||
const isSubscribedToMilestone = await db["milestone-subs"].findOne({
|
||||
userID,
|
||||
milestoneID: milestone.milestoneID,
|
||||
|
||||
+3
-1
@@ -113,6 +113,7 @@ router.get("/:milestoneID", GetMilestone, GetMilestoneSubscription, async (req,
|
||||
}.`,
|
||||
body: {
|
||||
milestoneSub,
|
||||
milestone,
|
||||
results,
|
||||
goals,
|
||||
},
|
||||
@@ -164,6 +165,7 @@ router.put(
|
||||
// Users should be able to subscribe to milestones EVEN IF they would instantly
|
||||
// achieve them.
|
||||
|
||||
// This is impossible, as the third argument to the above function is false!
|
||||
// if (subResult === SubscribeFailReasons.ALREADY_ACHIEVED) {
|
||||
// return res.status(400).json({
|
||||
// success: false,
|
||||
@@ -181,7 +183,7 @@ router.put(
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
description: `Subscribed to milestone '${milestone.name}'.`,
|
||||
body: subResult,
|
||||
body: { ...subResult, milestone },
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user