Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put expiration service on top of cozy client helpers #330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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}`)
120 changes: 13 additions & 107 deletions src/helpers/service.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}

/**
Expand Down
78 changes: 11 additions & 67 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 All @@ -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 = {
Expand All @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4972,16 +4972,17 @@ [email protected]:
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"
Expand Down