Skip to content

Commit

Permalink
feat: extend attestations get all api to support search
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrach-tayo committed Dec 30, 2024
1 parent 8ae9456 commit 2f994cf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
26 changes: 22 additions & 4 deletions desci-server/src/controllers/attestations/recommendations.ts
Original file line number Diff line number Diff line change
@@ -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, _) => ({
Expand All @@ -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);
};

Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion desci-server/src/routes/v1/attestations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions desci-server/src/routes/v1/attestations/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}),
});
10 changes: 4 additions & 6 deletions desci-server/src/services/Attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
Expand Down

0 comments on commit 2f994cf

Please sign in to comment.