Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update functions to accept new settings format + faculty_email field #22

Merged
merged 5 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions _shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,14 @@ export const sendOrgEmail = async (
subject = subject.replace(/{ORG_NAME}/g, orgName);
text = text.replace(/{ORG_NAME}/g, orgName);

await Transport.sendMail({
from: Deno.env.get('NODEMAILER_FROM')!,
bcc: recipientEmails,
subject,
text,
});
if (recipientEmails?.length > 0) {
await Transport.sendMail({
from: Deno.env.get('NODEMAILER_FROM')!,
bcc: recipientEmails,
subject,
text,
});
}
};

export const sendMemberEmail = async (
Expand Down Expand Up @@ -283,3 +285,16 @@ export const sendMemberEmail = async (
text,
});
};

export const fetchMemberRequirement = async () => {
type styp = {
setting_value: number;
};
const { data } = await supabaseAdmin.from('settings')
.select(`
setting_value
`).eq('name', 'required_members')
.single()
.returns<styp>();
return data?.setting_value || 0;
};
19 changes: 6 additions & 13 deletions approve-member/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
import corsHeaders from '../_shared/cors.ts';
import supabaseAdmin from '../_shared/supabaseAdmin.ts';
import { sendMemberEmail, sendOrgEmail } from '../_shared/utils.ts';
import {
fetchMemberRequirement,
sendMemberEmail,
sendOrgEmail,
} from '../_shared/utils.ts';
import { footer } from '../_shared/strings.ts';

type BodyType = {
Expand Down Expand Up @@ -110,18 +114,7 @@ We hope you enjoy your club experience at Stuy.` + footer;
if (orgData && orgData.length && !orgDataError) {
const org = orgData[0];

type styp = {
required_members: number;
};
const { data: siteSettings } = await supabaseAdmin.from('settings')
.select(`
required_members
`)
.returns<styp[]>();
let required_members = 0;
if (siteSettings) {
required_members = siteSettings[0].required_members;
}
const required_members = await fetchMemberRequirement();

if (
org.state === 'LOCKED' &&
Expand Down
12 changes: 2 additions & 10 deletions approve-organization/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
import { sendOrgEmail } from '../_shared/utils.ts';
import { fetchMemberRequirement, sendOrgEmail } from '../_shared/utils.ts';
import corsHeaders from '../_shared/cors.ts';
import { footer } from '../_shared/strings.ts';

Expand Down Expand Up @@ -33,15 +33,7 @@ Deno.serve(async (req: Request) => {
const { data: userData } = await supabaseClient.auth.getUser(jwt);
const user = userData.user;

const { data: siteSettings } = await supabaseClient.from('settings')
.select(`
required_members
`)
.returns<styp[]>();
let required_members = 0;
if (siteSettings) {
required_members = siteSettings[0].required_members;
}
const required_members = await fetchMemberRequirement();

/* Failed to fetch supabase user */
if (!user) {
Expand Down
20 changes: 20 additions & 0 deletions create-organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type BodyType = {
is_returning: boolean;
returning_info: string;
fair?: boolean;
faculty_email?: string;
};

const RESERVED_PATHS = [
Expand Down Expand Up @@ -52,6 +53,25 @@ Deno.serve(async (req: Request) => {
{ global: { headers: { Authorization: authHeader } } },
);

type styp = {
setting_value: number;
};
const { data } = await supabaseAdmin.from('settings')
.select(`
setting_value
`).eq('name', 'charter_deadline')
.single()
.returns<styp>();
if (data?.setting_value) {
const deadline = new Date(data.setting_value);
if (new Date() > deadline) {
return new Response(
`Chartering for this year has been disabled as of ${deadline.toLocaleString()}.`,
{ status: 403 },
);
}
}

const jwt = authHeader.split(' ')[1];
const { data: userData } = await supabaseClient.auth.getUser(jwt);
const user = userData.user;
Expand Down
7 changes: 5 additions & 2 deletions export-voters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ Deno.serve(async (req: Request) => {
// verify JWT provided by the user
try {
const ecPublicKey = await jose.importSPKI(key, 'RS256');
const { payload: { user: userPayload } } = await jose.jwtVerify(jwt, ecPublicKey);

const { payload: { user: userPayload } } = await jose.jwtVerify(
jwt,
ecPublicKey,
);

if (!userPayload.adminPrivileges) {
return new Response(
JSON.stringify({
Expand Down