@@ -3,42 +3,41 @@ import { z } from 'zod';
3
3
import { dbWrite } from '~/server/db/client' ;
4
4
import { bustImageModRulesCache } from '~/server/services/image.service' ;
5
5
import { bustModelModRulesCache } from '~/server/services/model.service' ;
6
- import { handleEndpointError , ModEndpoint } from '~/server/utils/endpoint-helpers' ;
6
+ import { handleEndpointError , WebhookEndpoint } from '~/server/utils/endpoint-helpers' ;
7
7
import { handleLogError } from '~/server/utils/errorHandling' ;
8
8
import { EntityType , ModerationRuleAction } from '~/shared/utils/prisma/enums' ;
9
9
10
10
const payloadSchema = z . object ( {
11
11
id : z . number ( ) ,
12
12
definition : z . object ( { } ) . passthrough ( ) ,
13
+ userId : z . number ( ) ,
13
14
action : z . nativeEnum ( ModerationRuleAction ) ,
14
15
entityType : z . enum ( [ 'Model' , 'Image' ] ) ,
15
16
enabled : z . boolean ( ) . optional ( ) . default ( true ) ,
16
17
order : z . number ( ) . optional ( ) ,
17
18
reason : z . string ( ) . optional ( ) ,
18
19
} ) ;
19
20
20
- const deleteQuerySchema = z . object ( {
21
- id : z . coerce . number ( ) ,
22
- } ) ;
21
+ const deleteQuerySchema = z . object ( { id : z . coerce . number ( ) } ) ;
23
22
24
- export default ModEndpoint (
25
- async function handler ( req , res ) {
26
- try {
27
- switch ( req . method ) {
28
- case 'POST' :
29
- return upsertModRule ( req , res ) ;
30
- case 'DELETE' :
31
- return deleteModRule ( req , res ) ;
32
- default : {
33
- return res . status ( 405 ) . json ( { error : 'Method Not Allowed' } ) ;
34
- }
23
+ export default WebhookEndpoint ( async function handler ( req , res ) {
24
+ if ( req . method && [ 'POST' , 'DELETE' ] . includes ( req . method ) )
25
+ return res . status ( 405 ) . json ( { error : 'Method Not Allowed' } ) ;
26
+
27
+ try {
28
+ switch ( req . method ) {
29
+ case 'POST' :
30
+ return upsertModRule ( req , res ) ;
31
+ case 'DELETE' :
32
+ return deleteModRule ( req , res ) ;
33
+ default : {
34
+ return res . status ( 405 ) . json ( { error : 'Method Not Allowed' } ) ;
35
35
}
36
- } catch ( error ) {
37
- return handleEndpointError ( res , error ) ;
38
36
}
39
- } ,
40
- [ 'POST' , 'DELETE' ]
41
- ) ;
37
+ } catch ( error ) {
38
+ return handleEndpointError ( res , error ) ;
39
+ }
40
+ } ) ;
42
41
43
42
async function upsertModRule ( req : NextApiRequest , res : NextApiResponse ) {
44
43
if ( req . body . id ) {
@@ -47,7 +46,7 @@ async function upsertModRule(req: NextApiRequest, res: NextApiResponse) {
47
46
return res . status ( 400 ) . json ( { error : 'Bad Request' , details : schemaResult . error . format ( ) } ) ;
48
47
49
48
try {
50
- const { id, ...data } = schemaResult . data ;
49
+ const { id, userId , ...data } = schemaResult . data ;
51
50
await dbWrite . moderationRule . update ( { where : { id } , data } ) ;
52
51
} catch ( error ) {
53
52
return res . status ( 500 ) . json ( { error : 'Could not update rule' , details : error } ) ;
@@ -58,8 +57,8 @@ async function upsertModRule(req: NextApiRequest, res: NextApiResponse) {
58
57
return res . status ( 400 ) . json ( { error : 'Bad Request' , details : schemaResult . error . format } ) ;
59
58
60
59
try {
61
- const data = schemaResult . data ;
62
- await dbWrite . moderationRule . create ( { data } ) ;
60
+ const { userId : createdById , ... data } = schemaResult . data ;
61
+ await dbWrite . moderationRule . create ( { data : { ... data , createdById } } ) ;
63
62
} catch ( error ) {
64
63
return res . status ( 500 ) . json ( { error : 'Could not create rule' , details : error } ) ;
65
64
}
0 commit comments