Skip to content

Commit

Permalink
added patching and deleting to events portal
Browse files Browse the repository at this point in the history
  • Loading branch information
newracket committed May 15, 2024
1 parent 03e2a12 commit 28b1674
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 10 deletions.
61 changes: 57 additions & 4 deletions src/components/admin/event/ManageEventCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Button } from '@/components/common';
import { GoogleCalendarButton } from '@/components/events/CalendarButtons';
import { config, showToast } from '@/lib';
import { AdminEventManager } from '@/lib/managers';
import { CookieService } from '@/lib/services';
import { PublicEvent } from '@/lib/types/apiResponses';
import { CookieType } from '@/lib/types/enums';
import { reportError } from '@/lib/utils';
import Image from 'next/image';
import Link from 'next/link';
Expand All @@ -17,6 +19,7 @@ interface IProps {
const ManageEventCard = ({ event }: IProps) => {
const router = useRouter();
const [acmurlLoading, setAcmurlLoading] = useState(false);
const [discordEvent, setDiscordEvent] = useState<string | null | undefined>(event.discordEvent);

const publicEventLink = `https://acmucsd.com/events/${event.uuid}`;

Expand All @@ -25,14 +28,63 @@ const ManageEventCard = ({ event }: IProps) => {
};

const generateDiscordEvent = () => {
AdminEventManager.createDiscordEvent({
if (discordEvent) {
AdminEventManager.patchDiscordEvent({
...event,
eventID: discordEvent,
image: event.cover,
onSuccessCallback: () => {
showToast('Successfully edited event!', 'Check your server to confirm all details');
},
onFailCallback: e => {
reportError('Error while editing Discord Event!', e);
},
});
} else {
AdminEventManager.createDiscordEvent({
...event,
image: event.cover,
onSuccessCallback: data => {
const AUTH_TOKEN = CookieService.getClientCookie(CookieType.ACCESS_TOKEN);
const eventID = data.split(' ').pop();

AdminEventManager.editEvent({
token: AUTH_TOKEN,
uuid: event.uuid,
event: {
...event,
discordEvent: eventID,
},
onFailCallback: error => {
reportError('Unable to edit event', error);
},
});

setDiscordEvent(eventID);
showToast('Successfully created event!', 'Check your server to confirm all details');
},
onFailCallback: e => {
reportError('Error while generating Discord Event!', e);
},
});
}
};

const deleteDiscordEvent = () => {
if (!discordEvent) {
showToast("Couldn't delete Discord Event...", `${event.title} doesn't have a Discord Event!`);
return;
}

AdminEventManager.deleteDiscordEvent({
...event,
image: event.cover,
discordEvent,
onSuccessCallback: () => {
showToast('Successfully created event!', 'Check your server to confirm all details');
setDiscordEvent(null);
showToast('Successfully deleted event!', 'Check your server to confirm deletion');
},
onFailCallback: e => {
reportError('Error while generating Discord Event!', e);
reportError('Error while deleting Discord Event!', e);
},
});
};
Expand Down Expand Up @@ -85,6 +137,7 @@ const ManageEventCard = ({ event }: IProps) => {
<Link href={`${config.admin.events.editRoute}/${event.uuid}`}>Edit Details</Link>
<Button onClick={duplicateEvent}>Duplicate Event</Button>
<Button onClick={generateDiscordEvent}>Generate Discord Event</Button>
{discordEvent && <Button onClick={deleteDiscordEvent}>Delete Discord Event</Button>}
<Button onClick={generateACMURL} disabled={acmurlLoading}>
Generate ACMURL
</Button>
Expand Down
34 changes: 31 additions & 3 deletions src/lib/api/KlefkiAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { config } from '@/lib';
import { URL } from '@/lib/types';
import { CreateDiscordEventRequest, GenerateACMURLRequest } from '@/lib/types/apiRequests';
import {
CreateDiscordEventRequest,
DeleteDiscordEventRequest,
GenerateACMURLRequest,
PatchDiscordEventRequest,
} from '@/lib/types/apiRequests';
import type { NotionEventDetails, NotionEventPreview } from '@/lib/types/apiResponses';
import axios from 'axios';
import totp from 'totp-generator';
Expand Down Expand Up @@ -34,11 +39,34 @@ export const getNotionEventPage = async (pageUrl: URL): Promise<NotionEventDetai
return response.data;
};

export const createDiscordEvent = async (event: CreateDiscordEventRequest): Promise<void> => {
export const createDiscordEvent = async (event: CreateDiscordEventRequest): Promise<any> => {
const { klefki } = config;
const requestUrl = `${klefki.baseUrl}${klefki.endpoints.discord.event}`;

await axios.post<void>(requestUrl, event, {
return axios.post<void>(requestUrl, event, {
headers: {
Authorization: `Bearer ${generateToken(klefki.key)}`,
},
});
};

export const patchDiscordEvent = async (event: PatchDiscordEventRequest): Promise<any> => {
const { klefki } = config;
const requestUrl = `${klefki.baseUrl}${klefki.endpoints.discord.event}`;

return axios.patch<void>(requestUrl, event, {
headers: {
Authorization: `Bearer ${generateToken(klefki.key)}`,
},
});
};

export const deleteDiscordEvent = async (event: DeleteDiscordEventRequest): Promise<void> => {
const { klefki } = config;
const requestUrl = `${klefki.baseUrl}${klefki.endpoints.discord.event}`;

await axios.delete<void>(requestUrl, {
data: event,
headers: {
Authorization: `Bearer ${generateToken(klefki.key)}`,
},
Expand Down
47 changes: 44 additions & 3 deletions src/lib/managers/AdminEventManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventAPI, KlefkiAPI } from '@/lib/api';
import config from '@/lib/config';
import { CookieService } from '@/lib/services';
import type { APIHandlerProps, AuthAPIHandlerProps, URL, UUID } from '@/lib/types';
import {
CancelPickupEventRequest,
Expand All @@ -11,6 +12,7 @@ import {
Event,
GenerateACMURLRequest,
OrderPickupEvent,
PatchDiscordEventRequest,
UploadEventImageRequest,
} from '@/lib/types/apiRequests';
import type {
Expand All @@ -19,6 +21,7 @@ import type {
PublicOrder,
PublicOrderPickupEvent,
} from '@/lib/types/apiResponses';
import { CookieType } from '@/lib/types/enums';

interface GetEventFromNotion {
pageUrl: URL;
Expand Down Expand Up @@ -49,12 +52,28 @@ export const getEventFromNotionURL = async (
* @param data
*/
export const createDiscordEvent = async (
data: CreateDiscordEventRequest & APIHandlerProps<void>
data: CreateDiscordEventRequest & APIHandlerProps<string>
) => {
const { onSuccessCallback, onFailCallback, ...event } = data;
try {
await KlefkiAPI.createDiscordEvent(event);
onSuccessCallback?.();
const response = await KlefkiAPI.createDiscordEvent(event);
onSuccessCallback?.(response.data.message);
} catch (e: any) {
onFailCallback?.(e.response.data.error);
}
};

/**
* Patch Discord event
* @param data
*/
export const patchDiscordEvent = async (
data: PatchDiscordEventRequest & APIHandlerProps<string>
) => {
const { onSuccessCallback, onFailCallback, ...event } = data;
try {
const response = await KlefkiAPI.patchDiscordEvent(event);
onSuccessCallback?.(response.data.message);
} catch (e: any) {
onFailCallback?.(e.response.data.error);
}
Expand Down Expand Up @@ -116,6 +135,28 @@ export const editEvent = async (data: EditEventRequest & AuthAPIHandlerProps<Pub
}
};

export const deleteDiscordEvent = async (data: PublicEvent & APIHandlerProps<void>) => {
const { onSuccessCallback, onFailCallback, ...event } = data;
try {
if (event.discordEvent) {
await KlefkiAPI.deleteDiscordEvent({
eventID: event.discordEvent,
});
}

const AUTH_TOKEN = CookieService.getClientCookie(CookieType.ACCESS_TOKEN);
await editEvent({
token: AUTH_TOKEN,
uuid: event.uuid,
event: { ...event, discordEvent: null },
});

onSuccessCallback?.();
} catch (e: any) {
onFailCallback?.(e.response.data.error);
}
};

export const createPickupEvent = async (
token: string,
pickupEvent: OrderPickupEvent
Expand Down
9 changes: 9 additions & 0 deletions src/lib/types/apiRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface OptionalEventProperties {
eventLink?: string;
requiresStaff?: boolean;
staffPointBonus?: number;
discordEvent?: string | null;
}

export interface Event extends OptionalEventProperties {
Expand Down Expand Up @@ -416,6 +417,14 @@ export interface CreateDiscordEventRequest {
image?: string;
}

export interface PatchDiscordEventRequest extends CreateDiscordEventRequest {
eventID: string;
}

export interface DeleteDiscordEventRequest {
eventID: string;
}

export interface GenerateACMURLRequest {
shortlink: string;
longlink: string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/apiResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export interface PublicEvent {
pointValue: number;
requiresStaff: boolean;
staffPointBonus: number;
discordEvent?: string | null;
}

export interface GetPastEventsResponse extends ApiResponse {
Expand Down

0 comments on commit 28b1674

Please sign in to comment.