-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add expiration helpers #1274
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3