Skip to content

Commit

Permalink
fix: Cannot read property 'fileIdAttributes' error
Browse files Browse the repository at this point in the history
This happens when some files on the cozy do not have the metadata
attribute. Now the files without metadata will be replaced.
  • Loading branch information
doubleface authored and doubleface committed Nov 13, 2023
1 parent cf55d0c commit 2241606
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/libs/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export default class Launcher {
const createdByApp = konnector.slug
if (!sourceAccountIdentifier) {
throw new Error(
'createExistingFileIndex: unexpected undefined sourceAccountIdentifier'
'getExistingFilesIndex: unexpected undefined sourceAccountIdentifier'
)
}

Expand Down Expand Up @@ -443,7 +443,10 @@ export default class Launcher {
)
// @ts-ignore
const existingFilesIndex = existingFiles.reduce((map, file) => {
map.set(file.metadata.fileIdAttributes, file)
if (file.metadata?.fileIdAttributes) {
// files without metadata will be replaced
map.set(file.metadata.fileIdAttributes, file)
}
return map
}, new Map())
return (this.existingFilesIndex = existingFilesIndex)
Expand Down
57 changes: 56 additions & 1 deletion src/libs/Launcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,62 @@ describe('Launcher', () => {
sourceAccountIdentifier: 'testsourceaccountidentifier'
})
)
//
})
it('should ignore files without metadata or fileIdAttributes in the index', async () => {
const launcher = new Launcher()
launcher.setUserData({
sourceAccountIdentifier: 'testsourceaccountidentifier'
})

const konnector = { slug: 'testkonnectorslug' }
const trigger = {
message: {
folder_to_save: 'testfolderid',
account: 'testaccountid'
}
}
const job = {
message: { account: 'testaccountid', folder_to_save: 'testfolderid' }
}
const client = {
queryAll: jest.fn().mockResolvedValue([
{
_id: 'tokeep',
metadata: {
fileIdAttributes: 'fileidattribute'
}
},
{
_id: 'toignore'
}
]),
query: jest.fn().mockResolvedValue({ data: { path: 'folderPath' } })
}
launcher.setStartContext({
konnector,
client,
launcherClient: client,
trigger,
job
})

await launcher.saveFiles([{}], {})

expect(saveFiles).toHaveBeenCalledWith(client, [{}], 'folderPath', {
downloadAndFormatFile: expect.any(Function),
manifest: expect.any(Object),
existingFilesIndex: new Map([
[
'fileidattribute',
{
_id: 'tokeep',
metadata: { fileIdAttributes: 'fileidattribute' }
}
]
]),
sourceAccount: 'testaccountid',
sourceAccountIdentifier: 'testsourceaccountidentifier'
})
})
})
})

0 comments on commit 2241606

Please sign in to comment.