diff --git a/src/lib/typesense-client.test.ts b/src/lib/typesense-client.test.ts index 1e37ee9..0bbcc44 100644 --- a/src/lib/typesense-client.test.ts +++ b/src/lib/typesense-client.test.ts @@ -9,7 +9,6 @@ describe('Typesense Client InvTypes', () => { it('single type', async () => { const result = await GetInvTypes([630]); - expect(result.size).toBe(1); expect(result.get(630)).toBeDefined(); expect(fetchSpy).toHaveBeenCalledOnce(); @@ -217,7 +216,7 @@ describe('Typesense Client GetTypeCategories', () => { expect(results.get(630)).toBeDefined(); expect(fetchSpy).toHaveBeenCalled(); }); - + it('get different categories from different type ids', async () => { const results = await GetTypeCategories([450, 1829, 582]); expect(results.size).toBe(3); @@ -251,7 +250,7 @@ describe('Typesense Client AreTypesBlueprints', () => { expect(results.get(582)).toBe(false); expect(fetchSpy).toHaveBeenCalled(); }); - + it('type is blueprint', async () => { const results = await AreTypesBlueprints([11135]); expect(results.size).toBe(1); @@ -341,12 +340,12 @@ describe('Typesense Client GetIndustryActivityProducts', () => { const results = await GetIndustryActivityProducts([11135]); if (results.size > 0) { const product = results.get(11135); - if (product) { - expect(product).toHaveProperty('typeID'); - expect(product).toHaveProperty('activityID'); - expect(product).toHaveProperty('productTypeID'); - expect(product).toHaveProperty('quantity'); - } + expect(product).toBeDefined(); + expect(product?.length).toBe(1); + expect(product?.[0]).toHaveProperty('typeID'); + expect(product?.[0]).toHaveProperty('activityID'); + expect(product?.[0]).toHaveProperty('productTypeID'); + expect(product?.[0]).toHaveProperty('quantity'); } }); }); @@ -414,12 +413,12 @@ describe('Typesense Client GetIndustryActivityMaterials', () => { const results = await GetIndustryActivityMaterials([11135]); if (results.size > 0) { const material = results.get(11135); - if (material) { - expect(material).toHaveProperty('typeID'); - expect(material).toHaveProperty('activityID'); - expect(material).toHaveProperty('materialTypeID'); - expect(material).toHaveProperty('quantity'); - } + expect(material).toBeDefined(); + expect(material?.length).toBe(1); + expect(material?.[0]).toHaveProperty('typeID'); + expect(material?.[0]).toHaveProperty('activityID'); + expect(material?.[0]).toHaveProperty('materialTypeID'); + expect(material?.[0]).toHaveProperty('quantity'); } }); }); diff --git a/src/lib/typesense-client.ts b/src/lib/typesense-client.ts index e53ebda..b8a5195 100644 --- a/src/lib/typesense-client.ts +++ b/src/lib/typesense-client.ts @@ -42,7 +42,7 @@ async function Get(ids: number[], collection: string, id_key: string): Promis reqBatches.push(batch); } - console.log("reqBatches", reqBatches); + // console.log("reqBatches", reqBatches); // Fetch the batches await Promise.all( reqBatches.map(async (batch) => { @@ -72,24 +72,37 @@ async function Get(ids: number[], collection: string, id_key: string): Promis return Promise.resolve(resp); } +function flatMap(inMap: Map): Map { + const resp = new Map(); + for (const [id, value] of inMap.entries()) + resp.set(id, value[0] ?? null); + return resp; +} + +// A bit of a mess... But it'll do for now, until I get a better idea export async function GetInvTypes(typeIds: number[]): Promise> { - return (await Get(typeIds, "invTypes", "typeID"))[0] ?? null; + const result = await Get(typeIds, "invTypes", "typeID"); + return flatMap(result); } export async function GetInvGroups(groupIds: number[]): Promise> { - return (await Get(groupIds, "invGroups", "groupID"))[0] ?? null; + const result = await Get(groupIds, "invGroups", "groupID"); + return flatMap(result); } export async function GetInvCategories(categoryIds: number[]): Promise> { - return (await Get(categoryIds, "invCategories", "categoryID"))[0] ?? null; + const result = await Get(categoryIds, "invCategories", "categoryID"); + return flatMap(result); } export async function GetRAMActivities(activityIds: number[]): Promise> { - return (await Get(activityIds, "ramActivities", "activityID"))[0] ?? null; + const result = await Get(activityIds, "ramActivities", "activityID"); + return flatMap(result); } export async function GetStaStations(stationIds: number[]): Promise> { - return (await Get(stationIds, "staStations", "stationID"))[0] ?? null; + const result = await Get(stationIds, "staStations", "stationID"); + return flatMap(result); } export async function GetIndustryActivityProducts(typeIds: number[]): Promise> { @@ -100,8 +113,9 @@ export async function GetIndustryActivityMaterials(typeIds: number[]): Promise(typeIds, "industryActivityMaterials", "typeID"); } -export async function GetIndustryActivity(typeIds: number[]): Promise> { - return await Get(typeIds, "industryActivity", "typeID"); +export async function GetIndustryActivity(typeIds: number[]): Promise> { + const result = await Get(typeIds, "industryActivity", "typeID"); + return flatMap(result); } async function queryTypesense(params: URLSearchParams, collection: string): Promise> {