-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from mindset-labs/feat/invites-api
add Invite model, router, controller and service
- Loading branch information
Showing
17 changed files
with
1,616 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-- CreateEnum | ||
CREATE TYPE "InviteStatus" AS ENUM ('OPEN', 'CLOSED'); | ||
|
||
-- AlterEnum | ||
ALTER TYPE "CommunityRole" ADD VALUE 'MANAGED_MEMBER'; | ||
|
||
-- CreateTable | ||
CREATE TABLE "Invite" ( | ||
"id" TEXT NOT NULL, | ||
"code" TEXT NOT NULL, | ||
"email" TEXT, | ||
"communityId" TEXT NOT NULL, | ||
"walletId" TEXT, | ||
"inviteById" TEXT NOT NULL, | ||
"status" "InviteStatus" NOT NULL DEFAULT 'OPEN', | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"expiresAt" TIMESTAMP(3), | ||
"maxUses" INTEGER DEFAULT 999, | ||
"uses" INTEGER NOT NULL DEFAULT 0, | ||
|
||
CONSTRAINT "Invite_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Invite_code_key" ON "Invite"("code"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_communityId_fkey" FOREIGN KEY ("communityId") REFERENCES "Community"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_inviteById_fkey" FOREIGN KEY ("inviteById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
model Invite { | ||
id String @id @default(uuid()) | ||
code String @unique | ||
email String? | ||
communityId String | ||
walletId String? | ||
inviteById String | ||
status InviteStatus @default(OPEN) | ||
createdAt DateTime @default(now()) | ||
expiresAt DateTime? | ||
maxUses Int? @default(999) | ||
uses Int @default(0) | ||
// relationships | ||
community Community @relation(fields: [communityId], references: [id]) | ||
inviteBy User @relation(fields: [inviteById], references: [id]) | ||
} | ||
|
||
enum InviteStatus { | ||
OPEN | ||
CLOSED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Request, RequestHandler, Response } from "express" | ||
import { inviteService } from "./inviteService" | ||
import { handleSuccessResponse, handleErrorResponse } from '@/common/utils/httpHandlers' | ||
import { generateApiKey, generateUUID } from '@/common/utils/random' | ||
|
||
export class InviteController { | ||
queryInvites: RequestHandler = async (req: Request, res: Response) => { | ||
const { communityId } = req.query | ||
|
||
return inviteService | ||
.queryInvites( | ||
{ communityId: communityId as string }, | ||
(req.query.include || {}) as Record<string, string>, | ||
(req.query.paging || {}) as Record<string, string> | ||
) | ||
.then((invites) => handleSuccessResponse({ invites }, res)) | ||
.catch((error) => handleErrorResponse(error, res)) | ||
} | ||
|
||
createInvite: RequestHandler = async (req: Request, res: Response) => { | ||
const { code, communityId } = req.body | ||
|
||
return inviteService | ||
.createInvite(req.userId!, { | ||
communityId, | ||
code: code || generateApiKey() | ||
}) | ||
.then((invite) => handleSuccessResponse({ invite }, res)) | ||
.catch((error) => handleErrorResponse(error, res)) | ||
} | ||
|
||
acceptInvite: RequestHandler = async (req: Request, res: Response) => { | ||
const { inviteCode } = req.body | ||
|
||
return inviteService | ||
.acceptInvite(inviteCode, req.userId!) | ||
.then((invite) => handleSuccessResponse({ invite }, res)) | ||
.catch((error) => handleErrorResponse(error, res)) | ||
} | ||
} | ||
|
||
export const inviteController = new InviteController() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { InviteIncludeSchema, InviteWhereInputSchema } from '@zodSchema/index' | ||
import { z } from "zod" | ||
|
||
export const QueryInviteSchema = z.object({ | ||
query: z.object({ | ||
where: InviteWhereInputSchema.optional(), | ||
include: InviteIncludeSchema.optional(), | ||
paging: z.object({ | ||
take: z.string().optional(), | ||
skip: z.string().optional(), | ||
}).optional(), | ||
}), | ||
}) | ||
|
||
export const AcceptInviteSchema = z.object({ | ||
inviteCode: z.string(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { OpenAPIRegistry } from "@asteasolutions/zod-to-openapi" | ||
import express, { type Router } from "express" | ||
import { z } from "zod" | ||
|
||
import { createApiResponse } from "@/api-docs/openAPIResponseBuilders" | ||
import verifyJWT, { verifyJWTAndRole } from "@/common/middleware/verifyJWT" | ||
import { inviteController } from "./inviteController" | ||
import { InviteSchema } from '@zodSchema/index' | ||
import { Role } from '@prisma/client' | ||
import { AcceptInviteSchema, QueryInviteSchema } from './inviteRequestValidation' | ||
import { validateRequest } from '@/common/utils/httpHandlers' | ||
|
||
export const inviteRegistry = new OpenAPIRegistry() | ||
export const inviteRouter: Router = express.Router() | ||
|
||
inviteRegistry.register("Invite", InviteSchema) | ||
|
||
// Get invites | ||
inviteRegistry.registerPath({ | ||
method: "get", | ||
path: "/invites", | ||
tags: ["Invite"], | ||
responses: createApiResponse(z.object({ | ||
invites: z.array(InviteSchema), | ||
total: z.number(), | ||
}), "Success"), | ||
}) | ||
|
||
inviteRouter.get("/", verifyJWT, validateRequest(QueryInviteSchema), inviteController.queryInvites) | ||
|
||
// Create an invite | ||
inviteRegistry.registerPath({ | ||
method: "post", | ||
path: "/invites", | ||
tags: ["Invite"], | ||
responses: createApiResponse(InviteSchema, "Success"), | ||
}) | ||
|
||
inviteRouter.post("/", verifyJWTAndRole([Role.ADMIN]), inviteController.createInvite) | ||
|
||
// Accept an invite | ||
inviteRegistry.registerPath({ | ||
method: "post", | ||
path: "/invites/accept", | ||
tags: ["Invite"], | ||
responses: createApiResponse(z.object({ | ||
invite: InviteSchema, | ||
}), "Success"), | ||
}) | ||
|
||
inviteRouter.post("/accept", verifyJWT, validateRequest(AcceptInviteSchema), inviteController.acceptInvite) |
Oops, something went wrong.