From 76597b4fd15378091b7b31a53f086135ec90095d Mon Sep 17 00:00:00 2001 From: shadrach Date: Mon, 30 Dec 2024 14:23:59 +0100 Subject: [PATCH] feat: integrate attestations search api --- .../attestations/recommendations.ts | 26 ++++++++++++++++--- .../src/routes/v1/attestations/index.ts | 4 ++- .../src/routes/v1/attestations/schema.ts | 6 +++++ desci-server/src/services/Attestation.ts | 10 +++---- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/desci-server/src/controllers/attestations/recommendations.ts b/desci-server/src/controllers/attestations/recommendations.ts index 42d9bbca6..4f05f7c65 100644 --- a/desci-server/src/controllers/attestations/recommendations.ts +++ b/desci-server/src/controllers/attestations/recommendations.ts @@ -1,16 +1,34 @@ import { NextFunction, Request, Response } from 'express'; import _ from 'lodash'; +import z from 'zod'; import { NotFoundError } from '../../core/ApiError.js'; import { SuccessResponse } from '../../core/ApiResponse.js'; import { logger as parentLogger } from '../../logger.js'; +import { searchAttestationsSchema } from '../../routes/v1/attestations/schema.js'; import { attestationService } from '../../services/Attestation.js'; import { communityService } from '../../services/Communities.js'; const logger = parentLogger.child({ module: 'Recommendations' }); -export const getAllRecommendations = async (_req: Request, res: Response, _next: NextFunction) => { - const attestations = await attestationService.getRecommendedAttestations(); +export const getAllRecommendations = async (req: Request, res: Response, _next: NextFunction) => { + const { query } = await searchAttestationsSchema.parseAsync(req); + logger.trace({ query }, 'getAllRecommendations'); + const attestations = await attestationService.getRecommendedAttestations( + query.search + ? { + where: { + attestationVersion: { + OR: [ + { name: { contains: query.search, mode: 'insensitive' } }, + { description: { contains: query.search, mode: 'insensitive' } }, + ], + }, + desciCommunity: { hidden: false }, + }, + } + : undefined, + ); const attestationEntries = _(attestations) .groupBy((x) => x.attestationId) .map((value, _) => ({ @@ -30,7 +48,7 @@ export const getAllRecommendations = async (_req: Request, res: Response, _next: .value() .sort((entryA, entryB) => entryB.communities.length - entryA.communities.length); - logger.info({ attestationEntries }, 'getAllRecommendations'); + logger.info({ attestationEntries: attestationEntries.length }, 'getAllRecommendations'); return new SuccessResponse(attestationEntries).send(res); }; @@ -47,7 +65,7 @@ export const getCommunityRecommendations = async (req: Request, res: Response, _ const key2 = c2.verifications + c2.annotations + c2.reactions; return key2 - key1; }); - logger.info({ attestations }); + // logger.info({ attestations }); logger.info({ attestations: attestations.length, communityName }, 'GetCommunityRecommendations'); return new SuccessResponse(attestations).send(res); diff --git a/desci-server/src/routes/v1/attestations/index.ts b/desci-server/src/routes/v1/attestations/index.ts index b40473785..b79c2a257 100644 --- a/desci-server/src/routes/v1/attestations/index.ts +++ b/desci-server/src/routes/v1/attestations/index.ts @@ -32,11 +32,13 @@ import { showCommunityClaimsSchema, showNodeAttestationsSchema, claimEntryAttestationsSchema, + searchAttestationsSchema, } from './schema.js'; const router = Router(); -router.get('/suggestions/all', [], asyncHandler(getAllRecommendations)); +// router.get('/', [validate(searchAttestationsSchema)], asyncHandler(searchAttestations)); +router.get('/suggestions/all', [validate(searchAttestationsSchema)], asyncHandler(getAllRecommendations)); router.get('/suggestions/protected', [], asyncHandler(getValidatedRecommendations)); router.get( '/claims/:communityId/:dpid', diff --git a/desci-server/src/routes/v1/attestations/schema.ts b/desci-server/src/routes/v1/attestations/schema.ts index 60ea61243..fca1ba6f7 100644 --- a/desci-server/src/routes/v1/attestations/schema.ts +++ b/desci-server/src/routes/v1/attestations/schema.ts @@ -206,3 +206,9 @@ export const removeClaimSchema = z.object({ claimId: z.coerce.number(), }), }); + +export const searchAttestationsSchema = z.object({ + query: z.object({ + search: z.string().optional(), + }), +}); diff --git a/desci-server/src/services/Attestation.ts b/desci-server/src/services/Attestation.ts index 5e5301768..7d77d7d9c 100644 --- a/desci-server/src/services/Attestation.ts +++ b/desci-server/src/services/Attestation.ts @@ -905,8 +905,9 @@ export class AttestationService { return queryResult; } - async getRecommendedAttestations() { + async getRecommendedAttestations(filter?: Prisma.CommunityEntryAttestationFindManyArgs) { const attestations = await prisma.communityEntryAttestation.findMany({ + ...filter, include: { attestation: { select: { community: true } }, attestationVersion: { @@ -918,11 +919,8 @@ export class AttestationService { }, desciCommunity: { select: { name: true, hidden: true, image_url: true } }, }, - where: { - desciCommunity: { - hidden: false, - }, - }, + where: filter?.where ?? { desciCommunity: { hidden: false } }, + take: filter ? 50 : 5, }); return attestations;