feat: put T&C in its own query

This commit is contained in:
André Sá 2022-04-26 14:12:52 +01:00
parent 7b951f961f
commit 469f38b768
2 changed files with 54 additions and 19 deletions

View file

@ -68,18 +68,6 @@ const buildTriggers = (allTriggers) => {
})
}
/*
* TODO: From `lib/routes/termsAndConditionsRoutes.js` -- remove this after
* terms are removed from the GraphQL API too.
*/
const massageTerms = terms => (terms.active && terms.text) ? ({
delay: Boolean(terms.delay),
title: terms.title,
text: nmd(terms.text),
accept: terms.acceptButtonText,
cancel: terms.cancelButtonText,
}) : null
const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings, }) => {
const massageCoins = _.map(_.pick([
'batchable',
@ -112,7 +100,6 @@ const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings
configManager.getLocale(deviceId, settings.config),
configManager.getOperatorInfo(settings.config),
configManager.getReceipt(settings.config),
massageTerms(configManager.getTermsConditions(settings.config)),
!!configManager.getCashOut(deviceId, settings.config).active,
])
.then(([
@ -123,7 +110,6 @@ const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings
localeInfo,
operatorInfo,
receiptInfo,
terms,
twoWayMode,
]) =>
(currentConfigVersion && currentConfigVersion >= staticConf.configVersion) ?
@ -143,7 +129,6 @@ const staticConfig = ({ currentConfigVersion, deviceId, deviceName, pq, settings
twoWayMode,
speedtestFiles,
urlsToPing,
terms,
}),
_.update('triggersAutomation', _.mapValues(_.eq('Automatic'))),
addOperatorInfo(operatorInfo),
@ -232,8 +217,54 @@ const configs = (parent, { currentConfigVersion }, { deviceId, deviceName, opera
}),
}))
const massageTerms = terms => (terms.active && terms.text) ? ({
delay: Boolean(terms.delay),
title: terms.title,
text: nmd(terms.text),
accept: terms.acceptButtonText,
cancel: terms.cancelButtonText,
}) : null
/*
* The type of the result of `configManager.getTermsConditions()` is more or
* less `Maybe (Maybe Hash, Maybe TC)`. Each case has a specific meaning to the
* machine:
*
* Nothing => Nothing
* There are no T&C or they've been removed/disabled.
*
* Just (Nothing, _) => Nothing
* Shouldn't happen! Treated as if there were no T&C.
*
* Just (Just hash, Nothing) => Nothing
* May happen (after `massageTerms`) if T&C are disabled.
*
* Just (Just hash, Just tc) => Just (hash, Just tc) or Just (hash, Nothing)
* `tc` is sent depending on whether the `hash` differs from `currentHash` or
* not.
*/
const terms = (parent, { currentHash }, { settings }, info) => {
const isNone = x => _.isNil(x) || _.isEmpty(x)
const latestTerms = configManager.getTermsConditions(settings.config)
if (isNone(latestTerms)) return null
const hash = latestTerms.hash
if (!_.isString(hash)) return null
if (hash === currentHash) return { hash, details: null }
const details = massageTerms(latestTerms)
if (isNone(details)) return null
return { hash, details }
}
module.exports = {
Query: {
configs
configs,
terms,
}
}

View file

@ -69,7 +69,7 @@ type Trigger {
thresholdDays: Int
}
type Terms {
type TermsDetails {
delay: Boolean!
title: String!
text: String!
@ -77,6 +77,11 @@ type Terms {
cancel: String!
}
type Terms {
hash: String!
details: TermsDetails
}
type StaticConfig {
configVersion: Int!
@ -98,8 +103,6 @@ type StaticConfig {
triggersAutomation: TriggersAutomation!
triggers: [Trigger!]!
terms: Terms
}
type DynamicCoinValues {
@ -147,5 +150,6 @@ type Configs {
type Query {
configs(currentConfigVersion: Int): Configs!
terms(currentHash: String): Terms
}
`