fosrl.pangolin/server/routers/user/acceptInvite.ts

141 lines
4.1 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { roles, userInvites, userOrgs, users } from "@server/db/schema";
import { eq } from "drizzle-orm";
2025-01-01 21:41:31 -05:00
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";
2024-12-25 15:54:32 -05:00
import { checkValidInvite } from "@server/auth/checkValidInvite";
2025-01-01 21:41:31 -05:00
import { verifySession } from "@server/auth/sessions/verifySession";
2024-12-21 21:01:12 -05:00
const acceptInviteBodySchema = z
.object({
token: z.string(),
inviteId: z.string()
})
.strict();
2024-11-02 23:46:08 -04:00
export type AcceptInviteResponse = {
accepted: boolean;
orgId: string;
};
export async function acceptInvite(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
try {
const parsedBody = acceptInviteBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString()
)
);
}
const { token, inviteId } = parsedBody.data;
2024-12-25 15:54:32 -05:00
const { error, existingInvite } = await checkValidInvite({
token,
inviteId
});
2024-12-25 15:54:32 -05:00
if (error) {
return next(createHttpError(HttpCode.BAD_REQUEST, error));
2024-11-02 23:46:08 -04:00
}
2024-12-25 15:54:32 -05:00
if (!existingInvite) {
return next(
2024-12-25 15:54:32 -05:00
createHttpError(HttpCode.BAD_REQUEST, "Invite does not exist")
);
}
const existingUser = await db
.select()
.from(users)
2024-12-25 15:54:32 -05:00
.where(eq(users.email, existingInvite.email))
.limit(1);
if (!existingUser.length) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"User does not exist. Please create an account first."
)
);
}
const { user, session } = await verifySession(req);
// at this point we know the user exists
if (!user) {
return next(
createHttpError(
HttpCode.UNAUTHORIZED,
"You must be logged in to accept an invite"
)
);
}
if (user && user.email !== existingInvite.email) {
2024-11-02 23:46:08 -04:00
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invite is not for this user"
)
);
}
let roleId: number;
// get the role to make sure it exists
const existingRole = await db
.select()
.from(roles)
2024-12-25 15:54:32 -05:00
.where(eq(roles.roleId, existingInvite.roleId))
.limit(1);
if (existingRole.length) {
roleId = existingRole[0].roleId;
} else {
// TODO: use the default role on the org instead of failing
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Role does not exist. Please contact an admin."
)
);
}
2024-12-24 16:00:02 -05:00
await db.transaction(async (trx) => {
// add the user to the org
await trx.insert(userOrgs).values({
userId: existingUser[0].userId,
orgId: existingInvite.orgId,
roleId: existingInvite.roleId
2024-12-24 16:00:02 -05:00
});
2024-12-24 16:00:02 -05:00
// delete the invite
await trx
.delete(userInvites)
.where(eq(userInvites.inviteId, inviteId));
});
return response<AcceptInviteResponse>(res, {
2024-12-25 15:54:32 -05:00
data: { accepted: true, orgId: existingInvite.orgId },
success: true,
error: false,
message: "Invite accepted",
2024-12-21 21:01:12 -05:00
status: HttpCode.OK
});
} catch (error) {
logger.error(error);
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
);
}
}