Skip to content
Snippets Groups Projects
consent.service.ts 2.4 KiB
Newer Older
import axios, { AxiosRequestConfig } from 'axios'
import { DateTime } from 'luxon'
import { toast } from 'react-toastify'
Guilhem CARRON's avatar
Guilhem CARRON committed
import {
Bastien DUMONT's avatar
Bastien DUMONT committed
  ConsentEntity,
Guilhem CARRON's avatar
Guilhem CARRON committed
  ConsentPaginationEntity,
Bastien DUMONT's avatar
Bastien DUMONT committed
  IConsent,
Guilhem CARRON's avatar
Guilhem CARRON committed
  IConsentPagination,
Bastien DUMONT's avatar
Bastien DUMONT committed
} from '../models/consent.model'
Guilhem CARRON's avatar
Guilhem CARRON committed

export class ConsentService {
  /**
   * 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
Guilhem CARRON's avatar
Guilhem CARRON committed
  ): Promise<IConsentPagination | null> => {
    try {
      const { data } = await axios.get(
        `/api/admin/consent?search=${search}&limit=${limit}&page=${page}`,
Bastien DUMONT's avatar
Bastien DUMONT committed
        axiosHeaders
Guilhem CARRON's avatar
Guilhem CARRON committed
      )
      const consentPagination = data as ConsentPaginationEntity
      return this.parseConsentPagination(consentPagination)
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: ConsentEntity): IConsent => {
    const startDate: DateTime = DateTime.fromISO(consentEntity.CreatedAt, {
Guilhem CARRON's avatar
Guilhem CARRON committed
      zone: 'utc',
    }).setLocale('fr-FR')
    const endDate: DateTime = 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,
      startDate: startDate,
      endDate: endDate,
      firstname: consentEntity.firstname,
      lastname: consentEntity.lastname,
      pointID: consentEntity.pointID,
      address: consentEntity.address,
      postalCode: consentEntity.postalCode,
Bastien DUMONT's avatar
Bastien DUMONT committed
      city: consentEntity.city,
      safetyOnBoarding: consentEntity.safetyOnBoarding,
Guilhem CARRON's avatar
Guilhem CARRON committed
    }
  }

  /**
   * Converts consent pagination entity into consent pagination
   * @param consentPaginationEntity
   */
  public parseConsentPagination = (
    consentPaginationEntity: ConsentPaginationEntity
  ): IConsentPagination => {
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: IConsentPagination = {
      rows: rows,
      totalRows: consentPaginationEntity.totalRows,
      totalPages: consentPaginationEntity.totalPages,
    }
    return consentPagination
  }
}