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 38df33b3..b9902d74 100644 --- a/src/helpers/service.js +++ b/src/helpers/service.js @@ -1,17 +1,8 @@ -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 { models } from 'cozy-client' -import { - APP_SLUG, - DEFAULT_NOTICE_PERIOD_DAYS, - PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS, - TRIGGERS_DOCTYPE -} from 'src/constants' +import { APP_SLUG, TRIGGERS_DOCTYPE } from 'src/constants' import { buildAllFilesToNotifyQuery, buildTriggerByIdQuery, @@ -67,77 +58,6 @@ 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, expirationDate: string }[]} List of CozyFile that must be notified with their noticeDate & expirationDate diff --git a/src/helpers/service.spec.js b/src/helpers/service.spec.js index 4a7b44e7..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(() => { @@ -59,43 +35,6 @@ describe('Service', () => { created_at: '2022-09-01T00:00:00.000Z' } - 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() - }) - }) - describe('getfilesNeedNotified', () => { it('should return only files that need to be notified', () => { const res = getfilesNeedNotified([fakeFile01, fakeFile02, fakeFile03])