cloudservices/src/class/Report.ts

146 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-10-03 21:17:12 -04:00
import type { AxiosError } from 'axios';
import axios from 'axios';
2020-10-06 22:53:48 -04:00
import { response } from 'express';
2020-10-03 21:17:12 -04:00
export interface SoftReport {
status: 'SUCCESS' | 'UNAUTHORIZED' | 'PERMISSION_DENIED' | 'CLIENT_ERROR' | 'SERVER_ERROR';
userId?: string;
2020-10-03 21:17:12 -04:00
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) {
2020-10-06 22:53:48 -04:00
try {
const { data } = await axios({
method: 'get',
url: `https://eds.libraryofcode.org/cs/t2?userId=${userId}&auth=${auth}`,
2020-10-06 22:53:48 -04:00
});
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[] }> {
2020-10-03 21:17:12 -04:00
try {
const { data } = await axios({
method: 'get',
url: `https://loc.sh/int/pin?id=${userId}&auth=${auth}`,
2020-10-03 21:17:12 -04:00
});
return {
status: 'SUCCESS',
2020-10-03 22:00:10 -04:00
pin: data.pin,
2020-10-03 21:17:12 -04:00
};
} 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.
2020-10-03 21:17:12 -04:00
* @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> {
2020-10-03 21:17:12 -04:00
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,
2020-10-03 21:17:12 -04:00
pin,
},
});
return {
status: 'SUCCESS',
userId: data.message.userId,
2020-10-03 21:17:12 -04:00
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.
2020-10-03 21:17:12 -04:00
* @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');
2020-10-03 21:17:12 -04:00
* ```
*/
public static async hard(userId: string, pin: number, reason: string, auth: string): Promise<HardReport> {
2020-10-03 21:17:12 -04:00
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,
2020-10-03 21:17:12 -04:00
pin,
reason,
},
});
return {
status: 'SUCCESS',
userId: data.message.userId,
2020-10-03 21:17:12 -04:00
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);
}
}
}