Skip to content

Commit

Permalink
refactor: Put getfilesNeedNotified on top of new cozy-client helpers
Browse files Browse the repository at this point in the history
With a new test.
  • Loading branch information
PolariTOON committed Dec 6, 2022
1 parent f42005f commit bdb02bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
37 changes: 11 additions & 26 deletions src/helpers/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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,
Expand All @@ -16,6 +18,8 @@ import {
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 @@ -136,38 +140,19 @@ const getPaperToNotify = file => {

/**
* @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: Date }[]} 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
return {
file,
expirationDate: computeExpirationDate(file)
}

return null
})
.filter(Boolean)
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/helpers/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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,6 +53,11 @@ describe('Service', () => {
referencedDate: '2021-09-23T11:35:58.118Z'
}
}
const fakeFile03 = {
_id: '03',
name: 'unknown',
created_at: '2022-09-01T00:00:00.000Z'
}

describe('computeExpirationDate', () => {
it('should return expirationDate', () => {
Expand Down Expand Up @@ -92,25 +98,24 @@ describe('Service', () => {

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

0 comments on commit bdb02bb

Please sign in to comment.