cloudservices/src/class/Report.ts

146 lines
5.4 KiB
TypeScript

import type { AxiosError } from 'axios';
import axios from 'axios';
import { response } from 'express';
export interface SoftReport {
status: 'SUCCESS' | 'UNAUTHORIZED' | 'PERMISSION_DENIED' | 'CLIENT_ERROR' | 'SERVER_ERROR';
userID?: string;
totalScore?: number;
}
export interface HardReport extends SoftReport {
activityScore?: number;
roleScore?: number;
moderationScore?: number;
cloudServicesScore?: number;
miscScore?: number;
otherScore?: number;
inquiries?: [ { name: string; date: Date }? ];
}
export default class Report {
public static async tier2(userID: string, auth: string) {
try {
const { data } = await axios({
method: 'get',
url: `https://eds.libraryofcode.org/cs/t2?userID=${userID}&auth=${auth}`,
});
return {
status: 'SUCCESS',
decision: data.decision,
};
} catch (err) {
const error = <AxiosError>err;
if (error.response?.status === 404 || error.response.status === 400 || error.response.status === 401) return { status: 'CLIENT_ERROR', decision: 'PRE-DECLINED' };
return { status: 'SERVER_ERROR', decision: 'PRE-DECLINED' };
}
}
public static async getPIN(userID: string, auth: string): Promise<{ status: 'SUCCESS' | 'UNAUTHORIZED' | 'PERMISSION_DENIED' | 'CLIENT_ERROR' | 'SERVER_ERROR'; pin?: number[] }> {
try {
const { data } = await axios({
method: 'get',
url: `https://loc.sh/int/pin?id=${userID}&auth=${auth}`,
});
return {
status: 'SUCCESS',
pin: data.pin,
};
} catch (err) {
const error = <AxiosError>err;
if (error.response?.status === 400) return { status: 'CLIENT_ERROR' };
if (error.response?.status === 401) return { status: 'UNAUTHORIZED' };
if (error.response?.status === 403) return { status: 'PERMISSION_DENIED' };
if ((typeof error.response?.status === 'number') && error.response?.status >= 500) return { status: 'SERVER_ERROR' };
throw new Error(err);
}
}
/**
* Requests a Soft Inquiry from Library of Code sp-us Community Relations.
* @author Matthew R <matthew@staff.libraryofcode.org>
* @param userID The user's ID, they must be in the server. Reports usually aren't generated for new users until the next recalculation.
* @param pin The last 4 digits of the member's PIN number.
* ```ts
* Report.soft('253600545972027394', 1102);
* ```
*/
public static async soft(userID: string, pin: number, auth: string): Promise<SoftReport> {
try {
if (pin < 4) throw new RangeError('PIN cannot be less than 4.');
const { data } = await axios({
method: 'post',
url: 'https://comm.libraryofcode.org/report/soft',
headers: { Authorization: auth },
data: {
userID,
pin,
},
});
return {
status: 'SUCCESS',
userID: data.message.userID,
totalScore: data.message.totalScore,
};
} catch (err) {
const error = <AxiosError>err;
if (error.response?.status === 400) return { status: 'CLIENT_ERROR' };
if (error.response?.status === 401) return { status: 'UNAUTHORIZED' };
if (error.response?.status === 403) return { status: 'PERMISSION_DENIED' };
if ((typeof error.response?.status === 'number') && error.response?.status >= 500) return { status: 'SERVER_ERROR' };
throw new Error(err);
}
}
/**
* Requests a Hard Inquiry from Library of Code sp-us Community Relations.
* - Members who elected to be notified for hard pulls will receive a notification if your request is successful.
* - If the member's Community Report is locked, `HardReport.status` will equal `PERMISSION_DENIED`.
* @author Matthew R <matthew@staff.libraryofcode.org>
* @param userID The user's ID, they must be in the server. Reports usually aren't generated for new users until the next recalculation.
* @param pin The last 4 digits of the member's PIN number.
* @param reason A reason for the hard inquiry.
* ```ts
* Report.hard('253600545972027394', 1102, 'Verification and Eligibility for Personal Account');
* ```
*/
public static async hard(userID: string, pin: number, reason: string, auth: string): Promise<HardReport> {
try {
if (pin < 4) throw new RangeError('PIN cannot be less than 4.');
const { data } = await axios({
method: 'post',
url: 'https://comm.libraryofcode.org/report/hard',
headers: { Authorization: auth },
data: {
userID,
pin,
reason,
},
});
return {
status: 'SUCCESS',
userID: data.message.userID,
totalScore: data.message.totalScore,
activityScore: data.message.activityScore,
roleScore: data.message.rolesScore,
moderationScore: data.message.moderationScore,
cloudServicesScore: data.message.cloudServicesScore,
miscScore: data.message.miscScore,
otherScore: data.message.otherScore,
inquiries: data.message.inquiries,
};
} catch (err) {
const error = <AxiosError>err;
if (error.response?.status === 400) return { status: 'CLIENT_ERROR' };
if (error.response?.status === 401) return { status: 'UNAUTHORIZED' };
if (error.response?.status === 403) return { status: 'PERMISSION_DENIED' };
if ((typeof error.response?.status === 'number') && error.response?.status >= 500) return { status: 'SERVER_ERROR' };
throw new Error(err);
}
}
}