From e0b57e4243da91d9b148a8e76eef0c93acfdd240 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Wed, 4 Sep 2024 15:40:43 +0200 Subject: [PATCH] fix: Don't use attributes on models/instance Calls of those methods have been checked and `instanceInfo` parameter is always a result of `useInstanceInfo` so we know that `.attributes` can be omited --- packages/cozy-client/src/models/instance.js | 20 +++------ .../cozy-client/src/models/instance.spec.js | 42 ++++++------------- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/packages/cozy-client/src/models/instance.js b/packages/cozy-client/src/models/instance.js index fa8dcaadc6..1238ad8034 100644 --- a/packages/cozy-client/src/models/instance.js +++ b/packages/cozy-client/src/models/instance.js @@ -20,19 +20,17 @@ const PREMIUM_QUOTA = 50 * GB // If manager URL is present, then the instance is not self-hosted export const isSelfHosted = instanceInfo => { - return get(instanceInfo, 'context.data.attributes.manager_url') ? false : true + return get(instanceInfo, 'context.data.manager_url') ? false : true } export const arePremiumLinksEnabled = instanceInfo => { - return get(instanceInfo, 'context.data.attributes.enable_premium_links') - ? true - : false + return get(instanceInfo, 'context.data.enable_premium_links') ? true : false } export const isFreemiumUser = instanceInfo => { - const quota = get(instanceInfo, 'diskUsage.data.attributes.quota', false) + const quota = get(instanceInfo, 'diskUsage.data.quota', false) return parseInt(quota) <= PREMIUM_QUOTA } export const getUuid = instanceInfo => { - return get(instanceInfo, 'instance.data.attributes.uuid') + return get(instanceInfo, 'instance.data.uuid') } /** @@ -70,11 +68,7 @@ export const hasAnOffer = data => { * @param {InstanceInfo} instanceInfo - Instance information */ export const buildPremiumLink = instanceInfo => { - const managerUrl = get( - instanceInfo, - 'context.data.attributes.manager_url', - false - ) + const managerUrl = get(instanceInfo, 'context.data.manager_url', false) const uuid = getUuid(instanceInfo) if (managerUrl && uuid) { return `${managerUrl}/cozy/instances/${uuid}/premium` @@ -92,9 +86,7 @@ export const buildPremiumLink = instanceInfo => { export const hasPasswordDefinedAttribute = async client => { try { const { - data: { - attributes: { password_defined } - } + data: { password_defined } } = await client.fetchQueryAndGetFromState({ definition: Q('io.cozy.settings').getById('io.cozy.settings.instance'), options: { diff --git a/packages/cozy-client/src/models/instance.spec.js b/packages/cozy-client/src/models/instance.spec.js index d3957d3d4a..3fd3e3027e 100644 --- a/packages/cozy-client/src/models/instance.spec.js +++ b/packages/cozy-client/src/models/instance.spec.js @@ -3,39 +3,29 @@ import { instance } from './' const noSelfHostedInstance = { context: { data: { - attributes: { - manager_url: 'https://manager.cozy.cc', - enable_premium_links: true - } + manager_url: 'https://manager.cozy.cc', + enable_premium_links: true } }, instance: { data: { - attributes: { - uuid: '1234' - } + uuid: '1234' } }, diskUsage: { data: { - attributes: { - quota: '400000000' - } + quota: '400000000' } } } const selftHostedInstance = { context: { - data: { - attributes: {} - } + data: {} }, diskUsage: { data: { - attributes: { - quota: '6000000000000' - } + quota: '6000000000000' } } } @@ -43,24 +33,18 @@ const selftHostedInstance = { const hadAnOfferInstance = { context: { data: { - attributes: { - manager_url: 'https://manager.cozy.cc', - enable_premium_links: true - } + manager_url: 'https://manager.cozy.cc', + enable_premium_links: true } }, instance: { data: { - attributes: { - uuid: '1234' - } + uuid: '1234' } }, diskUsage: { data: { - attributes: { - quota: '60000000000' - } + quota: '60000000000' } } } @@ -163,17 +147,17 @@ describe('instance', () => { }) it('should return false if attribute password_defined is undefined', async () => { - const res = await setup({ attributes: { password_defined: undefined } }) + const res = await setup({ password_defined: undefined }) expect(res).toBe(false) }) it('should return false if attribute password_defined is false', async () => { - const res = await setup({ attributes: { password_defined: false } }) + const res = await setup({ password_defined: false }) expect(res).toBe(false) }) it('should return true if attribute password_defined is true', async () => { - const res = await setup({ attributes: { password_defined: true } }) + const res = await setup({ password_defined: true }) expect(res).toBe(true) }) })