Skip to content

Commit

Permalink
feat(mespapiers): Add possibility to define a specific doctype
Browse files Browse the repository at this point in the history
to store metadata
  • Loading branch information
JF-Cozy committed May 29, 2024
1 parent ad6b010 commit 6e315d5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SubmitButton from './widgets/SubmitButton'
import IlluGenericInputDate from '../../assets/icons/IlluGenericInputDate.svg'
import IlluGenericInputText from '../../assets/icons/IlluGenericInputText.svg'
import { KEYS } from '../../constants/const'
import { FILES_DOCTYPE } from '../../doctypes'
import { makeInputsInformationStep } from '../../helpers/makeInputsInformationStep'
import { hasNextvalue } from '../../utils/hasNextvalue'
import CompositeHeader from '../CompositeHeader/CompositeHeader'
Expand All @@ -28,6 +29,7 @@ const InformationDialog = ({ currentStep, onClose, onBack, onSubmit }) => {
illustration,
illustrationSize = 'medium',
text,
doctype = FILES_DOCTYPE,
attributes
} = currentStep
const { t } = useI18n()
Expand All @@ -45,6 +47,7 @@ const InformationDialog = ({ currentStep, onClose, onBack, onSubmit }) => {

const newFormData = normalizeFormdataMetadata({
formData,
doctype,
newMetadata: value
})

Expand Down
15 changes: 11 additions & 4 deletions packages/cozy-mespapiers-lib/src/components/ModelSteps/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,20 @@ export const getDefaultSelectedVersion = ({
* @param {Object} param
* @param {import('../../types').FormData} param.formData
* @param {import('cozy-client/types/types').FileMetadata} param.newMetadata
* @param {string} param.doctype
* @returns {import('../../types').FormData}
*/
export const normalizeFormdataMetadata = ({ formData, newMetadata }) => {
export const normalizeFormdataMetadata = ({
formData,
newMetadata,
doctype
}) => {
const metadataNormalized = {
metadata: Object.entries(newMetadata).reduce((acc, [key, value]) => {
return merge(acc, set({}, key, value))
}, {})
metadata: {
[doctype]: Object.entries(newMetadata).reduce((acc, [key, value]) => {
return merge(acc, set({}, key, value))
}, {})
}
}

return merge({}, formData, metadataNormalized)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getFormDataFilesForOcr,
normalizeFormdataMetadata
} from './helpers'
import { FILES_DOCTYPE } from '../../doctypes'

describe('ModalSteps helpers', () => {
describe('isFileAlreadySelected', () => {
Expand Down Expand Up @@ -200,11 +201,12 @@ describe('ModalSteps helpers', () => {
it('should return the original formData when new metadata is empty', () => {
const result = normalizeFormdataMetadata({
formData: { metadata: {}, data: [], contacts: [] },
newMetadata: {}
newMetadata: {},
doctype: FILES_DOCTYPE
})

expect(result).toEqual({
metadata: {},
metadata: { [FILES_DOCTYPE]: {} },
data: [],
contacts: []
})
Expand All @@ -213,28 +215,36 @@ describe('ModalSteps helpers', () => {
it('should merge new metadata into formData metadata', () => {
const result = normalizeFormdataMetadata({
formData: {
metadata: { foo: 'bar' },
metadata: { [FILES_DOCTYPE]: { foo: 'bar' } },
data: [],
contacts: []
},
newMetadata: { key: 'value' }
newMetadata: { key: 'value' },
doctype: FILES_DOCTYPE
})

expect(result).toEqual({
metadata: { foo: 'bar', key: 'value' },
metadata: { [FILES_DOCTYPE]: { foo: 'bar', key: 'value' } },
data: [],
contacts: []
})
})

it('should merge new "complex" metadata into the metadata of formData', () => {
const result = normalizeFormdataMetadata({
formData: { metadata: { foo: 'bar' }, data: [], contacts: [] },
newMetadata: { 'key.complex': 'value' }
formData: {
metadata: { [FILES_DOCTYPE]: { foo: 'bar' } },
data: [],
contacts: []
},
newMetadata: { 'key.complex': 'value' },
doctype: FILES_DOCTYPE
})

expect(result).toEqual({
metadata: { foo: 'bar', key: { complex: 'value' } },
metadata: {
[FILES_DOCTYPE]: { foo: 'bar', key: { complex: 'value' } }
},
data: [],
contacts: []
})
Expand Down
25 changes: 14 additions & 11 deletions packages/cozy-mespapiers-lib/src/helpers/createPdfAndSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ export const updateMetadata = ({
}) => {
return {
...metadata,
...addCountryValueByQualification(qualification),
qualification: {
...qualification
},
datetime: metadata.featureDate ?? pdfDoc.getCreationDate(),
datetimeLabel: featureDate || 'datetime'
[FILES_DOCTYPE]: {
...metadata[FILES_DOCTYPE],
...addCountryValueByQualification(qualification),
qualification: {
...qualification
},
datetime: metadata[FILES_DOCTYPE].featureDate ?? pdfDoc.getCreationDate(),
datetimeLabel: featureDate || 'datetime'
}
}
}

Expand Down Expand Up @@ -101,8 +104,8 @@ export const createPdfAndSave = async ({
})

const date =
updatedMetadata[featureDate] &&
f(updatedMetadata[featureDate], 'YYYY.MM.DD')
updatedMetadata[FILES_DOCTYPE][featureDate] &&
f(updatedMetadata[FILES_DOCTYPE][featureDate], 'YYYY.MM.DD')

// If all files are to be considered as one.
const isMultiPage = data.some(({ fileMetadata }) => fileMetadata.multipage)
Expand All @@ -128,8 +131,8 @@ export const createPdfAndSave = async ({
})

// Created metadata for pdf file
const metadataWithPage = {
...updatedMetadata,
const fileMetadataWithPage = {
...updatedMetadata[FILES_DOCTYPE],
...(fileMetadata.page && { page: fileMetadata.page })
}

Expand All @@ -141,7 +144,7 @@ export const createPdfAndSave = async ({
{
name: paperName,
contentType: 'application/pdf',
metadata: metadataWithPage,
metadata: fileMetadataWithPage,
dirId: appFolderID,
conflictStrategy: 'rename'
}
Expand Down
17 changes: 10 additions & 7 deletions packages/cozy-mespapiers-lib/src/helpers/createPdfAndSave.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
updateMetadata,
createPdfAndSave
} from './createPdfAndSave'
import { FILES_DOCTYPE } from '../doctypes'

jest.mock('cozy-client/dist/models/file', () => ({
...jest.requireActual('cozy-client/dist/models/file'),
Expand All @@ -26,7 +27,7 @@ const mockParams = file => ({
fileMetadata: {}
}
],
metadata: {},
metadata: { [FILES_DOCTYPE]: {} },
contacts: []
},
qualification: {},
Expand Down Expand Up @@ -90,18 +91,20 @@ describe('addCountryValueByQualification', () => {
describe('updateMetadata', () => {
it('should return metadata with other keys', () => {
const result = updateMetadata({
metadata: { name: 'name' },
metadata: { [FILES_DOCTYPE]: { name: 'name' } },
qualification: { label: 'national_id_card' },
featureDate: 'referencedDate',
pdfDoc: mockPDFDocument
})

expect(result).toEqual({
name: 'name',
qualification: { label: 'national_id_card' },
datetime: 'mockDate',
datetimeLabel: 'referencedDate',
country: 'FR'
[FILES_DOCTYPE]: {
name: 'name',
qualification: { label: 'national_id_card' },
datetime: 'mockDate',
datetimeLabel: 'referencedDate',
country: 'FR'
}
})
})
})

0 comments on commit 6e315d5

Please sign in to comment.