Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: konnectors/libs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: konnectors/libs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fixCCimport
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Jan 18, 2024

  1. Copy the full SHA
    f1a4d3c View commit details

Commits on Jan 22, 2024

  1. wip

    LucsT committed Jan 22, 2024
    Copy the full SHA
    625716e View commit details
  2. fix

    doubleface committed Jan 22, 2024
    Copy the full SHA
    7fa115b View commit details
1 change: 1 addition & 0 deletions packages/cozy-konnector-libs/package.json
Original file line number Diff line number Diff line change
@@ -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",
2 changes: 2 additions & 0 deletions packages/cozy-konnector-libs/src/libs/BaseKonnector.spec.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ jest.mock('./cozyclient', () => ({

jest.mock('fs', () => ({
existsSync: jest.fn(),
access: jest.fn(),
readFile: jest.fn(),
promises: {
readFile: jest.fn()
}
47 changes: 43 additions & 4 deletions packages/cozy-konnector-libs/src/libs/saveFiles.js
Original file line number Diff line number Diff line change
@@ -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
93 changes: 90 additions & 3 deletions packages/cozy-konnector-libs/src/libs/saveFiles.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {