Skip to content

Commit

Permalink
fix(database): canModify extensions remove redundant nested fields ch…
Browse files Browse the repository at this point in the history
…eck, parse mongo nested update syntax (#1169)

* fix(database): canModify extensions check nested user input

* fix(database): canModify extensions redundant nested fields check
  • Loading branch information
Renc17 authored Sep 24, 2024
1 parent ba872ec commit be4adbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
**/resourceRegistry.log
.turbo
.DS_Store
**/env/
26 changes: 10 additions & 16 deletions modules/database/src/permissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DeclaredSchemaExtension, Schema } from '../interfaces/index.js';
import { ConduitModel, Indexable } from '@conduitplatform/grpc-sdk';
import { convertObjectToDotNotation } from '../adapters/sequelize-adapter/utils/extractors/index.js';
import { Indexable } from '@conduitplatform/grpc-sdk';

export async function canCreate(moduleName: string, schema: Schema) {
if (moduleName === 'database' && schema.originalSchema.name === '_DeclaredSchema')
Expand Down Expand Up @@ -30,20 +29,15 @@ export async function canModify(moduleName: string, schema: Schema, data?: Index
extension.ownerModule === moduleName || extension.ownerModule === 'database',
)
.map((extension: DeclaredSchemaExtension) => extension.fields)
.map((fields: ConduitModel) => {
const flattenedFields = {};
convertObjectToDotNotation(
fields,
flattenedFields,
undefined,
undefined,
'',
'.',
);
return Object.keys(flattenedFields);
})
.reduce((acc: string[], curr: string[]) => [...acc, ...curr], []);
const dataFields = Object.keys(data);
.reduce(
(acc: string[], curr: DeclaredSchemaExtension) => [...acc, ...Object.keys(curr)],
[],
);
const dataFields = Object.keys(data).reduce((acc: string[], curr: string) => {
const head = curr.split('.')[0];
if (!acc.includes(head)) acc.push(head);
return acc;
}, []);
return dataFields.every((field: string) => extensionFields.includes(field));
}
return false;
Expand Down

0 comments on commit be4adbd

Please sign in to comment.