Guest User

Untitled

a guest
Sep 5th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. import axios from 'axios';
  2. import auth from './auth';
  3. import config from '../config';
  4. import { List } from 'immutable';
  5. import { lookupTeam, UserRecord } from '../lib/records';
  6. import Parse from './parse';
  7.  
  8. const api = axios.create({
  9. baseURL: config.API_BASE_URL,
  10. timeout: 10000,
  11. transformResponse: [
  12. function(data) {
  13. // API sends string instead of JSON?
  14. if (typeof data === 'string') {
  15. try {
  16. return JSON.parse(data);
  17. } catch (err) {
  18. return data;
  19. }
  20. } else {
  21. return data;
  22. }
  23. },
  24. ],
  25. });
  26.  
  27. const createAndRefreshSessions = (data) => {
  28. auth.refreshSessions(data); // not a promise
  29. return auth.createSession(data.sessionToken); // returns UserRecord;
  30. };
  31.  
  32. export const signup = data => {
  33. return api
  34. .post('/auth/session/signup', data)
  35. .then(({ data }) => auth.createSession(data.sessionToken));
  36. };
  37.  
  38. export const login = (email, password) => {
  39. return api
  40. .post('/auth/session/login', { email, password })
  41. .then(({ data }) => createAndRefreshSessions(data));
  42. };
  43.  
  44. export const loginBackground = (email, password) => {
  45. return api.post('/auth/session/login', { email, password })
  46. .then(({ data }) => data);
  47. };
  48.  
  49. export const loginTwitter = data => {
  50. return api
  51. .post('/web/auth/session/twitter', data)
  52. .then(({ data }) => createAndRefreshSessions(data));
  53. };
  54.  
  55. export const oauthTwitter = data => {
  56. return api
  57. .post('/web/auth/session/oauth-twitter', data)
  58. .then(resp => resp.data);
  59. };
  60.  
  61. export const getPlanInfo = data => {
  62. return api.get('/').then(resp => resp.data);
  63. };
  64.  
  65. export const fetchSubscriptionPlans = data => {
  66. return api.get('/').then(resp => []);
  67. };
  68.  
  69. export const requestResetPassword = data => {
  70. return api
  71. .post('/auth/session/reset-password', data)
  72. .then(resp => resp.data);
  73. };
  74.  
  75. export const resetPassword = data => {
  76. return api
  77. .post('/auth/session/set-password', data)
  78. .then(resp => resp.data);
  79. };
  80.  
  81.  
  82. const ParseInvitation = Parse.Object.extend('Invitation');
  83.  
  84. export async function invite(email, teamId) {
  85. const inv = new ParseInvitation();
  86. inv.setACL(new Parse.ACL(Parse.User.current()));
  87. inv.set('email', email.toLowerCase());
  88. inv.set('teamId', teamId);
  89. return inv.save(null);
  90. }
  91.  
  92. export async function uninvite(email) {
  93. const q = new Parse.Query(ParseInvitation);
  94. q.equalTo('email', email);
  95. const invitation = await q.first();
  96.  
  97. if (invitation) {
  98. return invitation.destroy();
  99. } else {
  100. return null;
  101. }
  102. }
  103.  
  104. export async function sendInvitationEmail(email) {
  105. return api
  106. .post('/auth/send-invite', { email })
  107. .then(resp => resp.data);
  108. }
  109.  
  110. export function openInvitations() {
  111. const q = new Parse.Query(ParseInvitation);
  112. return q.find().then(invs => invs.map(i => i.get('email')));
  113. }
  114.  
  115. async function fetchUserIfExists(userId) {
  116. let user = new Parse.User();
  117. user.id = userId;
  118. try {
  119. user = await user.fetch();
  120. return UserRecord.fromParseUser(user);
  121. } catch (e) {
  122. return null;
  123. }
  124. }
  125.  
  126. export async function listStaff(myId) {
  127. const team = await lookupTeam();
  128. if (!team) {
  129. return List();
  130. }
  131. const users = [];
  132. for (const userId in team.getACL().toJSON()) {
  133. users.push(fetchUserIfExists(userId));
  134. }
  135. const userRecords = await Promise.all(users);
  136. return List(userRecords).filterNot(u => u === null || u.objectId === myId);
  137. }
  138.  
  139. export async function listStaffAll() {
  140. const team = await lookupTeam();
  141. if(!team) {
  142. return List();
  143. }
  144. const users = [];
  145. if(team.getACL()){
  146. for (const userId in team.getACL().toJSON()) {
  147. users.push(fetchUserIfExists(userId));
  148. }
  149. }
  150. return await Promise.all(users);
  151. }
  152.  
  153. export async function deleteStaff(objectId) {
  154. return api
  155. .post('/auth/session/remove-user', {
  156. userId: objectId,
  157. sessionToken: Parse.User.current().getSessionToken(),
  158. })
  159. .then(resp => resp.data);
  160. }
  161.  
  162. export async function getReport(username) {
  163. try{
  164. return api.get(`/reports/get/${username}`,
  165. {params: { sessionToken: Parse.User.current().getSessionToken() }},
  166. { validateStatus: (code) => code <= 500 },
  167. ).then(({ data }) => data && typeof data === 'string' ? null : data);
  168. } catch(err){
  169. console.log('Error retieving Athlete Report', err);
  170. }
  171. }
  172.  
  173. export async function requestUpdateReport(twitter) {
  174. return api.post(`/reports/request-update`, {
  175. twitter,
  176. email: Parse.User.current().getEmail(),
  177. }, {params: { sessionToken: Parse.User.current().getSessionToken() }})
  178. .then(() => true).catch(() => false);
  179. }
  180.  
  181. export async function requestReport(twitter) {
  182. return api.post('/reports/request', {
  183. twitter,
  184. email: Parse.User.current().getEmail(),
  185. }, {params: { sessionToken: Parse.User.current().getSessionToken() }})
  186. .then(() => true).catch(() => false);
  187. }
Add Comment
Please, Sign In to add comment