item ancestors

master
Lauren Liberda 2021-07-20 23:32:48 +02:00
parent 92993ef105
commit 9e22638fc2
3 changed files with 16 additions and 0 deletions

View File

@ -16,6 +16,9 @@ export class ItemModel {
@Field({ nullable: true })
parent?: ItemModel;
@Field((type) => [ItemModel])
ancestors: ItemModel[];
@Field((type) => [ItemModel])
children: ItemModel[];

View File

@ -25,6 +25,11 @@ export class ItemsResolver {
return this.itemsService.getItemParent(item);
}
@ResolveField()
async ancestors(@Parent() item: ItemModel) {
return this.itemsService.getItemAncestors(item);
}
@ResolveField()
async children(@Parent() item: ItemModel) {
return this.itemsService.getItemChildren(item);

View File

@ -28,6 +28,14 @@ export class ItemsService {
return childItem.parent;
}
async getItemAncestors(item: ItemModel): Promise<Item[]> {
const childItem = await this.itemRepository.findOneOrFail(item.id, {
relations: ['parent'],
});
// TreeRepository.findAncestors()[0] is always the child item
return (await this.treeRepository.findAncestors(childItem)).slice(1);
}
async getItemChildren(item: ItemModel): Promise<Item[]> {
return this.itemRepository.find({
where: {