mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-30 23:55:49 +02:00
Add invitation management
This commit is contained in:
parent
40040af957
commit
d7f50bac6a
10 changed files with 574 additions and 9 deletions
73
server/routers/user/removeInvitation.ts
Normal file
73
server/routers/user/removeInvitation.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { userInvites } from "@server/db/schemas";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const removeInvitationParamsSchema = z
|
||||
.object({
|
||||
orgId: z.string(),
|
||||
inviteId: z.string()
|
||||
})
|
||||
.strict();
|
||||
|
||||
export async function removeInvitation(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
// Validate path parameters
|
||||
const parsedParams = removeInvitationParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { orgId, inviteId } = parsedParams.data;
|
||||
|
||||
// Delete the invitation from the database
|
||||
const deletedInvitation = await db
|
||||
.delete(userInvites)
|
||||
.where(
|
||||
and(
|
||||
eq(userInvites.orgId, orgId),
|
||||
eq(userInvites.inviteId, inviteId)
|
||||
)
|
||||
)
|
||||
.returning();
|
||||
|
||||
// If no rows were deleted, the invitation was not found
|
||||
if (deletedInvitation.length === 0) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.NOT_FOUND,
|
||||
`Invitation with ID ${inviteId} not found in organization ${orgId}`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Return success response
|
||||
return response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Invitation removed successfully",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue