From 76833183d6d63fdf498a12935a600cd386b791e2 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Tue, 4 Oct 2022 09:44:10 +0000
Subject: [PATCH] feat: update endDate and DeletedAt fields in Meilisearch on
 delete

---
 .gitlab-ci.yml             | 62 +++++++++++++++++++++++++++++++++++++-
 internal/models/consent.go | 22 ++++++++++++--
 2 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index abddb22..b046936 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -17,9 +17,10 @@ variables:
   GIT_DEPTH: 0
 
 stages:
+  - quality
   - build
 
-build:
+build-dev-master:
   image: docker:18.09
   services:
     - docker:18.09-dind
@@ -32,3 +33,62 @@ build:
     - dev
     - master
 
+build-mr:
+  image: docker:18.09
+  services:
+    - docker:18.09-dind
+  stage: build
+  script:
+    - docker build .
+  only:
+    - merge_requests
+
+sonarqube:
+  stage: quality
+  only:
+    - dev
+  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/sonarsource/sonar-scanner-cli:4
+  variables:
+    SONAR_USER_HOME: '${CI_PROJECT_DIR}/.sonar' # Defines the location of the analysis task cache
+    GIT_DEPTH: '0' # T
+  cache:
+    key: '${CI_JOB_NAME}'
+    paths:
+      - .sonar/cache
+  script:
+    - >
+      sonar-scanner
+      -Dsonar.projectName=ecolyo-agent-server
+      -Dsonar.projectVersion=1.0
+      -Dsonar.sourceEncoding=UTF-8
+      -Dsonar.projectBaseDir=.
+      -Dsonar.host.url=${SONAR_URL}
+      -Dsonar.projectKey=ecolyo-agent-server
+      -Dsonar.login=${SONAR_TOKEN}
+      -Dsonar.cpd.exclusions=internal/mocks/**,internal/**/*_test.go
+      -Dsonar.qualitygate.wait=true
+
+sonarqube:
+  stage: quality
+  only:
+    - merge_requests
+  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/sonarsource/sonar-scanner-cli:4
+  variables:
+    SONAR_USER_HOME: '${CI_PROJECT_DIR}/.sonar' # Defines the location of the analysis task cache
+    GIT_DEPTH: '0' # T
+  cache:
+    key: '${CI_JOB_NAME}'
+    paths:
+      - .sonar/cache
+  script:
+    - >
+      sonar-scanner
+      -Dsonar.projectName=ecolyo-agent-server-mr
+      -Dsonar.projectVersion=1.0
+      -Dsonar.sourceEncoding=UTF-8
+      -Dsonar.projectBaseDir=.
+      -Dsonar.host.url=${SONAR_URL}
+      -Dsonar.projectKey=ecolyo-agent-server-mr
+      -Dsonar.login=${SONAR_MR_TOKEN}
+      -Dsonar.cpd.exclusions=internal/mocks/**,internal/**/*_test.go
+      -Dsonar.qualitygate.wait=true
diff --git a/internal/models/consent.go b/internal/models/consent.go
index c76ed46..abd5f79 100644
--- a/internal/models/consent.go
+++ b/internal/models/consent.go
@@ -3,7 +3,6 @@ package models
 import (
 	"encoding/json"
 	"errors"
-	"fmt"
 	"log"
 	"net/http"
 	"time"
@@ -151,7 +150,7 @@ func (dh *DataHandler) DeleteConsentById(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	// Update and save consent
+	// Update and save consent in MySQL
 	consent.EndDate = time.Now()
 	err = dh.sqlClient.Save(&consent).Error
 	if err != nil {
@@ -162,6 +161,16 @@ func (dh *DataHandler) DeleteConsentById(w http.ResponseWriter, r *http.Request)
 
 	dh.sqlClient.Delete(&consent)
 
+	// Update and save consent in Meilisearch
+	deletedConsent := []map[string]interface{}{
+		{
+			"ID":        consent.ID,
+			"endDate":   consent.EndDate,
+			"DeletedAt": consent.DeletedAt,
+		},
+	}
+	dh.meiliClient.Index("consents").UpdateDocuments(deletedConsent)
+
 	log.Printf("| deleted consent | id : %d | %v", id, r.RemoteAddr)
 }
 
@@ -179,10 +188,17 @@ func (dh *DataHandler) SearchConsent(w http.ResponseWriter, r *http.Request) {
 
 		hits, err := json.Marshal(resp.Hits)
 		if err != nil {
-			fmt.Println("error:", err)
+			http.Error(w, "error when marshal hits", http.StatusInternalServerError)
+			log.Println(err.Error())
+			return
 		}
 		var consents []Consent
 		err = json.Unmarshal(hits, &consents)
+		if err != nil {
+			http.Error(w, "error when unmarshal hits", http.StatusInternalServerError)
+			log.Println(err.Error())
+			return
+		}
 
 		w.Header().Set("Content-Type", "application/json")
 		json.NewEncoder(w).Encode(consents)
-- 
GitLab