1
1
import { Prisma } from '@/prisma/generated/client'
2
2
import { jsonRes } from '@/services/backend/response'
3
3
import { devboxDB } from '@/services/db/init'
4
+ import { getRegionUid } from '@/utils/env'
4
5
import { NextRequest } from 'next/server'
5
6
import { z } from 'zod'
6
7
export const dynamic = 'force-dynamic'
@@ -11,45 +12,36 @@ export async function GET(req: NextRequest) {
11
12
const search = searchParams . get ( 'search' ) || ''
12
13
const page = z . number ( ) . int ( ) . positive ( ) . safeParse ( Number ( searchParams . get ( 'page' ) ) ) . data || 1
13
14
const pageSize = z . number ( ) . int ( ) . min ( 1 ) . safeParse ( Number ( searchParams . get ( 'pageSize' ) ) ) . data || 30
14
- const dbquery : Prisma . TemplateRepositoryWhereInput = {
15
-
16
- ...( tags && tags . length > 0
17
- ? {
18
- AND : tags . map ( ( tag ) => ( {
19
- templateRepositoryTags : {
20
- some : {
21
- tagUid : tag
22
- }
23
- }
24
- } ) )
25
- }
26
- : { } ) ,
27
- ...( search && search . length > 0
28
- ? {
29
- name : {
30
- contains : search
31
- }
32
- }
33
- : { } )
34
- }
35
15
const [ templateRepositoryList , totalItems ] = await devboxDB . $transaction ( async tx => {
36
- const validRepoIds = await tx . template . findMany ( {
37
- where : {
38
- isDeleted : false ,
39
- } ,
40
- select : {
41
- templateRepositoryUid : true
42
- } ,
43
- distinct : [ 'templateRepositoryUid' ]
44
- } )
45
16
17
+ const regionUid = getRegionUid ( )
46
18
const where : Prisma . TemplateRepositoryWhereInput = {
47
- uid : {
48
- in : validRepoIds . map ( r => r . templateRepositoryUid )
19
+ templates : {
20
+ some : {
21
+ isDeleted : false
22
+ }
49
23
} ,
50
24
isPublic : true ,
51
25
isDeleted : false ,
52
- ...dbquery
26
+ regionUid,
27
+ ...( tags && tags . length > 0
28
+ ? {
29
+ AND : tags . map ( ( tag ) => ( {
30
+ templateRepositoryTags : {
31
+ some : {
32
+ tagUid : tag
33
+ }
34
+ }
35
+ } ) )
36
+ }
37
+ : { } ) ,
38
+ ...( search && search . length > 0
39
+ ? {
40
+ name : {
41
+ contains : search
42
+ }
43
+ }
44
+ : { } )
53
45
}
54
46
const [ templateRepositoryList , totalItems ] = await Promise . all ( [
55
47
tx . templateRepository . findMany ( {
@@ -88,7 +80,7 @@ export async function GET(req: NextRequest) {
88
80
]
89
81
} ) ,
90
82
tx . templateRepository . count ( {
91
- where : dbquery ,
83
+ where,
92
84
} )
93
85
] )
94
86
return [ templateRepositoryList , totalItems ]
0 commit comments