Skip to content

Commit

Permalink
Merge pull request #1618 from Shelf-nu/fix-advanced-asset-query
Browse files Browse the repository at this point in the history
fix: bug related to asset query when results are empty caused by reminders
  • Loading branch information
DonKoko authored Jan 31, 2025
2 parents 099ff9c + 91eafd0 commit ec8bcd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
60 changes: 33 additions & 27 deletions app/modules/asset/query.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,33 +1273,39 @@ export const assetQueryJoins = Prisma.sql`
LEFT JOIN public."TeamMember" btm ON b."custodianTeamMemberId" = btm.id
`;

// 4. Return
/**
* Returns SQL fragment for building assets array, ensuring proper handling of empty results
* @returns Prisma.Sql fragment that safely handles no results
*/
export const assetReturnFragment = Prisma.sql`
json_agg(
jsonb_build_object(
'id', aq."assetId",
'qrId', aq."qrId",
'title', aq."assetTitle",
'description', aq."assetDescription",
'createdAt', aq."assetCreatedAt",
'updatedAt', aq."assetUpdatedAt",
'userId', aq."assetUserId",
'mainImage', aq."assetMainImage",
'mainImageExpiration', aq."assetMainImageExpiration",
'categoryId', aq."assetCategoryId",
'locationId', aq."assetLocationId",
'organizationId', aq."assetOrganizationId",
'status', aq."assetStatus",
'valuation', aq."assetValue",
'availableToBook', aq."assetAvailableToBook",
'kitId', aq."assetKitId",
'kit', CASE WHEN aq."kitId" IS NOT NULL THEN jsonb_build_object('id', aq."kitId", 'name', aq."kitName") ELSE NULL END,
'category', CASE WHEN aq."categoryId" IS NOT NULL THEN jsonb_build_object('id', aq."categoryId", 'name', aq."categoryName", 'color', aq."categoryColor") ELSE NULL END,
'tags', aq.tags,
'location', jsonb_build_object('name', aq."locationName"),
'custody', aq.custody,
'customFields', COALESCE(aq."customFields", '[]'::jsonb),
'upcomingReminder', aq.upcomingReminder
)
COALESCE(
json_agg(
jsonb_build_object(
'id', aq."assetId",
'qrId', aq."qrId",
'title', aq."assetTitle",
'description', aq."assetDescription",
'createdAt', aq."assetCreatedAt",
'updatedAt', aq."assetUpdatedAt",
'userId', aq."assetUserId",
'mainImage', aq."assetMainImage",
'mainImageExpiration', aq."assetMainImageExpiration",
'categoryId', aq."assetCategoryId",
'locationId', aq."assetLocationId",
'organizationId', aq."assetOrganizationId",
'status', aq."assetStatus",
'valuation', aq."assetValue",
'availableToBook', aq."assetAvailableToBook",
'kitId', aq."assetKitId",
'kit', CASE WHEN aq."kitId" IS NOT NULL THEN jsonb_build_object('id', aq."kitId", 'name', aq."kitName") ELSE NULL END,
'category', CASE WHEN aq."categoryId" IS NOT NULL THEN jsonb_build_object('id', aq."categoryId", 'name', aq."categoryName", 'color', aq."categoryColor") ELSE NULL END,
'tags', aq.tags,
'location', jsonb_build_object('name', aq."locationName"),
'custody', aq.custody,
'customFields', COALESCE(aq."customFields", '[]'::jsonb),
'upcomingReminder', aq.upcomingReminder
)
) FILTER (WHERE aq."assetId" IS NOT NULL),
'[]'
) AS assets
`;
2 changes: 1 addition & 1 deletion app/modules/asset/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export type AdvancedIndexAsset = Pick<
// Type for the entire query result
export type AdvancedIndexQueryResult = Array<{
total_count: number;
assets: AdvancedIndexAsset[];
assets: AdvancedIndexAsset[]; // This is now guaranteed to be an array, never null
}>;

export interface CustomFieldSorting {
Expand Down

0 comments on commit ec8bcd3

Please sign in to comment.