From 35fd9c903dcdc10c08594219665a30ea072e7271 Mon Sep 17 00:00:00 2001 From: Lauren Liberda Date: Wed, 21 Jul 2021 01:14:20 +0200 Subject: [PATCH] getting items by EAN-13 --- src/items/ean/ean.service.ts | 4 ++++ src/items/items.model.ts | 2 +- src/items/items.service.ts | 43 +++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/items/ean/ean.service.ts b/src/items/ean/ean.service.ts index e8f0309..e4b0267 100644 --- a/src/items/ean/ean.service.ts +++ b/src/items/ean/ean.service.ts @@ -21,6 +21,10 @@ export class EANService { } toID(ean: string) { + if (ean.length === 12) { + // it already is an ID + return ean; + } if (!this.isValid(ean)) { throw new Error(`Invalid EAN: "${ean}"`); } diff --git a/src/items/items.model.ts b/src/items/items.model.ts index 61a6e9e..de5ab8c 100644 --- a/src/items/items.model.ts +++ b/src/items/items.model.ts @@ -4,7 +4,7 @@ import { Field, ID, ObjectType } from '@nestjs/graphql'; description: 'Either the inventored thing or a box containing them', }) export class ItemModel { - @Field((type) => ID) + @Field((type) => ID, { description: 'Database ID or EAN-13 code' }) id: string; @Field() diff --git a/src/items/items.service.ts b/src/items/items.service.ts index 98ba123..93a6e04 100644 --- a/src/items/items.service.ts +++ b/src/items/items.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'; import { EntityManager, Repository, TreeRepository } from 'typeorm'; import { NewItemInput } from './dto/new-item.input'; +import { EANService } from './ean/ean.service'; import { Item } from './items.entity'; import { ItemModel } from './items.model'; @@ -12,26 +13,33 @@ export class ItemsService { private itemRepository: Repository, @InjectEntityManager() private entityManager: EntityManager, + private eans: EANService, ) { this.treeRepository = this.entityManager.getTreeRepository(Item); } treeRepository: TreeRepository; async getItem(id: string): Promise { - return this.itemRepository.findOne(id); + return this.itemRepository.findOne(this.eans.toID(id)); } async getItemParent(item: ItemModel): Promise { - const childItem = await this.itemRepository.findOneOrFail(item.id, { - relations: ['parent'], - }); + const childItem = await this.itemRepository.findOneOrFail( + this.eans.toID(item.id), + { + relations: ['parent'], + }, + ); return childItem.parent; } async getItemAncestors(item: ItemModel): Promise { - const childItem = await this.itemRepository.findOneOrFail(item.id, { - relations: ['parent'], - }); + const childItem = await this.itemRepository.findOneOrFail( + this.eans.toID(item.id), + { + relations: ['parent'], + }, + ); // TreeRepository.findAncestors()[0] is always the child item return (await this.treeRepository.findAncestors(childItem)).slice(1); } @@ -39,15 +47,18 @@ export class ItemsService { async getItemChildren(item: ItemModel): Promise { return this.itemRepository.find({ where: { - parent: item.id, + parent: this.eans.toID(item.id), }, }); } async getItemDescendants(item: ItemModel): Promise { - const parentItem = await this.itemRepository.findOneOrFail(item.id, { - relations: ['parent'], - }); + const parentItem = await this.itemRepository.findOneOrFail( + this.eans.toID(item.id), + { + relations: ['parent'], + }, + ); // TreeRepository.findDescendants()[0] is always the parent item return (await this.treeRepository.findDescendants(parentItem)).slice(1); } @@ -55,7 +66,10 @@ export class ItemsService { async createItem(input: NewItemInput): Promise { // must use Repository.save() for the closure table to work, // so we have to check whether the provided ID exists in the first place - if (input.id && (await this.itemRepository.count({ id: input.id })) !== 0) { + if ( + input.id && + (await this.itemRepository.count({ id: this.eans.toID(input.id) })) !== 0 + ) { throw new Error('Item with this ID already exists'); } @@ -72,7 +86,10 @@ export class ItemsService { const item = this.itemRepository.create({ ...input, // if id not provided, use the highest one in db +1 - id: highestId ? (BigInt(highestId) + 1n).toString(10) : input.id, + id: highestId + ? (BigInt(highestId) + 1n).toString(10) + : // @ts-ignore input.id must exist here + this.eans.toID(input.id), parent: input.parent ? await this.itemRepository.findOneOrFail(input.parent) : undefined,