Skip to content

Commit 2b80daa

Browse files
committed
fix(plugin): improve input validation and data handling
This commit addresses several issues related to input validation and data handling within the plugin registry. In `create-plugin-command.handler.ts`, the validation logic has been refined to more accurately check for required version information, particularly when `version.sources` is present but empty. The destructuring of `input` has also been moved after the validation to prevent potential errors. In `update-plugin-command.handler.ts`, a check for `input` being null or undefined has been added before attempting to access its properties, preventing potential runtime errors. Additionally, the update logic now correctly uses `input.propertyName` instead of `input?.propertyName` for mandatory fields. In `plugin-billing.controller.ts`, the condition for applying search filters has been made more explicit by introducing a `hasFilters` variable, improving readability. This change ensures that filters are only applied when there are actual search options defined.
1 parent 3e04acd commit 2b80daa

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

packages/plugins/registry/src/lib/application/plugin/commands/handlers/create-plugin-command.handler.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ export class CreatePluginCommandHandler implements ICommandHandler<CreatePluginC
2626
*/
2727
public async execute(command: CreatePluginCommand): Promise<IPlugin> {
2828
const { input } = command;
29-
const { subscriptionPlans = [], ...pluginInput } = input;
3029

31-
// Validate input
32-
if (
33-
!pluginInput ||
34-
!pluginInput.version ||
35-
(pluginInput.version.sources && pluginInput.version.sources.length === 0 && !pluginInput.version)
36-
) {
30+
if (!input || !input.version || (Array.isArray(input.version.sources) && input.version.sources.length === 0)) {
3731
throw new BadRequestException('Invalid plugin data: Source requires version information');
3832
}
3933

34+
const { subscriptionPlans = [], ...pluginInput } = input;
35+
4036
const queryRunner = this.dataSource.createQueryRunner();
4137
await queryRunner.connect();
4238
await queryRunner.startTransaction();

packages/plugins/registry/src/lib/application/plugin/commands/handlers/update-plugin-command.handler.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class UpdatePluginCommandHandler implements ICommandHandler<UpdatePluginC
3232
throw new BadRequestException('Plugin ID is required');
3333
}
3434

35+
if (!input) {
36+
throw new BadRequestException('Plugin update input is required');
37+
}
38+
3539
// Start a transaction for updating the plugin and related entities
3640
const queryRunner = this.dataSource.createQueryRunner();
3741
await queryRunner.connect();
@@ -68,17 +72,17 @@ export class UpdatePluginCommandHandler implements ICommandHandler<UpdatePluginC
6872
}
6973
} // Update plugin with only provided fields
7074
const pluginUpdate: Partial<IPlugin> = {
71-
name: input?.name,
72-
type: input?.type,
73-
status: input?.status,
74-
description: input?.description,
75-
isActive: input?.isActive,
76-
repository: input?.repository,
77-
author: input?.author,
78-
license: input?.license,
79-
homepage: input?.homepage,
80-
requiresSubscription: input?.requiresSubscription,
81-
...(input?.categoryId && { categoryId: input.categoryId })
75+
name: input.name,
76+
type: input.type,
77+
status: input.status,
78+
description: input.description,
79+
isActive: input.isActive,
80+
repository: input.repository,
81+
author: input.author,
82+
license: input.license,
83+
homepage: input.homepage,
84+
requiresSubscription: input.requiresSubscription,
85+
...(input.categoryId && { categoryId: input.categoryId })
8286
};
8387
await this.pluginService.update(id, pluginUpdate);
8488

packages/plugins/registry/src/lib/infrastructure/controllers/plugin-billing.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ export class PluginBillingController {
9797
searchOptions.status = statusMap[status];
9898
}
9999

100-
if (searchOptions && Object.keys(searchOptions).length > 0) {
100+
const hasFilters = Object.keys(searchOptions).length > 0;
101+
if (hasFilters) {
101102
const result = await this.pluginBillingService.findBillings(searchOptions);
102103
return {
103104
items: result,

0 commit comments

Comments
 (0)