Skip to content

Commit

Permalink
refactor: router accepts array of ids and returns ids that do not exist
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
buckhalt committed Mar 26, 2024
1 parent d877e85 commit a3be68e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
41 changes: 15 additions & 26 deletions hooks/useProtocolImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -165,42 +165,31 @@ export const useProtocolImport = () => {

const newAssets: typeof assets = [];

const assetsWithCombinedMetadata: z.infer<typeof assetInsertSchema> = [];
const existingAssetIds: string[] = [];

let newAssetsWithCombinedMetadata: z.infer<typeof assetInsertSchema> = [];

// 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({
Expand Down Expand Up @@ -300,7 +289,7 @@ export const useProtocolImport = () => {
protocol: protocolJson,
protocolName: fileName,
newAssets: newAssetsWithCombinedMetadata,
existingAssets: assetsWithCombinedMetadata,
existingAssetIds: existingAssetIds,
});

if (result.error) {
Expand Down
14 changes: 9 additions & 5 deletions server/routers/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}),
});
6 changes: 3 additions & 3 deletions server/routers/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -165,7 +165,7 @@ export const protocolRouter = router({
protocol: inputProtocol,
protocolName,
newAssets,
existingAssets,
existingAssetIds,
} = input;

const protocol = inputProtocol as Protocol;
Expand All @@ -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 })),
},
},
});
Expand Down

0 comments on commit a3be68e

Please sign in to comment.