@@ -2,10 +2,8 @@ import * as serloAuth from '@serlo/authorization'
2
2
import { instanceToScope , Scope } from '@serlo/authorization'
3
3
import { createHash } from 'crypto'
4
4
import { array as A , either as E , function as F , option as O } from 'fp-ts'
5
- import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray'
6
5
import * as t from 'io-ts'
7
6
import * as R from 'ramda'
8
- import { URL } from 'url'
9
7
10
8
import { resolveUnrevisedEntityIds } from '../abstract-entity/resolvers'
11
9
import { UuidResolver } from '../abstract-uuid/resolvers'
@@ -27,6 +25,7 @@ import {
27
25
generateRole ,
28
26
isGlobalRole ,
29
27
} from '~/internals/graphql'
28
+ import { CellValues , MajorDimension } from '~/model'
30
29
import { EntityDecoder , UserDecoder } from '~/model/decoder'
31
30
import {
32
31
getPermissionsForRole ,
@@ -38,11 +37,6 @@ import { createThreadResolvers } from '~/schema/thread/utils'
38
37
import { createUuidResolvers } from '~/schema/uuid/abstract-uuid/utils'
39
38
import { Instance , Resolvers } from '~/types'
40
39
41
- enum MajorDimension {
42
- Rows = 'ROWS' ,
43
- Columns = 'COLUMNS' ,
44
- }
45
-
46
40
export const ActiveUserIdsResolver = createCachedResolver <
47
41
Record < string , never > ,
48
42
number [ ]
@@ -206,12 +200,12 @@ export const resolvers: Resolvers = {
206
200
User : {
207
201
...createUuidResolvers ( ) ,
208
202
...createThreadResolvers ( ) ,
209
- async motivation ( user , _args , _context ) {
210
- const spreadsheetId = process . env . GOOGLE_SPREADSHEET_API_MOTIVATION
211
- const range = 'Formularantworten!B:D'
212
-
203
+ async motivation ( user , _args , context ) {
213
204
return F . pipe (
214
- await getSpreadsheetValues ( { spreadsheetId, range } ) ,
205
+ await context . dataSources . model . googleSpreadsheetApi . getValues ( {
206
+ spreadsheetId : process . env . GOOGLE_SPREADSHEET_API_MOTIVATION ,
207
+ range : 'Formularantworten!B:D' ,
208
+ } ) ,
215
209
E . mapLeft (
216
210
addContext ( {
217
211
location : 'motivationSpreadsheet' ,
@@ -633,14 +627,11 @@ async function fetchActivityByType(
633
627
return result
634
628
}
635
629
636
- async function activeDonorIDs ( _context : Context ) {
637
- const spreadsheetId = process . env . GOOGLE_SPREADSHEET_API_ACTIVE_DONORS
638
- const range = 'Tabellenblatt1!A:A'
639
-
630
+ async function activeDonorIDs ( context : Context ) {
640
631
return F . pipe (
641
- await getSpreadsheetValues ( {
642
- spreadsheetId,
643
- range,
632
+ await context . dataSources . model . googleSpreadsheetApi . getValues ( {
633
+ spreadsheetId : process . env . GOOGLE_SPREADSHEET_API_ACTIVE_DONORS ,
634
+ range : 'Tabellenblatt1!A:A' ,
644
635
majorDimension : MajorDimension . Columns ,
645
636
} ) ,
646
637
extractIDsFromFirstColumn ,
@@ -689,50 +680,3 @@ async function deleteKratosUser(
689
680
await authServices . kratos . admin . deleteIdentity ( { id : identity . id } )
690
681
}
691
682
}
692
-
693
- type CellValues = NonEmptyArray < string [ ] >
694
-
695
- interface GetSpreadsheetValuesArgs {
696
- spreadsheetId : string
697
- range : string
698
- majorDimension ?: MajorDimension
699
- }
700
-
701
- async function getSpreadsheetValues (
702
- args : GetSpreadsheetValuesArgs ,
703
- ) : Promise < E . Either < ErrorEvent , CellValues > > {
704
- const { spreadsheetId, range } = args
705
- const majorDimension = args . majorDimension ?? MajorDimension . Rows
706
- const url = new URL (
707
- `https://sheets.googleapis.com/v4/spreadsheets/${ spreadsheetId } /values/${ range } ` ,
708
- )
709
- url . searchParams . append ( 'majorDimension' , majorDimension )
710
- const apiSecret = process . env . GOOGLE_SPREADSHEET_API_SECRET
711
- url . searchParams . append ( 'key' , apiSecret )
712
-
713
- const specifyErrorLocation = E . mapLeft (
714
- addContext ( {
715
- location : 'googleSpreadSheetApi' ,
716
- locationContext : { ...args } ,
717
- } ) ,
718
- )
719
-
720
- try {
721
- const response = await fetch ( url . toString ( ) )
722
- const data = ( await response . json ( ) ) as { values ?: string [ ] [ ] }
723
-
724
- if (
725
- ! data . values ||
726
- ! Array . isArray ( data . values ) ||
727
- data . values . length === 0
728
- ) {
729
- return specifyErrorLocation (
730
- E . left ( { error : new Error ( 'invalid response or empty range' ) } ) ,
731
- )
732
- }
733
-
734
- return specifyErrorLocation ( E . right ( data . values as CellValues ) )
735
- } catch ( error ) {
736
- return specifyErrorLocation ( E . left ( { error : E . toError ( error ) } ) )
737
- }
738
- }
0 commit comments