2021-07-08 19:40:14 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-07-08 23:58:07 +02:00
|
|
|
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { EntityManager, Repository, TreeRepository } from 'typeorm';
|
2021-07-08 19:40:14 +02:00
|
|
|
import { NewItemInput } from './dto/new-item.input';
|
2021-07-21 01:14:20 +02:00
|
|
|
import { EANService } from './ean/ean.service';
|
2021-07-08 19:40:14 +02:00
|
|
|
import { Item } from './items.entity';
|
2021-07-08 23:58:07 +02:00
|
|
|
import { ItemModel } from './items.model';
|
2021-07-08 19:40:14 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ItemsService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(Item)
|
|
|
|
private itemRepository: Repository<Item>,
|
2021-07-08 23:58:07 +02:00
|
|
|
@InjectEntityManager()
|
|
|
|
private entityManager: EntityManager,
|
2021-07-21 01:14:20 +02:00
|
|
|
private eans: EANService,
|
2021-07-08 23:58:07 +02:00
|
|
|
) {
|
|
|
|
this.treeRepository = this.entityManager.getTreeRepository(Item);
|
|
|
|
}
|
|
|
|
treeRepository: TreeRepository<Item>;
|
2021-07-08 19:40:14 +02:00
|
|
|
|
|
|
|
async getItem(id: string): Promise<Item | undefined> {
|
2021-07-21 01:14:20 +02:00
|
|
|
return this.itemRepository.findOne(this.eans.toID(id));
|
2021-07-08 19:40:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 18:34:30 +02:00
|
|
|
async getItemParent(item: ItemModel): Promise<Item | undefined> {
|
2021-07-21 01:14:20 +02:00
|
|
|
const childItem = await this.itemRepository.findOneOrFail(
|
|
|
|
this.eans.toID(item.id),
|
|
|
|
{
|
|
|
|
relations: ['parent'],
|
|
|
|
},
|
|
|
|
);
|
2021-07-17 17:07:19 +02:00
|
|
|
return childItem.parent;
|
2021-07-08 23:58:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 23:32:48 +02:00
|
|
|
async getItemAncestors(item: ItemModel): Promise<Item[]> {
|
2021-07-21 01:14:20 +02:00
|
|
|
const childItem = await this.itemRepository.findOneOrFail(
|
|
|
|
this.eans.toID(item.id),
|
|
|
|
{
|
|
|
|
relations: ['parent'],
|
|
|
|
},
|
|
|
|
);
|
2021-07-21 02:44:14 +02:00
|
|
|
// TreeRepository.findAncestors()[-1] is always the child item itself
|
|
|
|
return (await this.treeRepository.findAncestors(childItem)).slice(0, -1);
|
2021-07-20 23:32:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 18:34:30 +02:00
|
|
|
async getItemChildren(item: ItemModel): Promise<Item[]> {
|
|
|
|
return this.itemRepository.find({
|
|
|
|
where: {
|
2021-07-21 01:14:20 +02:00
|
|
|
parent: this.eans.toID(item.id),
|
2021-07-17 18:34:30 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:34:25 +02:00
|
|
|
async getItemDescendants(item: ItemModel): Promise<Item[]> {
|
2021-07-21 01:14:20 +02:00
|
|
|
const parentItem = await this.itemRepository.findOneOrFail(
|
|
|
|
this.eans.toID(item.id),
|
|
|
|
{
|
|
|
|
relations: ['parent'],
|
|
|
|
},
|
|
|
|
);
|
2021-07-20 00:34:15 +02:00
|
|
|
// TreeRepository.findDescendants()[0] is always the parent item
|
|
|
|
return (await this.treeRepository.findDescendants(parentItem)).slice(1);
|
2021-07-17 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 19:40:14 +02:00
|
|
|
async createItem(input: NewItemInput): Promise<Item> {
|
2021-07-19 23:29:59 +02:00
|
|
|
// must use Repository.save() for the closure table to work,
|
|
|
|
// so we have to check whether the provided ID exists in the first place
|
2021-07-21 01:14:20 +02:00
|
|
|
if (
|
|
|
|
input.id &&
|
|
|
|
(await this.itemRepository.count({ id: this.eans.toID(input.id) })) !== 0
|
|
|
|
) {
|
2021-07-19 23:29:59 +02:00
|
|
|
throw new Error('Item with this ID already exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
const highestId = input.id
|
|
|
|
? // nobody cares in this case
|
|
|
|
null
|
|
|
|
: (
|
|
|
|
await this.itemRepository.findOne({
|
|
|
|
select: ['id'],
|
|
|
|
order: { id: 'DESC' },
|
|
|
|
})
|
|
|
|
)?.id || '139999999999';
|
|
|
|
|
2021-07-17 16:44:34 +02:00
|
|
|
const item = this.itemRepository.create({
|
|
|
|
...input,
|
2021-07-19 23:29:59 +02:00
|
|
|
// if id not provided, use the highest one in db +1
|
2021-07-21 01:14:20 +02:00
|
|
|
id: highestId
|
|
|
|
? (BigInt(highestId) + 1n).toString(10)
|
|
|
|
: // @ts-ignore input.id must exist here
|
|
|
|
this.eans.toID(input.id),
|
2021-07-17 16:44:34 +02:00
|
|
|
parent: input.parent
|
|
|
|
? await this.itemRepository.findOneOrFail(input.parent)
|
|
|
|
: undefined,
|
|
|
|
});
|
|
|
|
|
2021-07-19 23:29:59 +02:00
|
|
|
await this.itemRepository.save(item);
|
2021-07-17 16:44:34 +02:00
|
|
|
|
|
|
|
return item;
|
2021-07-08 19:40:14 +02:00
|
|
|
}
|
|
|
|
}
|