Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios';
- import auth from './auth';
- import config from '../config';
- import { List } from 'immutable';
- import { lookupTeam, UserRecord } from '../lib/records';
- import Parse from './parse';
- const api = axios.create({
- baseURL: config.API_BASE_URL,
- timeout: 10000,
- transformResponse: [
- function(data) {
- // API sends string instead of JSON?
- if (typeof data === 'string') {
- try {
- return JSON.parse(data);
- } catch (err) {
- return data;
- }
- } else {
- return data;
- }
- },
- ],
- });
- const createAndRefreshSessions = (data) => {
- auth.refreshSessions(data); // not a promise
- return auth.createSession(data.sessionToken); // returns UserRecord;
- };
- export const signup = data => {
- return api
- .post('/auth/session/signup', data)
- .then(({ data }) => auth.createSession(data.sessionToken));
- };
- export const login = (email, password) => {
- return api
- .post('/auth/session/login', { email, password })
- .then(({ data }) => createAndRefreshSessions(data));
- };
- export const loginBackground = (email, password) => {
- return api.post('/auth/session/login', { email, password })
- .then(({ data }) => data);
- };
- export const loginTwitter = data => {
- return api
- .post('/web/auth/session/twitter', data)
- .then(({ data }) => createAndRefreshSessions(data));
- };
- export const oauthTwitter = data => {
- return api
- .post('/web/auth/session/oauth-twitter', data)
- .then(resp => resp.data);
- };
- export const getPlanInfo = data => {
- return api.get('/').then(resp => resp.data);
- };
- export const fetchSubscriptionPlans = data => {
- return api.get('/').then(resp => []);
- };
- export const requestResetPassword = data => {
- return api
- .post('/auth/session/reset-password', data)
- .then(resp => resp.data);
- };
- export const resetPassword = data => {
- return api
- .post('/auth/session/set-password', data)
- .then(resp => resp.data);
- };
- const ParseInvitation = Parse.Object.extend('Invitation');
- export async function invite(email, teamId) {
- const inv = new ParseInvitation();
- inv.setACL(new Parse.ACL(Parse.User.current()));
- inv.set('email', email.toLowerCase());
- inv.set('teamId', teamId);
- return inv.save(null);
- }
- export async function uninvite(email) {
- const q = new Parse.Query(ParseInvitation);
- q.equalTo('email', email);
- const invitation = await q.first();
- if (invitation) {
- return invitation.destroy();
- } else {
- return null;
- }
- }
- export async function sendInvitationEmail(email) {
- return api
- .post('/auth/send-invite', { email })
- .then(resp => resp.data);
- }
- export function openInvitations() {
- const q = new Parse.Query(ParseInvitation);
- return q.find().then(invs => invs.map(i => i.get('email')));
- }
- async function fetchUserIfExists(userId) {
- let user = new Parse.User();
- user.id = userId;
- try {
- user = await user.fetch();
- return UserRecord.fromParseUser(user);
- } catch (e) {
- return null;
- }
- }
- export async function listStaff(myId) {
- const team = await lookupTeam();
- if (!team) {
- return List();
- }
- const users = [];
- for (const userId in team.getACL().toJSON()) {
- users.push(fetchUserIfExists(userId));
- }
- const userRecords = await Promise.all(users);
- return List(userRecords).filterNot(u => u === null || u.objectId === myId);
- }
- export async function listStaffAll() {
- const team = await lookupTeam();
- if(!team) {
- return List();
- }
- const users = [];
- if(team.getACL()){
- for (const userId in team.getACL().toJSON()) {
- users.push(fetchUserIfExists(userId));
- }
- }
- return await Promise.all(users);
- }
- export async function deleteStaff(objectId) {
- return api
- .post('/auth/session/remove-user', {
- userId: objectId,
- sessionToken: Parse.User.current().getSessionToken(),
- })
- .then(resp => resp.data);
- }
- export async function getReport(username) {
- try{
- return api.get(`/reports/get/${username}`,
- {params: { sessionToken: Parse.User.current().getSessionToken() }},
- { validateStatus: (code) => code <= 500 },
- ).then(({ data }) => data && typeof data === 'string' ? null : data);
- } catch(err){
- console.log('Error retieving Athlete Report', err);
- }
- }
- export async function requestUpdateReport(twitter) {
- return api.post(`/reports/request-update`, {
- twitter,
- email: Parse.User.current().getEmail(),
- }, {params: { sessionToken: Parse.User.current().getSessionToken() }})
- .then(() => true).catch(() => false);
- }
- export async function requestReport(twitter) {
- return api.post('/reports/request', {
- twitter,
- email: Parse.User.current().getEmail(),
- }, {params: { sessionToken: Parse.User.current().getSessionToken() }})
- .then(() => true).catch(() => false);
- }
Add Comment
Please, Sign In to add comment