diff --git a/server/src/lib/targets/milestones.ts b/server/src/lib/targets/milestones.ts index 09e3d7679..629baa426 100644 --- a/server/src/lib/targets/milestones.ts +++ b/server/src/lib/targets/milestones.ts @@ -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; 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, diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts index abcdfef10..b94147d9c 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/targets/milestones/router.ts @@ -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 }, }); } );