diff --git a/src/moa-square/services/catalogs.service.ts b/src/moa-square/services/catalogs.service.ts index 7a07b19f..a5855107 100644 --- a/src/moa-square/services/catalogs.service.ts +++ b/src/moa-square/services/catalogs.service.ts @@ -54,6 +54,7 @@ export class CatalogsService extends EntityRepositoryService { await this.itemsService.delete({ catalogId: moaCatalog.id }); await this.variationsService.delete({ catalogId: moaCatalog.id }); + await this.modifiersService.delete({ catalogId: moaCatalog.id }); await this.modifierListsService.delete({ catalogId: moaCatalog.id }); await this.categoriesService.delete({ catalogId: moaCatalog.id }); await this.catalogImagesService.delete({ catalogId: moaCatalog.id }); @@ -70,34 +71,39 @@ export class CatalogsService extends EntityRepositoryService { }, ); for (const modifierList of response.result.objects ?? []) { - await this.modifierListsService.process({ - catalogObject: modifierList, - moaCatalogId: catalogId, - }); + try { + await this.modifierListsService.process({ + catalogObject: modifierList, + catalogId: catalogId, + merchantId: merchantId, + }); + } catch (error) { + this.logger.error(error); + } } modifierListCursor = response.result.cursor; } - let modifierCursor: string | undefined | null = null; - while (modifierCursor !== undefined) { - const response = await this.squareService.retryOrThrow( - accessToken, - async (client) => { - return await client.catalogApi.listCatalog( - modifierCursor ?? undefined, - 'MODIFIER', - ); - }, - ); - for (const squareModifier of response.result.objects ?? []) { - await this.modifiersService.process({ - squareCatalogObject: squareModifier, - catalogId, - merchantId, - }); - } - modifierCursor = response.result.cursor; - } + // let modifierCursor: string | undefined | null = null; + // while (modifierCursor !== undefined) { + // const response = await this.squareService.retryOrThrow( + // accessToken, + // async (client) => { + // return await client.catalogApi.listCatalog( + // modifierCursor ?? undefined, + // 'MODIFIER', + // ); + // }, + // ); + // for (const squareModifier of response.result.objects ?? []) { + // await this.modifiersService.process({ + // squareCatalogObject: squareModifier, + // catalogId, + // merchantId, + // }); + // } + // modifierCursor = response.result.cursor; + // } let categoriesCursor: string | undefined | null = null; while (categoriesCursor !== undefined) { @@ -112,11 +118,16 @@ export class CatalogsService extends EntityRepositoryService { ); let squareCategoryIndex = 0; for (const category of response.result.objects ?? []) { - await this.categoriesService.process({ - squareCategoryCatalogObject: category, - moaCatalogId: catalogId, - moaOrdinal: squareCategoryIndex, - }); + try { + await this.categoriesService.process({ + squareCategoryCatalogObject: category, + moaCatalogId: catalogId, + moaOrdinal: squareCategoryIndex, + }); + } catch (error) { + this.logger.error(error); + } + squareCategoryIndex++; } categoriesCursor = response.result.cursor; @@ -134,10 +145,14 @@ export class CatalogsService extends EntityRepositoryService { }, ); for (const category of response.result.objects ?? []) { - await this.catalogImagesService.process({ - catalogImage: category, - catalogId: catalogId, - }); + try { + await this.catalogImagesService.process({ + catalogImage: category, + catalogId: catalogId, + }); + } catch (error) { + this.logger.error(error); + } } catalogImagesCursor = response.result.cursor; } @@ -155,12 +170,17 @@ export class CatalogsService extends EntityRepositoryService { ); let index = 0; for (const item of response.result.objects ?? []) { - await this.itemsService.process({ - merchantId, - squareItemCatalogObject: item, - catalogId: catalogId, - moaOrdinal: index, - }); + try { + await this.itemsService.process({ + merchantId, + squareItemCatalogObject: item, + catalogId: catalogId, + moaOrdinal: index, + }); + } catch (error) { + this.logger.error(error); + } + index++; } itemsCursor = response.result.cursor; @@ -234,7 +254,8 @@ export class CatalogsService extends EntityRepositoryService { for (const squareModifierList of squareModifierListCatalogObjects) { await this.modifierListsService.process({ catalogObject: squareModifierList, - moaCatalogId: moaCatalog.id, + catalogId: moaCatalog.id, + merchantId, }); } diff --git a/src/moa-square/services/modifier-lists.service.ts b/src/moa-square/services/modifier-lists.service.ts index c8790f14..c3195a18 100644 --- a/src/moa-square/services/modifier-lists.service.ts +++ b/src/moa-square/services/modifier-lists.service.ts @@ -1,11 +1,13 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { Inject, Injectable, Logger, forwardRef } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { CatalogObject } from 'square'; import { Repository } from 'typeorm'; import { EntityRepositoryService } from '../../database/entity-repository-service.js'; +import { type WrapperType } from '../../utils/wrapper-type.js'; import { MoaSelectionType } from '../dto/catalogs/catalogs.types.js'; import { ModifierListEntity } from '../entities/modifier-list.entity.js'; import { ModifierLocationOverridesService } from './modifier-location-overrides.service.js'; +import { ModifiersService } from './modifiers.service.js'; @Injectable() export class ModifierListsService extends EntityRepositoryService { @@ -15,19 +17,54 @@ export class ModifierListsService extends EntityRepositoryService, protected readonly modifierLocationOverridesService: ModifierLocationOverridesService, + @Inject(forwardRef(() => ModifiersService)) + protected readonly modifiersService: WrapperType, ) { const logger = new Logger(ModifierListsService.name); super(repository, logger); this.logger = logger; } + /* +{ + "type": "MODIFIER_LIST", + "id": "4DROX2O3EJQOT4FJYIPU2KWJ", + "updated_at": "2023-10-17T01:54:48.474Z", + "created_at": "2023-10-17T01:54:48.474Z", + "version": 1697507688474, + "is_deleted": false, + "present_at_all_locations": true, + "modifier_list_data": { + "name": "Material", + "ordinal": 99999999, + "selection_type": "SINGLE", + "modifiers": [ + { + "type": "MODIFIER", + "id": "KTNXWECU3H6GXO6QMTVI2M63", + "updated_at": "2023-10-17T01:54:48.474Z", + "created_at": "2023-10-17T01:54:48.474Z", + "version": 1697507688474, + "is_deleted": false, + "present_at_all_locations": true, + "modifier_data": { + "name": "Plastic", + "ordinal": 1, + "modifier_list_id": "4DROX2O3EJQOT4FJYIPU2KWJ" + } + }, + ] + } + }, + */ async process(params: { catalogObject: CatalogObject; - moaCatalogId: string; + catalogId: string; + merchantId: string; }) { this.logger.verbose(this.process.name); + const { catalogObject, catalogId, merchantId } = params; const squareModifierList = params.catalogObject.modifierListData; - const moaCatalogId = params.moaCatalogId; this.logger.debug( `Processing modifier list ${squareModifierList?.name} ${params.catalogObject.id}.`, @@ -35,8 +72,8 @@ export class ModifierListsService extends EntityRepositoryService { constructor( @InjectRepository(ModifierEntity) protected readonly repository: Repository, - protected readonly modifierListsService: ModifierListsService, + @Inject(forwardRef(() => ModifierListsService)) + protected readonly modifierListsService: WrapperType, protected readonly modifierLocationOverridesService: ModifierLocationOverridesService, protected readonly locationsService: LocationsService, ) {