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

added patching and deleting to events portal #258

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
63 changes: 61 additions & 2 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>(event.discordEvent);

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

Expand All @@ -28,7 +31,23 @@ const ManageEventCard = ({ event }: IProps) => {
AdminEventManager.createDiscordEvent({
...event,
image: event.cover,
onSuccessCallback: () => {
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);
},
});

if (eventID) setDiscordEvent(eventID);
showToast('Successfully created event!', 'Check your server to confirm all details');
},
onFailCallback: e => {
Expand All @@ -37,6 +56,44 @@ const ManageEventCard = ({ event }: IProps) => {
});
};

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

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);
},
});
};

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

AdminEventManager.deleteDiscordEvent({
...event,
discordEvent,
onSuccessCallback: () => {
setDiscordEvent(null);
showToast('Successfully deleted event!', 'Check your server to confirm deletion');
},
onFailCallback: e => {
reportError('Error while deleting Discord Event!', e);
},
});
};

const generateACMURL = () => {
if (!event.eventLink) {
showToast("Couldn't generate ACMURL...", `${event.title} doesn't have an event link!`);
Expand Down Expand Up @@ -84,7 +141,9 @@ const ManageEventCard = ({ event }: IProps) => {
<a href={publicEventLink}>View Public Event Page</a>
<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={generateDiscordEvent}>Create New Discord Event</Button>}
{discordEvent && <Button onClick={updateDiscordEvent}>Update Discord Event</Button>}
{discordEvent && <Button onClick={deleteDiscordEvent}>Delete Discord Event</Button>}
<Button onClick={generateACMURL} disabled={acmurlLoading}>
Generate ACMURL
</Button>
Expand Down
48 changes: 44 additions & 4 deletions src/lib/api/KlefkiAPI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { config } from '@/lib';
import { URL } from '@/lib/types';
import { CreateDiscordEventRequest, GenerateACMURLRequest } from '@/lib/types/apiRequests';
import type { NotionEventDetails, NotionEventPreview } from '@/lib/types/apiResponses';
import {
CreateDiscordEventRequest,
DeleteDiscordEventRequest,
GenerateACMURLRequest,
PatchDiscordEventRequest,
} from '@/lib/types/apiRequests';
import type {
KlefkiAPIResponse,
NotionEventDetails,
NotionEventPreview,
} from '@/lib/types/apiResponses';
import axios from 'axios';
import totp from 'totp-generator';

Expand Down Expand Up @@ -34,11 +43,42 @@ 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<KlefkiAPIResponse> => {
const { klefki } = config;
const requestUrl = `${klefki.baseUrl}${klefki.endpoints.discord.event}`;

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

return response.data;
};

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

const response = await axios.patch<KlefkiAPIResponse>(requestUrl, event, {
headers: {
Authorization: `Bearer ${generateToken(klefki.key)}`,
},
});

return response.data;
};

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: 40 additions & 7 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 All @@ -44,17 +47,25 @@ export const getEventFromNotionURL = async (
}
};

/**
* Create Discord event
* @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 data = await KlefkiAPI.createDiscordEvent(event);
onSuccessCallback?.(data.message);
} catch (e: any) {
onFailCallback?.(e.response.data.error);
}
};

export const patchDiscordEvent = async (
data: PatchDiscordEventRequest & APIHandlerProps<string>
) => {
const { onSuccessCallback, onFailCallback, ...event } = data;
try {
const data = await KlefkiAPI.patchDiscordEvent(event);
onSuccessCallback?.(data.message);
} catch (e: any) {
onFailCallback?.(e.response.data.error);
}
Expand Down Expand Up @@ -116,6 +127,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;
newracket marked this conversation as resolved.
Show resolved Hide resolved
}

export interface GenerateACMURLRequest {
shortlink: string;
longlink: string;
Expand Down
6 changes: 6 additions & 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 Expand Up @@ -510,6 +511,11 @@ export interface PatchResumeResponse extends ApiResponse {

export interface DeleteResumeResponse extends ApiResponse {}

export interface KlefkiAPIResponse {
message: string;
error: string;
}

export interface NotionEventDetails {
title: string;
community: string;
Expand Down
Loading