Skip to content

Commit

Permalink
refactor: Remove the previous implementation of expiration helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
PolariTOON committed Dec 6, 2022
1 parent bdb02bb commit 362fb83
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 145 deletions.
2 changes: 0 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
82 changes: 1 addition & 81 deletions src/helpers/service.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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: Date }[]} List of CozyFile that must be notified with their noticeDate & expirationDate
Expand Down
63 changes: 1 addition & 62 deletions src/helpers/service.spec.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 362fb83

Please sign in to comment.