Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ next-env.d.ts

# Sentry Config File
.env.sentry-build-plugin
.env*.local
9 changes: 4 additions & 5 deletions src/features/items-sync/lib/SyncedItems.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions src/features/settings/lib/ProductMappings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 6 additions & 2 deletions src/lib/copilot/CopilotAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ export class CopilotAPI {
async _getProducts(
productIds: string[] | 'all',
args: CopilotListArgs = { limit: MAX_FETCH_COPILOT_RESOURCES },
): Promise<Record<string, CopilotProduct>> {
): Promise<Record<string, CopilotProduct> | null> {
const allProductsResponse = await this.copilot.listProducts(args)

if (!allProductsResponse.data) return null
const allProducts = z.array(CopilotProductSchema).parse(allProductsResponse.data)

return allProducts.reduce<Record<string, CopilotProduct>>((acc, product) => {
Expand All @@ -164,8 +166,10 @@ export class CopilotAPI {
async _getPrices(
priceIds: string[] | 'all',
args = { limit: '10_000' },
): Promise<Record<string, CopilotPrice>> {
): Promise<Record<string, CopilotPrice> | null> {
const allPricesResponse = await this.copilot.listPrices(args)

if (!allPricesResponse.data) return null
Comment on lines +171 to +172
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are returning a hashmap of priceId in key, price in value - we should keep our typing semantic and return {} instead if there are no prices.

That way we only have to change 1 line in this PR.

const allPrices = z.array(CopilotPriceSchema).parse(allPricesResponse.data)

return allPrices.reduce<Record<string, CopilotPrice>>((acc, price) => {
Expand Down