Skip to content
Snippets Groups Projects
grdfConsent.service.ts 2.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • import axios, { AxiosRequestConfig } from 'axios'
    import { DateTime } from 'luxon'
    import { toast } from 'react-toastify'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import {
    
      GrdfConsentEntity,
      GrdfConsentPaginationEntity,
      IGrdfConsent,
      IGrdfConsentPagination,
    } from '../models/grdfConsent'
    
    export class GrdfConsentService {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
       * Search for consents
       * @param search
       * @param limit
       * @param page
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public searchConsents = async (
        search: string,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        limit: number,
        page: number,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<IGrdfConsentPagination | null> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
    
          const { data } = await axios.get<GrdfConsentPaginationEntity>(
            `/api/admin/grdf/consent?search=${search}&limit=${limit}&page=${page}`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          return this.parseConsentPagination(data)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          } else {
    
            toast.error('Erreur lors de la récupération des consentements')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          }
          console.error(e)
          return null
        }
      }
    
      /**
       * Converts consent entity into consent
       * @param consentEntity
       */
    
      public parseConsent = (consentEntity: GrdfConsentEntity): IGrdfConsent => {
    
        const createdAt = DateTime.fromISO(consentEntity.CreatedAt, {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          zone: 'utc',
    
        }).setLocale('fr-FR')
    
        const endDate = DateTime.fromISO(consentEntity.endDate, {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          zone: 'utc',
    
        }).setLocale('fr-FR')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        return {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          ID: consentEntity.ID,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          firstname: consentEntity.firstname,
          lastname: consentEntity.lastname,
    
          pce: consentEntity.pce,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          postalCode: consentEntity.postalCode,
        }
      }
    
      /**
       * Converts consent pagination entity into consent pagination
       * @param consentPaginationEntity
       */
      public parseConsentPagination = (
    
        consentPaginationEntity: GrdfConsentPaginationEntity
      ): IGrdfConsentPagination => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const rows = consentPaginationEntity.rows.map(consent =>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          this.parseConsent(consent)
        )
    
    
        const consentPagination: IGrdfConsentPagination = {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          rows: rows,
          totalRows: consentPaginationEntity.totalRows,
          totalPages: consentPaginationEntity.totalPages,
        }
        return consentPagination
      }
    }