Skip to content

Commit

Permalink
fix: Redirect to correct folder id thanks to accounts and triggers
Browse files Browse the repository at this point in the history
The good way to get the folder id of a pronote account is with
accounts and triggers like in this commit.
  • Loading branch information
zatteo committed Nov 5, 2024
1 parent 768aef6 commit 9f63b4f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
5 changes: 5 additions & 0 deletions manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
"type": "io.cozy.identities",
"description": "Required by Papillon",
"verbs": ["GET"]
},
"triggers": {
"type": "io.cozy.triggers",
"description": "Required by Papillon",
"verbs": ["GET"]
}
}
}
27 changes: 20 additions & 7 deletions src/hooks/useCurrentAccountFolderLink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react'
import { buildAccountFolderQuery } from 'src/queries'
import { buildAccountQuery, buildTriggerQuery } from 'src/queries'

import { useClient, generateWebLink } from 'cozy-client'

Expand All @@ -13,21 +13,34 @@ const useCurrentAccountFolderLink = () => {

useEffect(() => {
const fetchCurrentAccountFolderLink = async () => {
const accountFolderQuery = buildAccountFolderQuery(
currentAccount?.cozyMetadata?.sourceAccountIdentifier
const accountQuery = buildAccountQuery()

const { data: accountResult } = await client.query(
accountQuery.definition(),
accountQuery.options
)

const { data } = await client.query(
accountFolderQuery.definition(),
accountFolderQuery.options
if (!accountResult[0]?._id) {
return
}

const triggerQuery = buildTriggerQuery(accountResult[0]._id)

const { data: triggerResult } = await client.query(
triggerQuery.definition(),
triggerQuery.options
)

if (!triggerResult[0]?.message?.folder_to_save) {
return
}

const webLink = generateWebLink({
cozyUrl: client.getStackClient().uri,
slug: 'drive',
subDomainType: client.capabilities.flat_subdomains ? 'flat' : 'nested',
pathname: '',
hash: `/folder/${data[0]._id}`,
hash: `/folder/${triggerResult[0].message.folder_to_save}`,
searchParams: []
})

Expand Down
39 changes: 30 additions & 9 deletions src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,38 @@ export const buildPresenceQuery = sourceAccountIdentifier => ({
}
})

export const buildAccountFolderQuery = accountIdentifier => ({
export const buildAccountQuery = () => ({
definition: () =>
Q('io.cozy.files')
.partialIndex({ type: 'directory', trashed: false })
.referencedBy({
_id: accountIdentifier,
_type: 'io.cozy.accounts.sourceAccountIdentifier'
Q('io.cozy.accounts')
.where({
account_type: 'pronote'
})
.partialIndex({
account_type: 'pronote'
}),
options: {
as: 'io.cozy.files/byReferencedAccountIdentifier/' + accountIdentifier,
fetchPolicy: defaultFetchPolicy,
singleDocData: true
as: 'io.cozy.files/account_type/pronote',
fetchPolicy: defaultFetchPolicy
}
})

export const buildTriggerQuery = accountId => ({
definition: () =>
Q('io.cozy.triggers')
.where({
message: {
account: accountId,
konnector: 'pronote'
}
})
.partialIndex({
message: {
account: accountId,
konnector: 'pronote'
}
}),
options: {
as: 'io.cozy.triggers/konnector/pronote/account/' + accountId,
fetchPolicy: defaultFetchPolicy
}
})

0 comments on commit 9f63b4f

Please sign in to comment.