@@ -2,17 +2,26 @@ import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs';
2
2
import { NextApiRequest , NextApiResponse } from 'next' ;
3
3
import { generateErrorMessage } from 'zod-error' ;
4
4
import {
5
- errorClientNotAuthenticated ,
6
- errorInternalServerError ,
7
- hasErrorMessage ,
5
+ Communities ,
6
+ CreateCommunityInput ,
8
7
createCommunityRequestSchema ,
9
8
CreateCommunityResponse ,
9
+ errorClientNotAuthenticated ,
10
+ errorCommunityNameNotAvailable ,
11
+ errorCommunitySlugNotAvailable ,
10
12
errorInputValidation ,
13
+ errorInternalServerError ,
11
14
generateSlug ,
12
- CreateCommunityInput ,
13
15
getLogger ,
16
+ hasErrorMessage ,
14
17
} from '@knowii/common' ;
15
- import { daoFnCreateCommunity , errorMessageOptions , getInternalUserIdFromSupabaseSession } from '@knowii/server' ;
18
+ import {
19
+ daoFnCreateCommunity ,
20
+ daoFnIsCommunityNameAvailable ,
21
+ daoFnIsCommunitySlugAvailable ,
22
+ errorMessageOptions ,
23
+ getInternalUserIdFromSupabaseSession ,
24
+ } from '@knowii/server' ;
16
25
import { PrismaClient } from '@prisma/client' ;
17
26
18
27
export default async function handler ( req : NextApiRequest , res : NextApiResponse < CreateCommunityResponse > ) {
@@ -33,9 +42,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
33
42
} = await supabaseClient . auth . getSession ( ) ;
34
43
35
44
if ( ! session ) {
36
- return res . status ( 401 ) . json ( {
37
- error : errorClientNotAuthenticated . code ,
38
- errorDetails : errorClientNotAuthenticated . description ,
45
+ return res . status ( errorClientNotAuthenticated . statusCode ) . json ( {
46
+ errors : {
47
+ type : errorClientNotAuthenticated . type ,
48
+ title : errorClientNotAuthenticated . code ,
49
+ titleKey : errorClientNotAuthenticated . key ,
50
+ errorDetails : [
51
+ {
52
+ detail : errorClientNotAuthenticated . description ,
53
+ detailKey : errorClientNotAuthenticated . key ,
54
+ status : errorClientNotAuthenticated . statusCode ,
55
+ } ,
56
+ ] ,
57
+ } ,
39
58
} ) ;
40
59
}
41
60
@@ -45,10 +64,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
45
64
const errorMessage = generateErrorMessage ( requestValidationResult . error . issues , errorMessageOptions ) ;
46
65
logger . warn ( `${ errorInputValidation . description } . Error(s) detected: %s` , errorMessage ) ;
47
66
48
- res . status ( 400 ) . json ( {
49
- error : errorInputValidation . code ,
50
- errorDescription : errorInputValidation . description ,
51
- errorDetails : errorMessage ,
67
+ res . status ( errorInputValidation . statusCode ) . json ( {
68
+ errors : {
69
+ type : errorInputValidation . type ,
70
+ title : errorInputValidation . code ,
71
+ titleKey : errorInputValidation . key ,
72
+ errorDetails : [
73
+ {
74
+ detail : errorInputValidation . description ,
75
+ detailKey : errorInputValidation . key ,
76
+ status : errorInputValidation . statusCode ,
77
+ } ,
78
+ ] ,
79
+ } ,
52
80
} ) ;
53
81
return ;
54
82
}
@@ -66,35 +94,101 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
66
94
slug,
67
95
} ;
68
96
69
- logger . info ( 'Proceeding with the creation: %o' , creationPayload ) ;
70
-
71
97
try {
72
98
const prismaClient = new PrismaClient ( ) ;
73
99
74
- const createdCommunity = await daoFnCreateCommunity ( creationPayload , prismaClient ) ;
100
+ logger . info ( 'Verifying if the community name is available' ) ;
101
+ const isCommunityNameAvailable = await daoFnIsCommunityNameAvailable ( creationPayload . name , prismaClient ) ;
102
+
103
+ if ( ! isCommunityNameAvailable ) {
104
+ logger . info ( 'The community name is not available' ) ;
105
+ return res . status ( errorCommunityNameNotAvailable . statusCode ) . json ( {
106
+ errors : {
107
+ type : errorCommunityNameNotAvailable . type ,
108
+ title : errorCommunityNameNotAvailable . code ,
109
+ titleKey : errorCommunityNameNotAvailable . key ,
110
+ errorDetails : [
111
+ {
112
+ detail : errorCommunityNameNotAvailable . description ,
113
+ detailKey : errorCommunityNameNotAvailable . key ,
114
+ status : errorCommunityNameNotAvailable . statusCode ,
115
+ } ,
116
+ ] ,
117
+ } ,
118
+ } ) ;
119
+ }
120
+
121
+ logger . info ( 'Verifying if the community slug is available' ) ;
122
+ const isCommunitySlugAvailable = await daoFnIsCommunitySlugAvailable ( creationPayload . slug , prismaClient ) ;
123
+
124
+ if ( ! isCommunitySlugAvailable ) {
125
+ logger . info ( 'The community slug is not available' ) ;
126
+ return res . status ( errorCommunitySlugNotAvailable . statusCode ) . json ( {
127
+ errors : {
128
+ type : errorCommunitySlugNotAvailable . type ,
129
+ title : errorCommunitySlugNotAvailable . code ,
130
+ titleKey : errorCommunitySlugNotAvailable . key ,
131
+ errorDetails : [
132
+ {
133
+ detail : errorCommunitySlugNotAvailable . description ,
134
+ detailKey : errorCommunitySlugNotAvailable . key ,
135
+ status : errorCommunitySlugNotAvailable . statusCode ,
136
+ } ,
137
+ ] ,
138
+ } ,
139
+ } ) ;
140
+ }
141
+
142
+ logger . info ( 'Proceeding with the creation: %o' , creationPayload ) ;
143
+
144
+ const createdCommunity = < Communities > await daoFnCreateCommunity ( creationPayload , prismaClient ) ;
75
145
76
146
logger . info ( 'Created community: %o' , createdCommunity ) ;
77
147
78
148
const responseBody : CreateCommunityResponse = {
79
- data : createdCommunity ,
149
+ data : {
150
+ name : createdCommunity . name ,
151
+ description : createdCommunity . description ,
152
+ slug : createdCommunity . slug ,
153
+ } ,
80
154
} ;
81
155
82
156
return res . status ( 200 ) . json ( responseBody ) ;
83
157
} catch ( err : unknown ) {
84
158
if ( hasErrorMessage ( err ) ) {
85
- logger . warn ( 'Error while creating a new community: %' , err . message ) ;
86
-
87
- return res . status ( 500 ) . json ( {
88
- error : errorInternalServerError . code ,
89
- errorDescription : errorInternalServerError . description ,
159
+ logger . warn ( 'Error while creating a new community: %o' , err . message ) ;
160
+
161
+ return res . status ( errorInternalServerError . statusCode ) . json ( {
162
+ errors : {
163
+ type : errorInternalServerError . type ,
164
+ title : errorInternalServerError . code ,
165
+ titleKey : errorInternalServerError . key ,
166
+ errorDetails : [
167
+ {
168
+ detail : errorInternalServerError . description ,
169
+ detailKey : errorInternalServerError . key ,
170
+ status : errorInternalServerError . statusCode ,
171
+ } ,
172
+ ] ,
173
+ } ,
90
174
} ) ;
91
175
}
92
176
93
177
logger . warn ( 'Error while creating a new community: %o' , err ) ;
94
178
95
- res . status ( 500 ) . json ( {
96
- error : errorInternalServerError . code ,
97
- errorDescription : errorInternalServerError . description ,
179
+ res . status ( errorInternalServerError . statusCode ) . json ( {
180
+ errors : {
181
+ type : errorInternalServerError . type ,
182
+ title : errorInternalServerError . code ,
183
+ titleKey : errorInternalServerError . key ,
184
+ errorDetails : [
185
+ {
186
+ detail : errorInternalServerError . description ,
187
+ detailKey : errorInternalServerError . key ,
188
+ status : errorInternalServerError . statusCode ,
189
+ } ,
190
+ ] ,
191
+ } ,
98
192
} ) ;
99
193
}
100
194
}
0 commit comments