add logging

This commit is contained in:
Milo Schwartz 2024-11-24 12:46:55 -05:00
parent e9ffffa419
commit 5361873672
No known key found for this signature in database

View file

@ -38,7 +38,7 @@ const inviteTracker: Record<string, { timestamps: number[] }> = {};
export async function inviteUser( export async function inviteUser(
req: Request, req: Request,
res: Response, res: Response,
next: NextFunction next: NextFunction,
): Promise<any> { ): Promise<any> {
try { try {
const parsedParams = inviteUserParamsSchema.safeParse(req.params); const parsedParams = inviteUserParamsSchema.safeParse(req.params);
@ -46,8 +46,8 @@ export async function inviteUser(
return next( return next(
createHttpError( createHttpError(
HttpCode.BAD_REQUEST, HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString() fromError(parsedParams.error).toString(),
) ),
); );
} }
@ -56,8 +56,8 @@ export async function inviteUser(
return next( return next(
createHttpError( createHttpError(
HttpCode.BAD_REQUEST, HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString() fromError(parsedBody.error).toString(),
) ),
); );
} }
@ -79,13 +79,14 @@ export async function inviteUser(
return next( return next(
createHttpError( createHttpError(
HttpCode.TOO_MANY_REQUESTS, HttpCode.TOO_MANY_REQUESTS,
"User has already been invited 3 times in the last hour" "User has already been invited 3 times in the last hour",
) ),
); );
} }
inviteTracker[email].timestamps.push(currentTime); inviteTracker[email].timestamps.push(currentTime);
logger.debug("here0")
const org = await db const org = await db
.select() .select()
.from(orgs) .from(orgs)
@ -93,10 +94,11 @@ export async function inviteUser(
.limit(1); .limit(1);
if (!org.length) { if (!org.length) {
return next( return next(
createHttpError(HttpCode.NOT_FOUND, "Organization not found") createHttpError(HttpCode.NOT_FOUND, "Organization not found"),
); );
} }
logger.debug("here1")
const existingUser = await db const existingUser = await db
.select() .select()
.from(users) .from(users)
@ -107,28 +109,31 @@ export async function inviteUser(
return next( return next(
createHttpError( createHttpError(
HttpCode.BAD_REQUEST, HttpCode.BAD_REQUEST,
"User is already a member of this organization" "User is already a member of this organization",
) ),
); );
} }
logger.debug("here2")
const inviteId = generateRandomString( const inviteId = generateRandomString(
10, 10,
alphabet("a-z", "A-Z", "0-9") alphabet("a-z", "A-Z", "0-9"),
); );
const token = generateRandomString(32, alphabet("a-z", "A-Z", "0-9")); const token = generateRandomString(32, alphabet("a-z", "A-Z", "0-9"));
const expiresAt = createDate(new TimeSpan(validHours, "h")).getTime(); const expiresAt = createDate(new TimeSpan(validHours, "h")).getTime();
const tokenHash = await hashPassword(token); const tokenHash = await hashPassword(token);
logger.debug("here3")
// delete any existing invites for this email // delete any existing invites for this email
await db await db
.delete(userInvites) .delete(userInvites)
.where( .where(
and(eq(userInvites.email, email), eq(userInvites.orgId, orgId)) and(eq(userInvites.email, email), eq(userInvites.orgId, orgId)),
) )
.execute(); .execute();
logger.debug("here4")
await db.insert(userInvites).values({ await db.insert(userInvites).values({
inviteId, inviteId,
orgId, orgId,
@ -140,6 +145,7 @@ export async function inviteUser(
const inviteLink = `${config.app.base_url}/invite?token=${inviteId}-${token}`; const inviteLink = `${config.app.base_url}/invite?token=${inviteId}-${token}`;
logger.debug("here5")
await sendEmail( await sendEmail(
SendInviteLink({ SendInviteLink({
email, email,
@ -152,9 +158,10 @@ export async function inviteUser(
to: email, to: email,
from: config.email?.no_reply, from: config.email?.no_reply,
subject: "You're invited to join a Fossorial organization", subject: "You're invited to join a Fossorial organization",
} },
); );
logger.debug("here6")
return response<InviteUserResponse>(res, { return response<InviteUserResponse>(res, {
data: { data: {
inviteLink, inviteLink,
@ -166,9 +173,12 @@ export async function inviteUser(
status: HttpCode.OK, status: HttpCode.OK,
}); });
} catch (error) { } catch (error) {
throw error; console.error(error);
return next( return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred") createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred",
),
); );
} }
} }