diff --git a/libs/auth-domain/src/lib/services/auth.service.ts b/libs/auth-domain/src/lib/services/auth.service.ts index fe4f6b85..493fe9bd 100644 --- a/libs/auth-domain/src/lib/services/auth.service.ts +++ b/libs/auth-domain/src/lib/services/auth.service.ts @@ -51,14 +51,12 @@ export class AuthService { async createUser(data: AuthData): Promise> { try { - const checkEmail = await this.repository.findByEmail(data.email); - const checkUsername = await this.repository.findByUsername( - data.username, - ); - if (checkEmail !== null) { + const checkEmail = await this.findByEmail(data.email); + if (checkEmail.isSuccess) { return Result.fail(AuthErrorCodes.EMAIL_FOUND); } - if (checkUsername !== null) { + const checkUsername = await this.findByUsername(data.username); + if (checkUsername.isSuccess) { return Result.fail(AuthErrorCodes.USERNAME_FOUND); } const aggregate = this.factory.createDomain(data); @@ -73,26 +71,22 @@ export class AuthService { password: string, ): Promise> { try { - const checkUsername = - await this.repository.findByUsername(username); - if (checkUsername === null) { + const checkUsername = await this.findByUsername(username); + if (checkUsername.isFailure) { return Result.fail(UserErrorCodes.NOT_FOUND); } - if (checkUsername.password !== password) { + if (checkUsername.value.password !== password) { return Result.fail(AuthErrorCodes.INVALID_PASSWORD); } - if (checkUsername.isBanned) { + if (checkUsername.value.isBanned) { return Result.fail(UserErrorCodes.IS_BANNED); } - if (!checkUsername.isActive) { + if (!checkUsername.value.isActive) { return Result.fail(UserErrorCodes.NOT_ACTIVE); } - // - const aggregate = this.factory.reconstitute(checkUsername); + const aggregate = this.factory.reconstitute(checkUsername.value); aggregate.updateLogin(); - await this.save(aggregate.toAnemic()); - // - return Result.ok(checkUsername); + return await this.save(aggregate.toAnemic()); } catch (error) { return Result.fail(error); } @@ -103,17 +97,17 @@ export class AuthService { token: string, ): Promise> { try { - const result = await this.repository.findByEmail(email); - if (result !== null) { - if (result.isActive) { + const result = await this.findByEmail(email); + if (result.isSuccess) { + if (result.value.isActive) { return Result.fail(AuthErrorCodes.IS_ACTIVE); } - if (result.activation_token !== token) { + if (result.value.activation_token !== token) { return Result.fail( AuthErrorCodes.TOKEN_UNMATCH, ); } - const aggregate = this.factory.reconstitute(result); + const aggregate = this.factory.reconstitute(result.value); aggregate.updateActivation(); return await this.save(aggregate.toAnemic()); } diff --git a/libs/foodfolio-domain/src/lib/repositories/category.repository.ts b/libs/foodfolio-domain/src/lib/repositories/category.repository.ts index ccf1d7ff..11f80ca6 100644 --- a/libs/foodfolio-domain/src/lib/repositories/category.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/category.repository.ts @@ -3,10 +3,11 @@ import { CategoryAnemic } from '../anemics'; import { Chainable, Nullable } from '@toxictoast/azkaban-base-types'; interface CategoryAdditions { - findByParentId(parentId: Nullable): Promise>; + findByParentId(parentId: Nullable): Promise>; + findByTitle(title: string): Promise; } export type CategoryRepository = Chainable< - CategoryAdditions, - Repository + CategoryAdditions, + Repository >; diff --git a/libs/foodfolio-domain/src/lib/repositories/company.repository.ts b/libs/foodfolio-domain/src/lib/repositories/company.repository.ts index 929923b2..419fb783 100644 --- a/libs/foodfolio-domain/src/lib/repositories/company.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/company.repository.ts @@ -1,4 +1,12 @@ import { Repository } from '@toxictoast/azkaban-base-domain'; import { CompanyAnemic } from '../anemics'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type CompanyRepository = Repository; +interface CompanyAdditions { + findByTitle(title: string): Promise; +} + +export type CompanyRepository = Chainable< + CompanyAdditions, + Repository +>; diff --git a/libs/foodfolio-domain/src/lib/repositories/item-variant.repository.ts b/libs/foodfolio-domain/src/lib/repositories/item-variant.repository.ts index 807cea39..093d72a0 100644 --- a/libs/foodfolio-domain/src/lib/repositories/item-variant.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/item-variant.repository.ts @@ -18,6 +18,7 @@ interface ItemVariantAdditions { findByWarehouseId( warehouseId: Nullable, ): Promise>; + findByTitle(title: string): Promise; } export type ItemVariantRepository = Chainable< diff --git a/libs/foodfolio-domain/src/lib/repositories/item.repository.ts b/libs/foodfolio-domain/src/lib/repositories/item.repository.ts index f00b31f4..7c137c4d 100644 --- a/libs/foodfolio-domain/src/lib/repositories/item.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/item.repository.ts @@ -1,4 +1,9 @@ import { Repository } from '@toxictoast/azkaban-base-domain'; import { ItemAnemic } from '../anemics'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type ItemRepository = Repository; +interface ItemAdditions { + findByTitle(title: string): Promise; +} + +export type ItemRepository = Chainable>; diff --git a/libs/foodfolio-domain/src/lib/repositories/location.repository.ts b/libs/foodfolio-domain/src/lib/repositories/location.repository.ts index 9a59440e..4767d7c3 100644 --- a/libs/foodfolio-domain/src/lib/repositories/location.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/location.repository.ts @@ -3,11 +3,12 @@ import { LocationAnemic } from '../anemics'; import { Chainable, Nullable } from '@toxictoast/azkaban-base-types'; interface LocationAdditions { - findByParentId(parentId: Nullable): Promise>; - findByFreezer(freezer: boolean): Promise>; + findByParentId(parentId: Nullable): Promise>; + findByFreezer(freezer: boolean): Promise>; + findByTitle(title: string): Promise; } export type LocationRepository = Chainable< - LocationAdditions, - Repository + LocationAdditions, + Repository >; diff --git a/libs/foodfolio-domain/src/lib/repositories/size.repository.ts b/libs/foodfolio-domain/src/lib/repositories/size.repository.ts index 40a600b8..eaf8241f 100644 --- a/libs/foodfolio-domain/src/lib/repositories/size.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/size.repository.ts @@ -1,4 +1,9 @@ import { Repository } from '@toxictoast/azkaban-base-domain'; import { SizeAnemic } from '../anemics'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type SizeRepository = Repository; +interface SizeAdditions { + findByTitle(title: string): Promise; +} + +export type SizeRepository = Chainable>; diff --git a/libs/foodfolio-domain/src/lib/repositories/type.repository.ts b/libs/foodfolio-domain/src/lib/repositories/type.repository.ts index c0d240c8..9503eb85 100644 --- a/libs/foodfolio-domain/src/lib/repositories/type.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/type.repository.ts @@ -1,4 +1,9 @@ import { Repository } from '@toxictoast/azkaban-base-domain'; import { TypeAnemic } from '../anemics'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type TypeRepository = Repository; +interface TypeAdditions { + findByTitle(title: string): Promise; +} + +export type TypeRepository = Chainable>; diff --git a/libs/foodfolio-domain/src/lib/repositories/warehouse.repository.ts b/libs/foodfolio-domain/src/lib/repositories/warehouse.repository.ts index fd4bce5b..6151ddab 100644 --- a/libs/foodfolio-domain/src/lib/repositories/warehouse.repository.ts +++ b/libs/foodfolio-domain/src/lib/repositories/warehouse.repository.ts @@ -1,4 +1,12 @@ import { Repository } from '@toxictoast/azkaban-base-domain'; import { WarehouseAnemic } from '../anemics'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type WarehouseRepository = Repository; +interface WarehouseAdditions { + findByTitle(title: string): Promise; +} + +export type WarehouseRepository = Chainable< + WarehouseAdditions, + Repository +>; diff --git a/libs/foodfolio-domain/src/lib/services/category.service.ts b/libs/foodfolio-domain/src/lib/services/category.service.ts index 82d93ff2..68ffec26 100644 --- a/libs/foodfolio-domain/src/lib/services/category.service.ts +++ b/libs/foodfolio-domain/src/lib/services/category.service.ts @@ -4,166 +4,187 @@ import { CategoryFactory } from '../factories'; import { CategoryRepository } from '../repositories'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; -import { FoodFolioCategoryErrorCodes } from '@toxictoast/azkaban-base-helpers'; +import { + FoodFolioCategoryErrorCodes, + GenericErrorCodes, +} from '@toxictoast/azkaban-base-helpers'; export class CategoryService { - private readonly factory: CategoryFactory = new CategoryFactory(); + private readonly factory: CategoryFactory = new CategoryFactory(); - constructor(private readonly repository: CategoryRepository) {} + constructor(private readonly repository: CategoryRepository) {} - private async save( - anemic: CategoryAnemic, - ): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save( + anemic: CategoryAnemic, + ): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getCategories( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getCategories( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getCategoryById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async getCategoryById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } - async getCategoryByParentId( - parent_id: Nullable, - ): Promise>> { - try { - const result = await this.repository.findByParentId(parent_id); - if (result !== null) { - return Result.ok>(result); - } - return Result.fail>( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail>(error); - } - } + async getCategoryByParentId( + parent_id: Nullable, + ): Promise>> { + try { + const result = await this.repository.findByParentId(parent_id); + if (result !== null) { + return Result.ok>(result); + } + return Result.fail>( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail>(error); + } + } - async createCategory(data: CategoryData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getCategoryByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } - async deleteCategory(id: string): Promise> { - try { - const category = await this.getCategoryById(id); - if (category.isSuccess) { - const categoryValue = category.value; - const aggregate = this.factory.reconstitute(categoryValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async createCategory(data: CategoryData): Promise> { + try { + const check = await this.getCategoryByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreCategory(id: string): Promise> { - try { - const category = await this.getCategoryById(id); - if (category.isSuccess) { - const categoryValue = category.value; - const aggregate = this.factory.reconstitute(categoryValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async deleteCategory(id: string): Promise> { + try { + const category = await this.getCategoryById(id); + if (category.isSuccess) { + const categoryValue = category.value; + const aggregate = this.factory.reconstitute(categoryValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } - async updateParentId( - id: string, - parent_id: Nullable, - ): Promise> { - try { - const category = await this.getCategoryById(id); - if (category.isSuccess) { - const categoryValue = category.value; - const aggregate = this.factory.reconstitute(categoryValue); - aggregate.changeParentId(parent_id); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async restoreCategory(id: string): Promise> { + try { + const category = await this.getCategoryById(id); + if (category.isSuccess) { + const categoryValue = category.value; + const aggregate = this.factory.reconstitute(categoryValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } - async updateTitle( - id: string, - title: string, - ): Promise> { - try { - const category = await this.getCategoryById(id); - if (category.isSuccess) { - const categoryValue = category.value; - const aggregate = this.factory.reconstitute(categoryValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateParentId( + id: string, + parent_id: Nullable, + ): Promise> { + try { + const category = await this.getCategoryById(id); + if (category.isSuccess) { + const categoryValue = category.value; + const aggregate = this.factory.reconstitute(categoryValue); + aggregate.changeParentId(parent_id); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const category = await this.getCategoryById(id); - if (category.isSuccess) { - const categoryValue = category.value; - const aggregate = this.factory.reconstitute(categoryValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle( + id: string, + title: string, + ): Promise> { + try { + const category = await this.getCategoryById(id); + if (category.isSuccess) { + const categoryValue = category.value; + const aggregate = this.factory.reconstitute(categoryValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } + + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const category = await this.getCategoryById(id); + if (category.isSuccess) { + const categoryValue = category.value; + const aggregate = this.factory.reconstitute(categoryValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-domain/src/lib/services/company.service.ts b/libs/foodfolio-domain/src/lib/services/company.service.ts index aad1a9b5..a1f10272 100644 --- a/libs/foodfolio-domain/src/lib/services/company.service.ts +++ b/libs/foodfolio-domain/src/lib/services/company.service.ts @@ -3,126 +3,142 @@ import { CompanyRepository } from '../repositories'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { - FoodFolioCategoryErrorCodes, - GenericErrorCodes, + FoodFolioCategoryErrorCodes, + GenericErrorCodes, } from '@toxictoast/azkaban-base-helpers'; import { CompanyAnemic } from '../anemics'; import { CompanyData } from '../data'; export class CompanyService { - private readonly factory: CompanyFactory = new CompanyFactory(); + private readonly factory: CompanyFactory = new CompanyFactory(); - constructor(private readonly repository: CompanyRepository) {} + constructor(private readonly repository: CompanyRepository) {} - private async save(anemic: CompanyAnemic): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save(anemic: CompanyAnemic): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getCompanies( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getCompanies( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getCompanyById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async getCompanyById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async createCompany(data: CompanyData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getCompanyByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async deleteCompany(id: string): Promise> { - try { - const company = await this.getCompanyById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async createCompany(data: CompanyData): Promise> { + try { + const check = await this.getCompanyByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreCompany(id: string): Promise> { - try { - const company = await this.getCompanyById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async deleteCompany(id: string): Promise> { + try { + const company = await this.getCompanyById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateTitle( - id: string, - title: string, - ): Promise> { - try { - const company = await this.getCompanyById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async restoreCompany(id: string): Promise> { + try { + const company = await this.getCompanyById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a CompanyErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const company = await this.getCompanyById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle( + id: string, + title: string, + ): Promise> { + try { + const company = await this.getCompanyById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } + + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const company = await this.getCompanyById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-domain/src/lib/services/item-variant.service.ts b/libs/foodfolio-domain/src/lib/services/item-variant.service.ts index 0bad1253..9d6dac16 100644 --- a/libs/foodfolio-domain/src/lib/services/item-variant.service.ts +++ b/libs/foodfolio-domain/src/lib/services/item-variant.service.ts @@ -129,10 +129,27 @@ export class ItemVariantService { } } + async getItemVariantByTitle( + title: string, + ): Promise> { + try { + const result = await this.repository.findByTitle(title); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } + async createItemVariant( data: ItemVariantData, ): Promise> { try { + const check = await this.getItemVariantByTitle(data.title); + if (check.isSuccess) { + return Result.fail( + GenericErrorCodes.UNKNOWN, + ); + } const aggregate = this.factory.createDomain(data); return await this.save(aggregate.toAnemic()); } catch (error) { diff --git a/libs/foodfolio-domain/src/lib/services/item.service.ts b/libs/foodfolio-domain/src/lib/services/item.service.ts index 3a1817e5..cb2632ce 100644 --- a/libs/foodfolio-domain/src/lib/services/item.service.ts +++ b/libs/foodfolio-domain/src/lib/services/item.service.ts @@ -44,8 +44,24 @@ export class ItemService { } } + async getItemByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } + async createItem(data: ItemData): Promise> { try { + const check = await this.getItemByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } const aggregate = this.factory.createDomain(data); return await this.save(aggregate.toAnemic()); } catch (error) { diff --git a/libs/foodfolio-domain/src/lib/services/location.service.ts b/libs/foodfolio-domain/src/lib/services/location.service.ts index a0923df0..1bb3c550 100644 --- a/libs/foodfolio-domain/src/lib/services/location.service.ts +++ b/libs/foodfolio-domain/src/lib/services/location.service.ts @@ -7,175 +7,191 @@ import { LocationAnemic } from '../anemics'; import { LocationData } from '../data'; export class LocationService { - private readonly factory: LocationFactory = new LocationFactory(); + private readonly factory: LocationFactory = new LocationFactory(); - constructor(private readonly repository: LocationRepository) {} + constructor(private readonly repository: LocationRepository) {} - private async save( - anemic: LocationAnemic, - ): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save( + anemic: LocationAnemic, + ): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getLocations( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getLocations( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getLocationById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async getLocationById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async getLocationByParentId( - parent_id: Nullable, - ): Promise>> { - try { - const result = await this.repository.findByParentId(parent_id); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getLocationByParentId( + parent_id: Nullable, + ): Promise>> { + try { + const result = await this.repository.findByParentId(parent_id); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getLocationByFreezer( - freezer: boolean, - ): Promise>> { - try { - const result = await this.repository.findByFreezer(freezer); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getLocationByFreezer( + freezer: boolean, + ): Promise>> { + try { + const result = await this.repository.findByFreezer(freezer); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async createLocation(data: LocationData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getLocationByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async deleteLocation(id: string): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async createLocation(data: LocationData): Promise> { + try { + const check = await this.getLocationByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreLocation(id: string): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async deleteLocation(id: string): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateParentId( - id: string, - parent_id: Nullable, - ): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeParentId(parent_id); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async restoreLocation(id: string): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a LocationErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateTitle( - id: string, - title: string, - ): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async updateParentId( + id: string, + parent_id: Nullable, + ): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeParentId(parent_id); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle( + id: string, + title: string, + ): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateFreezer( - id: string, - freezer: boolean, - ): Promise> { - try { - const company = await this.getLocationById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeFreezer(freezer); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } + + async updateFreezer( + id: string, + freezer: boolean, + ): Promise> { + try { + const company = await this.getLocationById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeFreezer(freezer); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-domain/src/lib/services/shopping-list.service.ts b/libs/foodfolio-domain/src/lib/services/shopping-list.service.ts index 6b462028..f6593dab 100644 --- a/libs/foodfolio-domain/src/lib/services/shopping-list.service.ts +++ b/libs/foodfolio-domain/src/lib/services/shopping-list.service.ts @@ -58,6 +58,12 @@ export class ShoppingListService { data: ShoppingListData, ): Promise> { try { + const check = await this.getShoppingListByItemId(data.item_id); + if (check.isSuccess) { + return Result.fail( + GenericErrorCodes.UNKNOWN, + ); + } const aggregate = this.factory.createDomain(data); return await this.save(aggregate.toAnemic()); } catch (error) { diff --git a/libs/foodfolio-domain/src/lib/services/size.service.ts b/libs/foodfolio-domain/src/lib/services/size.service.ts index 132cf951..7fb8401b 100644 --- a/libs/foodfolio-domain/src/lib/services/size.service.ts +++ b/libs/foodfolio-domain/src/lib/services/size.service.ts @@ -3,123 +3,139 @@ import { SizeRepository } from '../repositories'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { - FoodFolioCategoryErrorCodes, - GenericErrorCodes, + FoodFolioCategoryErrorCodes, + GenericErrorCodes, } from '@toxictoast/azkaban-base-helpers'; import { SizeAnemic } from '../anemics'; import { SizeData } from '../data'; export class SizeService { - private readonly factory: SizeFactory = new SizeFactory(); + private readonly factory: SizeFactory = new SizeFactory(); - constructor(private readonly repository: SizeRepository) {} + constructor(private readonly repository: SizeRepository) {} - private async save(anemic: SizeAnemic): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save(anemic: SizeAnemic): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getSizes( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getSizes( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getSizeById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a SizeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async getSizeById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async createSize(data: SizeData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getSizeByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async deleteSize(id: string): Promise> { - try { - const company = await this.getSizeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a SizeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async createSize(data: SizeData): Promise> { + try { + const check = await this.getSizeByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreSize(id: string): Promise> { - try { - const company = await this.getSizeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a SizeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async deleteSize(id: string): Promise> { + try { + const company = await this.getSizeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateTitle(id: string, title: string): Promise> { - try { - const company = await this.getSizeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async restoreSize(id: string): Promise> { + try { + const company = await this.getSizeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const company = await this.getSizeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle(id: string, title: string): Promise> { + try { + const company = await this.getSizeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } + + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const company = await this.getSizeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-domain/src/lib/services/type.service.ts b/libs/foodfolio-domain/src/lib/services/type.service.ts index 53d2a19a..bbc81caf 100644 --- a/libs/foodfolio-domain/src/lib/services/type.service.ts +++ b/libs/foodfolio-domain/src/lib/services/type.service.ts @@ -3,123 +3,139 @@ import { TypeRepository } from '../repositories'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { - FoodFolioCategoryErrorCodes, - GenericErrorCodes, + FoodFolioCategoryErrorCodes, + GenericErrorCodes, } from '@toxictoast/azkaban-base-helpers'; import { TypeAnemic } from '../anemics'; import { TypeData } from '../data'; export class TypeService { - private readonly factory: TypeFactory = new TypeFactory(); + private readonly factory: TypeFactory = new TypeFactory(); - constructor(private readonly repository: TypeRepository) {} + constructor(private readonly repository: TypeRepository) {} - private async save(anemic: TypeAnemic): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save(anemic: TypeAnemic): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getTypes( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getTypes( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getTypeById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a TypeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async getTypeById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async createType(data: TypeData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getTypeByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async deleteType(id: string): Promise> { - try { - const company = await this.getTypeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a TypeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async createType(data: TypeData): Promise> { + try { + const check = await this.getTypeByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreType(id: string): Promise> { - try { - const company = await this.getTypeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a TypeErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async deleteType(id: string): Promise> { + try { + const company = await this.getTypeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a TypeErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateTitle(id: string, title: string): Promise> { - try { - const company = await this.getTypeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async restoreType(id: string): Promise> { + try { + const company = await this.getTypeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a TypeErrorCodes enum + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const company = await this.getTypeById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle(id: string, title: string): Promise> { + try { + const company = await this.getTypeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } + + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const company = await this.getTypeById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-domain/src/lib/services/warehouse.service.ts b/libs/foodfolio-domain/src/lib/services/warehouse.service.ts index 4c43774c..334b12d1 100644 --- a/libs/foodfolio-domain/src/lib/services/warehouse.service.ts +++ b/libs/foodfolio-domain/src/lib/services/warehouse.service.ts @@ -3,130 +3,146 @@ import { WarehouseRepository } from '../repositories'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { - FoodFolioCategoryErrorCodes, - GenericErrorCodes, + FoodFolioCategoryErrorCodes, + GenericErrorCodes, } from '@toxictoast/azkaban-base-helpers'; import { WarehouseAnemic } from '../anemics'; import { WarehouseData } from '../data'; export class WarehouseService { - private readonly factory: WarehouseFactory = new WarehouseFactory(); + private readonly factory: WarehouseFactory = new WarehouseFactory(); - constructor(private readonly repository: WarehouseRepository) {} + constructor(private readonly repository: WarehouseRepository) {} - private async save( - anemic: WarehouseAnemic, - ): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save( + anemic: WarehouseAnemic, + ): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getWarehouses( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getWarehouses( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getWarehouseById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a WarehouseErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async getWarehouseById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async createWarehouse( - data: WarehouseData, - ): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getWarehouseByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async deleteWarehouse(id: string): Promise> { - try { - const company = await this.getWarehouseById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a WarehouseErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async createWarehouse( + data: WarehouseData, + ): Promise> { + try { + const check = await this.getWarehouseByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreWarehouse(id: string): Promise> { - try { - const company = await this.getWarehouseById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GenericErrorCodes.NOT_FOUND); // TODO: Create a WarehouseErrorCodes enum - } catch (error) { - return Result.fail(error); - } - } + async deleteWarehouse(id: string): Promise> { + try { + const company = await this.getWarehouseById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateTitle( - id: string, - title: string, - ): Promise> { - try { - const company = await this.getWarehouseById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async restoreWarehouse(id: string): Promise> { + try { + const company = await this.getWarehouseById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GenericErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise> { - try { - const company = await this.getWarehouseById(id); - if (company.isSuccess) { - const companyValue = company.value; - const aggregate = this.factory.reconstitute(companyValue); - aggregate.changeActivatedAt(activated_at); - return await this.save(aggregate.toAnemic()); - } - return Result.fail( - FoodFolioCategoryErrorCodes.NOT_FOUND, - ); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle( + id: string, + title: string, + ): Promise> { + try { + const company = await this.getWarehouseById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } + + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise> { + try { + const company = await this.getWarehouseById(id); + if (company.isSuccess) { + const companyValue = company.value; + const aggregate = this.factory.reconstitute(companyValue); + aggregate.changeActivatedAt(activated_at); + return await this.save(aggregate.toAnemic()); + } + return Result.fail( + FoodFolioCategoryErrorCodes.NOT_FOUND, + ); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/category.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/category.repository.ts index faf2c0e1..285736a0 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/category.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/category.repository.ts @@ -1,6 +1,6 @@ import { - CategoryRepository as DomainRepository, - CategoryAnemic, + CategoryRepository as DomainRepository, + CategoryAnemic, } from '@azkaban/foodfolio-domain'; import { Repository } from 'typeorm'; import { CategoryMapper } from '../mappers'; @@ -9,51 +9,59 @@ import { CategoryDAO } from '../../dao'; import { Nullable } from '@toxictoast/azkaban-base-types'; export class CategoryRepository implements DomainRepository { - private readonly mapper: CategoryMapper = new CategoryMapper(); - - constructor(private readonly repository: Repository) {} - - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } - - async findByParentId( - parent_id: Nullable, - ): Promise> { - const entities = await this.repository.find({ - withDeleted: true, - where: { parent_id }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } - - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } - - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } - - async save(data: CategoryDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + private readonly mapper: CategoryMapper = new CategoryMapper(); + + constructor(private readonly repository: Repository) {} + + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } + + async findByParentId( + parent_id: Nullable, + ): Promise> { + const entities = await this.repository.find({ + withDeleted: true, + where: { parent_id }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } + + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } + + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } + + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: CategoryDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/company.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/company.repository.ts index 48935a29..0b560b66 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/company.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/company.repository.ts @@ -1,6 +1,6 @@ import { - CompanyRepository as DomainRepository, - CompanyAnemic, + CompanyRepository as DomainRepository, + CompanyAnemic, } from '@azkaban/foodfolio-domain'; import { CompanyMapper } from '../mappers'; import { Repository } from 'typeorm'; @@ -8,41 +8,49 @@ import { CompanyEntity } from '../entities'; import { CompanyDAO } from '../../dao'; export class CompanyRepository implements DomainRepository { - private readonly mapper: CompanyMapper = new CompanyMapper(); + private readonly mapper: CompanyMapper = new CompanyMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } - async save(data: CompanyDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: CompanyDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item-variant.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item-variant.repository.ts index 02ccee06..5c346eb7 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item-variant.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item-variant.repository.ts @@ -106,6 +106,14 @@ export class ItemVariantRepository implements DomainRepository { return this.mapper.toDomain(entity); } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } + async delete(id: string): Promise { await this.repository.softDelete(id); return await this.findById(id); diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item.repository.ts index 4645ebac..4abe1462 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/item.repository.ts @@ -35,6 +35,14 @@ export class ItemRepository implements DomainRepository { return this.mapper.toDomain(entity); } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } + async delete(id: string): Promise { await this.repository.softDelete(id); return await this.findById(id); diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/location.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/location.repository.ts index 4d487ae6..5d15b97c 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/location.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/location.repository.ts @@ -1,6 +1,6 @@ import { - LocationRepository as DomainRepository, - LocationAnemic, + LocationRepository as DomainRepository, + LocationAnemic, } from '@azkaban/foodfolio-domain'; import { Repository } from 'typeorm'; import { LocationMapper } from '../mappers'; @@ -9,59 +9,67 @@ import { LocationDAO } from '../../dao'; import { Nullable } from '@toxictoast/azkaban-base-types'; export class LocationRepository implements DomainRepository { - private readonly mapper: LocationMapper = new LocationMapper(); + private readonly mapper: LocationMapper = new LocationMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findByParentId( - parent_id: Nullable, - ): Promise> { - const entities = await this.repository.find({ - withDeleted: true, - where: { parent_id }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findByParentId( + parent_id: Nullable, + ): Promise> { + const entities = await this.repository.find({ + withDeleted: true, + where: { parent_id }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findByFreezer(freezer: boolean): Promise> { - const entities = await this.repository.find({ - withDeleted: true, - where: { freezer }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findByFreezer(freezer: boolean): Promise> { + const entities = await this.repository.find({ + withDeleted: true, + where: { freezer }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } - async save(data: LocationDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: LocationDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/size.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/size.repository.ts index d5972ac7..d0aa8867 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/size.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/size.repository.ts @@ -1,6 +1,6 @@ import { - SizeRepository as DomainRepository, - SizeAnemic, + SizeRepository as DomainRepository, + SizeAnemic, } from '@azkaban/foodfolio-domain'; import { Repository } from 'typeorm'; import { SizeMapper } from '../mappers'; @@ -8,41 +8,49 @@ import { SizeEntity } from '../entities'; import { SizeDAO } from '../../dao'; export class SizeRepository implements DomainRepository { - private readonly mapper: SizeMapper = new SizeMapper(); + private readonly mapper: SizeMapper = new SizeMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } - async save(data: SizeDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: SizeDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/type.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/type.repository.ts index 503c3d03..9bf552ef 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/type.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/type.repository.ts @@ -1,6 +1,6 @@ import { - TypeRepository as DomainRepository, - TypeAnemic, + TypeRepository as DomainRepository, + TypeAnemic, } from '@azkaban/foodfolio-domain'; import { Repository } from 'typeorm'; import { TypeMapper } from '../mappers'; @@ -8,41 +8,49 @@ import { TypeEntity } from '../entities'; import { TypeDAO } from '../../dao'; export class TypeRepository implements DomainRepository { - private readonly mapper: TypeMapper = new TypeMapper(); + private readonly mapper: TypeMapper = new TypeMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } - async save(data: TypeDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: TypeDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/warehouse.repository.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/warehouse.repository.ts index 42244395..7fb1225a 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/warehouse.repository.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/repositories/warehouse.repository.ts @@ -1,6 +1,6 @@ import { - WarehouseRepository as DomainRepository, - WarehouseAnemic, + WarehouseRepository as DomainRepository, + WarehouseAnemic, } from '@azkaban/foodfolio-domain'; import { Repository } from 'typeorm'; import { WarehouseMapper } from '../mappers'; @@ -8,41 +8,49 @@ import { WarehouseEntity } from '../entities'; import { WarehouseDAO } from '../../dao'; export class WarehouseRepository implements DomainRepository { - private readonly mapper: WarehouseMapper = new WarehouseMapper(); + private readonly mapper: WarehouseMapper = new WarehouseMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList( - limit?: number, - offset?: number, - ): Promise> { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList( + limit?: number, + offset?: number, + ): Promise> { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - return this.mapper.toDomain(entity); - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + return this.mapper.toDomain(entity); + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + return this.mapper.toDomain(entity); + } - async save(data: WarehouseDAO): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - return this.mapper.toDomain(saved); - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: WarehouseDAO): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + return this.mapper.toDomain(saved); + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/category.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/category.service.ts index ba8e3e09..cc86f759 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/category.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/category.service.ts @@ -6,113 +6,123 @@ import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { BadRequestException, NotFoundException } from '@nestjs/common'; export class CategoryService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: CategoryRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: CategoryRepository) { + this.domainService = new DomainService(repository); + } - async getCategoryList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getCategories(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getCategoryList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getCategories(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getCategoryByParentId( - parent_id: Nullable, - ): Promise> { - const result = - await this.domainService.getCategoryByParentId(parent_id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getCategoryByParentId( + parent_id: Nullable, + ): Promise> { + const result = + await this.domainService.getCategoryByParentId(parent_id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async getCategoryById(id: string): Promise { - const result = await this.domainService.getCategoryById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getCategoryById(id: string): Promise { + const result = await this.domainService.getCategoryById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createCategory(data: CreateCategoryDTO): Promise { - const result = await this.domainService.createCategory(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getCategoryByTitle(title: string): Promise { + const result = await this.domainService.getCategoryByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateParentId( - id: string, - parent_id: Nullable, - ): Promise { - const result = await this.domainService.updateParentId(id, parent_id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createCategory(data: CreateCategoryDTO): Promise { + const result = await this.domainService.createCategory(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateParentId( + id: string, + parent_id: Nullable, + ): Promise { + const result = await this.domainService.updateParentId(id, parent_id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteCategory(id: string): Promise { - const result = await this.domainService.deleteCategory(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreCategory(id: string): Promise { - const result = await this.domainService.restoreCategory(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteCategory(id: string): Promise { + const result = await this.domainService.deleteCategory(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreCategory(id: string): Promise { + const result = await this.domainService.restoreCategory(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/company.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/company.service.ts index 7a117b41..15ba4c27 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/company.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/company.service.ts @@ -6,87 +6,97 @@ import { NotFoundException } from '@nestjs/common'; import { CreateCompanyDTO } from '../../dto'; export class CompanyService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: CompanyRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: CompanyRepository) { + this.domainService = new DomainService(repository); + } - async getCompanyList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getCompanies(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getCompanyList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getCompanies(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getCompanyById(id: string): Promise { - const result = await this.domainService.getCompanyById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getCompanyById(id: string): Promise { + const result = await this.domainService.getCompanyById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createCompany(data: CreateCompanyDTO): Promise { - const result = await this.domainService.createCompany(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getCompanyByTitle(title: string): Promise { + const result = await this.domainService.getCompanyByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async createCompany(data: CreateCompanyDTO): Promise { + const result = await this.domainService.createCompany(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async deleteCompany(id: string): Promise { - const result = await this.domainService.deleteCompany(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async restoreCompany(id: string): Promise { - const result = await this.domainService.restoreCompany(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteCompany(id: string): Promise { + const result = await this.domainService.deleteCompany(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreCompany(id: string): Promise { + const result = await this.domainService.restoreCompany(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/item-variant.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/item-variant.service.ts index 3e400bc9..a8e6c4f2 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/item-variant.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/item-variant.service.ts @@ -122,6 +122,16 @@ export class ItemVariantService { } } + async getItemVariantByTitle(title: string): Promise { + const result = await this.domainService.getItemVariantByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + async createItemVariant( data: CreateItemVariantDTO, ): Promise { diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/item.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/item.service.ts index 8083cf9a..d64befb3 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/item.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/item.service.ts @@ -34,6 +34,16 @@ export class ItemService { } } + async getItemByTitle(title: string): Promise { + const result = await this.domainService.getItemByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + async createItem(data: CreateItemDTO): Promise { const result = await this.domainService.createItem(data); if (result.isSuccess) { diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/location.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/location.service.ts index 9b6cd403..7df4a19d 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/location.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/location.service.ts @@ -6,133 +6,143 @@ import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { BadRequestException, NotFoundException } from '@nestjs/common'; export class LocationService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: LocationRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: LocationRepository) { + this.domainService = new DomainService(repository); + } - async getLocationList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getLocations(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getLocationList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getLocations(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getLocationByFreezer(freezer: boolean): Promise> { - const result = await this.domainService.getLocationByFreezer(freezer); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getLocationByFreezer(freezer: boolean): Promise> { + const result = await this.domainService.getLocationByFreezer(freezer); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async getLocationByParentId( - parent_id: Nullable, - ): Promise> { - const result = - await this.domainService.getLocationByParentId(parent_id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getLocationByParentId( + parent_id: Nullable, + ): Promise> { + const result = + await this.domainService.getLocationByParentId(parent_id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async getLocationById(id: string): Promise { - const result = await this.domainService.getLocationById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getLocationById(id: string): Promise { + const result = await this.domainService.getLocationById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createLocation(data: CreateLocationDTO): Promise { - const result = await this.domainService.createLocation(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getLocationByTitle(title: string): Promise { + const result = await this.domainService.getLocationByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateParentId( - id: string, - parent_id: Nullable, - ): Promise { - const result = await this.domainService.updateParentId(id, parent_id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createLocation(data: CreateLocationDTO): Promise { + const result = await this.domainService.createLocation(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateParentId( + id: string, + parent_id: Nullable, + ): Promise { + const result = await this.domainService.updateParentId(id, parent_id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateFreezer(id: string, freezer: boolean): Promise { - const result = await this.domainService.updateFreezer(id, freezer); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteLocation(id: string): Promise { - const result = await this.domainService.deleteLocation(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateFreezer(id: string, freezer: boolean): Promise { + const result = await this.domainService.updateFreezer(id, freezer); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreLocation(id: string): Promise { - const result = await this.domainService.restoreLocation(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteLocation(id: string): Promise { + const result = await this.domainService.deleteLocation(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreLocation(id: string): Promise { + const result = await this.domainService.restoreLocation(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/size.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/size.service.ts index 4ce5d558..46c0fb82 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/size.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/size.service.ts @@ -6,87 +6,97 @@ import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { BadRequestException, NotFoundException } from '@nestjs/common'; export class SizeService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: SizeRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: SizeRepository) { + this.domainService = new DomainService(repository); + } - async getSizeList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getSizes(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getSizeList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getSizes(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getSizeById(id: string): Promise { - const result = await this.domainService.getSizeById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getSizeById(id: string): Promise { + const result = await this.domainService.getSizeById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createSize(data: CreateSizeDTO): Promise { - const result = await this.domainService.createSize(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getSizeByTitle(title: string): Promise { + const result = await this.domainService.getSizeByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createSize(data: CreateSizeDTO): Promise { + const result = await this.domainService.createSize(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteSize(id: string): Promise { - const result = await this.domainService.deleteSize(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreSize(id: string): Promise { - const result = await this.domainService.restoreSize(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteSize(id: string): Promise { + const result = await this.domainService.deleteSize(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreSize(id: string): Promise { + const result = await this.domainService.restoreSize(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/type.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/type.service.ts index c1de1e6b..85ac8727 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/type.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/type.service.ts @@ -6,87 +6,97 @@ import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { BadRequestException, NotFoundException } from '@nestjs/common'; export class TypeService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: TypeRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: TypeRepository) { + this.domainService = new DomainService(repository); + } - async getTypeList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getTypes(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getTypeList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getTypes(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getTypeById(id: string): Promise { - const result = await this.domainService.getTypeById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getTypeById(id: string): Promise { + const result = await this.domainService.getTypeById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createType(data: CreateTypeDTO): Promise { - const result = await this.domainService.createType(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getTypeByTitle(title: string): Promise { + const result = await this.domainService.getTypeByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createType(data: CreateTypeDTO): Promise { + const result = await this.domainService.createType(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteType(id: string): Promise { - const result = await this.domainService.deleteType(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreType(id: string): Promise { - const result = await this.domainService.restoreType(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteType(id: string): Promise { + const result = await this.domainService.deleteType(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreType(id: string): Promise { + const result = await this.domainService.restoreType(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/foodfolio-infrastructure/src/lib/typeorm/services/warehouse.service.ts b/libs/foodfolio-infrastructure/src/lib/typeorm/services/warehouse.service.ts index 8f785139..630bb1eb 100644 --- a/libs/foodfolio-infrastructure/src/lib/typeorm/services/warehouse.service.ts +++ b/libs/foodfolio-infrastructure/src/lib/typeorm/services/warehouse.service.ts @@ -6,87 +6,97 @@ import { Nullable, Optional } from '@toxictoast/azkaban-base-types'; import { BadRequestException, NotFoundException } from '@nestjs/common'; export class WarehouseService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: WarehouseRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: WarehouseRepository) { + this.domainService = new DomainService(repository); + } - async getWarehouseList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getWarehouses(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getWarehouseList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getWarehouses(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getWarehouseById(id: string): Promise { - const result = await this.domainService.getWarehouseById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getWarehouseById(id: string): Promise { + const result = await this.domainService.getWarehouseById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createWarehouse(data: CreateWarehouseDTO): Promise { - const result = await this.domainService.createWarehouse(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getWarehouseByTitle(title: string): Promise { + const result = await this.domainService.getWarehouseByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createWarehouse(data: CreateWarehouseDTO): Promise { + const result = await this.domainService.createWarehouse(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActivatedAt( - id: string, - activated_at: Nullable, - ): Promise { - const result = await this.domainService.updateActivatedAt( - id, - activated_at, - ); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteWarehouse(id: string): Promise { - const result = await this.domainService.deleteWarehouse(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActivatedAt( + id: string, + activated_at: Nullable, + ): Promise { + const result = await this.domainService.updateActivatedAt( + id, + activated_at, + ); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreWarehouse(id: string): Promise { - const result = await this.domainService.restoreWarehouse(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteWarehouse(id: string): Promise { + const result = await this.domainService.deleteWarehouse(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreWarehouse(id: string): Promise { + const result = await this.domainService.restoreWarehouse(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } } diff --git a/libs/group-domain/src/lib/repositories/group.repository.ts b/libs/group-domain/src/lib/repositories/group.repository.ts index 4acf8a7f..f253b8ef 100644 --- a/libs/group-domain/src/lib/repositories/group.repository.ts +++ b/libs/group-domain/src/lib/repositories/group.repository.ts @@ -1,4 +1,12 @@ import { GroupAnemic } from '../anemics'; import { Repository } from '@toxictoast/azkaban-base-domain'; +import { Chainable } from '@toxictoast/azkaban-base-types'; -export type GroupRepository = Repository; +interface GroupAdditions { + findByTitle(title: string): Promise; +} + +export type GroupRepository = Chainable< + GroupAdditions, + Repository +>; diff --git a/libs/group-domain/src/lib/services/group.service.ts b/libs/group-domain/src/lib/services/group.service.ts index 0fb9dbff..5d59fb90 100644 --- a/libs/group-domain/src/lib/services/group.service.ts +++ b/libs/group-domain/src/lib/services/group.service.ts @@ -4,130 +4,149 @@ import { GroupAnemic } from '../anemics'; import { Result } from '@toxictoast/azkaban-base-domain'; import { Optional } from '@toxictoast/azkaban-base-types'; import { GroupData } from '../data'; -import { GroupErrorCodes } from '@toxictoast/azkaban-base-helpers'; +import { + GenericErrorCodes, + GroupErrorCodes, +} from '@toxictoast/azkaban-base-helpers'; export class GroupService { - private readonly factory: GroupFactory = new GroupFactory(); + private readonly factory: GroupFactory = new GroupFactory(); - constructor(private readonly repository: GroupRepository) {} + constructor(private readonly repository: GroupRepository) {} - private async save(anemic: GroupAnemic): Promise> { - try { - const result = await this.repository.save(anemic); - return Result.ok(result); - } catch (error) { - return Result.fail(error); - } - } + private async save(anemic: GroupAnemic): Promise> { + try { + const result = await this.repository.save(anemic); + return Result.ok(result); + } catch (error) { + return Result.fail(error); + } + } - async getGroups( - limit?: Optional, - offset?: Optional, - ): Promise>> { - try { - const result = await this.repository.findList(limit, offset); - return Result.ok>(result); - } catch (error) { - return Result.fail>(error); - } - } + async getGroups( + limit?: Optional, + offset?: Optional, + ): Promise>> { + try { + const result = await this.repository.findList(limit, offset); + return Result.ok>(result); + } catch (error) { + return Result.fail>(error); + } + } - async getGroupById(id: string): Promise> { - try { - const result = await this.repository.findById(id); - if (result !== null) { - return Result.ok(result); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async getGroupById(id: string): Promise> { + try { + const result = await this.repository.findById(id); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async createGroup(data: GroupData): Promise> { - try { - const aggregate = this.factory.createDomain(data); - return await this.save(aggregate.toAnemic()); - } catch (error) { - return Result.fail(error); - } - } + async getGroupByTitle(title: string): Promise> { + try { + const result = await this.repository.findByTitle(title); + if (result !== null) { + return Result.ok(result); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async deleteGroup(id: string): Promise> { - try { - const group = await this.getGroupById(id); - if (group.isSuccess) { - const groupValue = group.value; - const aggregate = this.factory.reconstitute(groupValue); - aggregate.delete(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async createGroup(data: GroupData): Promise> { + try { + const check = await this.getGroupByTitle(data.title); + if (check.isSuccess) { + return Result.fail(GenericErrorCodes.UNKNOWN); + } + const aggregate = this.factory.createDomain(data); + return await this.save(aggregate.toAnemic()); + } catch (error) { + return Result.fail(error); + } + } - async restoreGroup(id: string): Promise> { - try { - const group = await this.getGroupById(id); - if (group.isSuccess) { - const groupValue = group.value; - const aggregate = this.factory.reconstitute(groupValue); - aggregate.restore(); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async deleteGroup(id: string): Promise> { + try { + const group = await this.getGroupById(id); + if (group.isSuccess) { + const groupValue = group.value; + const aggregate = this.factory.reconstitute(groupValue); + aggregate.delete(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateTitle(id: string, title: string): Promise> { - try { - const group = await this.getGroupById(id); - if (group.isSuccess) { - const groupValue = group.value; - const aggregate = this.factory.reconstitute(groupValue); - aggregate.updateTitle(title); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async restoreGroup(id: string): Promise> { + try { + const group = await this.getGroupById(id); + if (group.isSuccess) { + const groupValue = group.value; + const aggregate = this.factory.reconstitute(groupValue); + aggregate.restore(); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateSlug(id: string, slug: string): Promise> { - try { - const group = await this.getGroupById(id); - if (group.isSuccess) { - const groupValue = group.value; - const aggregate = this.factory.reconstitute(groupValue); - aggregate.updateSlug(slug); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async updateTitle(id: string, title: string): Promise> { + try { + const group = await this.getGroupById(id); + if (group.isSuccess) { + const groupValue = group.value; + const aggregate = this.factory.reconstitute(groupValue); + aggregate.updateTitle(title); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } - async updateActive( - id: string, - active: boolean, - ): Promise> { - try { - const group = await this.getGroupById(id); - if (group.isSuccess) { - const groupValue = group.value; - const aggregate = this.factory.reconstitute(groupValue); - aggregate.updateActive(active); - return await this.save(aggregate.toAnemic()); - } - return Result.fail(GroupErrorCodes.NOT_FOUND); - } catch (error) { - return Result.fail(error); - } - } + async updateSlug(id: string, slug: string): Promise> { + try { + const group = await this.getGroupById(id); + if (group.isSuccess) { + const groupValue = group.value; + const aggregate = this.factory.reconstitute(groupValue); + aggregate.updateSlug(slug); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } + + async updateActive( + id: string, + active: boolean, + ): Promise> { + try { + const group = await this.getGroupById(id); + if (group.isSuccess) { + const groupValue = group.value; + const aggregate = this.factory.reconstitute(groupValue); + aggregate.updateActive(active); + return await this.save(aggregate.toAnemic()); + } + return Result.fail(GroupErrorCodes.NOT_FOUND); + } catch (error) { + return Result.fail(error); + } + } } diff --git a/libs/group-infrastructure/src/lib/typeorm/repositories/group.repository.ts b/libs/group-infrastructure/src/lib/typeorm/repositories/group.repository.ts index 8ca5dbbf..88e3cc82 100644 --- a/libs/group-infrastructure/src/lib/typeorm/repositories/group.repository.ts +++ b/libs/group-infrastructure/src/lib/typeorm/repositories/group.repository.ts @@ -1,50 +1,61 @@ import { - GroupRepository as DomainRepository, - GroupAnemic, + GroupRepository as DomainRepository, + GroupAnemic, } from '@azkaban/group-domain'; import { GroupMapper } from '../mappers'; import { Repository } from 'typeorm'; import { GroupEntity } from '../entities'; export class GroupRepository implements DomainRepository { - private readonly mapper: GroupMapper = new GroupMapper(); + private readonly mapper: GroupMapper = new GroupMapper(); - constructor(private readonly repository: Repository) {} + constructor(private readonly repository: Repository) {} - async findList(limit?: number, offset?: number): Promise { - const entities = await this.repository.find({ - take: limit, - skip: offset, - withDeleted: true, - order: { - created_at: 'ASC', - }, - }); - return entities.map((entity) => this.mapper.toDomain(entity)); - } + async findList(limit?: number, offset?: number): Promise { + const entities = await this.repository.find({ + take: limit, + skip: offset, + withDeleted: true, + order: { + created_at: 'ASC', + }, + }); + return entities.map((entity) => this.mapper.toDomain(entity)); + } - async findById(id: string): Promise { - const entity = await this.repository.findOne({ - withDeleted: true, - where: { id }, - }); - if (entity) { - return this.mapper.toDomain(entity); - } - return null; - } + async findById(id: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { id }, + }); + if (entity) { + return this.mapper.toDomain(entity); + } + return null; + } - async delete(id: string): Promise { - await this.repository.softDelete(id); - return await this.findById(id); - } + async findByTitle(title: string): Promise { + const entity = await this.repository.findOne({ + withDeleted: true, + where: { title }, + }); + if (entity) { + return this.mapper.toDomain(entity); + } + return null; + } - async save(data: GroupAnemic): Promise { - const entity = this.mapper.toEntity(data); - const saved = await this.repository.save(entity); - if (saved) { - return this.mapper.toDomain(saved); - } - return null; - } + async delete(id: string): Promise { + await this.repository.softDelete(id); + return await this.findById(id); + } + + async save(data: GroupAnemic): Promise { + const entity = this.mapper.toEntity(data); + const saved = await this.repository.save(entity); + if (saved) { + return this.mapper.toDomain(saved); + } + return null; + } } diff --git a/libs/group-infrastructure/src/lib/typeorm/services/group.service.ts b/libs/group-infrastructure/src/lib/typeorm/services/group.service.ts index 788fe5e2..1e609a01 100644 --- a/libs/group-infrastructure/src/lib/typeorm/services/group.service.ts +++ b/libs/group-infrastructure/src/lib/typeorm/services/group.service.ts @@ -6,91 +6,101 @@ import { BadRequestException, NotFoundException } from '@nestjs/common'; import { CreateGroupDTO } from '../../dto'; export class GroupService { - private readonly domainService: DomainService; + private readonly domainService: DomainService; - constructor(private readonly repository: GroupRepository) { - this.domainService = new DomainService(repository); - } + constructor(private readonly repository: GroupRepository) { + this.domainService = new DomainService(repository); + } - async getGroupList( - limit?: Optional, - offset?: Optional, - ): Promise> { - const result = await this.domainService.getGroups(limit, offset); - if (result.isSuccess) { - return result.value; - } else { - return []; - } - } + async getGroupList( + limit?: Optional, + offset?: Optional, + ): Promise> { + const result = await this.domainService.getGroups(limit, offset); + if (result.isSuccess) { + return result.value; + } else { + return []; + } + } - async getGroupById(id: string): Promise { - const result = await this.domainService.getGroupById(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async getGroupById(id: string): Promise { + const result = await this.domainService.getGroupById(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async createGroup(data: CreateGroupDTO): Promise { - const result = await this.domainService.createGroup(data); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async getGroupByTitle(title: string): Promise { + const result = await this.domainService.getGroupByTitle(title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } - async updateTitle(id: string, title: string): Promise { - const result = await this.domainService.updateTitle(id, title); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async createGroup(data: CreateGroupDTO): Promise { + const result = await this.domainService.createGroup(data); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateSlug(id: string, slug: string): Promise { - const result = await this.domainService.updateSlug(id, slug); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateTitle(id: string, title: string): Promise { + const result = await this.domainService.updateTitle(id, title); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async updateActive(id: string, active: boolean): Promise { - const result = await this.domainService.updateActive(id, active); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new BadRequestException(errorMessage); - } - } + async updateSlug(id: string, slug: string): Promise { + const result = await this.domainService.updateSlug(id, slug); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async deleteGroup(id: string): Promise { - const result = await this.domainService.deleteGroup(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async updateActive(id: string, active: boolean): Promise { + const result = await this.domainService.updateActive(id, active); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new BadRequestException(errorMessage); + } + } - async restoreGroup(id: string): Promise { - const result = await this.domainService.restoreGroup(id); - if (result.isSuccess) { - return result.value; - } else { - const errorMessage = result.errorValue; - throw new NotFoundException(errorMessage); - } - } + async deleteGroup(id: string): Promise { + const result = await this.domainService.deleteGroup(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } + + async restoreGroup(id: string): Promise { + const result = await this.domainService.restoreGroup(id); + if (result.isSuccess) { + return result.value; + } else { + const errorMessage = result.errorValue; + throw new NotFoundException(errorMessage); + } + } }