1
1
import { type SQL , and , asc , desc , eq , exists , gt , lt } from "drizzle-orm" ;
2
- import type { z } from "zod" ;
2
+ import { z } from "zod" ;
3
3
import {
4
4
advertisementsTable ,
5
5
advertisementsTableInsertSchema ,
6
6
} from "~/src/drizzle/tables/advertisements" ;
7
7
import { Advertisement } from "~/src/graphql/types/Advertisement/Advertisement" ;
8
8
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError" ;
9
9
import {
10
- defaultGraphQLConnectionArgumentsSchema ,
11
- transformDefaultGraphQLConnectionArguments ,
10
+ type ParsedDefaultGraphQLConnectionArgumentsWithWhere ,
11
+ createGraphQLConnectionWithWhereSchema ,
12
+ type defaultGraphQLConnectionArgumentsSchema ,
13
+ transformGraphQLConnectionArgumentsWithWhere ,
12
14
transformToDefaultGraphQLConnection ,
13
15
} from "~/src/utilities/defaultGraphQLConnection" ;
14
16
import envConfig from "~/src/utilities/graphqLimits" ;
17
+ import { AdvertisementWhereInput } from "../../inputs/QueryOrganizationInput" ;
15
18
import { Organization } from "./Organization" ;
16
- const advertisementsArgumentsSchema = defaultGraphQLConnectionArgumentsSchema
17
- . transform ( transformDefaultGraphQLConnectionArguments )
18
- . transform ( ( arg , ctx ) => {
19
- let cursor : z . infer < typeof cursorSchema > | undefined = undefined ;
20
-
21
- try {
22
- if ( arg . cursor !== undefined ) {
23
- cursor = cursorSchema . parse (
24
- JSON . parse ( Buffer . from ( arg . cursor , "base64url" ) . toString ( "utf-8" ) ) ,
25
- ) ;
26
- }
27
- } catch ( error ) {
28
- ctx . addIssue ( {
29
- code : "custom" ,
30
- message : "Not a valid cursor." ,
31
- path : [ arg . isInversed ? "before" : "after" ] ,
32
- } ) ;
33
- }
34
19
35
- return {
36
- cursor,
37
- isInversed : arg . isInversed ,
38
- limit : arg . limit ,
39
- } ;
40
- } ) ;
20
+ const advertisementWhereSchema = z
21
+ . object ( {
22
+ isCompleted : z . boolean ( ) . optional ( ) ,
23
+ } )
24
+ . optional ( ) ;
25
+
26
+ const advertisementsArgumentsSchema = createGraphQLConnectionWithWhereSchema (
27
+ advertisementWhereSchema ,
28
+ ) . transform ( ( arg , ctx ) => {
29
+ const transformedArg = transformGraphQLConnectionArgumentsWithWhere (
30
+ { ...arg , where : arg . where || { } } as z . infer <
31
+ typeof defaultGraphQLConnectionArgumentsSchema
32
+ > & { where : unknown } ,
33
+ ctx ,
34
+ ) ;
35
+ let cursor : z . infer < typeof cursorSchema > | undefined = undefined ;
36
+ try {
37
+ if ( transformedArg . cursor !== undefined ) {
38
+ cursor = cursorSchema . parse (
39
+ JSON . parse (
40
+ Buffer . from ( transformedArg . cursor , "base64url" ) . toString ( "utf-8" ) ,
41
+ ) ,
42
+ ) ;
43
+ }
44
+ } catch ( error ) {
45
+ ctx . addIssue ( {
46
+ code : "custom" ,
47
+ message : "Not a valid cursor." ,
48
+ path : [ transformedArg . isInversed ? "before" : "after" ] ,
49
+ } ) ;
50
+ }
51
+ return {
52
+ cursor,
53
+ isInversed : transformedArg . isInversed ,
54
+ limit : transformedArg . limit ,
55
+ where : transformedArg . where || { } , // Default to empty object if where is undefined
56
+ } ;
57
+ } ) ;
41
58
42
59
const cursorSchema = advertisementsTableInsertSchema . pick ( {
43
60
name : true ,
@@ -55,6 +72,13 @@ Organization.implement({
55
72
multiplier : args . first || args . last || 1 ,
56
73
} ;
57
74
} ,
75
+ args : {
76
+ where : t . arg ( {
77
+ type : AdvertisementWhereInput ,
78
+ description : "Filter criteria for advertisements" ,
79
+ required : false ,
80
+ } ) ,
81
+ } ,
58
82
resolve : async ( parent , args , ctx ) => {
59
83
if ( ! ctx . currentClient . isAuthenticated ) {
60
84
throw new TalawaGraphQLError ( {
@@ -69,7 +93,6 @@ Organization.implement({
69
93
error,
70
94
success,
71
95
} = advertisementsArgumentsSchema . safeParse ( args ) ;
72
-
73
96
if ( ! success ) {
74
97
throw new TalawaGraphQLError ( {
75
98
extensions : {
@@ -124,7 +147,15 @@ Organization.implement({
124
147
} ) ;
125
148
}
126
149
127
- const { cursor, isInversed, limit } = parsedArgs ;
150
+ const {
151
+ cursor,
152
+ isInversed,
153
+ limit,
154
+ where : extendedArgs ,
155
+ } = parsedArgs as ParsedDefaultGraphQLConnectionArgumentsWithWhere <
156
+ { name : string } ,
157
+ { isCompleted ?: boolean }
158
+ > ;
128
159
129
160
const orderBy = isInversed
130
161
? [ desc ( advertisementsTable . name ) ]
@@ -134,7 +165,7 @@ Organization.implement({
134
165
135
166
if ( isInversed ) {
136
167
if ( cursor !== undefined ) {
137
- where = and (
168
+ const baseCondition = and (
138
169
exists (
139
170
ctx . drizzleClient
140
171
. select ( )
@@ -149,12 +180,51 @@ Organization.implement({
149
180
eq ( advertisementsTable . organizationId , parent . id ) ,
150
181
lt ( advertisementsTable . name , cursor . name ) ,
151
182
) ;
183
+
184
+ if ( extendedArgs . isCompleted !== undefined ) {
185
+ const today = new Date ( ) ;
186
+
187
+ if ( extendedArgs . isCompleted ) {
188
+ where = and (
189
+ baseCondition ,
190
+ lt ( advertisementsTable . endAt , today ) ,
191
+ ) ;
192
+ } else {
193
+ where = and (
194
+ baseCondition ,
195
+ gt ( advertisementsTable . endAt , today ) ,
196
+ ) ;
197
+ }
198
+ } else {
199
+ where = baseCondition ;
200
+ }
152
201
} else {
153
- where = eq ( advertisementsTable . organizationId , parent . id ) ;
202
+ const baseCondition = eq (
203
+ advertisementsTable . organizationId ,
204
+ parent . id ,
205
+ ) ;
206
+
207
+ if ( extendedArgs . isCompleted !== undefined ) {
208
+ const today = new Date ( ) ;
209
+
210
+ if ( extendedArgs . isCompleted ) {
211
+ where = and (
212
+ baseCondition ,
213
+ lt ( advertisementsTable . endAt , today ) ,
214
+ ) ;
215
+ } else {
216
+ where = and (
217
+ baseCondition ,
218
+ gt ( advertisementsTable . endAt , today ) ,
219
+ ) ;
220
+ }
221
+ } else {
222
+ where = baseCondition ;
223
+ }
154
224
}
155
225
} else {
156
226
if ( cursor !== undefined ) {
157
- where = and (
227
+ const baseCondition = and (
158
228
exists (
159
229
ctx . drizzleClient
160
230
. select ( )
@@ -169,8 +239,47 @@ Organization.implement({
169
239
eq ( advertisementsTable . organizationId , parent . id ) ,
170
240
gt ( advertisementsTable . name , cursor . name ) ,
171
241
) ;
242
+
243
+ if ( extendedArgs . isCompleted !== undefined ) {
244
+ const today = new Date ( ) ;
245
+
246
+ if ( extendedArgs . isCompleted ) {
247
+ where = and (
248
+ baseCondition ,
249
+ lt ( advertisementsTable . endAt , today ) ,
250
+ ) ;
251
+ } else {
252
+ where = and (
253
+ baseCondition ,
254
+ gt ( advertisementsTable . endAt , today ) ,
255
+ ) ;
256
+ }
257
+ } else {
258
+ where = baseCondition ;
259
+ }
172
260
} else {
173
- where = eq ( advertisementsTable . organizationId , parent . id ) ;
261
+ const baseCondition = eq (
262
+ advertisementsTable . organizationId ,
263
+ parent . id ,
264
+ ) ;
265
+
266
+ if ( extendedArgs . isCompleted !== undefined ) {
267
+ const today = new Date ( ) ;
268
+
269
+ if ( extendedArgs . isCompleted ) {
270
+ where = and (
271
+ baseCondition ,
272
+ lt ( advertisementsTable . endAt , today ) ,
273
+ ) ;
274
+ } else {
275
+ where = and (
276
+ baseCondition ,
277
+ gt ( advertisementsTable . endAt , today ) ,
278
+ ) ;
279
+ }
280
+ } else {
281
+ where = baseCondition ;
282
+ }
174
283
}
175
284
}
176
285
0 commit comments