Source

vendors/AWS/tmaApi.js

  1. // File imports
  2. import axios from "axios";
  3. /**
  4. * The server API for the app
  5. * @module tmaApi
  6. * @category Server
  7. * @author Dan Levy <danlevy124@gmail.com>
  8. * @author Daniel Griessler <dgriessler20@gmail.com>
  9. */
  10. // Sets the base URL for the server
  11. axios.defaults.baseURL = process.env.REACT_APP_SERVER_BASE_URL;
  12. /**
  13. * Sets the Axios auth token.
  14. * The auth token only needs to be set when auth changes.
  15. * @function
  16. * @param {string} authToken - The auth token to use for API requests
  17. */
  18. export const setAxiosAuthToken = (authToken) => {
  19. axios.defaults.headers.common["Authorization"] = authToken;
  20. };
  21. /**
  22. * A server error
  23. * @typedef {object} ServerError
  24. * @property {object} response - The error response
  25. * @property {string} response.status - The error status code
  26. * @property {object} response.data - The error data (usually just a string)
  27. */
  28. /**
  29. * A user from the server
  30. * @typedef {object} User
  31. * @property {object} data - The response data
  32. * @property {string} data.firstName - The user's first name
  33. * @property {string} data.lastName - The user's last name
  34. * @property {boolean} data.hasPicture - Indicates if the user has a profile picture
  35. */
  36. /**
  37. * Gets the user
  38. * @function
  39. * @returns {Promise<module:tmaApi~User|module:tmaApi~ServerError>} A promise containing the user or an error
  40. */
  41. export const getUser = () => {
  42. return axios.get("/person");
  43. };
  44. /**
  45. * Adds a user to the database
  46. * @function
  47. * @param {module:tmaApi~User} data - User data
  48. * @returns {Promise<null|ServerError>} A promise containing nothing on success or an error
  49. */
  50. export const addUser = (data) => {
  51. return axios.post("/person", data);
  52. };
  53. /**
  54. * Updates a user in the database
  55. * @function
  56. * @param {module:tmaApi~User} data - User data
  57. * @returns {Promise<null|module:tmaApi~ServerError>} A promise containing nothing on success or an error
  58. */
  59. export const updateUser = (data) => {
  60. return axios.put("/person", data);
  61. };
  62. /**
  63. * The choir access code
  64. * @typedef {object} ChoirAccess
  65. * @property {string} accessCode - The access code of the generated choir
  66. */
  67. /**
  68. * Adds a choir to the database
  69. * @function
  70. * @param {object} data - Choir data
  71. * @param {string} data.choirName - The name of the choir
  72. * @param {string} data.description - A description of the choir
  73. * @param {string} data.memberType - The member type of the member creating the choir
  74. * @param {string} data.memberRole - The member role of the member creating the choir
  75. * @returns {Promise<module:tmaApi~ChoirAccess|module:tmaApi~ServerError>} A promise containing the access code of the generated choir or an error
  76. */
  77. export const addChoir = (data) => {
  78. return axios.post("/choir", data);
  79. };
  80. /**
  81. * A list of choirs
  82. * @typedef {object} ChoirList
  83. * @property {object} data - Choir list data
  84. * @property {module:tmaApi~Choir[]} data.choirs - Choir list
  85. */
  86. /**
  87. * A choir
  88. * @typedef {object} Choir
  89. * @property {string} choir_id - The ID of the choir
  90. * @property {string} choir_name - The name of the choir
  91. * @property {string} description - A description of the choir
  92. */
  93. /**
  94. * Gets the choirs that the current user is a part of
  95. * @function
  96. * @returns {Promise<module:tmaApi~ChoirList|module:tmaApi~ServerError>} A promise containing choir information or an error
  97. */
  98. export const getUsersChoirs = () => {
  99. return axios.get("/choir");
  100. };
  101. /**
  102. * A list of choir members
  103. * @typedef {object} MemberList
  104. * @property {module:tmaApi~ChoirMember[]} data - Choir member list
  105. */
  106. /**
  107. * A choir member
  108. * @typedef {object} ChoirMember
  109. * @property {string} first_name The first name of the member
  110. * @property {string} last_name The last name of the member
  111. * @property {string} member_role The role of the member
  112. * @property {string} person_id The ID of the person attached to the member
  113. * @property {boolean} has_picture Whether the given member is attached to a person with a profile picture or not
  114. */
  115. /**
  116. * Gets the given choir members
  117. * @function
  118. * @param {object} data - Choir data
  119. * @param {string} data.choirId - The ID of the choir to receive members of
  120. * @returns {Promise<module:tmaApi~MemberList|module:tmaApi~ServerError>} - A promise containing members or an error
  121. */
  122. export const getChoirMembers = (data) => {
  123. return axios.request({
  124. method: "GET",
  125. url: "/choir/members",
  126. params: data,
  127. });
  128. };
  129. /**
  130. * Accepts a given choir member into the given choir
  131. * @function
  132. * @param {object} data - Member data
  133. * @param {object} data.memberId - The ID of the member to accept
  134. * @param {string} data.choirId - The choir ID of the choir that the user should be accepted into
  135. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  136. */
  137. export const acceptChoirMember = (data) => {
  138. return axios.put("/member/accept", data);
  139. };
  140. /**
  141. * Rejects a given choir member from entering the given choir
  142. * @function
  143. * @param {object} data - Member data
  144. * @param {object} data.memberId - The ID of the member to reject
  145. * @param {string} data.choirId - The choir ID of the choir that the user should be rejected from
  146. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  147. */
  148. export const rejectChoirMember = (data) => {
  149. return axios.put("/member/reject", data);
  150. };
  151. /**
  152. * A list of choir members.
  153. * This array contains more member data than the {@link module:tmaApi~MemberList}.
  154. * @typedef MemberListExtended
  155. * @property {module:tmaApi~ChoirMemberExtended[]} data - Choir member list
  156. */
  157. /**
  158. * A choir member.
  159. * This object contains more member data than the {@link module:tmaApi~ChoirMember}.
  160. * @typedef {object} ChoirMemberExtended
  161. * @property {string} member_id - The ID of the member
  162. * @property {string} first_name - The first name of the member
  163. * @property {string} email - The email of the member
  164. * @property {string} member_type - The type of the member
  165. * @property {string} member_role - The role of the member
  166. * @property {boolean} has_picture - Indicates if the member has a profile picture
  167. */
  168. /**
  169. * Gets pending members of the given choir
  170. * @function
  171. * @param {object} data - Choir data
  172. * @param {string} data.choirId - The ID of the choir to get pending members from
  173. * @returns {Promise<module:tmaApi~MemberListExtended|module:tmaApi~ServerError>} - A promise containing pending members or an error
  174. */
  175. export const getPendingMembers = (data) => {
  176. return axios.request({
  177. method: "GET",
  178. url: "/member/pending",
  179. params: data,
  180. });
  181. };
  182. /**
  183. * @typedef {object} GetsFeedback
  184. * @property {object} data - Response data
  185. * @property {boolean} gets_feedback Indicates if the user gets feedback (i.e. real-time feedback and performance data)
  186. */
  187. /**
  188. * Gets if the user recieves feedback
  189. * @function
  190. * @param {Object} data - Sheet music data
  191. * @param {string} data.sheetMusicId - The id of the sheet music the user is singing
  192. * @returns {Promise<module:tmaApi~GetsFeedback|module:tmaApi~ServerError>} - A promise containing whether the user gets feedback or an error
  193. */
  194. export const doesUserGetFeedback = (data) => {
  195. return axios.request({
  196. method: "GET",
  197. url: `/member/gets-feedback`,
  198. params: data,
  199. });
  200. };
  201. /**
  202. * Adds the user as a pending member of the given choir
  203. * @function
  204. * @param {object} data - Member and choir data
  205. * @param {string} data.memberType - The member type that you are attempting to join as
  206. * @param {string} data.memberRole - The member role that you are attempting to join as
  207. * @param {string} data.accessCode - The access code for the choir you are attempting to join
  208. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  209. */
  210. export const joinChoir = (data) => {
  211. return axios.post("/member", data);
  212. };
  213. /**
  214. * @typedef {object} SheetMusicInfoList
  215. * @property data - Sheet music data
  216. * @property {module:tmaApi~SheetMusicInfo[]} sheet_music - An array of sheet music info
  217. */
  218. /**
  219. * @typedef {object} SheetMusicInfo
  220. * @property {string} sheet_music_id - The ID of the sheet music
  221. * @property {string} title - The title of the sheet music
  222. * @property {string} composer_names - The names of the composers
  223. */
  224. /**
  225. * Gets all sheet music for a choir
  226. * @function
  227. * @param data - Choir data
  228. * @param {string} data.choirId - The id of the choir to get sheet music info for
  229. * @returns {Promise<module:tmaApi~SheetMusicInfoList|module:tmaApi~ServerError>} - A promise containing sheet music info or an error
  230. */
  231. export const getSheetMusic = (data) => {
  232. return axios.request({
  233. method: "GET",
  234. url: `/sheet-music`,
  235. params: data,
  236. });
  237. };
  238. /**
  239. * @typedef {object} SheetMusic
  240. * @property {object} data - Sheet music data
  241. * @property {string} data.sheet_music - The AlphaTex of the sheet music
  242. * @property {string[]} data.part_list - A list of the part (i.e. track) names
  243. * @property {string[]} data.clefs - The clefs per staff
  244. * @property {string} data.part - If not null, then the part of the current user in the sheet music
  245. */
  246. /**
  247. * Gets a specific piece of sheet music
  248. * @function
  249. * @param {object} data
  250. * @param {string} data.sheetMusicId - The sheet music id to retrieve
  251. * @returns {Promise<module:tmaApi~SheetMusic|module:tmaApi~ServerError>} - A promise containing sheet music information or an error
  252. */
  253. export const getSpecificSheetMusic = (data) => {
  254. return axios.request({
  255. method: "GET",
  256. url: "/sheet-music/specific",
  257. params: data,
  258. });
  259. };
  260. /**
  261. * @typedef {object} SheetMusicPart
  262. * @property {object} data - Part data
  263. * @property {number[]} data.performance_expectation - A 1D array of even size with the ith index as the midi value and the i+1 index as the duration of that note for i%2==0
  264. * @property {number[]} data.lower_upper - A 1D two valued array with the lower and upper midi values
  265. * @property {number[]} data.measure_lengths - A collection of the lengths of each Measure. The ith index contains the length in seconds of the i+1 Measure
  266. */
  267. /**
  268. * Gets a specific part from a specific piece of sheet music
  269. * @function
  270. * @param {object} data - Music data
  271. * @param {string} data.sheetMusicId - The sheet music id to retrieve
  272. * @param {string} data.partName - The name of the part to be retrieved
  273. * @returns {Promise<module:tmaApi~SheetMusicPart|module:tmaApi~ServerError>} - A promise containing part information or an error
  274. */
  275. export const getPartSheetMusic = (data) => {
  276. return axios.request({
  277. method: "GET",
  278. url: "/sheet-music/part",
  279. params: data,
  280. });
  281. };
  282. /**
  283. * @typedef {object} MemberPostNewNoAnalysisPackage
  284. * @property {string} performance_id The ID of the newly generated performance data
  285. */
  286. /**
  287. * Initalizes a performance for the current user for the provided sheet music
  288. * @function
  289. * @param {Object} data - Music data
  290. * @param {string} data.performanceData - The performance data to be added
  291. * @param {string} data.sheetMusicId - The sheet music id to add the performance to, also used to authenticate user so this is required
  292. * @param {string} data.exerciseId - If not null then the performance will be attached to this exercise otherwise attached to sheet music
  293. * @param {Boolean} data.isDurationExercise - If exercise, specify if it is a duration exercise
  294. * @param {number} data.measureStart - Start of performance
  295. * @param {number} data.measureEnd - End of performance
  296. * @returns {Promise<module:tmaApi~MemberPostNewNoAnalysisPackage|module:tmaApi~ServerError>} - A promise containing the performance id or an error
  297. */
  298. export const initializeRunningPerformance = (data) => {
  299. return axios.post("/performance/new/no-analysis", data);
  300. };
  301. /**
  302. * Adds a new performance for the current user for the provided sheet music and analyzes it immediately
  303. * @function
  304. * @param {Object} data
  305. * @param {string} data.performanceData - The performance data to be added
  306. * @param {string} data.sheetMusicId - The sheet music id to add the performance to, also used to authenticate user so this is required
  307. * @param {string} data.exerciseId - If not null then the performance will be attached to this exercise otherwise attached to sheet music
  308. * @param {Boolean} data.isDurationExercise - If exercise, specify if it is a duration exercise
  309. * @param {number} data.measureStart - Start of performance
  310. * @param {number} data.measureEnd - End of performance
  311. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  312. */
  313. export const addPerformance = (data) => {
  314. return axios.post("/performance/new/analysis", data);
  315. };
  316. /**
  317. * Updates a stored performance with additional values
  318. * @function
  319. * @param {Object} data
  320. * @param {string} data.performanceData - The performance data to be added
  321. * @param {string} data.performanceId - The performance id to update
  322. * @param {string} data.sheetMusicId - The sheet music id for the performance
  323. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  324. */
  325. export const updateRunningPerformance = (data) => {
  326. return axios.put("/performance/no-analysis", data);
  327. };
  328. /**
  329. * Closes out a running performance with the most recent data and asks to analyze it
  330. * @function
  331. * @param {Object} data
  332. * @param {string} data.performanceData - The performance data to be added
  333. * @param {string} data.performanceId - The performance id to update
  334. * @param {string} data.sheetMusicId - The sheet music id for the performance
  335. * @param {string} data.exerciseId - If not null then the performance will be attached to this exercise otherwise attached to sheet music
  336. * @param {Boolean} data.isDurationExercise - If exercise, specify if it is a duration exercise
  337. * @param {number} data.measureStart - Start of performance
  338. * @param {number} data.measureEnd - End of performance
  339. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  340. */
  341. export const closeRunningPerformance = (data) => {
  342. return axios.put("/performance/analysis", data);
  343. };
  344. /**
  345. * Updates a choir member
  346. * Must be an admin of the choir to update a member
  347. * @function
  348. * @param {object} data
  349. * @param {string} data.memberId
  350. * @param {string} data.memberType
  351. * @param {string} data.memberRole
  352. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  353. */
  354. export const constUpdateMember = (data) => {
  355. return axios.put("/member/update", data);
  356. };
  357. /**
  358. * Deletes a choir member
  359. * Must be an admin of the choir to update a member
  360. * @function
  361. * @param {object} data
  362. * @param {string} data.memberId
  363. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  364. */
  365. export const deleteMember = (data) => {
  366. return axios.delete("/member", data);
  367. };
  368. /**
  369. * Gets all performances for the user for a given piece of sheet music
  370. * @function
  371. * @param {object} data
  372. * @param {string} data.sheetMusicId
  373. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  374. */
  375. export const getUsersPerformancesForSheetMusic = (data) => {
  376. return axios.request({
  377. method: "GET",
  378. url: "/performance/all",
  379. params: data,
  380. });
  381. };
  382. /**
  383. * @typedef {object} ExerciseGetPackage
  384. * @property {string} sheet_music The Tex for the exercise
  385. * @property {string[]} part_list A list of the part names of the AlphaTex
  386. * @property {string[]} clefs A list of clefs per staff
  387. * @property {number[]} performance_expectation A 1D array of even size with the ith index as the midi value and the i+1 index as the duration of that note for i%2==0
  388. * @property {number[]} lower_upper A 1D two valued array with the lower and upper midi values
  389. * @property {number[]} measure_lengths A collection of the lengths of each Measure. The ith index contains the length in seconds of the i+1 Measure
  390. * @property {string} part The name of the main exercise to be rendered
  391. */
  392. /**
  393. * Gets the alphaTex for an exercise
  394. * @function
  395. * @param {Object} data
  396. * @param {string} data.sheetMusicId - The sheet music id from which to generate the exercise
  397. * @param {number} data.trackNumber - The track number to access (note: this is +1 more than the track index for any array)
  398. * @param {number} data.staffNumber - The staff number to access (note: this is +1 more than the staff index for any array)
  399. * @param {number} data.measureStart - The measure number to start with
  400. * @param {number} data.measureEnd - The measure number to end with
  401. * @param {Boolean} data.isDurationExercise - If true, generates a duration exercise otherwise just a normal exercise
  402. * @returns {Promise<module:tmaApi~ExerciseGetPackage|module:tmaApi~ServerError>} - A promise containing information about the exercise or an error
  403. */
  404. export const getExercise = (data) => {
  405. return axios.request({
  406. method: "GET",
  407. url: `/exercise`,
  408. params: data,
  409. });
  410. };
  411. /**
  412. * @typedef {object} SheetMusicPartGetPackage
  413. * @property {string} sheet_music The created AlphaTex isolating the user's part
  414. * @property {string[]} part_list A list of part (i.e. track) names in the generated AlphaTex
  415. * @property {string[]} clefs Clefs per staff
  416. * @property {number[]} performance_expectation A 1D array of even size with the ith index as the midi value and the i+1 index as the duration of that note for i%2==0
  417. * @property {number[]} lower_upper A 1D two valued array with the lower and upper midi values
  418. * @property {number[]} measure_lengths A collection of the lengths of each Measure. The ith index contains the length in seconds of the i+1 Measure
  419. * @property {string} exerciseId The ID of the exercise created
  420. */
  421. /**
  422. * Gets the sheet music and performance data for the user's specific part
  423. * @function
  424. * @param {Object} data
  425. * @param {string} data.sheetMusicId - The sheet music id to retrieve the part from
  426. * @returns {Promise<module:tmaApi~SheetMusicPartGetPackage|module:tmaApi~ServerError>} - A promise containing information the sheet music or an error
  427. */
  428. export const getSinglePartSheetMusic = (data) => {
  429. return axios.request({
  430. method: "GET",
  431. url: `/sheet-music-part`,
  432. params: data,
  433. });
  434. };
  435. /**
  436. * Adds selected part to sheet music for given member receiving nothing
  437. * @function
  438. * @param {Object} data
  439. * @param {string} data.sheetMusicId - The sheet music id to which the part for the member is being added
  440. * @param {string} data.part - The part from the sheet music that the member is selecting
  441. * @param {string} data.memberId - The member who is selecting a part
  442. * @returns {Promise<null|module:tmaApi~ServerError>} - A promise containing nothing on success or an error
  443. */
  444. export const pickPartInSheetMusic = (data) => {
  445. return axios.post("/sheet-music-part", data);
  446. };
  447. /**
  448. * Gets performance progress of member for the given piece of sheet music
  449. * @function
  450. * @param {Object} data
  451. * @param {string} data.sheetMusicId - The sheet music id to which the part for the member is being added
  452. */
  453. export const getPerformanceProgress = (data) => {
  454. return axios.request({
  455. method: "GET",
  456. url: `/performance-progress`,
  457. params: data,
  458. });
  459. };