diff --git a/.gitignore b/.gitignore index dd146b5..443d662 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ next-env.d.ts # Sentry Config File .env.sentry-build-plugin +.env*.local diff --git a/src/features/items-sync/lib/SyncedItems.service.ts b/src/features/items-sync/lib/SyncedItems.service.ts index 00b4210..bb4f0a5 100644 --- a/src/features/items-sync/lib/SyncedItems.service.ts +++ b/src/features/items-sync/lib/SyncedItems.service.ts @@ -152,7 +152,7 @@ class SyncedItemsService extends AuthenticatedXeroService { for (const price of prices) { const productMap = await this.copilot.getProductsMapById([price.productId]) - const product = productMap[price.productId] + const product = productMap?.[price.productId] if (!product) { throw new APIError('Could not find product for mapping', status.BAD_REQUEST) } @@ -300,10 +300,9 @@ class SyncedItemsService extends AuthenticatedXeroService { copilotPriceMapPromise, xeroItemMapPromise, ]) - - const copilotProduct = copilotProductMap[productId] - const copilotPrice = copilotPriceMap[priceId] - const xeroItem = xeroItemMap[itemId] + const copilotProduct = copilotProductMap?.[productId] || null + const copilotPrice = copilotPriceMap?.[priceId] || null + const xeroItem = xeroItemMap?.[itemId] || null return { copilotProduct, copilotPrice, xeroItem } } catch (_) { return { copilotProduct: null, copilotPrice: null, xeroItem: null } diff --git a/src/features/settings/lib/ProductMappings.service.ts b/src/features/settings/lib/ProductMappings.service.ts index d2b0953..46e43d8 100644 --- a/src/features/settings/lib/ProductMappings.service.ts +++ b/src/features/settings/lib/ProductMappings.service.ts @@ -41,6 +41,8 @@ class ProductMappingsService extends AuthenticatedXeroService { this.copilot.getPricesMapById('all'), ]) + if (!copilotPrices || !copilotProducts) return [] + const mappings = Object.values(copilotPrices) // Sort by decreasing createdAt date .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) diff --git a/src/lib/copilot/CopilotAPI.ts b/src/lib/copilot/CopilotAPI.ts index 0e333e1..68a3a2e 100644 --- a/src/lib/copilot/CopilotAPI.ts +++ b/src/lib/copilot/CopilotAPI.ts @@ -145,8 +145,10 @@ export class CopilotAPI { async _getProducts( productIds: string[] | 'all', args: CopilotListArgs = { limit: MAX_FETCH_COPILOT_RESOURCES }, - ): Promise> { + ): Promise | null> { const allProductsResponse = await this.copilot.listProducts(args) + + if (!allProductsResponse.data) return null const allProducts = z.array(CopilotProductSchema).parse(allProductsResponse.data) return allProducts.reduce>((acc, product) => { @@ -164,8 +166,10 @@ export class CopilotAPI { async _getPrices( priceIds: string[] | 'all', args = { limit: '10_000' }, - ): Promise> { + ): Promise | null> { const allPricesResponse = await this.copilot.listPrices(args) + + if (!allPricesResponse.data) return null const allPrices = z.array(CopilotPriceSchema).parse(allPricesResponse.data) return allPrices.reduce>((acc, price) => {