From 469f38b7683d48b95805e0341f44f7839863f702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 26 Apr 2022 14:12:52 +0100 Subject: [PATCH] feat: put T&C in its own query --- lib/graphql/resolvers.js | 63 ++++++++++++++++++++++++++++++---------- lib/graphql/types.js | 10 +++++-- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/lib/graphql/resolvers.js b/lib/graphql/resolvers.js index 01c2adb5..2e0c68b0 100644 --- a/lib/graphql/resolvers.js +++ b/lib/graphql/resolvers.js @@ -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, } } diff --git a/lib/graphql/types.js b/lib/graphql/types.js index a9318978..ae4b24d3 100644 --- a/lib/graphql/types.js +++ b/lib/graphql/types.js @@ -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 } `