From a3be68e49cdca722f050a9a157639cfdd0f4cf87 Mon Sep 17 00:00:00 2001 From: Caden Buckhalt Date: Tue, 26 Mar 2024 08:06:28 -0700 Subject: [PATCH] refactor: router accepts array of ids and returns ids that do not exist makes it so that there is only one db call. existingIds are passed directly in insertProtocol and connected. removes needing to return entire existing object asset. --- hooks/useProtocolImport.tsx | 41 ++++++++++++++----------------------- server/routers/asset.ts | 14 ++++++++----- server/routers/protocol.ts | 6 +++--- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/hooks/useProtocolImport.tsx b/hooks/useProtocolImport.tsx index f83c0a97..ffe9201d 100644 --- a/hooks/useProtocolImport.tsx +++ b/hooks/useProtocolImport.tsx @@ -37,7 +37,7 @@ export const useProtocolImport = () => { const { mutateAsync: getProtocolExists } = api.protocol.get.byHash.useMutation(); - const { mutateAsync: getAsset } = api.asset.get.useMutation(); + const { mutateAsync: getNewAssetIds } = api.asset.get.useMutation(); /** * This is the main job processing function. Takes a file, and handles all @@ -165,42 +165,31 @@ export const useProtocolImport = () => { const newAssets: typeof assets = []; - const assetsWithCombinedMetadata: z.infer = []; + const existingAssetIds: string[] = []; let newAssetsWithCombinedMetadata: z.infer = []; // Check if the assets are already in the database. - // If yes, add them to assetsWithCombinedMetadata to be connected to the protocol. + // If yes, add them to existingAssetIds to be connected to the protocol. // If not, add them to newAssets to be uploaded. - if (assets.length > 0) { - const assetPromises = assets.map(async (asset) => { - const existingAsset = await getAsset(asset.assetId); - return existingAsset - ? { - key: existingAsset.key, - assetId: asset.assetId, - name: asset.name, - type: asset.type, - url: existingAsset.url, - size: existingAsset.size, - } - : asset; - }); - - const resolvedAssets = await Promise.all(assetPromises); + try { + const newAssetIds = await getNewAssetIds( + assets.map((asset) => asset.assetId), + ); - // If the asset has a key, it's an existing asset - resolvedAssets.forEach((resolvedAsset) => { - if ('key' in resolvedAsset) { - assetsWithCombinedMetadata.push(resolvedAsset); + assets.forEach((asset) => { + if (newAssetIds.includes(asset.assetId)) { + newAssets.push(asset); } else { - newAssets.push(resolvedAsset); + existingAssetIds.push(asset.assetId); } }); + } catch (e) { + throw new Error('Error checking for existing assets'); } - // we're going to upload the new assets + // Upload the new assets if (newAssets.length > 0) { dispatch({ @@ -300,7 +289,7 @@ export const useProtocolImport = () => { protocol: protocolJson, protocolName: fileName, newAssets: newAssetsWithCombinedMetadata, - existingAssets: assetsWithCombinedMetadata, + existingAssetIds: existingAssetIds, }); if (result.error) { diff --git a/server/routers/asset.ts b/server/routers/asset.ts index 1ace039c..178fac90 100644 --- a/server/routers/asset.ts +++ b/server/routers/asset.ts @@ -5,13 +5,17 @@ import { z } from 'zod'; export const assetRouter = router({ get: protectedProcedure - .input(z.string()) - .mutation(async ({ input: assetId }) => { - const asset = await prisma.asset.findFirst({ + .input(z.array(z.string())) + .mutation(async ({ input: assetIds }) => { + const assets = await prisma.asset.findMany({ where: { - assetId, + assetId: { + in: assetIds, + }, }, }); - return asset; + const existingAssets = assets.map((asset) => asset.assetId); + // Return the assetIds that are not in the database + return assetIds.filter((assetId) => !existingAssets.includes(assetId)); }), }); diff --git a/server/routers/protocol.ts b/server/routers/protocol.ts index 8b422221..a42180a5 100644 --- a/server/routers/protocol.ts +++ b/server/routers/protocol.ts @@ -155,7 +155,7 @@ export const protocolRouter = router({ protocol: z.unknown(), // TODO: replace this with zod schema version of Protocol type protocolName: z.string(), newAssets: assetInsertSchema, - existingAssets: assetInsertSchema, + existingAssetIds: z.array(z.string()), }) .passthrough() .parse(value); @@ -165,7 +165,7 @@ export const protocolRouter = router({ protocol: inputProtocol, protocolName, newAssets, - existingAssets, + existingAssetIds, } = input; const protocol = inputProtocol as Protocol; @@ -185,7 +185,7 @@ export const protocolRouter = router({ description: protocol.description, assets: { create: newAssets, - connect: existingAssets.map((a) => ({ key: a.key })), + connect: existingAssetIds.map((assetId) => ({ assetId })), }, }, });