Skip to content

Commit

Permalink
feat: add customizable preset limit
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoperra committed Apr 27, 2023
1 parent 0a2ba9a commit 9a99209
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module 'fastify' {
CLIENT_SECRET_AUTH?: string;
GRANT_TYPE_AUTH0?: string;
ALLOWED_ORIGINS?: string;
PRESETS_LIMIT?: number;
};
}
}
Expand Down Expand Up @@ -45,6 +46,7 @@ const app: FastifyPluginAsync<AppOptions> = async (
AUDIENCE_AUTH0: Type.RegEx(/^https?:/),
GRANT_TYPE_AUTH0: Type.String(),
ALLOWED_ORIGINS: Type.String(),
PRESETS_LIMIT: Type.Number({default: Number.MAX_SAFE_INTEGER}),
}),
});

Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/common/exceptions/UnprocessableEntityException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {HandlerError} from './HandlerError';

export abstract class UnprocessableEntityException<
Args extends Record<string, string | number> | void = void,
> extends HandlerError<Args> {
statusCode = HandlerError.httpStatusCode.HTTP_STATUS_UNPROCESSABLE_ENTITY;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
abstract createMessage(args: Args): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {UnprocessableEntityException} from '../../../common/exceptions/UnprocessableEntityException';

export class ExceedPresetLimitException extends UnprocessableEntityException<{
limit: number;
}> {
limit: number;

constructor(options: {limit: number}) {
super();
this.limit = options.limit;
}

createMessage(): string {
return `Preset limit per account exceeded`;
}
}
8 changes: 7 additions & 1 deletion apps/api/src/modules/preset/handlers/create.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import {HandlerBuilder} from '../../../common/domainFunctions/builder';
import {ExceedPresetLimitException} from '../exceptions/ExceedPresetLimitException';
import {PresetCreateDto} from '../schema/preset-create-dto.schema';
import {PresetDto} from '../schema/preset-dto.schema';
import {PresetHandlerDependencies} from './';

export const create =
HandlerBuilder.withDependencies<PresetHandlerDependencies>()
.withName('createPreset')
.withImplementation(({repository, mapper}) => {
.withImplementation(({repository, mapper, config, logger}) => {
return async (
ownerId: string,
data: PresetCreateDto,
): Promise<PresetDto> => {
const count = repository.countByOwnerId(ownerId);
if (config.PRESETS_LIMIT && (await count) > config.PRESETS_LIMIT) {
throw new ExceedPresetLimitException({limit: config.PRESETS_LIMIT});
}

const createdPreset = await repository.create({
name: data.name,
data: data.data,
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/modules/preset/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {FastifyInstance} from 'fastify';
import {PresetMapper} from '../mapper';
import {PresetRepository} from '../repository';

export type PresetHandlerDependencies = {
repository: PresetRepository;
mapper: PresetMapper;
config: FastifyInstance['config'];
logger: FastifyInstance['log'];
};
7 changes: 6 additions & 1 deletion apps/api/src/modules/preset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ const preset: FastifyPluginAsync = async fastify => {
'presetService',
registerHandlers(
handlers,
{repository: fastify.presetRepository, mapper},
{
repository: fastify.presetRepository,
mapper,
config: fastify.config,
logger: fastify.log,
},
fastify.handlerRegistry,
),
);
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/modules/preset/repository/preset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface PresetRepository {

findAllByOwnerId(ownerId: string): Promise<readonly Preset[]>;

countByOwnerId(ownerId: string): Promise<number>;

create(data: PresetCreateRequest): Promise<Preset>;

update(id: string, data: PresetUpdateRequest): Promise<Preset>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class PrismaPresetRepository implements PresetRepository {
});
}

countByOwnerId(ownerId: string): Promise<number> {
return this.client.preset.count({where: {ownerId}});
}

update(id: string, data: PresetCreateRequest): Promise<Preset> {
return this.client.preset.update({
where: {
Expand Down
8 changes: 3 additions & 5 deletions apps/api/src/plugins/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {NotFoundEntityException} from '../common/exceptions/NotFoundEntityExcept

export default fp(
async fastify => {
fastify.setErrorHandler(error => {
fastify.setErrorHandler((error, request, reply) => {
let httpError: HttpError | null = null;

if (error.statusCode) {
Expand All @@ -22,13 +22,11 @@ export default fp(
httpError.stack = error.stack;
httpError.code = error.code;

return httpError;
return reply.send(httpError);
}

return error;
reply.send(error);
});

return;
},
{name: 'appErrorsHandler'},
);

0 comments on commit 9a99209

Please sign in to comment.