utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import gql from "graphql-tag"
  2. import { BehaviorSubject } from "rxjs"
  3. /**
  4. * Returns an observable list of team members in the given Team
  5. *
  6. * @param {ApolloClient<any>} apollo - Instance of ApolloClient
  7. * @param {string} teamID - ID of the team to observe
  8. *
  9. * @returns {{user: {uid: string, email: string}, role: 'OWNER' | 'EDITOR' | 'VIEWER'}}
  10. */
  11. export async function getLiveTeamMembersList(apollo, teamID) {
  12. const subject = new BehaviorSubject([])
  13. const { data } = await apollo.query({
  14. query: gql`
  15. query GetTeamMembers($teamID: String!) {
  16. team(teamID: $teamID) {
  17. members {
  18. user {
  19. uid
  20. email
  21. }
  22. role
  23. }
  24. }
  25. }
  26. `,
  27. variables: {
  28. teamID,
  29. },
  30. })
  31. subject.next(data.team.members)
  32. const addedSub = apollo
  33. .subscribe({
  34. query: gql`
  35. subscription TeamMemberAdded($teamID: String!) {
  36. teamMemberAdded(teamID: $teamID) {
  37. user {
  38. uid
  39. email
  40. }
  41. role
  42. }
  43. }
  44. `,
  45. variables: {
  46. teamID,
  47. },
  48. })
  49. .subscribe(({ data }) => {
  50. subject.next([...subject.value, data.teamMemberAdded])
  51. })
  52. const updateSub = apollo
  53. .subscribe({
  54. query: gql`
  55. subscription TeamMemberUpdated($teamID: String!) {
  56. teamMemberUpdated(teamID: $teamID) {
  57. user {
  58. uid
  59. email
  60. }
  61. role
  62. }
  63. }
  64. `,
  65. variables: {
  66. teamID,
  67. },
  68. })
  69. .subscribe(({ data }) => {
  70. const val = subject.value.find(
  71. (member) => member.user.uid === data.teamMemberUpdated.user.uid
  72. )
  73. if (!val) return
  74. Object.assign(val, data.teamMemberUpdated)
  75. })
  76. const removeSub = apollo
  77. .subscribe({
  78. query: gql`
  79. subscription TeamMemberRemoved($teamID: String!) {
  80. teamMemberRemoved(teamID: $teamID)
  81. }
  82. `,
  83. variables: {
  84. teamID,
  85. },
  86. })
  87. .subscribe(({ data }) => {
  88. subject.next(
  89. subject.value.filter(
  90. (member) => member.user.uid !== data.teamMemberAdded.user.uid
  91. )
  92. )
  93. })
  94. const mainSub = subject.subscribe({
  95. complete() {
  96. addedSub.unsubscribe()
  97. updateSub.unsubscribe()
  98. removeSub.unsubscribe()
  99. mainSub.unsubscribe()
  100. },
  101. })
  102. return subject
  103. }
  104. export function createTeam(apollo, name) {
  105. return apollo.mutate({
  106. mutation: gql`
  107. mutation ($name: String!) {
  108. createTeam(name: $name) {
  109. name
  110. }
  111. }
  112. `,
  113. variables: {
  114. name,
  115. },
  116. })
  117. }
  118. export function addTeamMemberByEmail(apollo, userRole, userEmail, teamID) {
  119. return apollo.mutate({
  120. mutation: gql`
  121. mutation addTeamMemberByEmail(
  122. $userRole: TeamMemberRole!
  123. $userEmail: String!
  124. $teamID: String!
  125. ) {
  126. addTeamMemberByEmail(
  127. userRole: $userRole
  128. userEmail: $userEmail
  129. teamID: $teamID
  130. ) {
  131. role
  132. }
  133. }
  134. `,
  135. variables: {
  136. userRole,
  137. userEmail,
  138. teamID,
  139. },
  140. })
  141. }
  142. export function updateTeamMemberRole(apollo, userID, newRole, teamID) {
  143. return apollo.mutate({
  144. mutation: gql`
  145. mutation updateTeamMemberRole(
  146. $newRole: TeamMemberRole!
  147. $userUid: String!
  148. $teamID: String!
  149. ) {
  150. updateTeamMemberRole(
  151. newRole: $newRole
  152. userUid: $userUid
  153. teamID: $teamID
  154. ) {
  155. role
  156. }
  157. }
  158. `,
  159. variables: {
  160. newRole,
  161. userUid: userID,
  162. teamID,
  163. },
  164. })
  165. }
  166. export function renameTeam(apollo, name, teamID) {
  167. return apollo.mutate({
  168. mutation: gql`
  169. mutation renameTeam($newName: String!, $teamID: String!) {
  170. renameTeam(newName: $newName, teamID: $teamID) {
  171. id
  172. }
  173. }
  174. `,
  175. variables: {
  176. newName: name,
  177. teamID,
  178. },
  179. })
  180. }
  181. export function removeTeamMember(apollo, userID, teamID) {
  182. return apollo.mutate({
  183. mutation: gql`
  184. mutation removeTeamMember($userUid: String!, $teamID: String!) {
  185. removeTeamMember(userUid: $userUid, teamID: $teamID)
  186. }
  187. `,
  188. variables: {
  189. userUid: userID,
  190. teamID,
  191. },
  192. })
  193. }
  194. export async function deleteTeam(apollo, teamID) {
  195. let response
  196. while (true) {
  197. response = await apollo.mutate({
  198. mutation: gql`
  199. mutation ($teamID: String!) {
  200. deleteTeam(teamID: $teamID)
  201. }
  202. `,
  203. variables: {
  204. teamID,
  205. },
  206. })
  207. if (response !== undefined) break
  208. }
  209. return response
  210. }
  211. export function exitTeam(apollo, teamID) {
  212. return apollo.mutate({
  213. mutation: gql`
  214. mutation ($teamID: String!) {
  215. leaveTeam(teamID: $teamID)
  216. }
  217. `,
  218. variables: {
  219. teamID,
  220. },
  221. })
  222. }
  223. export async function rootCollectionsOfTeam(apollo, teamID) {
  224. const collections = []
  225. let cursor = ""
  226. while (true) {
  227. const response = await apollo.query({
  228. query: gql`
  229. query rootCollectionsOfTeam($teamID: String!, $cursor: String!) {
  230. rootCollectionsOfTeam(teamID: $teamID, cursor: $cursor) {
  231. id
  232. title
  233. }
  234. }
  235. `,
  236. variables: {
  237. teamID,
  238. cursor,
  239. },
  240. fetchPolicy: "no-cache",
  241. })
  242. if (response.data.rootCollectionsOfTeam.length === 0) break
  243. response.data.rootCollectionsOfTeam.forEach((collection) => {
  244. collections.push(collection)
  245. })
  246. cursor = collections[collections.length - 1].id
  247. }
  248. return collections
  249. }
  250. export async function getCollectionChildren(apollo, collectionID) {
  251. const children = []
  252. const response = await apollo.query({
  253. query: gql`
  254. query getCollectionChildren($collectionID: String!) {
  255. collection(collectionID: $collectionID) {
  256. children {
  257. id
  258. title
  259. }
  260. }
  261. }
  262. `,
  263. variables: {
  264. collectionID,
  265. },
  266. fetchPolicy: "no-cache",
  267. })
  268. response.data.collection.children.forEach((child) => {
  269. children.push(child)
  270. })
  271. return children
  272. }
  273. export async function getCollectionRequests(apollo, collectionID) {
  274. const requests = []
  275. let cursor = ""
  276. while (true) {
  277. const response = await apollo.query({
  278. query: gql`
  279. query getCollectionRequests($collectionID: String!, $cursor: String) {
  280. requestsInCollection(collectionID: $collectionID, cursor: $cursor) {
  281. id
  282. title
  283. request
  284. }
  285. }
  286. `,
  287. variables: {
  288. collectionID,
  289. cursor,
  290. },
  291. fetchPolicy: "no-cache",
  292. })
  293. response.data.requestsInCollection.forEach((request) => {
  294. requests.push(request)
  295. })
  296. if (response.data.requestsInCollection.length < 10) {
  297. break
  298. }
  299. cursor = requests[requests.length - 1].id
  300. }
  301. return requests
  302. }
  303. export async function renameCollection(apollo, title, id) {
  304. let response
  305. while (true) {
  306. response = await apollo.mutate({
  307. mutation: gql`
  308. mutation ($newTitle: String!, $collectionID: String!) {
  309. renameCollection(newTitle: $newTitle, collectionID: $collectionID) {
  310. id
  311. }
  312. }
  313. `,
  314. variables: {
  315. newTitle: title,
  316. collectionID: id,
  317. },
  318. })
  319. if (response !== undefined) break
  320. }
  321. return response
  322. }
  323. export async function updateRequest(apollo, request, requestName, requestID) {
  324. let response
  325. while (true) {
  326. response = await apollo.mutate({
  327. mutation: gql`
  328. mutation ($data: UpdateTeamRequestInput!, $requestID: String!) {
  329. updateRequest(data: $data, requestID: $requestID) {
  330. id
  331. }
  332. }
  333. `,
  334. variables: {
  335. data: {
  336. request: JSON.stringify(request),
  337. title: requestName,
  338. },
  339. requestID,
  340. },
  341. })
  342. if (response !== undefined) break
  343. }
  344. return response
  345. }
  346. export async function addChildCollection(apollo, title, id) {
  347. let response
  348. while (true) {
  349. response = await apollo.mutate({
  350. mutation: gql`
  351. mutation ($childTitle: String!, $collectionID: String!) {
  352. createChildCollection(
  353. childTitle: $childTitle
  354. collectionID: $collectionID
  355. ) {
  356. id
  357. }
  358. }
  359. `,
  360. variables: {
  361. childTitle: title,
  362. collectionID: id,
  363. },
  364. })
  365. if (response !== undefined) break
  366. }
  367. return response
  368. }
  369. export async function deleteCollection(apollo, id) {
  370. let response
  371. while (true) {
  372. response = await apollo.mutate({
  373. mutation: gql`
  374. mutation ($collectionID: String!) {
  375. deleteCollection(collectionID: $collectionID)
  376. }
  377. `,
  378. variables: {
  379. collectionID: id,
  380. },
  381. })
  382. if (response !== undefined) break
  383. }
  384. return response
  385. }
  386. export async function deleteRequest(apollo, requestID) {
  387. let response
  388. while (true) {
  389. response = await apollo.mutate({
  390. mutation: gql`
  391. mutation ($requestID: String!) {
  392. deleteRequest(requestID: $requestID)
  393. }
  394. `,
  395. variables: {
  396. requestID,
  397. },
  398. })
  399. if (response !== undefined) break
  400. }
  401. return response
  402. }
  403. export async function createNewRootCollection(apollo, title, id) {
  404. let response
  405. while (true) {
  406. response = await apollo.mutate({
  407. mutation: gql`
  408. mutation ($title: String!, $teamID: String!) {
  409. createRootCollection(title: $title, teamID: $teamID) {
  410. id
  411. }
  412. }
  413. `,
  414. variables: {
  415. title,
  416. teamID: id,
  417. },
  418. })
  419. if (response !== undefined) break
  420. }
  421. return response
  422. }
  423. export async function saveRequestAsTeams(
  424. apollo,
  425. request,
  426. title,
  427. teamID,
  428. collectionID
  429. ) {
  430. const x = await apollo.mutate({
  431. mutation: gql`
  432. mutation ($data: CreateTeamRequestInput!, $collectionID: String!) {
  433. createRequestInCollection(data: $data, collectionID: $collectionID) {
  434. id
  435. collection {
  436. id
  437. team {
  438. id
  439. name
  440. }
  441. }
  442. }
  443. }
  444. `,
  445. variables: {
  446. collectionID,
  447. data: {
  448. teamID,
  449. title,
  450. request,
  451. },
  452. },
  453. })
  454. return x.data?.createRequestInCollection
  455. }
  456. export async function overwriteRequestTeams(apollo, request, title, requestID) {
  457. await apollo.mutate({
  458. mutation: gql`
  459. mutation updateRequest(
  460. $data: UpdateTeamRequestInput!
  461. $requestID: String!
  462. ) {
  463. updateRequest(data: $data, requestID: $requestID) {
  464. id
  465. title
  466. }
  467. }
  468. `,
  469. variables: {
  470. requestID,
  471. data: {
  472. request,
  473. title,
  474. },
  475. },
  476. })
  477. }
  478. export async function importFromMyCollections(apollo, collectionID, teamID) {
  479. const response = await apollo.mutate({
  480. mutation: gql`
  481. mutation importFromMyCollections(
  482. $fbCollectionPath: String!
  483. $teamID: String!
  484. ) {
  485. importCollectionFromUserFirestore(
  486. fbCollectionPath: $fbCollectionPath
  487. teamID: $teamID
  488. ) {
  489. id
  490. title
  491. }
  492. }
  493. `,
  494. variables: {
  495. fbCollectionPath: collectionID,
  496. teamID,
  497. },
  498. })
  499. return response.data != null
  500. }
  501. export async function importFromJSON(apollo, collections, teamID) {
  502. const response = await apollo.mutate({
  503. mutation: gql`
  504. mutation importFromJSON($jsonString: String!, $teamID: String!) {
  505. importCollectionsFromJSON(jsonString: $jsonString, teamID: $teamID)
  506. }
  507. `,
  508. variables: {
  509. jsonString: JSON.stringify(collections),
  510. teamID,
  511. },
  512. })
  513. return response.data != null
  514. }
  515. export async function replaceWithJSON(apollo, collections, teamID) {
  516. const response = await apollo.mutate({
  517. mutation: gql`
  518. mutation replaceWithJSON($jsonString: String!, $teamID: String!) {
  519. replaceCollectionsWithJSON(jsonString: $jsonString, teamID: $teamID)
  520. }
  521. `,
  522. variables: {
  523. jsonString: JSON.stringify(collections),
  524. teamID,
  525. },
  526. })
  527. return response.data != null
  528. }
  529. export async function exportAsJSON(apollo, teamID) {
  530. const response = await apollo.query({
  531. query: gql`
  532. query exportAsJSON($teamID: String!) {
  533. exportCollectionsToJSON(teamID: $teamID)
  534. }
  535. `,
  536. variables: {
  537. teamID,
  538. },
  539. })
  540. return response.data.exportCollectionsToJSON
  541. }