Discord_API_Bot/helpers/api.js
2024-04-08 04:12:41 -06:00

141 lines
No EOL
3.2 KiB
JavaScript

const axios = require("axios");
const { api_url, api_token } = require("../config");
module.exports = {
getUser: async function (email) {
const headers = {
Authorization: `Bearer ${api_token}`
};
const options = {
method: "GET",
url: `${api_url}/users?filter[email]=${email}&include=orders`,
headers: headers
};
return new Promise((resolve, reject) => {
axios.request(options)
.then(response => {
resolve(response.data);
})
.catch(error => {
resolve(false);
});
});
},
getDiscordUser: async function (discordId) {
const headers = {
Authorization: `Bearer ${api_token}`
};
const options = {
method: "GET",
url: `${api_url}/oauth-connections?filter[data->id]=${discordId}`,
headers: headers
};
return new Promise((resolve, reject) => {
axios.request(options)
.then(response => {
resolve(response.data);
})
.catch(error => {
resolve(false);
});
});
},
getNewOauths: async function () {
const headers = {
Authorization: `Bearer ${api_token}`
};
const options = {
method: "GET",
url: `${api_url}/oauth-connections?date=3days`,
headers: headers
};
return new Promise((resolve, reject) => {
axios.request(options)
.then(response => {
resolve(response.data);
})
.catch(error => {
resolve(false);
});
});
},
getOauthUsers: async function () {
let page = 1;
let oauthUsers = [];
let hasMorePages = true;
while (hasMorePages) {
const data = await pagination(`oauth-connections?page=${page}`);
oauthUsers.push(...data.data);
if (!data.next_page_url) {
hasMorePages = false;
} else {
page = data.next_page_url.split("page=")[1];
}
}
return oauthUsers;
},
getActiveOrders: async function () {
let page = 1;
let activeOrders = [];
let hasMorePages = true;
while (hasMorePages) {
const data = await pagination(`orders?filter[status]=active&page=${page}`);
activeOrders.push(...data.data);
if (!data.next_page_url) {
hasMorePages = false;
} else {
page = data.next_page_url.split('page=')[1];
}
}
return activeOrders;
},
getAllUsers: async function () {
let page = 1;
let allUsers = [];
let hasMorePages = true;
while (hasMorePages) {
const data = await pagination(`users?page=${page}`);
allUsers.push(...data.data);
if (!data.next_page_url) {
hasMorePages = false;
} else {
page = data.next_page_url.split("page=")[1];
}
}
return allUsers;
}
};
async function pagination(endpoint) {
const headers = {
Authorization: `Bearer ${api_token}`
};
const options = {
method: "GET",
url: `${api_url}/${endpoint}`,
headers: headers
};
return new Promise((resolve, reject) => {
axios.request(options)
.then(async response => {
return resolve(response.data);
})
.catch(error => {
resolve(false);
});
});
}