Skip to content

Commit

Permalink
Merge branch 'main' into justinxue/bre-27-implement-date-and-time-sel…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
celinechoiii authored Dec 2, 2024
2 parents b5de0e8 + d63fbd3 commit a3ff7ec
Show file tree
Hide file tree
Showing 56 changed files with 2,673 additions and 425 deletions.
120 changes: 120 additions & 0 deletions api/supabase/queries/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import supabase from '@/api/supabase/createClient';

export async function handleSignUp(
email: string,
password: string,
): Promise<{ success: boolean; message: string }> {
try {
const { data, error } = await supabase.auth.signUp({ email, password });

if (error) {
return { success: false, message: `Sign-up failed: ${error.message}` };
}

const user = data.user;
if (!user) {
return {
success: false,
message: 'Sign-up failed: User was not created.',
};
}

const { error: insertError } = await supabase.from('volunteers').insert([
{
user_id: user.id,
email,
first_name: '',
last_name: '',
phone_number: '',
notifications_opt_in: true, // default value
},
]);

if (insertError) {
return {
success: false,
message: `Error storing user data: ${insertError.message}`,
};
}

return { success: true, message: 'Sign-up successful!' };
} catch (err) {
if (err instanceof Error) {
return { success: false, message: `Sign-up failed: ${err.message}` };
}
return {
success: false,
message: 'Sign-up failed: An unknown error occurred.',
};
}
}

export async function handleSignIn(
email: string,
password: string,
): Promise<{ success: boolean; message: string }> {
try {
const { error: signInError } = await supabase.auth.signInWithPassword({
email,
password,
});

if (signInError) {
return {
success: false,
message: `Login failed: ${signInError.message}`,
};
}

const { data: sessionData, error: sessionError } =
await supabase.auth.getSession();

if (sessionError || !sessionData?.session) {
return {
success: false,
message: 'Failed to retrieve session information after login.',
};
}

const user_id = sessionData.session.user.id;

const userExists = await checkUserExists(user_id, 'volunteer');

if (!userExists) {
return { success: false, message: 'User not found in volunteers table.' };
}

return { success: true, message: 'Login successful!' };
} catch (err) {
if (err instanceof Error) {
return { success: false, message: `Login failed: ${err.message}` };
}
return {
success: false,
message: 'Login failed: An unknown error occurred.',
};
}
}

export async function checkUserExists(
userId: string,
userType: 'volunteer' | 'facility',
): Promise<boolean> {
try {
const table = userType === 'volunteer' ? 'volunteers' : 'facilities';

const { data, error } = await supabase
.from(table)
.select('user_id')
.eq('user_id', userId)
.single();

if (error || !data) {
return false;
}
return true;
} catch (err) {
console.error('Error checking user existence:', err);
return false;
}
}
44 changes: 42 additions & 2 deletions api/supabase/queries/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export async function fetchAllEvents() {
return data;
}

export async function fetchAcceptedEventsByVolunteer(volunteer_id: UUID) {
export async function fetchAcceptedEventsByVolunteer(user_id: UUID) {
const { data, error } = await supabase
.from('event_signups')
.select('*')
.eq('user_id', volunteer_id)
.eq('user_id', user_id)
.eq('is_accepted', true);

if (error) {
Expand All @@ -31,6 +31,7 @@ export async function fetchAcceptedEventsByVolunteer(volunteer_id: UUID) {
const { data: events, error: eventsError } = await supabase
.from('events')
.select('*')
.eq('event_status', 'Active')
.in('event_id', eventIDs);

if (eventsError) {
Expand All @@ -52,3 +53,42 @@ export async function fetchAllActiveEvents() {

return data;
}

export async function fetchEventById(event_id: string) {
const { data, error } = await supabase
.from('events')
.select('*')
.eq('event_id', event_id)
.single();
if (error) {
throw new Error(error.message);
}

return data;
}

export async function fetchEventHostByID(event_id: UUID) {
const { data, error } = await supabase
.from('event_signups')
.select('*')
.eq('event_id', event_id)
.eq('role', 'HOST')
.eq('is_accepted', true)
.single();

if (error) {
throw new Error(error.message);
}

const { data: host, error: hosterror } = await supabase
.from('volunteers')
.select('*')
.eq('user_id', data.user_id)
.single();

if (hosterror) {
throw new Error(hosterror.message);
}

return host;
}
30 changes: 30 additions & 0 deletions api/supabase/queries/facilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { UUID } from 'crypto';
import supabase from '../createClient';

// fetches an event by its event_id
export async function fetchFacilityById(facility_id: string) {
const { data, error } = await supabase
.from('facilities')
.select('*')
.eq('facility_id', facility_id)
.single();
if (error) {
throw new Error(error.message);
}

return data;
}

export async function fetchFacilityContactByID(facility_id: UUID) {
const { data, error } = await supabase
.from('facility_contacts')
.select('*')
.eq('facility_id', facility_id)
.single();

if (error) {
throw new Error(error.message);
}

return data;
}
45 changes: 45 additions & 0 deletions api/supabase/queries/onboarding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GeneralInfo, Preferences } from '@/utils/onboardingContext';
import supabase from '../createClient';

export async function submitOnboardingData(
generalInfo: GeneralInfo,
preferences: Preferences,
): Promise<void> {
try {
const { data: volunteerData, error: volunteerError } = await supabase
.from('volunteers')
.insert([
{
first_name: generalInfo.firstName,
last_name: generalInfo.lastName,
phone_number: generalInfo.phoneNumber,
},
]);

if (volunteerError)
throw new Error(`Volunteer data error: ${volunteerError.message}`);

const { data: preferencesData, error: preferencesError } = await supabase
.from('volunteer_preferences')
.insert([
{
facility_type: preferences.facilityType,
city: preferences.location,
audience: preferences.audience,
instruments: preferences.preferredEquipment,
type_of_act: preferences.typeOfAct,
genre: preferences.genre,
},
]);

if (preferencesError)
throw new Error(`Preferences data error: ${preferencesError.message}`);

console.log('Onboarding data submitted successfully:', {
volunteerData,
preferencesData,
});
} catch (error) {
console.error('Error submitting onboarding data:', error);
}
}
28 changes: 28 additions & 0 deletions api/supabase/queries/volunteers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { UUID } from 'crypto';
import supabase from '../createClient';

export async function fetchPerformer(event_id: UUID) {
const { data, error } = await supabase
.from('event_signups')
.select('*')
.eq('event_id', event_id)
.eq('role', 'PERFORMER')
.eq('is_accepted', true)
.single();

if (error) {
throw new Error(error.message);
}

const { data: performer, error: performererror } = await supabase
.from('volunteers')
.select('*')
.eq('user_id', data.user_id)
.single();

if (performererror) {
throw new Error(performererror.message);
}

return performer;
}
Loading

0 comments on commit a3ff7ec

Please sign in to comment.