( req: MonkeyRequest<GetResultsQuery>, )
| 82 | } |
| 83 | |
| 84 | export async function getResults( |
| 85 | req: MonkeyRequest<GetResultsQuery>, |
| 86 | ): Promise<GetResultsResponse> { |
| 87 | const { uid } = req.ctx.decodedToken; |
| 88 | const premiumFeaturesEnabled = req.ctx.configuration.users.premium.enabled; |
| 89 | const { onOrAfterTimestamp = NaN, offset = 0 } = req.query; |
| 90 | const userHasPremium = await UserDAL.checkIfUserIsPremium(uid); |
| 91 | |
| 92 | const maxLimit = |
| 93 | premiumFeaturesEnabled && userHasPremium |
| 94 | ? req.ctx.configuration.results.limits.premiumUser |
| 95 | : req.ctx.configuration.results.limits.regularUser; |
| 96 | |
| 97 | let limit = |
| 98 | req.query.limit ?? |
| 99 | Math.min(req.ctx.configuration.results.maxBatchSize, maxLimit); |
| 100 | |
| 101 | //check if premium features are disabled and current call exceeds the limit for regular users |
| 102 | if ( |
| 103 | userHasPremium && |
| 104 | !premiumFeaturesEnabled && |
| 105 | limit + offset > req.ctx.configuration.results.limits.regularUser |
| 106 | ) { |
| 107 | throw new MonkeyError(503, "Premium feature disabled."); |
| 108 | } |
| 109 | |
| 110 | if (limit + offset > maxLimit) { |
| 111 | if (offset < maxLimit) { |
| 112 | //batch is partly in the allowed ranged. Set the limit to the max allowed and return partly results. |
| 113 | limit = maxLimit - offset; |
| 114 | } else { |
| 115 | throw new MonkeyError(422, `Max results limit of ${maxLimit} exceeded.`); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | const results = await ResultDAL.getResults(uid, { |
| 120 | onOrAfterTimestamp, |
| 121 | limit, |
| 122 | offset, |
| 123 | }); |
| 124 | void addLog( |
| 125 | "user_results_requested", |
| 126 | { |
| 127 | limit, |
| 128 | offset, |
| 129 | onOrAfterTimestamp, |
| 130 | isPremium: userHasPremium, |
| 131 | }, |
| 132 | uid, |
| 133 | ); |
| 134 | |
| 135 | return new MonkeyResponse("Results retrieved", replaceObjectIds(results)); |
| 136 | } |
| 137 | |
| 138 | export async function getResultById( |
| 139 | req: MonkeyRequest<undefined, undefined, GetResultByIdPath>, |
nothing calls this directly
no test coverage detected