diff --git a/package.json b/package.json index 4ee60629..9427b6bd 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "stylint": "2.0.0" }, "dependencies": { - "cozy-client": "^34.2.0", + "cozy-client": "^34.4.0", "cozy-device-helper": "2.2.1", "cozy-doctypes": "^1.83.8", "cozy-flags": "^2.9.0", diff --git a/src/constants.js b/src/constants.js index ed4d23aa..01b18ce4 100644 --- a/src/constants.js +++ b/src/constants.js @@ -4,7 +4,5 @@ export const TRIGGERS_DOCTYPE = 'io.cozy.triggers' export const APP_SLUG = 'mespapiers' export const EXPIRATION_SERVICE_NAME = 'expiration' -export const DEFAULT_NOTICE_PERIOD_DAYS = 90 -export const PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS = 15 export const lang = process.env.COZY_LOCALE || 'fr' export const dictRequire = lang => require(`locales/${lang}`) diff --git a/src/helpers/service.js b/src/helpers/service.js index bc6a3eef..b9902d74 100644 --- a/src/helpers/service.js +++ b/src/helpers/service.js @@ -1,21 +1,16 @@ -import sub from 'date-fns/sub' -import add from 'date-fns/add' - import log from 'cozy-logger' -import papersDefinitions from 'cozy-mespapiers-lib/dist/constants/papersDefinitions.json' -import { - APP_SLUG, - DEFAULT_NOTICE_PERIOD_DAYS, - PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS, - TRIGGERS_DOCTYPE -} from 'src/constants' +import { models } from 'cozy-client' + +import { APP_SLUG, TRIGGERS_DOCTYPE } from 'src/constants' import { buildAllFilesToNotifyQuery, buildTriggerByIdQuery, buildTriggerByServiceNameQuery } from 'src/helpers/queries' +const { computeExpirationDate, isExpired, isExpiringSoon } = models.paper + /** * @param {CozyClient} client - Instance of CozyClient * @param {string} serviceName - Name of the service @@ -63,111 +58,22 @@ export const fetchOrCreateTriggerByName = async (client, serviceName) => { return client.query(triggerByIdQuery.definition) } -/** - * @param {IOCozyFile} file - An CozyFile - * @param {string} dateLabel - Label of date - * @returns {string} Normalize expiration date (ISO) - */ -export const computeNormalizeExpirationDate = (file, dateLabel) => { - if (file.metadata[dateLabel]) { - if (dateLabel === 'referencedDate') { - return add(new Date(file.metadata[dateLabel] ?? file.created_at), { - days: 365 - }).toISOString() - } - return new Date(file.metadata[dateLabel]).toISOString() - } - - return null -} - -/** - * @param {IOCozyFile} file - An CozyFile - * @param {string} dateLabel - Label of date - * @returns {string} Notice date (ISO) - */ -export const computeNoticeDate = (file, dateLabel) => { - let noticeDays - if (file.metadata[dateLabel]) { - if (dateLabel === 'referencedDate') { - noticeDays = PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS - } - if (dateLabel === 'expirationDate') { - noticeDays = - parseInt(file.metadata.noticePeriod, 10) || DEFAULT_NOTICE_PERIOD_DAYS - } - } - if (!noticeDays) { - return null - } - - const normalizeExpirationDate = computeNormalizeExpirationDate( - file, - dateLabel - ) - - return normalizeExpirationDate - ? sub(new Date(normalizeExpirationDate), { - days: noticeDays - }).toISOString() - : null -} - -/** - * @param {IOCozyFile} file - An CozyFile - * @returns {{ label: string, country?: string, expirationDateAttribute: string }[]} papersToNotify - Rule in the paperDefinitions file - */ -const getPaperToNotify = file => { - const { papersToNotify } = papersDefinitions.notifications - - return papersToNotify.find(({ label, expirationDateAttribute, country }) => { - let validCountry = true - if (country && country !== 'fr') { - validCountry = file.metadata.country === country - } - - return ( - validCountry && - label === file.metadata.qualification.label && - file.metadata[expirationDateAttribute] - ) - }) -} - /** * @param {IOCozyFile[]} files - List of CozyFile - * @returns {{ file: IOCozyFile, noticeDate: string, expirationDate: string }[]} List of CozyFile that must be notified with their noticeDate & expirationDate + * @returns {{ file: IOCozyFile, expirationDate: string }[]} List of CozyFile that must be notified with their noticeDate & expirationDate */ export const getfilesNeedNotified = files => { return files + .filter(file => { + return isExpired(file) || isExpiringSoon(file) + }) .map(file => { - const paperToNotify = getPaperToNotify(file) - - if (paperToNotify) { - const noticeDate = computeNoticeDate( - file, - paperToNotify.expirationDateAttribute - ) - - if (!noticeDate) { - return null - } - - return new Date() >= new Date(noticeDate) - ? { - file, - noticeDate, - expirationDate: computeNormalizeExpirationDate( - file, - paperToNotify.expirationDateAttribute - ) - } - : null + const expirationDate = computeExpirationDate(file).toISOString() + return { + file, + expirationDate } - - return null }) - .filter(Boolean) } /** diff --git a/src/helpers/service.spec.js b/src/helpers/service.spec.js index 27f16ed4..f3910050 100644 --- a/src/helpers/service.spec.js +++ b/src/helpers/service.spec.js @@ -1,30 +1,6 @@ import MockDate from 'mockdate' -import { - computeNormalizeExpirationDate, - computeNoticeDate, - getfilesNeedNotified -} from 'src/helpers/service' - -jest.mock('cozy-mespapiers-lib/dist/constants/papersDefinitions.json', () => ({ - notifications: { - papersToNotify: [ - { - label: 'national_id_card', - country: 'fr', - expirationDateAttribute: 'expirationDate' - }, - { - label: 'residence_permit', - expirationDateAttribute: 'expirationDate' - }, - { - label: 'personal_sporting_licence', - expirationDateAttribute: 'referencedDate' - } - ] - } -})) +import { getfilesNeedNotified } from 'src/helpers/service' describe('Service', () => { beforeEach(() => { @@ -40,7 +16,8 @@ describe('Service', () => { created_at: '2022-09-01T00:00:00.000Z', metadata: { qualification: { label: 'national_id_card' }, - expirationDate: '2022-09-23T11:35:58.118Z' + expirationDate: '2022-09-23T11:35:58.118Z', + noticePeriod: '90' } } const fakeFile02 = { @@ -52,65 +29,32 @@ describe('Service', () => { referencedDate: '2021-09-23T11:35:58.118Z' } } - - describe('computeExpirationDate', () => { - it('should return expirationDate', () => { - const res = computeNormalizeExpirationDate(fakeFile01, 'expirationDate') - - expect(res).toBe('2022-09-23T11:35:58.118Z') - }) - it('should return referencedDate plus 365 days', () => { - const res = computeNormalizeExpirationDate(fakeFile02, 'referencedDate') - - expect(res).toBe('2022-09-23T11:35:58.118Z') - }) - - it('should return "null" if metadata is not found', () => { - const res = computeNormalizeExpirationDate(fakeFile02, 'expirationDate') - - expect(res).toBeNull() - }) - }) - - describe('computeNoticeDate', () => { - it('should return notice date for file with expirationDate metadata', () => { - const res = computeNoticeDate(fakeFile01, 'expirationDate') - - expect(res).toBe('2022-06-25T11:35:58.118Z') - }) - it('should return notice date for file with referencedDate metadata', () => { - const res = computeNoticeDate(fakeFile02, 'referencedDate') - - expect(res).toBe('2022-09-08T11:35:58.118Z') - }) - it('should return null for file without corresponding metadata', () => { - const res = computeNoticeDate(fakeFile02, 'expirationDate') - - expect(res).toBeNull() - }) - }) + const fakeFile03 = { + _id: '03', + name: 'unknown', + created_at: '2022-09-01T00:00:00.000Z' + } describe('getfilesNeedNotified', () => { it('should return only files that need to be notified', () => { - const res = getfilesNeedNotified([fakeFile01, fakeFile02]) + const res = getfilesNeedNotified([fakeFile01, fakeFile02, fakeFile03]) expect(res).toEqual([ { expirationDate: '2022-09-23T11:35:58.118Z', - noticeDate: '2022-06-25T11:35:58.118Z', file: { _id: '01', created_at: '2022-09-01T00:00:00.000Z', metadata: { + qualification: { label: 'national_id_card' }, expirationDate: '2022-09-23T11:35:58.118Z', - qualification: { label: 'national_id_card' } + noticePeriod: '90' }, name: 'national id card' } }, { expirationDate: '2022-09-23T11:35:58.118Z', - noticeDate: '2022-09-08T11:35:58.118Z', file: { _id: '02', name: 'personal sporting licence', diff --git a/yarn.lock b/yarn.lock index a3aed74d..92ee9e6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4972,16 +4972,17 @@ cozy-client@13.8.3: sift "^6.0.0" url-search-params-polyfill "^7.0.0" -cozy-client@^34.2.0: - version "34.2.0" - resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-34.2.0.tgz#d5af57b638b46dc640653e4bc603534353239632" - integrity sha512-t4YYXuGlrStQPEmReJm6nrrVWeKh4Jxrwuh4XSEuHCApH775wPGZZfDs1RdtYx/wCFPfI5PVM0BmvCosGju/bw== +cozy-client@^34.4.0: + version "34.4.0" + resolved "https://registry.yarnpkg.com/cozy-client/-/cozy-client-34.4.0.tgz#7d72aa3a38c08de4f3597c5fc68998553dd29f2f" + integrity sha512-Vh50SGKVHaxT1pDlmXn5AtEd8W2dzPFczXLOm4A9HuBttNUzS8cT2/MXfo9Xs7YV7nU/PhPlKtyvdrZqo9RxXQ== dependencies: "@cozy/minilog" "1.0.0" "@types/jest" "^26.0.20" "@types/lodash" "^4.14.170" btoa "^1.2.1" cozy-stack-client "^34.1.5" + date-fns "2.29.3" json-stable-stringify "^1.0.1" lodash "^4.17.13" microee "^0.0.6"