diff --git a/src/lib/api/EventAPI.ts b/src/lib/api/EventAPI.ts index a32531ca..31ec2014 100644 --- a/src/lib/api/EventAPI.ts +++ b/src/lib/api/EventAPI.ts @@ -1,8 +1,9 @@ import { config } from '@/lib'; import { FillInLater, UUID } from '@/lib/types'; -import { AttendEventRequest, Event } from '@/lib/types/apiRequests'; +import { AttendEventRequest, CreateBonusRequest, Event } from '@/lib/types/apiRequests'; import { AttendEventResponse, + CreateBonusResponse, CreateEventResponse, GetAllEventsResponse, GetAttendancesForUserResponse, @@ -159,3 +160,28 @@ export const uploadEventImage = async ( }, }); }; + +export const awardBonusPoints = async ( + token: string, + user: string, + points: number, + description: string +): Promise => { + const requestUrl = `${config.api.baseUrl}${config.api.endpoints.admin.bonus}`; + + const requestBody: CreateBonusRequest = { + bonus: { + users: [user], + description, + points, + }, + }; + + const response = await axios.post(requestUrl, requestBody, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + return response.data; +}; diff --git a/src/pages/admin/points.tsx b/src/pages/admin/points.tsx index afab8a93..4e200af7 100644 --- a/src/pages/admin/points.tsx +++ b/src/pages/admin/points.tsx @@ -1,8 +1,10 @@ import { SignInButton, SignInFormItem, SignInTitle } from '@/components/auth'; import { VerticalForm } from '@/components/common'; -import { config } from '@/lib'; +import { config, showToast } from '@/lib'; +import { EventAPI } from '@/lib/api'; import withAccessType from '@/lib/hoc/withAccessType'; -import { PermissionService, ValidationService } from '@/lib/services'; +import { CookieService, PermissionService, ValidationService } from '@/lib/services'; +import { CookieType } from '@/lib/types/enums'; import type { GetServerSideProps, NextPage } from 'next'; import { SubmitHandler, useForm } from 'react-hook-form'; import { AiOutlineMail } from 'react-icons/ai'; @@ -20,8 +22,11 @@ const AwardPointsPage: NextPage = () => { formState: { errors }, } = useForm(); - const onSubmit: SubmitHandler = () => { + const onSubmit: SubmitHandler = async ({ email, description, points }) => { // TODO + const token = CookieService.getClientCookie(CookieType.ACCESS_TOKEN); + const response = await EventAPI.awardBonusPoints(token, email, points, description); + showToast(`Successfully awarded bonus points for ${JSON.stringify(response.emails)}`); }; return ( @@ -60,6 +65,7 @@ const AwardPointsPage: NextPage = () => { placeholder="Point Value" formRegister={register('points', { required: 'Required', + valueAsNumber: true, })} error={errors.points} />