@@ -2,19 +2,26 @@ import type {
2
2
MembershipInvitationType ,
3
3
WithAPIErrorReponse ,
4
4
} from "@dust-tt/types" ;
5
+ import { isLeft } from "fp-ts/lib/Either" ;
6
+ import * as t from "io-ts" ;
7
+ import * as reporter from "io-ts-reporters" ;
5
8
import type { NextApiRequest , NextApiResponse } from "next" ;
6
9
10
+ import { getInvitation , updateInvitationStatus } from "@app/lib/api/invitation" ;
7
11
import { Authenticator , getSession } from "@app/lib/auth" ;
8
- import { MembershipInvitation } from "@app/lib/models" ;
9
12
import { apiError , withLogging } from "@app/logger/withlogging" ;
10
13
11
- export type GetMemberInvitationsResponseBody = {
12
- invitations : MembershipInvitationType [ ] ;
14
+ export type PostMemberInvitationsResponseBody = {
15
+ invitation : MembershipInvitationType ;
13
16
} ;
14
17
18
+ export const PostMemberInvitationBodySchema = t . type ( {
19
+ status : t . literal ( "revoked" ) ,
20
+ } ) ;
21
+
15
22
async function handler (
16
23
req : NextApiRequest ,
17
- res : NextApiResponse < WithAPIErrorReponse < GetMemberInvitationsResponseBody > >
24
+ res : NextApiResponse < WithAPIErrorReponse < PostMemberInvitationsResponseBody > >
18
25
) : Promise < void > {
19
26
const session = await getSession ( req , res ) ;
20
27
const auth = await Authenticator . fromSession (
@@ -44,8 +51,19 @@ async function handler(
44
51
} ) ;
45
52
}
46
53
47
- const invitationId = parseInt ( req . query . invitationId as string ) ;
48
- if ( isNaN ( invitationId ) ) {
54
+ if ( ! ( typeof req . query . iId === "string" ) ) {
55
+ return apiError ( req , res , {
56
+ status_code : 400 ,
57
+ api_error : {
58
+ type : "invalid_request_error" ,
59
+ message : "Invalid query parameters, `iId` (string) is required." ,
60
+ } ,
61
+ } ) ;
62
+ }
63
+
64
+ const invitationId = req . query . iId ;
65
+ let invitation = await getInvitation ( auth , { invitationId } ) ;
66
+ if ( ! invitation ) {
49
67
return apiError ( req , res , {
50
68
status_code : 404 ,
51
69
api_error : {
@@ -57,47 +75,26 @@ async function handler(
57
75
58
76
switch ( req . method ) {
59
77
case "POST" :
60
- if (
61
- ! req . body ||
62
- ! typeof ( req . body . status === "string" ) ||
63
- // For now we only allow revoking invitations.
64
- req . body . status !== "revoked"
65
- ) {
78
+ const bodyValidation = PostMemberInvitationBodySchema . decode ( req . body ) ;
79
+ if ( isLeft ( bodyValidation ) ) {
80
+ const pathError = reporter . formatValidationErrors ( bodyValidation . left ) ;
66
81
return apiError ( req , res , {
67
82
status_code : 400 ,
68
83
api_error : {
69
84
type : "invalid_request_error" ,
70
- message :
71
- 'The request body is invalid, expects { status: "revoked" }.' ,
85
+ message : `The request body is invalid: ${ pathError } ` ,
72
86
} ,
73
87
} ) ;
74
88
}
89
+ const body = bodyValidation . right ;
75
90
76
- const invitation = await MembershipInvitation . findOne ( {
77
- where : { id : invitationId } ,
91
+ invitation = await updateInvitationStatus ( auth , {
92
+ invitation,
93
+ status : body . status ,
78
94
} ) ;
79
95
80
- if ( ! invitation ) {
81
- return apiError ( req , res , {
82
- status_code : 404 ,
83
- api_error : {
84
- type : "invitation_not_found" ,
85
- message : "The invitation requested was not found." ,
86
- } ,
87
- } ) ;
88
- }
89
-
90
- invitation . status = req . body . status ;
91
- await invitation . save ( ) ;
92
96
res . status ( 200 ) . json ( {
93
- invitations : [
94
- {
95
- id : invitation . id ,
96
- status : invitation . status ,
97
- inviteEmail : invitation . inviteEmail ,
98
- initialRole : invitation . initialRole ,
99
- } ,
100
- ] ,
97
+ invitation,
101
98
} ) ;
102
99
return ;
103
100
0 commit comments