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

Add expiration helpers #1274

merged 3 commits into from
Dec 6, 2022

Conversation

PolariTOON
Copy link
Contributor

@PolariTOON PolariTOON commented Nov 29, 2022

This extracts and reimplements many helpers from mespapiers that were initially implemented in cozy/mespapiers#329 and also add new ones.
The rationale behind this change is to be able to use these helpers in the viewer (cozy-ui and thus also mespapiers and cozy-drive) and also cozy-mespapiers-lib, instead of only be able to use them for mespapiers services.

return true
}
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should start coding differently, something more declarative instead of having those imperative if... We should only rely on a configuration file and the code will do the rest:

const config = [
'frenchNationalCard: {
'isExpiring': true, 
'expiringCondition': file => .... 
}
]

Like that there is no need to update the code if we want to add a new paper, change expiring condition or else. We just have to edit this config. If we think even further this config can be a json file served directly by the cozy. Like that even the PO will be able to edit by him/herself the content.

Copy link
Contributor Author

@PolariTOON PolariTOON Nov 30, 2022

Choose a reason for hiding this comment

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

The original approach was indeed to declare everything in a JSON (see paperDefinitions in the first commit, that is later removed), but we discussed extensively between devs and decided to move away from a declarative coding since the conditions to determine if a given paper expires and how to compute their expiration and notice dates are already very different.

For each relevant paper, the expiring condition is different because we use different attributes and not as many. Moreover the way we compute dates is also already different, with different fallback behavior and default delays. Making something purely declarative and thus generic would need many parameters and add noise in my opinion. As you first suggested it is easier to define dedicated methods for each paper anyway, so at this point i don't really see the benefit to define either a very complex JSON config that won't really be extensible in practice or methods instead of functions.

Note that we also discussed this with the PO and confirmed that were was no need for them to be able to edit themselves a config.

Copy link
Contributor

@JF-Cozy JF-Cozy Nov 30, 2022

Choose a reason for hiding this comment

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

we discussed extensively between devs and decided to move away from a declarative coding

Attention à ne pas mélanger tous les sujets. A mon sens lors de nos échanges nous n'avons pas exclu spécifiquement le fait du faire du déclaratif de manière absolu. Là dessus, par défaut, perso je suis pas fermé.

Maintenant le fait que ce soit facile ou pas, et pertinent ou non (ce que tu évoques), et la modif via un PO (dont on a parlé aussi) sont d'autres points.

Copy link
Contributor

Choose a reason for hiding this comment

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

we discussed extensively between devs and decided to move away from a declarative coding since the conditions to determine if a given paper expires and how to compute their expiration and notice dates are already very different.

I don't buy this argument. Each entry of the config has its own conditions and expiration methods. This is totally normal.
Since you discussed that extensively, do you have a written conclusion to share?

We have a few declarative instructions in Drive & Harvest for this kind of stuff and this is very powerful, useful and help us a lot with the maintenance and scalability.

Note that we also discussed this with the PO and confirmed that were was no need for them to be able to edit themselves a config.

As for today. Not in the future.

In my opinion this kind of development is much more better in a declarative form and the config is much more readable than having a lot of if / else condition. Also, creating this "magical function" is maybe a bit harder but not so much, and writing test for it it's pretty easy.

To go further, I really don't like the fact to publish a new Cozy Client version each time we add a qualification label and now every time we add an expiration to something. Having a declarative way of doing it will give us the opportunity to have a better solution for that.

I understand that this is not the path you chose, I'll not block this PR for that, but next time please add me in the thinking loop either by creating a SEC and putting other in the loop.

return 'https://www.service-public.fr/particuliers/vosdroits/F1029'
}
return null
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Those code should be inside the config.

@PolariTOON PolariTOON force-pushed the feat/add-expiration-helpers branch from d611451 to 8f6cc27 Compare November 30, 2022 23:50
@JF-Cozy
Copy link
Contributor

JF-Cozy commented Dec 1, 2022

exemple de déclaratif :

import { national, passport } from 'identityLabels'
import { wedding } from 'familyLabels'

const papersExpirationConfig = {
  national_id_card: {
    expiration: {
      condition: file => national.condition(file),
      date: file => national.date(file)
    },
    notification: {
      sendingDate: () => {}
    },
    alert: {
      renewalLink: () => {}
    }
  },
  passport: {
    expiration: {
      condition: file => national.condition(file),
      date: file => passport.date(file)
    },
    notification: {
      sendingDate: () => {}
    },
    alert: {
      renewalLink: () => {}
    }
  }
}

export const cIsExpiring = file => {}
export const cExpirationDate = file => {
  if cIsExpiring(file)
  else
}
export const cSendingDate = file => {
  if cIsExpiring(file)
  else
}
export const cRenewalLink = file => {
  if cIsExpiring(file)
  else
}

@PolariTOON
Copy link
Contributor Author

Some of the changes since the latest review:

  • added date-fns to the direct dependencies and updated it
  • moved the helpers to paper.js
  • removed the link for the personal sporting license
  • fixed how the country is retrieved
  • removed the default notice period for the french national id card and the residence permit
  • used optional chaining instead of lodash
  • added missing types definition for attributes used in these helpers

I also opened an alternate PR with a final refactor at #1281 that could land instead of this one, or separately afterwards.
I think this is ready for another round of reviews. @Crash--, could you take another look ?

@@ -170,6 +171,11 @@ import { QueryDefinition } from './queries/dsl'
* @property {object} [schema] - the schema used by prosemirror (with notes and marks serialized as arrays to preserve the order).
* @property {string} [title] - the initial title of the note (that will also be used for the file name)
* @property {number} [version] - Number of a note
* @property {Qualification} [qualification] - Qualification of the file
* @property {string} [country] - Country of the paper
* @property {string} [expirationDate] - Expiration date of the paper
Copy link
Contributor

Choose a reason for hiding this comment

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

String? Are-you sure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The expiration date is currently set by the user in an date field in Mes Papiers and stored as is in the metadata. So yes this is a string.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not aware of this document. I have no strong opinion. I mean, this is a date, indeed, but formatted as a string. So for the purpose of type checking in JSDOC this is a string (unless we want to narrow it with template literal types), but conceptually this is a date, so that's not wrong to describe it like this in the documentation you pointed to.

* @property {Qualification} [qualification] - Qualification of the file
* @property {string} [country] - Country of the paper
* @property {string} [expirationDate] - Expiration date of the paper
* @property {string} [referencedDate] - Reference date of the paper
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reference date is also provided by the user as a string.

@Crash--
Copy link
Contributor

Crash-- commented Dec 5, 2022

Can you squash few commits please? I think it's not useful here to have the refactor between lodash.get and ? for instance.

I think having 3 commits can be great:

  • one to add date-fns as dep
  • one to add all the expiration stuff
  • one to add the missing jsdoc

@PolariTOON PolariTOON force-pushed the feat/add-expiration-helpers branch from 8fe24c3 to a6b74bd Compare December 6, 2022 10:52
@PolariTOON
Copy link
Contributor Author

I added a description for each helper to clarify what they do, and imported IOCozyFile and Qualification as JSDOC types instead of these "ghost" variables. I squashed the whole thing into 3 commits to eliminate the intermediate refactors as @Crash-- suggested. Can you take one (final?) look?

@@ -51,7 +52,7 @@
"cozy-flags": "2.10.2",
"cozy-logger": "1.7.0",
"cozy-ui": "48.0.0",
"date-fns": "2.27.0",
"date-fns": "2.29.3",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Vu qu'on l'ajoute en dep, aucune raison de la garder en devDep


/**
* @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

This extracts and rewrites expiration helpers from `mespapiers` that will be used directly in `cozy-ui`, `cozy-mespapiers-lib` and also `mespapiers`.

This avoids using `paperDefinitions` as a config.
It may not be generic enough in the future and
we don't have any need for that right now.

This also removes the need to pass a `dateLabel` argument to these helpers.

This also adds new helpers, one giving the notice link if available, and two other for comparing dates.

This also removes the default notice period, meaning that papers won't be considered as expiring if the user has not set any notice period and that the alert won't appear and no notification will be sent.
@PolariTOON PolariTOON force-pushed the feat/add-expiration-helpers branch from a6b74bd to b6722a3 Compare December 6, 2022 11:36
@PolariTOON PolariTOON merged commit e9fc08b into master Dec 6, 2022
@PolariTOON PolariTOON deleted the feat/add-expiration-helpers branch December 6, 2022 13:03
Crash-- added a commit to cozy/cozy-drive that referenced this pull request Jan 11, 2023
Since the cozy-client upgrade to 34.7.1 date-fns is a needed
dep for a few of its models. But we don't need them in Drive
for now and we have a few issues with the tree shaking of
cozy-client.

(see cozy/cozy-client#1274)

So the workaround is to force the resolution to date-fns 1
which is required by drive, in order to not duplicate the
package.

We should not have issue with that. When we'll upgrade
date-fns to v2 in drive & ui, then, we'll be able to
remove this line.
Crash-- added a commit that referenced this pull request Jan 11, 2023
In order to enhance the cozy-client tree shaking,
we should avoind importing * and exporting.

We had an issue with this PR
#1274

because this PR adds date-fns v2 as dep, but even
if the consuming app doesn't use the concerned model
it was imported.

BREAKING CHANGE: You can not use
import { models } from cozy-client
...
models.files.func()

You have to change it to:
import { func } from cozy-client/dist/models/files
cballevre pushed a commit to cozy/cozy-drive that referenced this pull request Jan 11, 2023
Since the cozy-client upgrade to 34.7.1 date-fns is a needed
dep for a few of its models. But we don't need them in Drive
for now and we have a few issues with the tree shaking of
cozy-client.

(see cozy/cozy-client#1274)

So the workaround is to force the resolution to date-fns 1
which is required by drive, in order to not duplicate the
package.

We should not have issue with that. When we'll upgrade
date-fns to v2 in drive & ui, then, we'll be able to
remove this line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants