Updating credits to the last version

This commit is contained in:
Christian Torbidone 2024-08-06 20:21:34 +02:00
parent 1fc297ef02
commit 3049877132

View file

@ -1,4 +1,5 @@
import axios from "axios"; import axios from "axios";
import { validateStatus } from "./index.js";
export default class CreditsManager { export default class CreditsManager {
token: string | undefined; token: string | undefined;
@ -16,9 +17,9 @@ export default class CreditsManager {
* @return {Promise<number>} The balance of the user. * @return {Promise<number>} The balance of the user.
*/ */
async balance(id: string|number) { async balance(id: string|number) {
let req = await axios.get(`${this.URL}/credits/${id}`); let req = await axios.get(`${this.URL}/inav/${id}/credits`, { validateStatus });
if (req.status == 404) throw new Error("User not Found"); if (req.status == 404) throw new Error("User not Found");
return req.data.credits; return req.data.credits as number;
} }
/** /**
@ -28,50 +29,12 @@ export default class CreditsManager {
* @param {number} number - The new credit number. * @param {number} number - The new credit number.
*/ */
async set(id: string|number, number: number) { async set(id: string|number, number: number) {
let req = await axios.post(`${this.URL}/credits/${id}`, { number }, { headers: { Authorization: `Bearer ${this.token}` } }); let req = await axios.post(
`${this.URL}/inav/${id}/credits`,
{ number },
{ headers: { Authorization: `Bearer ${this.token}`}, validateStatus}
);
if (req.status == 403) throw new Error("No Permission"); if (req.status == 403) throw new Error("No Permission");
if (req.status == 404) throw new Error("User not Found"); if (req.status == 404) throw new Error("User not Found");
} }
/**
* Verifies the given transaction.
*
* @param {number} code - The code of the transaction to be verified.
* @return {void} - This function does not return anything.
*/
async verifyTransaction(code: number) {
let req = await axios.post(`${this.URL}/credits/verify/${code}`, {}, { headers: { Authorization: `Bearer ${this.token}` } });
if (req.status == 403) throw new Error("No Permission");
if (req.status == 404) throw new Error("Transaction not Found");
}
/**
* Retrieves a transaction by its code.
*
* @param {number} code - The code of the transaction.
* @return {Promise<object>} The transaction object.
*/
async getTransaction(code: number) {
let req = await axios.get(`${this.URL}/credits/verify/${code}`);
if (req.status == 404) throw new Error("Transaction not Found");
return req.data.transaction;
}
/**
* Sends a payment request to the specified user.
*
* @param {number} id - The ID of the user making the payment.
* @param {number} toPayID - The ID of the user to whom the payment is being made.
* @param {number} amount - The amount of the payment.
* @return {void}
*/
async pay(id: number, toPayID: number, amount: number) {
if (amount < 0) throw new Error("Invalid Amount");
let req = await axios.patch(`${this.URL}/credits/${id}`, { amount, to: toPayID }, { headers: { Authorization: `Bearer ${this.token}` } }).catch();
if (req.data.error == "TOO_MANY_CREDITS") throw new Error("Not Enought Credits");
if (req.data.error == "NO_NEGATIVE_AMOUNT") throw new Error("Invalid Amount");
else if (req.status == 403) throw new Error("No Permission");
if (req.status == 404) throw new Error("User not Found");
}
} }