|
| 1 | +import { db, event, isStaging, sendEmail } from "#src/utils"; |
| 2 | + |
| 3 | +// |
| 4 | +// Admin-only: approve or deny an access request. Approving creates an invite, |
| 5 | +// which is what authorizes the email's SSO sign-in (see ensureSsoUser). |
| 6 | +// |
| 7 | + |
| 8 | +export const reviewAccessRequest = async () => { |
| 9 | + const { id, action } = event.body; |
| 10 | + if (!['approve', 'deny'].includes(action)) { |
| 11 | + return { status: 'error', message: 'Invalid action.' }; |
| 12 | + } |
| 13 | + |
| 14 | + await db.connect(); |
| 15 | + const requesterType = (await db.query({ |
| 16 | + text: `SELECT type FROM users WHERE id=$1`, |
| 17 | + values: [event.claims.sub], |
| 18 | + })).rows?.[0]?.type; |
| 19 | + if (requesterType !== 'admin') { |
| 20 | + await db.clean(); |
| 21 | + return { status: 'error', message: 'Admin access required.' }; |
| 22 | + } |
| 23 | + |
| 24 | + const request = (await db.query({ |
| 25 | + text: `SELECT "id", "name", "email", "status" FROM "access_requests" WHERE "id"=$1`, |
| 26 | + values: [id], |
| 27 | + })).rows?.[0]; |
| 28 | + if (!request) { |
| 29 | + await db.clean(); |
| 30 | + return { status: 'error', message: 'Access request not found.' }; |
| 31 | + } |
| 32 | + if (request.status !== 'pending') { |
| 33 | + await db.clean(); |
| 34 | + return { status: 'error', message: 'This request has already been reviewed.' }; |
| 35 | + } |
| 36 | + |
| 37 | + if (action === 'deny') { |
| 38 | + await db.query({ |
| 39 | + text: `UPDATE "access_requests" SET "status"='denied', "reviewed_by"=$2, "reviewed_at"=now(), "updated_at"=now() WHERE "id"=$1`, |
| 40 | + values: [id, event.claims.sub], |
| 41 | + }); |
| 42 | + await db.clean(); |
| 43 | + return { status: 'success', message: 'Request denied.' }; |
| 44 | + } |
| 45 | + |
| 46 | + // approve: create the invite unless one already exists for this email |
| 47 | + const inviteExists = (await db.query({ |
| 48 | + text: `SELECT id FROM invites WHERE lower(email)=lower($1)`, |
| 49 | + values: [request.email], |
| 50 | + })).rows?.[0]?.id; |
| 51 | + if (!inviteExists) { |
| 52 | + await db.query({ |
| 53 | + text: `INSERT INTO "invites" ("user_id", "email", "name") VALUES ($1, $2, $3)`, |
| 54 | + values: [event.claims.sub, request.email, request.name], |
| 55 | + }); |
| 56 | + } |
| 57 | + await db.query({ |
| 58 | + text: `UPDATE "access_requests" SET "status"='approved', "reviewed_by"=$2, "reviewed_at"=now(), "updated_at"=now() WHERE "id"=$1`, |
| 59 | + values: [id, event.claims.sub], |
| 60 | + }); |
| 61 | + await db.clean(); |
| 62 | + |
| 63 | + // notify the requester; the approval already succeeded, so an email failure |
| 64 | + // shouldn't surface as an error to the reviewing admin |
| 65 | + try { |
| 66 | + await sendEmail({ |
| 67 | + to: request.email, |
| 68 | + subject: `Your Equalify access request was approved`, |
| 69 | + body: `<tr> |
| 70 | + <td style="padding:24px 24px 8px 24px; font-size:16px; line-height:1.5; color:#334155;"> |
| 71 | + Hello, |
| 72 | + </td> |
| 73 | + </tr> |
| 74 | + <tr> |
| 75 | + <td style="padding:0 24px 24px 24px; font-size:16px; line-height:1.5; color:#334155;"> |
| 76 | + Your request to access Equalify has been approved. Sign in with your SSO account below to get started: |
| 77 | + </td> |
| 78 | + </tr> |
| 79 | +
|
| 80 | + <!-- Button --> |
| 81 | + <tr> |
| 82 | + <td align="left" style="padding:0 24px 24px 24px;"> |
| 83 | + <table role="presentation" cellpadding="0" cellspacing="0" border="0"> |
| 84 | + <tr> |
| 85 | + <td align="center" bgcolor="#186121" style="border-radius:6px;"> |
| 86 | + <a href="${process.env.APP_URL ?? `https://app${isStaging ? '-staging' : ''}.equalify.uic.edu`}/login" |
| 87 | + style="display:inline-block; padding:12px 24px; font-size:16px; font-weight:600; color:#ffffff; text-decoration:none; border-radius:6px; background-color:#186121;"> |
| 88 | + Sign In |
| 89 | + </a> |
| 90 | + </td> |
| 91 | + </tr> |
| 92 | + </table> |
| 93 | + </td> |
| 94 | + </tr>` |
| 95 | + }); |
| 96 | + } catch (emailError) { |
| 97 | + console.error('Approval email failed to send:', request.email, emailError); |
| 98 | + } |
| 99 | + |
| 100 | + return { status: 'success', message: 'Request approved — invite created.' }; |
| 101 | +}; |
0 commit comments