diff --git a/packages/cozy-konnector-libs/package.json b/packages/cozy-konnector-libs/package.json index 0a9eed96c..37bcfcec3 100644 --- a/packages/cozy-konnector-libs/package.json +++ b/packages/cozy-konnector-libs/package.json @@ -33,6 +33,7 @@ "date-fns": "^2.22.1", "file-type": "^16.5.0", "geco": "git+https://github.com/konnectors/geco.git#0.11.2", + "lodash": "^4.17.21", "lodash-id": "^0.14.0", "lowdb": "^1.0.0", "mime-types": "^2.1.31", diff --git a/packages/cozy-konnector-libs/src/libs/BaseKonnector.spec.js b/packages/cozy-konnector-libs/src/libs/BaseKonnector.spec.js index 721c1e443..5df024ba0 100644 --- a/packages/cozy-konnector-libs/src/libs/BaseKonnector.spec.js +++ b/packages/cozy-konnector-libs/src/libs/BaseKonnector.spec.js @@ -10,6 +10,8 @@ jest.mock('./cozyclient', () => ({ jest.mock('fs', () => ({ existsSync: jest.fn(), + access: jest.fn(), + readFile: jest.fn(), promises: { readFile: jest.fn() } diff --git a/packages/cozy-konnector-libs/src/libs/saveFiles.js b/packages/cozy-konnector-libs/src/libs/saveFiles.js index 79e5cf784..01b1806c0 100644 --- a/packages/cozy-konnector-libs/src/libs/saveFiles.js +++ b/packages/cozy-konnector-libs/src/libs/saveFiles.js @@ -10,11 +10,15 @@ const path = require('path') const requestFactory = require('./request') const omit = require('lodash/omit') const get = require('lodash/get') +const isEqual = require('lodash/isEqual') const log = require('cozy-logger').namespace('saveFiles') const manifest = require('./manifest') const cozy = require('./cozyclient') const client = cozy.new const { Q } = require('cozy-client/dist/queries/dsl') +const { models } = require('cozy-client') +//const models = cozy.new.models +const { Qualification } = models.document const errors = require('../helpers/errors') const stream = require('stream') const fileType = require('file-type') @@ -502,10 +506,45 @@ const shouldReplaceFile = async function (file, entry, options) { } const defaultShouldReplaceFile = (file, entry) => { const shouldForceMetadataAttr = attr => { - const result = - getAttribute(file, `metadata.${attr}`) !== - get(entry, `fileAttributes.metadata.${attr}`) - if (result) log('debug', `filereplacement: adding ${attr} metadata`) + let entryQualif = get(entry, `fileAttributes.metadata.${attr}`) + if (attr === 'qualification' && (entryQualif instanceof Qualification)) { + // If the entry come with a qualification type object we convert it before compare + entryQualif = entryQualif.toQualification() + } + const result = !isEqual( + getAttribute(file, `metadata.${attr}`), + entryQualif + ) + log('warn', `Result for ${attr} is ${result}`) + console.log('warn', `Result for ${attr} is ${result}`) + + if (result) { + log('debug', `filereplacement: adding ${attr} metadata`) + log('warn', 'isequal ' + isEqual( + getAttribute(file, `metadata.${attr}`), + get(entry,`fileAttributes.metadata.${attr}`))) + log('warn', 'not isEqual(should be false) ' + !isEqual( + getAttribute(file, `metadata.${attr}`), + get(entry, `fileAttributes.metadata.${attr}`))) + log('warn', 'Stringify equal(should be True) ' + `${JSON.stringify(getAttribute(file, `metadata.${attr}`))==JSON.stringify(get(entry, `fileAttributes.metadata.${attr}`))}`) + log('warn', 'file ' + JSON.stringify(getAttribute(file, `metadata.${attr}`))) + log('warn', 'entry ' + JSON.stringify(get(entry, `fileAttributes.metadata.${attr}`))) + log('warn', 'file ' + typeof(JSON.stringify(getAttribute(file, `metadata.${attr}`)))) + log('warn', 'entry ' + typeof(JSON.stringify(get(entry, `fileAttributes.metadata.${attr}`)))) + log('warn', 'file ' + typeof(getAttribute(file, `metadata.${attr}`))) + log('warn', 'entry ' + typeof(get(entry, `fileAttributes.metadata.${attr}`))) + log('warn', 'file ' + Object.getOwnPropertyNames(getAttribute(file, `metadata.${attr}`))) + log('warn', 'entry ' + Object.getOwnPropertyNames(get(entry, `fileAttributes.metadata.${attr}`))) + console.log('AA') + console.dir(getAttribute(file, `metadata.${attr}`)) + console.dir(get(entry,`fileAttributes.metadata.${attr}`)) + console.log(result) + console.log('BBB') + const D = get(entry,`fileAttributes.metadata.${attr}`) + console.dir(D) + console.dir(D instanceof Qualification) + + } return result } // replace all files with meta if there is file metadata to add diff --git a/packages/cozy-konnector-libs/src/libs/saveFiles.spec.js b/packages/cozy-konnector-libs/src/libs/saveFiles.spec.js index e64f2f6e5..80c13c7d8 100644 --- a/packages/cozy-konnector-libs/src/libs/saveFiles.spec.js +++ b/packages/cozy-konnector-libs/src/libs/saveFiles.spec.js @@ -19,6 +19,8 @@ cozyClient.new = { }) } const client = cozyClient.new +const { models } = require('cozy-client') +client.models = models const manifest = require('./manifest') const logger = require('cozy-logger') @@ -377,7 +379,6 @@ describe('saveFiles', function () { data: makeFile('existingFileId', { name: 'bill.pdf', metadata: { - carbonCopy: true, qualification: { item1: true, item2: 'toto' @@ -394,7 +395,6 @@ describe('saveFiles', function () { filename: 'bill.pdf', fileAttributes: { metadata: { - carbonCopy: true, qualification: { item1: true, item2: 'tata' @@ -410,7 +410,6 @@ describe('saveFiles', function () { expect(client.save).toHaveBeenCalledWith( expect.objectContaining({ metadata: { - carbonCopy: true, qualification: { item1: true, item2: 'tata' @@ -420,6 +419,94 @@ describe('saveFiles', function () { ) }) }) + + describe('when a qualification V2 do not need to be updated', () => { + it('should not update the file', async () => { + expect.assertions(1) + statByPath.mockImplementation(async path => { + // Must check if we are stating on the folder or on the file + return path === FOLDER_PATH + ? { data: { _id: 'folderId' } } + : { + data: makeFile('existingFileId', { + name: 'bill.pdf', + metadata: { + qualification: { + item1: true, + item2: 'tata' + } + } + }) + } + }) + + await saveFiles( + [ + { + fileurl: 'https://coucou.com/filetodownload.pdf', + filename: 'bill.pdf', + fileAttributes: { + metadata: { + qualification: { + item1: true, + item2: 'tata' + } + } + } + } + ], + { + folderPath: 'mainPath' + } + ) + expect(client.save).not.toHaveBeenCalled() + }) + }) + + describe('when a qualification is received', () => { + it('should convert qualification to a json', async () => { + expect.assertions(1) + + const qualif = models.document.Qualification.getByLabel('tax_return') + statByPath.mockImplementation(async path => { + // Must check if we are stating on the folder or on the file + return path === FOLDER_PATH + ? { data: { _id: 'folderId' } } + : { + data: makeFile('existingFileId', { + name: 'bill.pdf', + metadata: { + qualification: { + label: 'tax_return', + purpose: 'report', + sourceCategory: 'gov', + sourceSubCategory: 'tax', + subjects: [ 'tax' ] + } + } + }) + } + }) + + await saveFiles( + [ + { + fileurl: 'https://coucou.com/filetodownload.pdf', + filename: 'bill.pdf', + fileAttributes: { + metadata: { + qualification: qualif + } + } + } + ], + { + folderPath: 'mainPath' + } + ) + expect(client.save).not.toHaveBeenCalled() + }) + }) }) describe('subPath handling', () => {