Skip to content

Commit

Permalink
fix: Don't use attributes on models/instance
Browse files Browse the repository at this point in the history
Calls of those methods have been checked and `instanceInfo` parameter
is always a result of `useInstanceInfo` so we know that `.attributes`
can be omited
  • Loading branch information
Ldoppea committed Sep 12, 2024
1 parent 60ad859 commit e0b57e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
20 changes: 6 additions & 14 deletions packages/cozy-client/src/models/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

/**
Expand Down Expand Up @@ -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`
Expand All @@ -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: {
Expand Down
42 changes: 13 additions & 29 deletions packages/cozy-client/src/models/instance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,48 @@ 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'
}
}
}

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'
}
}
}
Expand Down Expand Up @@ -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)
})
})
Expand Down

0 comments on commit e0b57e4

Please sign in to comment.