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

Add expiration helpers #1274

Merged
merged 3 commits into from
Dec 6, 2022
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
5 changes: 3 additions & 2 deletions docs/api/cozy-client/modules/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [folder](models.folder.md)
* [instance](models.instance.md)
* [note](models.note.md)
* [paper](models.paper.md)
* [permission](models.permission.md)
* [sharing](models.sharing.md)
* [timeseries](models.timeseries.md)
Expand All @@ -28,7 +29,7 @@

*Defined in*

[packages/cozy-client/src/models/index.js:19](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/index.js#L19)
[packages/cozy-client/src/models/index.js:20](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/index.js#L20)

***

Expand All @@ -38,4 +39,4 @@

*Defined in*

[packages/cozy-client/src/models/index.js:18](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/index.js#L18)
[packages/cozy-client/src/models/index.js:19](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/index.js#L19)
153 changes: 153 additions & 0 deletions docs/api/cozy-client/modules/models.paper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
[cozy-client](../README.md) / [models](models.md) / paper

# Namespace: paper

[models](models.md).paper

## Type aliases

### IOCozyFile

Ƭ **IOCozyFile**<>: `IOCozyFile`

*Defined in*

[packages/cozy-client/src/models/paper.js:5](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L5)

## Functions

### computeExpirationDate

▸ **computeExpirationDate**(`file`): `Date`

**`description`** Computes et returns the expiration date of the given file, or null if it is not expiring

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`Date`

Expiration date

*Defined in*

[packages/cozy-client/src/models/paper.js:85](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L85)

***

### computeExpirationNoticeDate

▸ **computeExpirationNoticeDate**(`file`): `Date`

**`description`** Computes et returns the expiration notice date of the given file, or null if it is not expiring

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`Date`

Expiration notice date

*Defined in*

[packages/cozy-client/src/models/paper.js:121](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L121)

***

### computeExpirationNoticeLink

▸ **computeExpirationNoticeLink**(`file`): `string`

**`description`** Computes et returns the expiration notice link of the given file, or null if it has none or it is not expiring

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`string`

Expiration notice link

*Defined in*

[packages/cozy-client/src/models/paper.js:140](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L140)

***

### isExpired

▸ **isExpired**(`file`): `boolean`

**`description`** Tells if the given file is expiring and if today is after its expiration date

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`boolean`

*Defined in*

[packages/cozy-client/src/models/paper.js:155](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L155)

***

### isExpiring

▸ **isExpiring**(`file`): `boolean`

**`description`** Tells if a given file matches one of the known types of expiring papers

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`boolean`

*Defined in*

[packages/cozy-client/src/models/paper.js:67](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L67)

***

### isExpiringSoon

▸ **isExpiringSoon**(`file`): `boolean`

**`description`** Tells if the given file is expiring and if today is between its expiration notice date and its expiration date

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `file` | `IOCozyFile` | io.cozy.files document |

*Returns*

`boolean`

*Defined in*

[packages/cozy-client/src/models/paper.js:167](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L167)
2 changes: 1 addition & 1 deletion packages/cozy-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@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 Expand Up @@ -51,7 +52,6 @@
"cozy-flags": "2.10.2",
"cozy-logger": "1.7.0",
"cozy-ui": "48.0.0",
"date-fns": "2.27.0",
"jsdoc-plugin-intersection": "1.0.4",
"mockdate": "2.0.5",
"react": "16.14.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/cozy-client/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as timeseries from './timeseries'
import * as sharing from './sharing'
import * as dacc from './dacc'
import * as doctypes from './doctypes'
import * as paper from './paper'

// For backward compatibility before 9.0.0
const triggers = trigger
Expand All @@ -35,5 +36,6 @@ export {
timeseries,
sharing,
dacc,
doctypes
doctypes,
paper
}
177 changes: 177 additions & 0 deletions packages/cozy-client/src/models/paper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import add from 'date-fns/add'
import sub from 'date-fns/sub'

/**
* @typedef {import("../types").IOCozyFile} IOCozyFile
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3


const PERSONAL_SPORTING_LICENCE_PERIOD_DAYS = 365
const PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS = 15

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if a given file is a national id card, is French, has an expiration date set and has a notice period set
*/
const isExpiringFrenchNationalIdCard = file => {
const label = file.metadata?.qualification?.label
const country = file.metadata?.country
const expirationDate = file.metadata?.expirationDate
const noticePeriod = file.metadata?.noticePeriod
if (
label === 'national_id_card' &&
(!country || country === 'fr') &&
expirationDate &&
noticePeriod
) {
return true
}
return false
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if a given file is a residence permit, has an expiration date set and a notice period set
*/
const isExpiringResidencePermit = file => {
const label = file.metadata?.qualification?.label
const expirationDate = file.metadata?.expirationDate
const noticePeriod = file.metadata?.noticePeriod
if (label === 'residence_permit' && expirationDate && noticePeriod) {
return true
}
return false
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if a given file is a sporting license, has a reference date set or a creation date if not
*/
const isExpiringPersonalSportingLicense = file => {
const label = file.metadata?.qualification?.label
const referencedDate = file.metadata?.referencedDate
const created_at = file.created_at
if (label === 'personal_sporting_licence' && (referencedDate || created_at)) {
return true
}
return false
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if a given file matches one of the known types of expiring papers
*/
export const isExpiring = file => {
if (isExpiringFrenchNationalIdCard(file)) {
return true
}
if (isExpiringResidencePermit(file)) {
return true
}
if (isExpiringPersonalSportingLicense(file)) {
return true
}
return false
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {Date | null} Expiration date
* @description Computes et returns the expiration date of the given file, or null if it is not expiring
*/
export const computeExpirationDate = file => {
if (isExpiringFrenchNationalIdCard(file) || isExpiringResidencePermit(file)) {
const expirationDate = file.metadata?.expirationDate
return new Date(expirationDate)
}
if (isExpiringPersonalSportingLicense(file)) {
const referencedDate = file.metadata?.referencedDate
const created_at = file.created_at
return add(new Date(referencedDate ?? created_at), {
days: PERSONAL_SPORTING_LICENCE_PERIOD_DAYS
})
}
return null
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {number | null} Expiration notice period in days
* @description Computes et returns the expiration notice period of the given file, or null if it is not expiring
*/
const computeExpirationNoticePeriodInDays = file => {
if (isExpiringFrenchNationalIdCard(file) || isExpiringResidencePermit(file)) {
const noticePeriodInDays = file.metadata?.noticePeriod
return parseInt(noticePeriodInDays, 10)
}
if (isExpiringPersonalSportingLicense(file)) {
return PERSONAL_SPORTING_LICENCE_NOTICE_PERIOD_DAYS
}
return null
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {Date | null} Expiration notice date
* @description Computes et returns the expiration notice date of the given file, or null if it is not expiring
*/
export const computeExpirationNoticeDate = file => {
const expirationDate = computeExpirationDate(file)
if (expirationDate == null) {
return null
}
const noticePeriodInDays = computeExpirationNoticePeriodInDays(file)
if (noticePeriodInDays == null) {
return null
}
return sub(expirationDate, {
days: noticePeriodInDays
})
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {string | null} Expiration notice link
* @description Computes et returns the expiration notice link of the given file, or null if it has none or it is not expiring
*/
export const computeExpirationNoticeLink = file => {
if (isExpiringFrenchNationalIdCard(file)) {
return 'https://www.service-public.fr/particuliers/vosdroits/N358'
}
if (isExpiringResidencePermit(file)) {
return 'https://www.service-public.fr/particuliers/vosdroits/N110'
}
return null
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if the given file is expiring and if today is after its expiration date
*/
export const isExpired = file => {
const now = new Date()
const expirationDate = computeExpirationDate(file)
const isExpired = expirationDate != null && expirationDate <= now
return isExpired
}

/**
* @param {IOCozyFile} file - io.cozy.files document
* @returns {boolean}
* @description Tells if the given file is expiring and if today is between its expiration notice date and its expiration date
*/
export const isExpiringSoon = file => {
const now = new Date()
const expirationDate = computeExpirationDate(file)
const expirationNoticeDate = computeExpirationNoticeDate(file)
const isExpiringSoon =
expirationDate != null &&
expirationNoticeDate != null &&
expirationNoticeDate <= now &&
now < expirationDate
return isExpiringSoon
}
Loading