From cf6753b194f6a44fbb5211bbf208e30c1c2902fd Mon Sep 17 00:00:00 2001
From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com>
Date: Wed, 19 May 2021 17:26:39 +0200
Subject: [PATCH] add remote-doctype section and add todo for the service

---
 .../project_architecture/remote_doctypes.md   | 98 +++++++++++++++++++
 .../services/aggregator_usage_events.md       | 11 +++
 mkdocs.yml                                    |  1 +
 3 files changed, 110 insertions(+)
 create mode 100644 docs/ecolyo/project_architecture/remote_doctypes.md

diff --git a/docs/ecolyo/project_architecture/remote_doctypes.md b/docs/ecolyo/project_architecture/remote_doctypes.md
new file mode 100644
index 0000000..1332fdd
--- /dev/null
+++ b/docs/ecolyo/project_architecture/remote_doctypes.md
@@ -0,0 +1,98 @@
+# Creation of the remote doctype
+
+The remote doctype should be created in the cozy repo : [https://github.com/cozy/cozy-doctypes](https://github.com/cozy/cozy-doctypes)
+
+## Remote doctype org.ecolyo.usage
+
+To create the remote the first create a folder named 'org.ecolyo.usage' and then add a file name request in the folder.
+
+Here is the structure of the file:
+
+```bash
+POST https://API_URL
+Content-Type: application/json
+
+{{data}}
+```
+
+## Local remote doctype
+
+As the doctype if not yet avaible on cozy repo, we first need to test it on local. For that we can use the *—doctypes* command in the cozy-stack serve and use it to point to a local repo. 
+
+Here are the step:
+
+- clone the repo and add it in the ecolyo repo
+- create the remote doctype. Here we create a new folder named 'org.ecolyo.usage' and add the following request file (replace API_URL by the url of the API):
+
+```bash
+POST https://API_URL
+Content-Type: application/json
+
+{{data}}
+```
+
+- add the *—doctypes* command in the "docker/cozy-app-dev-with-app.sh" file and also the *-remote-allow-custom-port* flag
+
+```bash
+${COZY_STACK_PATH} serve --allow-root \
+                --appdir "${appdir}" \
+                --host "::" \
+                --port "${COZY_STACK_PORT}" \
+                --admin-port "${COZY_STACK_ADMIN_PORT}" \
+                --couchdb-url "${COUCHDB_URL}" \
+                --doctypes /data/cozy-doctypes \
+                --mail-port 1025 \
+                --mail-disable-tls \
+                --remote-allow-custom-port \
+                --fs-url "file://localhost${vfsdir}" &
+```
+
+- add the following content in "docker/disableCSP.yaml":
+
+```yaml
+remote_assets:
+    ecolyo-usage: http://cozy.tools:8080/remote/org.ecolyo.usage/
+```
+
+- build the docker image: docker build .
+- tag the image to [registry.forge.grandlyon.com/web-et-numerique/llle_project/ecolyo/cozy-env](http://registry.forge.grandlyon.com/web-et-numerique/llle_project/ecolyo/cozy-env)
+- add the following command to winstack script in the package.json:
+
+```yaml
+-v $PWD/cozy-doctypes:/data/cozy-doctypes
+```
+
+- start the server with the new image:
+
+```bash
+yarn winstack
+```
+
+- Change the content request param in io.cozy.doctypes/org.ecolyo.usage using the db tool ([http://localhost:5984/_utils](http://localhost:5984/_utils)):
+
+```bash
+"request": "POST https://API_URL\nContent-Type: application/json\n\n{{data}}",
+```
+
+# Use of remote doctype in the application
+
+Add permission in manifest.webapp
+
+```json
+"ecolyo-usage": {
+  "type": "org.ecolyo.usage",
+  "verbs": ["GET", "POST"]
+}
+```
+
+Don't forget to add the description
+
+Call the remote docType:
+
+```jsx
+const result = await client
+    .getStackClient()
+    .fetchJSON('POST', '/remote/org.ecolyo.usage', {
+      JSON.stringify(indicator),
+    })
+```
\ No newline at end of file
diff --git a/docs/ecolyo/services/aggregator_usage_events.md b/docs/ecolyo/services/aggregator_usage_events.md
index 55d8c7b..3cccc74 100644
--- a/docs/ecolyo/services/aggregator_usage_events.md
+++ b/docs/ecolyo/services/aggregator_usage_events.md
@@ -16,3 +16,14 @@ The service is responsible of :
 2. Aggregated them to indicators
 3. Send indicators to the remote doctype
 
+# To do
+
+As the remote doctype is not yet available for the service here are the remaining task to do:
+
+- In the manifest.webapp:
+    - Declare the remote doctype (see here)[/ecolyo/project_architecture/remote_doctypes.md]
+    - Define the cron to run every day: to check with cozy to ensure that all instances will not trigger the service at the same time
+    - Create the translation for "fr" and "en"
+- In the service:
+    - Update the remote doctype name in the sendIndicator method
+    - Add a check on the environment to ensure that the service will not post information on the remote doctype when its comes for alpha instance 
\ No newline at end of file
diff --git a/mkdocs.yml b/mkdocs.yml
index 25de77b..c1c42eb 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -40,6 +40,7 @@ nav:
           - Libraries: ecolyo/project_architecture/libraries.md
           - Environments: ecolyo/project_architecture/environments.md
           - DocTypes: ecolyo/project_architecture/doctypes.md
+          - Remote DocTypes: ecolyo/project_architecture/remote_doctypes.md
       - Application:
           - Description: ecolyo/application/description.md
           - Redux: ecolyo/application/redux.md
-- 
GitLab