Files
task-stream/libs/api/tracker/application/src/lib/proposal-quota.service.ts
Josep Piguillem 1383e15f3f fix(api): the API failed to start after moving the proposal limit to Redis
ProposalLimiter, in the presenters, asked for the RateLimitStore port,
which only the application layer sees, so Nest could not build it and
the container never became healthy. The count now lives in an
application service, ProposalQuota; the presenter only answers 429.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 21:01:38 +02:00

22 lines
682 B
TypeScript

import { Injectable } from '@nestjs/common';
import { RateLimitStore } from './ports/rate-limit.store';
/** Proposals from one address within the window. */
const LIMIT = 5;
const WINDOW_MS = 15 * 60_000;
/**
* Keeps one address from flooding a project with proposals. Counted in
* Redis, so the limit holds across every instance of the API.
*/
@Injectable()
export class ProposalQuota {
constructor(private readonly store: RateLimitStore) {}
/** Counts one more proposal from `address`; false once it is over. */
async take(address: string): Promise<boolean> {
const count = await this.store.hit(`proposals:${address}`, WINDOW_MS);
return count <= LIMIT;
}
}