Skip to content

Commit

Permalink
Merge pull request #1398 from Shelf-nu/fix-prevent-no-asset-id-in-ass…
Browse files Browse the repository at this point in the history
…etview

fix: Prevent no asset in in AssetSearchView
  • Loading branch information
DonKoko authored Nov 7, 2024
2 parents 807ca29 + 7685b05 commit 1b43ffc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
CREATE OR REPLACE VIEW public."AssetSearchView"
WITH (security_barrier = false, security_invoker = true) -- Explicitly set security
AS
WITH asset_base AS (
SELECT
a.id,
a."createdAt",
a.id as "assetId",
a.title,
a.description,
a."categoryId",
a."locationId",
a."organizationId"
FROM public."Asset" a
WHERE a.id IS NOT NULL
)
SELECT
ab.id,
ab."createdAt",
ab."assetId",
(
COALESCE(ab.title, '')
|| ' ' || COALESCE(c.name, '')
|| ' ' || COALESCE(ab.description, '')
|| ' ' || COALESCE(string_agg(tm.name, ' '), '')
|| ' ' || COALESCE(string_agg(t.name, ' '), '')
|| ' ' || COALESCE(l.name, '')
) as "searchVector"
FROM
asset_base ab
LEFT JOIN public."Category" c ON ab."categoryId" = c.id
LEFT JOIN public."Location" l ON ab."locationId" = l.id
LEFT JOIN public."_AssetToTag" atr ON ab.id = atr."A"
LEFT JOIN public."Tag" t ON atr."B" = t.id
LEFT JOIN public."Custody" custd ON ab.id = custd."assetId"
LEFT JOIN public."TeamMember" tm ON custd."teamMemberId" = tm.id
GROUP BY
ab.id,
ab."createdAt",
ab."assetId",
ab.title,
ab.description,
c.id,
c.name,
l.id,
l.name;
40 changes: 38 additions & 2 deletions app/modules/asset/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
import type {
AdvancedIndexAsset,
AdvancedIndexQueryResult,
AssetsFromViewItem,
CreateAssetFromBackupImportPayload,
CreateAssetFromContentImportPayload,
ShelfAssetCustomFieldValueType,
Expand Down Expand Up @@ -135,6 +136,13 @@ const unavailableBookingStatuses = [
BookingStatus.OVERDUE,
];

// Enhanced type safety for the query result
type AssetSearchResult = {

Check warning on line 140 in app/modules/asset/service.server.ts

View workflow job for this annotation

GitHub Actions / tests / ⬣ ESLint

'AssetSearchResult' is defined but never used. Allowed unused vars must match /^_/u
asset: AssetsFromViewItem | null;
} & {
[K in keyof AssetsFromViewItem]: AssetsFromViewItem[K];
};

/**
* Fetches assets from AssetSearchView
* This is used to have a more advanced search however its less performant
Expand Down Expand Up @@ -185,7 +193,11 @@ async function getAssetsFromView(params: {

/** Default value of where. Takes the assets belonging to current user */
let where: Prisma.AssetSearchViewWhereInput = {
asset: { organizationId },
asset: {
organizationId,
// Ensure asset exists
id: { not: undefined },
},
};

/** If the search string exists, add it to the where object */
Expand Down Expand Up @@ -407,7 +419,31 @@ async function getAssetsFromView(params: {
db.assetSearchView.count({ where }),
]);

return { assets: assetSearch.map((a) => a.asset), totalAssets };
// Filter out null assets while logging errors for monitoring
const validAssets = assetSearch.filter((result) => {
if (!result.asset) {
Logger.error(
new ShelfError({
cause: null,
message: "Found AssetSearchView record without associated asset",
label: "Assets",
additionalData: {
searchViewRecord: result,
organizationId,
query: "getAssetsFromView",
where,
},
})
);
return false;
}
return true;
});

return {
assets: validAssets.map((result) => result.asset),
totalAssets: totalAssets,
};
} catch (cause) {
throw new ShelfError({
cause,
Expand Down

0 comments on commit 1b43ffc

Please sign in to comment.