22
33import { withAuth } from "@/actions" ;
44import { isServiceError } from "@/lib/utils" ;
5- import { orgNotFound } from "@/lib/serviceError" ;
5+ import { orgNotFound , ServiceError } from "@/lib/serviceError" ;
66import { sew } from "@/actions" ;
77import { addUserToOrganization } from "@/lib/authUtils" ;
88import { prisma } from "@/prisma" ;
9+ import { StatusCodes } from "http-status-codes" ;
10+ import { ErrorCode } from "@/lib/errorCodes" ;
911
10- export const joinOrganization = ( orgId : number ) => sew ( async ( ) =>
12+ export const joinOrganization = ( orgId : number , inviteLinkId : string ) => sew ( async ( ) =>
1113 withAuth ( async ( userId ) => {
1214 const org = await prisma . org . findUnique ( {
1315 where : {
@@ -19,6 +21,22 @@ export const joinOrganization = (orgId: number) => sew(async () =>
1921 return orgNotFound ( ) ;
2022 }
2123
24+ if ( ! org . inviteLinkEnabled ) {
25+ return {
26+ statusCode : StatusCodes . BAD_REQUEST ,
27+ errorCode : ErrorCode . INVITE_LINK_NOT_ENABLED ,
28+ message : "Invite link is not enabled." ,
29+ } satisfies ServiceError ;
30+ }
31+
32+ if ( org . inviteLinkId !== inviteLinkId ) {
33+ return {
34+ statusCode : StatusCodes . BAD_REQUEST ,
35+ errorCode : ErrorCode . INVALID_INVITE_LINK ,
36+ message : "Invalid invite link." ,
37+ } satisfies ServiceError ;
38+ }
39+
2240 const addUserToOrgRes = await addUserToOrganization ( userId , org . id ) ;
2341 if ( isServiceError ( addUserToOrgRes ) ) {
2442 return addUserToOrgRes ;
0 commit comments