1+ const { DURATIONS_INTERVAL , DURATIONS_LIMIT } = require ( '../constants/durations' )
2+ const constants = require ( '../database/constants' )
3+ const requireAuth = require ( '../middlewares/requireAuth' )
4+ const pipe = require ( '../utils/pipe' )
5+
6+ const response = ( entry ) => ( {
7+ created : entry . created ,
8+ updated : entry . updated ,
9+ id : entry . name ,
10+ value : entry . value ,
11+ unit : entry . unit ,
12+ } )
13+
14+ const toApi = ( { name, ...data } ) => ( {
15+ ...data ,
16+ id : name ,
17+ } )
18+ const interval = async ( ) => {
19+ const name = 'DURATIONS_INTERVAL'
20+ return toApi ( await constants . get ( name ) || {
21+ name,
22+ value : DURATIONS_INTERVAL ,
23+ unit : 'seconds' ,
24+ } )
25+ }
26+ const limit = async ( ) => {
27+ const name = 'DURATIONS_LIMIT'
28+ return toApi ( await constants . get ( name ) || {
29+ name,
30+ value : DURATIONS_LIMIT ,
31+ unit : 'minutes' ,
32+ } )
33+ }
34+
35+ module . exports = {
36+ Query : {
37+ constants : pipe ( requireAuth , async ( ) => ( {
38+ durationsInterval : await interval ( ) ,
39+ durationsLimit : await limit ( ) ,
40+ } ) ) ,
41+ } ,
42+ Mutation : {
43+ setConstant : pipe ( requireAuth , async ( parent , { input } ) => {
44+ const { id, value, unit } = input
45+
46+ const entry = await constants . update ( id , value , unit )
47+ return {
48+ success : true ,
49+ payload : response ( entry ) ,
50+ }
51+ } ) ,
52+ } ,
53+ }
0 commit comments