Skip to content

Commit

Permalink
2.5.13 more careful error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljonescodes committed Dec 26, 2023
1 parent 05e723d commit 05da939
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 48 deletions.
101 changes: 61 additions & 40 deletions src/moa-square/services/catalogs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {

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 });
Expand All @@ -70,34 +71,39 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {
},
);
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) {
Expand All @@ -112,11 +118,16 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {
);
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;
Expand All @@ -134,10 +145,14 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {
},
);
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;
}
Expand All @@ -155,12 +170,17 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {
);
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;
Expand Down Expand Up @@ -234,7 +254,8 @@ export class CatalogsService extends EntityRepositoryService<CatalogEntity> {
for (const squareModifierList of squareModifierListCatalogObjects) {
await this.modifierListsService.process({
catalogObject: squareModifierList,
moaCatalogId: moaCatalog.id,
catalogId: moaCatalog.id,
merchantId,
});
}

Expand Down
58 changes: 52 additions & 6 deletions src/moa-square/services/modifier-lists.service.ts
Original file line number Diff line number Diff line change
@@ -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<ModifierListEntity> {
Expand All @@ -15,28 +17,63 @@ export class ModifierListsService extends EntityRepositoryService<ModifierListEn
@InjectRepository(ModifierListEntity)
protected readonly repository: Repository<ModifierListEntity>,
protected readonly modifierLocationOverridesService: ModifierLocationOverridesService,
@Inject(forwardRef(() => ModifiersService))
protected readonly modifiersService: WrapperType<ModifiersService>,
) {
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}.`,
);

let moaModifierList = await this.findOne({
where: {
squareId: params.catalogObject.id,
catalogId: moaCatalogId,
squareId: catalogObject.id,
catalogId,
},
relations: ['modifiers'],
});
Expand All @@ -49,7 +86,7 @@ export class ModifierListsService extends EntityRepositoryService<ModifierListEn
ordinal: squareModifierList?.ordinal,
selectionType: (squareModifierList?.selectionType ??
'SINGLE') as MoaSelectionType,
catalogId: moaCatalogId,
catalogId,
}),
);
moaModifierList.modifiers = [];
Expand All @@ -63,6 +100,15 @@ export class ModifierListsService extends EntityRepositoryService<ModifierListEn
'SINGLE') as MoaSelectionType;
moaModifierList = await this.save(moaModifierList);
}

for (const modifier of squareModifierList?.modifiers ?? []) {
await this.modifiersService.process({
squareCatalogObject: modifier,
catalogId,
merchantId,
});
}

this.logger.verbose(`Updated modifier list ${moaModifierList.name}.`);
return moaModifierList;
}
Expand Down
6 changes: 4 additions & 2 deletions src/moa-square/services/modifiers.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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 { ModifierEntity } from '../entities/modifier.entity.js';
import { LocationsService } from './locations.service.js';
import { ModifierListsService } from './modifier-lists.service.js';
Expand All @@ -15,7 +16,8 @@ export class ModifiersService extends EntityRepositoryService<ModifierEntity> {
constructor(
@InjectRepository(ModifierEntity)
protected readonly repository: Repository<ModifierEntity>,
protected readonly modifierListsService: ModifierListsService,
@Inject(forwardRef(() => ModifierListsService))
protected readonly modifierListsService: WrapperType<ModifierListsService>,
protected readonly modifierLocationOverridesService: ModifierLocationOverridesService,
protected readonly locationsService: LocationsService,
) {
Expand Down

0 comments on commit 05da939

Please sign in to comment.