11import { BadRequestException , Injectable } from '@nestjs/common' ;
2- import { ActionType } from '@prisma/client' ;
2+ import { ActionType , Prisma } from '@prisma/client' ;
33import { DatabaseService } from 'src/database/database.service' ;
44import {
55 PollNotFoundException ,
@@ -12,6 +12,21 @@ import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
1212export class PollService {
1313 constructor ( private readonly databaseService : DatabaseService ) { }
1414
15+ private async searchPolls ( searchTerm : string ) : Promise < number [ ] > {
16+ const searchQuery = searchTerm
17+ . split ( ' ' )
18+ . map ( ( word ) => `${ word } :*` )
19+ . join ( ' & ' ) ;
20+ const searchResults = await this . databaseService . $queryRaw <
21+ { pollId : number } [ ]
22+ > `
23+ SELECT "pollId" FROM "Poll"
24+ WHERE "searchVector" @@ to_tsquery('english', ${ searchQuery } )
25+ ORDER BY ts_rank("searchVector", to_tsquery('english', ${ searchQuery } )) DESC
26+ ` ;
27+ return searchResults . map ( ( result ) => result . pollId ) ;
28+ }
29+
1530 async createPoll ( createPollDto : CreatePollDto ) {
1631 const user = await this . databaseService . user . findUnique ( {
1732 where : { worldID : createPollDto . worldID } ,
@@ -72,12 +87,13 @@ export class PollService {
7287 isActive,
7388 userVoted,
7489 userCreated,
90+ search,
7591 sortBy = 'endDate' ,
7692 sortOrder = 'asc' ,
7793 } = query ;
7894 const skip = ( page - 1 ) * limit ;
7995 const now = new Date ( ) ;
80- const filters : any = { } ;
96+ const filters : Prisma . PollWhereInput = { } ;
8197 let userId : number | undefined ;
8298
8399 if ( isActive ) {
@@ -88,6 +104,7 @@ export class PollService {
88104 if ( isActive === false ) {
89105 filters . OR = [ { startDate : { gt : now } } , { endDate : { lte : now } } ] ;
90106 }
107+
91108 if ( ( userCreated || userVoted ) && query . worldID ) {
92109 const user = await this . databaseService . user . findUnique ( {
93110 where : { worldID : query . worldID } ,
@@ -99,25 +116,21 @@ export class PollService {
99116 }
100117 userId = user . id ;
101118 } else if ( userCreated || userVoted ) {
102- throw new Error ( 'worldId Not Provided' ) ;
119+ throw new BadRequestException ( 'worldId Not Provided' ) ;
103120 }
104121
105122 if ( userCreated && userVoted ) {
106- // Get polls user voted in
107123 const userVotes = await this . databaseService . vote . findMany ( {
108124 where : { userId } ,
109125 select : { pollId : true } ,
110126 } ) ;
111127 const votedPollIds = userVotes . map ( ( v ) => v . pollId ) ;
112128
113- // Use OR condition to get polls where user voted OR user created
114129 filters . OR = [ { authorUserId : userId } , { pollId : { in : votedPollIds } } ] ;
115130 } else {
116131 if ( userCreated ) {
117132 filters . authorUserId = userId ;
118133 }
119-
120- // Get polls user voted in
121134 let votedPollIds : number [ ] = [ ] ;
122135 if ( userVoted ) {
123136 const userVotes = await this . databaseService . vote . findMany ( {
@@ -129,15 +142,22 @@ export class PollService {
129142 }
130143 }
131144
132- // Sorting options
133- const orderBy : any = { } ;
145+ if ( search ) {
146+ const pollIds = await this . searchPolls ( search ) ;
147+ if ( Object . keys ( filters ) . length > 0 ) {
148+ filters . AND = [ filters , { pollId : { in : pollIds } } ] ;
149+ } else {
150+ filters . pollId = { in : pollIds } ;
151+ }
152+ }
153+
154+ const orderBy : Prisma . PollOrderByWithRelationInput = { } ;
134155 if ( sortBy ) {
135156 orderBy [ sortBy ] = sortOrder ;
136157 } else {
137158 orderBy . endDate = 'asc' ;
138159 }
139160
140- // Fetch polls with pagination
141161 const [ polls , total ] = await this . databaseService . $transaction ( [
142162 this . databaseService . poll . findMany ( {
143163 where : filters ,
0 commit comments