# PRESTAKIT : Upgrading from v16 to v17

For the commands in this guide, it is supposed that your are in the `front` folder of your project.

## PRESTATIONS-NG

The main feature in `prestations-ng` v17 is the update of the `@angular/*` peer dependencies
to `^17.2.4`, using `node 20+` to build, `npm 10+` for package manager and `Foehn 2+ (Bootstrap 5+)` for our design system.

### Migrating to ESLint

(NB : you can skip this step if you already have a `front/eslintrc.json` file, it means it is already done.)
Follow [our migration guide](ESLINT_MIGRATION_GUIDE.md) to migrate your project from TSLint to ESLint.

### Package manager NPM instead of YARN

We now recommend using `npm` instead of `yarn`.

**NB: Don't need `run` when running `npm` for https://docs.npmjs.com/cli/v10/commands**

- When using `prestations-ng` as a link, you need to run (you can do it in any case, it won't harm and like this you are sure):

```bash
yarn unlink @dsivd/prestations-ng
yarn install --force
```

- Delete `yarn.lock` file and `yarn-error.log` (if you have one).
- You can search for `yarn` in your project and replace it by `npm run` (do not take into account `.browserslistrc` as it will be replaced).

```bash
find . -type f ! -name '.browserslistrc' -exec sed -i 's/yarn/npm run/g' {} +
```

- Then run:

```bash
npm install
git add -A
```

If you have vulnerabilities, you can run:

```bash
npm update
```

NB: be patient...

### Updating to Angular 16

[https://update.angular.io/?l=3&v=15.0-16.0](https://update.angular.io/?l=3&v=15.0-16.0)

**NB: Commit before continuing so your repository is clean.**

Remove your `resolutions` in `package.json` before starting : if you have `resolutions` in `package.json`, remove manually the section.
In the next commands, commit before each ng update.

```bash
ng update @angular/core@15 @angular/cli@15
ng update @dsivd/prestations-ng@16

npm install @angular/cli@16 -g
ng update @angular/core@16 @angular/cli@16
```

### Running with node 20+

#### Switch to node 20+

##### If you are using our `slave-apps`

Be sure to update your `slave-apps` git repository and source your `.bashrc` (or whatever file you're using).

Verify your node version in your terminal by executing:

```bash
node -v
```

**NB: If version is still v16+ by default, type `node20` in your terminal to switch to node v20+**

```bash
node20
```

##### If you are NOT using our `slave-apps`

Be sure to use a node version 20+ in your terminal:

```bash
node -v
```

#### In your project

Update your `package.json` file to specify the node engine:

```json
{
    "engines": {
        "node": ">=20.11.0"
    }
}
```

#### Update your jenkins files

- Add `nodeVersion: 20` to use node 20
- Remove any `nodeLabel` (nodeLabels are now set by `snap2Devex`/`stable2Devex`)

##### UT folder

```diff
def args = [
+   nodeVersion: 20,
-   nodeLabel: 'chromium-headless'
]
```

##### devex > snap folder

```diff
def buildArgs = [
+   nodeVersion: 20,
-   nodeLabel: 'chromium-headless'
]
```

##### devex > stable folder

```diff
def args = [
+   nodeVersion: 20,
-   nodeLabel: 'chromium-headless'
]
```

### Updating to Angular 17

`prestations-ng` **v17+** now supports **Angular v17**.

Just FYI if you are curious about the details, you can find the Angular update guide at this address [https://update.angular.io/?l=3&v=16.0-17.0](https://update.angular.io/?l=3&v=16.0-17.0).

#### Update your `front/.gitignore` file

Add `/.nx/cache`

#### When having migrated to ESLint

```bash
npm install @angular/cli@17 -g
ng update @angular/core@17 @angular/cli@17 --force
ng update @dsivd/prestations-ng@17
ng update rxjs@7
ng update @angular-eslint/builder@17
ng update @angular-eslint/eslint-plugin@17 @angular-eslint/eslint-plugin-template@17 @angular-eslint/template-parser@17
ng update @typescript-eslint/eslint-plugin@7 @typescript-eslint/parser@7
ng update eslint-plugin-import eslint-plugin-jasmine eslint-plugin-jsdoc eslint-plugin-rxjs-angular
ng update eslint@8
ng update eslint-plugin-rxjs
ng update @types/jasmine@5 @types/node@20 ts-node@10

npm remove puppeteer
npm install puppeteer@~23.5.2 --save-dev

npm update
```

If you go errors like "npm error code ERESOLVE
npm error ERESOLVE could not resolve
" while executing `npm remove puppeteer`, you can run :
`npm i primeng@^17 primeicons@^7 @angular/cdk@^17 @angular/material@^17` to align versions first.

#### Align Angular & ESLint versions

Make sure that @angular & @angular-eslint packages are aligned up to the minor version (17.X).
If it is not the case, run the following command with the wanted version (here we downgrade from 17.5 to 17.3 as an exemple)

```bash
npm install @angular-eslint/builder@17.3.0 @angular-eslint/eslint-plugin@17.3.0 @angular-eslint/eslint-plugin-template@17.3.0 @angular-eslint/template-parser@17.3.0
```

and force the version by removing `^`

```diff
-   "@angular-eslint/builder": "^17.3.0",
-   "@angular-eslint/eslint-plugin": "^17.3.0",
-   "@angular-eslint/eslint-plugin-template": "^17.3.0",
-   "@angular-eslint/template-parser": "^17.3.0",
+   "@angular-eslint/builder": "17.3.0",
+   "@angular-eslint/eslint-plugin": "17.3.0",
+   "@angular-eslint/eslint-plugin-template": "17.3.0",
+   "@angular-eslint/template-parser": "17.3.0",
```

#### Browsers list update

Run update schematic:

```bash
ng g @dsivd/prestations-ng:replace-browsers-list
```

#### Migration to Foehn 2 and Boostrap 5

Run migration schematic:

```bash
ng g @dsivd/prestations-ng:foehn2-bootstrap5-migration
```

**NB: Be aware that you will have to check every changes made to your repository to be sure everything is fine.**

You will need to check manually your:

- Java `dictionary.properties` for any HTML styling (search for: `class=`)
- TypeScript (`.ts`) files for any dynamique CSS classes or tests (`.spec.ts`)

##### More information here

- https://getbootstrap.com/docs/5.0/migration/ for Bootstrap 5 migration.
- https://github.com/twbs/bootstrap/issues/32753 for the list of class changes in Bootstrap 5.
- https://github.com/DSI-VD/foehn/blob/master/CHANGELOG.md for Foehn 2 migration.

### Update ".eslintrc.json" file if your project was already migrated to ESLint

**NB: Replace your current `.eslintrc.json` file by the one found in [ESLINT_MIGRATION.GUIDE](ESLINT_MIGRATION_GUIDE.md#customizing_eslint__inspired_from_otia_and_prestations-ng_s_files_for_rules_).**
You will have to fix the json file after the copy as it won't copy the double quotes around the properties. (ask IntelliJ to do it for you, alt-enter, Wrap with double quotes -> fix all compliance with json standard ...)

Then run

```bash
ng lint --fix=true
```

and finally fix manually remaining errors. When encountering following warning in your `*.spec.ts`:

**`warning  Deprecated: Use provideRouter or RouterModule/RouterModule.forRoot instead`**

replace `RouterTestingModule` by `RouterModule.forRoot([])`

### Cloudflare (`app.module.ts`)

If you have captcha cloudflare setup already, you need to remove `CAPTCHA_URL_TOKEN` override

```diff
@NgModule({
    providers: [
        META_SERVICE_PROVIDER,
-       {
-           provide: CAPTCHA_URL_TOKEN,
-           useValue: 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=reCaptchaLoad&render=explicit&compat=recaptcha'
-       },
    ]
})
```

#### Cloudflare turnstile verify delegated to Cybercache

Verification of the captcha is now made by Cybercache and not by the application itself (through `prestations-be`). You should remove all properties like `recaptcha.XXX`.
All these properties are now stored in Cybercache's configuration.

Therefore, you can remove properties related to this configuration :

- `recaptcha.byPassUUID`
- `recaptcha.proxyUrl`
- `recaptcha.proxyPort`
- `recaptcha.secretkey`
- `recaptcha.publickey`

Cookie storage is kept as before, therefore you can keep cookie related properties that starts with `recaptcha.cookie.YYY` in your properties.

### gesdem-confirmation / foehn-feedback-notification

If you are not using directly `GesdemConfirmationComponent` in your `app-routing.module.ts` for your `confirmation` path, you either need to:

- use `<gesdem-confirmation></gesdem-confirmation>` component in your HTML template

**OR**

- add `<foehn-feedback-notification></foehn-feedback-notification>` component at the end of your HTML template (outside your `container` if you have one))

### upgrade styles for CSP (since 17.7.0)

**`⚠️ Order of styles in array will determine the final import order. Your "project src/styles.css" must always be placed at 
the end of the array as it may need to override other CSS. If you need to import "node_modules/bootstrap/dist/css/bootstrap.min.css",
 it should be first in array because "prestations-ng" styles need to override those from Bootstrap. ⚠️`**

You have to update your `styles.css` :

```diff
- @import '~@dsivd/foehn/dist/assets/styles/main.css';
```

and your `angular.json` (/projects/{nom-du-projet}/architect/build/options/styles):

```diff
            "styles": [
+             "node_modules/@dsivd/prestations-ng/assets/styles.css",
              "src/styles.css"
            ],
```

> If your application uses the BoCommonWebMvcConfig configuration class and your frontend is made using Angular, use SinglePageAppConfig configuration class instead.

### gesdem-action-recovery-registration

If you use the `<gesdem-action-recovery-registration>` component in a page which includes some `<foehn-error-pill>` or `<foehn-menu-item>` (i.e. e-vaudtax, pocama, any form with a menu)
you should set `dispatchFormErrors` to `true` :

- `<gesdem-action-recovery-registration [dispatchFormErrors]="true"><gesdem-action-recovery-registration>`

Otherwise the pills wil be set to "Renseigné" after a recovery registration even if some errors remains.

### foehn-table now extends FoehnInputComponent

- use `[model]` instead of `[list]` because it is deprecated
- add a `[name]` attribut to your component
- put `FoehnTableColumnConfiguration` templates inside `<foehn-table>[HERE]</foehn-table>` when they contain components extending `FoehnInputComponent`

### Language deprecations

When using `intellij`, you can run code inspection on your project to fix manually deprecations. Two ways:

- Intellij window menu, go to: `Code > Analyze code > Run inspection by name...` (or **Ctrl+Alt+Maj+i**) and type `Deprecated symbol used`.
- Or select your project in your project panel and `right click` on it then `Analyze > Run inspection by name...` (or **Ctrl+Alt+Maj+i**) and type `Deprecated symbol used`.

### Last but not least, check if your application is working!!!

#### Be sure your application gets a fresh start

```bash
rm -rf .angular/
rm -rf node_modules/
npm install
```

#### Build and launch your application

```bash
npm run build
npm start
```

Your are good if no errors are remaining!
Finally check if your tests run well :

```bash
npm run test
```

If there is a message complaining about HttpErrorResponse & NG0205: Injector has already been destroyed,
in your `app.component.spec.ts` file, add `HttpClientTestingModule` to `TestBed.configureTestingModule` in `beforeEach(waitForAsync` function

## PRESTATIONS-BE

From here on, we are back in the root folder of your project

### application.properties

```diff
recaptcha.cookie.path=${base.href}
- recaptcha.verify-url=https://challenges.cloudflare.com/turnstile/v0/siteverify
```

### pom.xml

#### With application parent

##### Use new application-parent

```diff
<parent>
    <groupId>ch.vd.cyber.parent</groupId>
    <artifactId>application-parent</artifactId>
-    <version>12.x.x</version>
+    <version>13.x.x</version>
    <relativePath/>
</parent>
```

##### Cleanup pom.xml

Then remove following plugin:

```diff
-<plugin>
-   <groupId>org.codehaus.mojo</groupId>
-   <artifactId>exec-maven-plugin</artifactId>
-   <version>${exec-maven-plugin.version}</version>
-   <configuration>
-       <workingDirectory>${basedir}/front</workingDirectory>
-   </configuration>
-   <executions>
-        <execution>
-            <id>exec-yarn-install</id>
-            <phase>prepare-package</phase>
-            <configuration>
-                <executable>${yarn.executable}</executable>
-                <arguments>
-                    <argument>--pure-lockfile</argument>
-                </arguments>
-                <skip>${build.nofront}</skip>
-            </configuration>
-            <goals>
-                <goal>exec</goal>
-            </goals>
-        </execution>
-        <execution>
-            <id>exec-ngcli-lint</id>
-            <phase>prepare-package</phase>
-            <configuration>
-                <executable>${yarn.executable}</executable>
-                <arguments>
-                    <argument>run</argument>
-                    <argument>lint</argument>
-                </arguments>
-                <skip>${skipTests}</skip>
-            </configuration>
-            <goals>
-                <goal>exec</goal>
-            </goals>
-        </execution>
-        <execution>
-            <id>exec-ngcli-test</id>
-            <phase>prepare-package</phase>
-            <configuration>
-                <executable>${yarn.executable}</executable>
-                <arguments>
-                    <argument>run</argument>
-                    <argument>test</argument>
-                </arguments>
-                <skip>${skipTests}</skip>
-            </configuration>
-            <goals>
-                <goal>exec</goal>
-            </goals>
-        </execution>
-        <execution>
-            <id>exec-ngcli-build</id>
-            <phase>prepare-package</phase>
-            <configuration>
-                <executable>${yarn.executable}</executable>
-                <arguments>
-                    <argument>run</argument>
-                    <argument>build</argument>
-                </arguments>
-                <skip>${build.nofront}</skip>
-            </configuration>
-            <goals>
-                <goal>exec</goal>
-           </goals>
-       </execution>
-   </executions>
-</plugin>
-<plugin>
-   <groupId>org.apache.maven.plugins</groupId>
-   <artifactId>maven-resources-plugin</artifactId>
-   <version>${maven-resources-plugin.version}</version>
-   <configuration>
-       <encoding>${project.build.sourceEncoding}</encoding>
-   </configuration>
-   <executions>
-       <execution>
-           <id>copy-ngcli-dist</id>
-           <phase>prepare-package</phase>
-           <goals>
-               <goal>copy-resources</goal>
-           </goals>
-           <configuration>
-               <outputDirectory>${basedir}/target/classes/static</outputDirectory>
-               <resources>
-                   <resource>
-                       <directory>front/dist</directory>
-                       <filtering>false</filtering>
-                   </resource>
-               </resources>
-           </configuration>
-       </execution>
-   </executions>
-</plugin>
```

If it is an Angular application but you have other configurations to build the front, add -Dskip-build-angular-front-npm in .mvn/maven-config at the same level as the pom.xml.

#### Without application parent

```diff
<properties>
-   <prestations-be.version>16.x.x</prestations-be.version>
+   <prestations-be.version>17.x.x</prestations-be.version>
</properties
```

Remove plugin executing some `yarn` (See `With application parent`) and replace by following plugin :

```xml
<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>${exec-maven-plugin.version}</version>
        <configuration>
            <workingDirectory>${front.srcPath}</workingDirectory>
        </configuration>
        <executions>
            <execution>
                <id>exec-npm-install</id>
                <phase>prepare-package</phase>
                <configuration>
                    <executable>${npm.executable}</executable>
                    <arguments>
                         <argument>ci</argument>
                        <argument>--pure-lockfile</argument>
                    </arguments>
                    <skip>${build.nofront}</skip>
                </configuration>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
            <execution>
                <id>exec-ngcli-lint</id>
                <phase>prepare-package</phase>
                <configuration>
                    <executable>${npm.executable}</executable>
                    <arguments>
                        <argument>run</argument>
                        <argument>lint</argument>
                    </arguments>
                    <skip>${skipTests}</skip>
                </configuration>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
            <execution>
                <id>exec-ngcli-test</id>
                <phase>prepare-package</phase>
                <configuration>
                    <executable>${npm.executable}</executable>
                    <arguments>
                        <argument>run</argument>
                        <argument>test</argument>
                    </arguments>
                    <skip>${skipTests}</skip>
                </configuration>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
            <execution>
                <id>exec-ngcli-build</id>
                <phase>prepare-package</phase>
                <configuration>
                    <executable>${npm.executable}</executable>
                    <arguments>
                        <argument>run</argument>
                        <argument>build</argument>
                    </arguments>
                    <skip>${build.nofront}</skip>
                </configuration>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>${maven-resources-plugin.version}</version>
        <configuration>
            <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
        <executions>
            <execution>
                <id>copy-ngcli-dist</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                    <resources>
                        <resource>
                            <directory>${front.distPath}</directory>
                            <filtering>false</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
            <execution>
                <id>copy-config</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    <resources>
                        <resource>
                            <directory>${project.build.directory}/unfiltered-config</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
```

Finally, run

```bash
mvn clean install
```

Hopefully, you should see a successful build.

### PostalLocalityHistoryId migration

Since version `@dsivd/prestations-ng@17.2.4`, the component `foehn-input-address` uses `Locality.zipCodeId` instead of `Locality.postalLocalityHistoryId`
to verify the equality of a `Locality`.

`Locality.postalLocalityHistoryId` is not a stable id and we should have used `Locality.zipCodeId` from the beginning.

If in your BO, you used to store `postalLocalityHistoryId` in your DB to retrieve a `Locality`, you should switch to storing the `zipCodeId`.

You can see an example of liquibase migration in `eldoradobo` :

- https://dsigit.etat-de-vaud.ch/outils/git/projects/CYBER/repos/eldorado/commits/e7ddc86edaac35d351d2c6cb0e175e3aeba40cd8
- https://dsigit.etat-de-vaud.ch/outils/git/projects/CYBER/repos/eldorado/commits/596b107b3ab9a630167c0a8f8a34312acd352d47

### Tag durable draft datas (since 17.6.0)

**As of this version the goal is to delete all draft data of an action after it has been transmitted to the BO**

You should set the parameter "Effacer les draft data" to `Oui` in catprest (check with Cyber or IMT team for more informations)

⚠️ Before that you have to make sure your application does not need to get a draft data after it has been transmitted

- This could be possible if you are required to display a value from the form in your confirmation page
- In that case, you have to annotate this field with `@KeepAfterTransmit` in your `I18nForm`

### Use @StringLength (since 17.6.1)

- to validate a String you should now use `ch.vd.cyber.prestationsbe.validation.StringLength`

### Use catprest-configurable email-sender (since 17.7.0)

- Instead of making your application send emails from `mail.from` property, get the sender from gesdem :
  gesDemService.getEmailSender(applicationKey)
- Even better (since 17.8.0), if you send a standard confirmation email, just use emailSdkService.
  sendCyberConfirmationEmail() or sendConfirmationEmail() instead of a local EmailNotificationService

### Use @ValidAddress (since 17.9.1)

- to validate a Address you should now use `ch.vd.cyber.prestationsbe.validation.ValidAddress`
- See [Address page](address#all_countries_address) to synchronize your back-end annotation with your front-end component
- ⚠️⚠️⚠️ **ACTION REQUIRED** ⚠️⚠️⚠️
    - When using **component as international** addresses (`isOnlySwiss = false`), you need to set this property to false in annotation
    - ex: **@ValidAddress(isOnlySwiss = false)**
- Annotation members:
    - boolean **isOnlySwiss()** default **true**
    - boolean **countryRequired()** default **true**
    - boolean **swissNpaLocalityRequired()** default **true**
    - boolean **streetRequired()** default **false**
    - boolean **streetNumberRequired()** default **false**
    - boolean **foreignZipCodeRequired()** default **true**
    - boolean **foreignLocalityRequired()** default **true**
    - boolean **foreignStreetRequired()** default **false**
    - boolean **foreignStreetNumberRequired()** default **false**
    - boolean **postOfficeBoxTextRequired()** default **false**
    - boolean **addressLine1Required()** default **false**
    - boolean **addressLine2Required()** default **false**
