2021-07-12 02:17:58 -07:00
|
|
|
import cookies from 'browser-cookies';
|
2021-08-02 15:39:40 +02:00
|
|
|
import envData from '../../../config/env.json';
|
2021-07-12 02:17:58 -07:00
|
|
|
|
2021-08-17 18:31:25 +01:00
|
|
|
import type {
|
|
|
|
ChallengeFile,
|
|
|
|
CompletedChallenge,
|
2021-11-09 19:51:46 +05:30
|
|
|
User
|
2021-08-17 18:31:25 +01:00
|
|
|
} from '../redux/prop-types';
|
2021-07-12 02:17:58 -07:00
|
|
|
|
|
|
|
const { apiLocation } = envData;
|
|
|
|
|
|
|
|
const base = apiLocation;
|
|
|
|
|
|
|
|
const defaultOptions: RequestInit = {
|
|
|
|
credentials: 'include'
|
|
|
|
};
|
|
|
|
|
2021-07-16 17:49:47 +02:00
|
|
|
// csrf_token is passed to the client as a cookie. The client must send
|
|
|
|
// this back as a header.
|
2021-07-12 02:17:58 -07:00
|
|
|
function getCSRFToken() {
|
2021-07-16 17:49:47 +02:00
|
|
|
const token =
|
|
|
|
typeof window !== 'undefined' ? cookies.get('csrf_token') : null;
|
|
|
|
return token ?? '';
|
2021-07-12 02:17:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function get<T>(path: string): Promise<T> {
|
|
|
|
return fetch(`${base}${path}`, defaultOptions).then<T>(res => res.json());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function post<T = void>(path: string, body: unknown): Promise<T> {
|
|
|
|
return request('POST', path, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
function put<T = void>(path: string, body: unknown): Promise<T> {
|
|
|
|
return request('PUT', path, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function request<T>(
|
|
|
|
method: 'POST' | 'PUT',
|
|
|
|
path: string,
|
|
|
|
body: unknown
|
|
|
|
): Promise<T> {
|
|
|
|
const options: RequestInit = {
|
|
|
|
...defaultOptions,
|
|
|
|
method,
|
|
|
|
headers: {
|
|
|
|
'CSRF-Token': getCSRFToken(),
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
};
|
|
|
|
return fetch(`${base}${path}`, options).then<T>(res => res.json());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** GET **/
|
|
|
|
|
|
|
|
interface SessionUser {
|
2021-11-09 19:51:46 +05:30
|
|
|
user?: { [username: string]: User };
|
2021-07-12 02:17:58 -07:00
|
|
|
sessionMeta: { activeDonations: number };
|
|
|
|
}
|
2021-08-17 18:31:25 +01:00
|
|
|
|
2021-11-11 19:09:50 +01:00
|
|
|
type ChallengeFilesForFiles = {
|
2021-08-17 18:31:25 +01:00
|
|
|
files: Array<Omit<ChallengeFile, 'fileKey'> & { key: string }>;
|
|
|
|
} & Omit<CompletedChallenge, 'challengeFiles'>;
|
|
|
|
|
|
|
|
type ApiSessionResponse = Omit<SessionUser, 'user'>;
|
|
|
|
type ApiUser = {
|
|
|
|
user: {
|
2021-11-11 19:09:50 +01:00
|
|
|
[username: string]: Omit<User, 'completedChallenges'> & {
|
|
|
|
completedChallenges?: ChallengeFilesForFiles[];
|
|
|
|
};
|
2021-08-17 18:31:25 +01:00
|
|
|
};
|
|
|
|
result?: string;
|
|
|
|
};
|
|
|
|
|
2021-11-11 19:09:50 +01:00
|
|
|
type UserResponse = {
|
2021-11-09 19:51:46 +05:30
|
|
|
user: { [username: string]: User } | Record<string, never>;
|
2021-08-17 18:31:25 +01:00
|
|
|
result: string | undefined;
|
|
|
|
};
|
|
|
|
|
2021-11-11 19:09:50 +01:00
|
|
|
function parseApiResponseToClientUser(data: ApiUser): UserResponse {
|
2021-08-17 18:31:25 +01:00
|
|
|
const userData = data.user?.[data?.result ?? ''];
|
|
|
|
let completedChallenges: CompletedChallenge[] = [];
|
|
|
|
if (userData) {
|
|
|
|
completedChallenges =
|
|
|
|
userData.completedChallenges?.reduce(
|
2021-11-11 19:09:50 +01:00
|
|
|
(acc: CompletedChallenge[], curr: ChallengeFilesForFiles) => {
|
2021-08-17 18:31:25 +01:00
|
|
|
return [
|
|
|
|
...acc,
|
|
|
|
{
|
|
|
|
...curr,
|
|
|
|
challengeFiles: curr.files.map(({ key: fileKey, ...file }) => ({
|
|
|
|
...file,
|
|
|
|
fileKey
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
];
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
) ?? [];
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
user: { [data.result ?? '']: { ...userData, completedChallenges } },
|
|
|
|
result: data.result
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-12 02:17:58 -07:00
|
|
|
export function getSessionUser(): Promise<SessionUser> {
|
2021-08-17 18:31:25 +01:00
|
|
|
const response: Promise<ApiUser & ApiSessionResponse> = get(
|
|
|
|
'/user/get-session-user'
|
|
|
|
);
|
|
|
|
// TODO: Once DB is migrated, no longer need to parse `files` -> `challengeFiles` etc.
|
|
|
|
return response.then(data => {
|
|
|
|
const { result, user } = parseApiResponseToClientUser(data);
|
|
|
|
return {
|
|
|
|
sessionMeta: data.sessionMeta,
|
|
|
|
result,
|
|
|
|
user
|
|
|
|
};
|
|
|
|
});
|
2021-07-12 02:17:58 -07:00
|
|
|
}
|
|
|
|
|
2021-08-17 18:31:25 +01:00
|
|
|
type UserProfileResponse = {
|
2021-11-11 19:09:50 +01:00
|
|
|
entities: Omit<UserResponse, 'result'>;
|
2021-08-17 18:31:25 +01:00
|
|
|
result: string | undefined;
|
|
|
|
};
|
|
|
|
export function getUserProfile(username: string): Promise<UserProfileResponse> {
|
|
|
|
const response: Promise<{ entities?: ApiUser; result?: string }> = get(
|
|
|
|
`/api/users/get-public-profile?username=${username}`
|
|
|
|
);
|
|
|
|
return response.then(data => {
|
|
|
|
const { result, user } = parseApiResponseToClientUser({
|
|
|
|
user: data.entities?.user ?? {},
|
|
|
|
result: data.result
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
entities: { user },
|
|
|
|
result
|
|
|
|
};
|
|
|
|
});
|
2021-07-12 02:17:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Cert {
|
|
|
|
certTitle: string;
|
|
|
|
username: string;
|
|
|
|
date: Date;
|
|
|
|
completionTime: string;
|
|
|
|
}
|
|
|
|
export function getShowCert(username: string, certSlug: string): Promise<Cert> {
|
|
|
|
return get(`/certificate/showCert/${username}/${certSlug}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUsernameExists(username: string): Promise<boolean> {
|
|
|
|
return get(`/api/users/exists?username=${username}`);
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:51:27 +01:00
|
|
|
// TODO: Does a GET return a bolean?
|
|
|
|
export function getVerifyCanClaimCert(
|
|
|
|
username: string,
|
|
|
|
superBlock: string
|
|
|
|
): Promise<boolean> {
|
|
|
|
return get(
|
|
|
|
`/certificate/verify-can-claim-cert?username=${username}&superBlock=${superBlock}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-12 02:17:58 -07:00
|
|
|
/** POST **/
|
|
|
|
|
|
|
|
interface Donation {
|
|
|
|
email: string;
|
|
|
|
amount: number;
|
|
|
|
duration: string;
|
|
|
|
provider: string;
|
|
|
|
subscriptionId: string;
|
|
|
|
customerId: string;
|
|
|
|
startDate: Date;
|
|
|
|
}
|
2021-08-04 06:21:11 -04:00
|
|
|
// TODO: Verify if the body has and needs this Donation type. The api seems to
|
|
|
|
// just need the body to exist, but doesn't seem to use the properties.
|
2021-07-12 02:17:58 -07:00
|
|
|
export function addDonation(body: Donation): Promise<void> {
|
|
|
|
return post('/donate/add-donation', body);
|
|
|
|
}
|
|
|
|
|
2021-08-08 23:22:25 +03:00
|
|
|
export function postChargeStripe(body: Donation): Promise<void> {
|
|
|
|
return post('/donate/charge-stripe', body);
|
|
|
|
}
|
2021-09-17 22:15:56 +03:00
|
|
|
|
|
|
|
export function postChargeStripeCard(body: Donation): Promise<void> {
|
|
|
|
return post('/donate/charge-stripe-card', body);
|
|
|
|
}
|
2021-07-12 02:17:58 -07:00
|
|
|
interface Report {
|
|
|
|
username: string;
|
|
|
|
reportDescription: string;
|
|
|
|
}
|
|
|
|
export function postReportUser(body: Report): Promise<void> {
|
|
|
|
return post('/user/report-user', body);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both are called without a payload in danger-zone-saga,
|
|
|
|
// which suggests both are sent without any body
|
|
|
|
// TODO: Convert to DELETE
|
|
|
|
export function postDeleteAccount(): Promise<void> {
|
|
|
|
return post('/account/delete', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postResetProgress(): Promise<void> {
|
|
|
|
return post('/account/reset-progress', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** PUT **/
|
|
|
|
|
|
|
|
interface MyAbout {
|
|
|
|
name: string;
|
|
|
|
location: string;
|
|
|
|
about: string;
|
|
|
|
picture: string;
|
|
|
|
}
|
|
|
|
export function putUpdateMyAbout(values: MyAbout): Promise<void> {
|
|
|
|
return put('/update-my-about', { ...values });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function putUpdateMyUsername(username: string): Promise<void> {
|
|
|
|
return put('/update-my-username', { username });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function putUpdateMyProfileUI(
|
2021-11-09 19:51:46 +05:30
|
|
|
profileUI: User['profileUI']
|
2021-07-12 02:17:58 -07:00
|
|
|
): Promise<void> {
|
|
|
|
return put('/update-my-profileui', { profileUI });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update should contain only one flag and one new value
|
|
|
|
// It's possible to constrain to only one key with TS, but is overkill for this
|
|
|
|
// https://stackoverflow.com/a/60807986
|
|
|
|
export function putUpdateUserFlag(
|
|
|
|
update: Record<string, string>
|
|
|
|
): Promise<void> {
|
|
|
|
return put('/update-user-flag', update);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function putUserAcceptsTerms(quincyEmails: boolean): Promise<void> {
|
|
|
|
return put('/update-privacy-terms', { quincyEmails });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function putUserUpdateEmail(email: string): Promise<void> {
|
|
|
|
return put('/update-my-email', { email });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function putVerifyCert(certSlug: string): Promise<void> {
|
|
|
|
return put('/certificate/verify', { certSlug });
|
|
|
|
}
|