Skip to content
Draft
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
125 changes: 125 additions & 0 deletions src/card_order/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { ApiPostError, ApiGetError } from "../errors";

import { getApiServer } from "../meta";
import { Oauth } from "../user"

import { BasicUser } from "../user";

export enum OrderStates {
CREATED = "CREATED",
IN_PROGRESS = "IN_PROGRESS",
FINISHED = "FINISHED",
CANCELLED = "CANCELLED",
}

interface BaseCardOrder {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk if the base type was made well or not

uuid: string;

created: number;
state: OrderStates
last_updated: number;
}

export type BasicCardOrder = BaseCardOrder & {
event_uuid: string;
subject_user: BasicUser;
creator_user: BasicUser;

updated_by_user: BasicUser;
}

//? init
export const createCardOrder = async (user_uuid: string) => {
const response = await fetch(`${getApiServer()}/card_order`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
body: JSON.stringify({
user_uuid: user_uuid
})
});

if (!response.ok) {
let error = ""
try {
error = (await response.json())["error"]
} catch (e) {
throw new ApiPostError("Unable to create card order");
}

throw new ApiPostError(error);
}
return await response.json() as BasicCardOrder;
}

export const getAllCardOrders = async (event_uuid:string|undefined) => {
let param = ""
if (typeof event_uuid !== "undefined") {
param = "?event_uuid=" + event_uuid
}
const response = await fetch(`${getApiServer()}/card_order/${param}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
});

if (response.status !== 200) {
throw new ApiGetError("Unable to get all card orders for event");
}

return (await response.json()) as Array<BasicCardOrder>;
};

//? instance
export const getCardOrder = async (uuid:string) => {
const response = await fetch(`${getApiServer()}/card_order/${uuid}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
});

if (response.status !== 200) {
throw new ApiGetError("Unable to get specified card order");
}

return (await response.json()) as BasicCardOrder;
};

export const generateCardFromOrder = async (uuid: string) => {
const result = await fetch(`${getApiServer()}/card_order/${uuid}/generate`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
});
return result
}

export const finishCardOrder = async (uuid: string) => {
const response = await fetch(`${getApiServer()}/card_order/${uuid}/finish`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
});
return (await response.json()) as BasicCardOrder;
}

export const cancelCardOrder = async (uuid: string) => {
const response = await fetch(`${getApiServer()}/card_order/${uuid}/cancel`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...(await Oauth.getAuthHeaders())
},
});
return (await response.json()) as BasicCardOrder;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as User from './user';
export * from './meta';
export * from './events';
export * as Crew from './crew';
export * as CardOrder from './card_order';
export * from './errors';
export * as Avatar from './avatar';
export * from './tos';
Expand Down