Skip to content

Commit

Permalink
story #11854 fix(design-system): add missing interface in mocked sche…
Browse files Browse the repository at this point in the history
…ma service
  • Loading branch information
Regzox committed Jul 29, 2024
1 parent f2d28dd commit 86d819e
Showing 1 changed file with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Collection, SchemaElement } from '../models';
import { SchemaService } from 'vitamui-library';
import { Collection, Schema, SchemaElement } from '../models';
import { ItemNode } from '../components/autocomplete';
import { map } from 'rxjs';

@Injectable()
export class MockSchemaService extends SchemaService {
Expand Down Expand Up @@ -13480,11 +13482,53 @@ export class MockSchemaService extends SchemaService {
},
];

getSchema(): Observable<SchemaElement[]> {
getSchema(_collection: Collection): Observable<Schema> {
return of(this.schema);
}

getSchemas(): Observable<SchemaElement[][]> {
getSchemas(_collections: Collection[]): Observable<Schema[]> {
return of([this.schema]);
}

public getDescriptiveSchemaTree(): Observable<ItemNode<SchemaElement>[]> {
const recursiveSort = (node: ItemNode<SchemaElement>) => {
node.children.sort((n1, n2) =>
n1.children.length && !n2.children.length
? 1
: !n1.children.length && n2.children.length
? -1
: n1.item.ShortName.localeCompare(n2.item.ShortName),
);
node.children.forEach((n) => recursiveSort(n));
};

const removeLeavesWithTypeObject = (node: ItemNode<SchemaElement>) => {
node.children = node.children.filter((child) => !(child.item.Type === 'OBJECT' && !child.children.length));
node.children.forEach((child) => removeLeavesWithTypeObject(child));
};

return this.getSchema(Collection.ARCHIVE_UNIT).pipe(
map((schema) => {
const rootNode = schema
.filter((e) => e.Category === 'DESCRIPTION' || e.Origin === 'EXTERNAL')
.reduce(
(acc, element) => {
const path = element.Path.split('.').slice(0, -1);
const parentNode = path.reduce((currentItem, p) => currentItem.children.find((n) => n.item.FieldName === p), acc) || acc;
parentNode.children.push({
item: element,
children: [],
});
return acc;
},
{ children: [] } as ItemNode<SchemaElement>,
);

removeLeavesWithTypeObject(rootNode);

recursiveSort(rootNode);
return rootNode.children;
}),
);
}
}

0 comments on commit 86d819e

Please sign in to comment.