From 3049877132191876017f72fc81424f84d26bf675 Mon Sep 17 00:00:00 2001 From: Christian Torbidone Date: Tue, 6 Aug 2024 20:21:34 +0200 Subject: [PATCH] Updating credits to the last version --- src/CreditsManager.ts | 53 +++++++------------------------------------ 1 file changed, 8 insertions(+), 45 deletions(-) diff --git a/src/CreditsManager.ts b/src/CreditsManager.ts index c00e95d..fe2fa1c 100644 --- a/src/CreditsManager.ts +++ b/src/CreditsManager.ts @@ -1,4 +1,5 @@ import axios from "axios"; +import { validateStatus } from "./index.js"; export default class CreditsManager { token: string | undefined; @@ -16,9 +17,9 @@ export default class CreditsManager { * @return {Promise} The balance of the user. */ 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"); - 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. */ 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 == 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} 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"); - } } \ No newline at end of file