-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new entities can be completely defined through the config (#1670)
closes #1056 Co-authored-by: Sebastian <sebastian.leidig@gmail.com>
- Loading branch information
1 parent
a7cdc75
commit db457c4
Showing
8 changed files
with
252 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...pp/core/entity-components/entity-details/related-entities/related-entities.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<app-entity-subrecord | ||
[records]="data" | ||
[filter]="filter" | ||
[columns]="columns" | ||
[newRecordFactory]="createNewRecordFactory()" | ||
></app-entity-subrecord> |
80 changes: 80 additions & 0 deletions
80
...core/entity-components/entity-details/related-entities/related-entities.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
|
||
import { RelatedEntitiesComponent } from "./related-entities.component"; | ||
import { MockedTestingModule } from "../../../../utils/mocked-testing.module"; | ||
import { EntityMapperService } from "../../../entity/entity-mapper.service"; | ||
import { Child } from "../../../../child-dev-project/children/model/child"; | ||
import { ChildSchoolRelation } from "../../../../child-dev-project/children/model/childSchoolRelation"; | ||
|
||
describe("RelatedEntitiesComponent", () => { | ||
let component: RelatedEntitiesComponent; | ||
let fixture: ComponentFixture<RelatedEntitiesComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [RelatedEntitiesComponent, MockedTestingModule.withState()], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(RelatedEntitiesComponent); | ||
component = fixture.componentInstance; | ||
await component.onInitFromDynamicConfig({ | ||
entity: new Child(), | ||
config: { | ||
entity: ChildSchoolRelation.ENTITY_TYPE, | ||
property: "childId", | ||
columns: [], | ||
}, | ||
}); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it("should create", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it("should load the entities which are linked with the passed one", async () => { | ||
const c1 = new Child(); | ||
const c2 = new Child(); | ||
const r1 = new ChildSchoolRelation(); | ||
r1.childId = c1.getId(); | ||
const r2 = new ChildSchoolRelation(); | ||
r2.childId = c1.getId(); | ||
const r3 = new ChildSchoolRelation(); | ||
r3.childId = c2.getId(); | ||
const entityMapper = TestBed.inject(EntityMapperService); | ||
await entityMapper.saveAll([c1, c2, r1, r2, r3]); | ||
const columns = ["start", "end", "schoolId"]; | ||
const filter = { start: { $exists: true } } as any; | ||
|
||
await component.onInitFromDynamicConfig({ | ||
entity: c1, | ||
config: { | ||
entity: ChildSchoolRelation.ENTITY_TYPE, | ||
property: "childId", | ||
columns, | ||
filter, | ||
}, | ||
}); | ||
|
||
expect(component.columns).toBe(columns); | ||
expect(component.data).toEqual([r1, r2, r3]); | ||
expect(component.filter).toEqual({ ...filter, childId: c1.getId() }); | ||
}); | ||
|
||
it("should create a new entity that references the related one", async () => { | ||
const related = new Child(); | ||
await component.onInitFromDynamicConfig({ | ||
entity: related, | ||
config: { | ||
entity: ChildSchoolRelation.ENTITY_TYPE, | ||
property: "childId", | ||
columns: [], | ||
}, | ||
}); | ||
|
||
const newEntity = component.createNewRecordFactory()(); | ||
|
||
expect(newEntity instanceof ChildSchoolRelation).toBeTrue(); | ||
expect(newEntity["childId"]).toBe(related.getId()); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
src/app/core/entity-components/entity-details/related-entities/related-entities.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Component } from "@angular/core"; | ||
import { DynamicComponent } from "../../../view/dynamic-components/dynamic-component.decorator"; | ||
import { OnInitDynamicComponent } from "../../../view/dynamic-components/on-init-dynamic-component.interface"; | ||
import { PanelConfig } from "../EntityDetailsConfig"; | ||
import { EntityMapperService } from "../../../entity/entity-mapper.service"; | ||
import { Entity, EntityConstructor } from "../../../entity/model/entity"; | ||
import { | ||
ColumnConfig, | ||
DataFilter, | ||
} from "../../entity-subrecord/entity-subrecord/entity-subrecord-config"; | ||
import { EntityRegistry } from "../../../entity/database-entity.decorator"; | ||
import { isArrayProperty } from "../../entity-utils/entity-utils"; | ||
import { EntitySubrecordComponent } from "../../entity-subrecord/entity-subrecord/entity-subrecord.component"; | ||
|
||
@DynamicComponent("RelatedEntities") | ||
@Component({ | ||
selector: "app-related-entities", | ||
templateUrl: "./related-entities.component.html", | ||
standalone: true, | ||
imports: [EntitySubrecordComponent], | ||
}) | ||
export class RelatedEntitiesComponent implements OnInitDynamicComponent { | ||
data: Entity[] = []; | ||
columns: ColumnConfig[] = []; | ||
filter: DataFilter<Entity>; | ||
relatedEntity: Entity; | ||
private entityType: EntityConstructor; | ||
private property; | ||
private isArray = false; | ||
|
||
constructor( | ||
private entityMapper: EntityMapperService, | ||
private entities: EntityRegistry | ||
) {} | ||
|
||
async onInitFromDynamicConfig( | ||
config: PanelConfig<{ | ||
entity: string; | ||
property: string; | ||
columns: ColumnConfig[]; | ||
filter?: DataFilter<Entity>; | ||
}> | ||
) { | ||
this.relatedEntity = config.entity; | ||
this.entityType = this.entities.get(config.config.entity); | ||
this.property = config.config.property; | ||
this.isArray = isArrayProperty(this.entityType, this.property); | ||
|
||
this.data = await this.entityMapper.loadType(this.entityType); | ||
this.filter = { | ||
...config.config.filter, | ||
[this.property]: this.isArray | ||
? { $elemMatch: { $eq: this.relatedEntity.getId() } } | ||
: this.relatedEntity.getId(), | ||
}; | ||
this.columns = config.config.columns; | ||
} | ||
|
||
createNewRecordFactory() { | ||
// TODO has a similar purpose like FilterService.alignEntityWithFilter | ||
return () => { | ||
const rec = new this.entityType(); | ||
rec[this.property] = this.isArray | ||
? [this.relatedEntity.getId()] | ||
: this.relatedEntity.getId(); | ||
return rec; | ||
}; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/app/core/entity-components/entity-utils/entity-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { arrayEntitySchemaDatatype } from "../../entity/schema-datatypes/datatype-array"; | ||
import { entityArrayEntitySchemaDatatype } from "../../entity/schema-datatypes/datatype-entity-array"; | ||
import { EntityConstructor } from "../../entity/model/entity"; | ||
|
||
export function isArrayProperty( | ||
entity: EntityConstructor, | ||
property: string | ||
): boolean { | ||
const dataType = entity.schema.get(property).dataType; | ||
return ( | ||
dataType === arrayEntitySchemaDatatype.name || | ||
dataType === entityArrayEntitySchemaDatatype.name | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters