Skip to content
Snippets Groups Projects
newsletter.service.ts 8.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import axios, { AxiosRequestConfig } from 'axios'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { toast } from 'react-toastify'
    
    import { IMailSubject } from '../models/mailSubject.model'
    
    import { IMonthlyInfo } from '../models/monthlyInfo.model'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { IMonthlyNews } from '../models/monthlyNews.model'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IPoll } from '../models/poll.model'
    
    export class NewsletterService {
    
      /**
       * Saves a mail subject for selected month
       * @param date
       * @param subject
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
       */
      public saveMailSubject = async (
        date: Date,
        subject: string,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<void> => {
        try {
          await axios.put(
    
            `/api/animator/mailSubject`,
    
            {
              month: date.getMonth() + 1,
              year: date.getFullYear(),
              subject: subject,
            },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Objet de la newsletter enregistré !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
          } else {
    
            toast.error(
    
              "Erreur lors de l’enregistrement de l'objet de la newsletter"
    
            )
          }
          console.error(e)
        }
      }
    
      /**
       * Gets the mail subject for selected month
       * @param date
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
       */
      public getSingleMailSubject = async (
        date: Date,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<IMailSubject | null> => {
        try {
          const { data } = await axios.get(
    
            `/api/animator/mailSubject/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          )
          return data as IMailSubject
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          console.error('error', e)
          return null
        }
      }
    
      /**
       * Deletes the mail subject for selected month
       * @param date
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
       */
      public deleteMailSubject = async (
        date: Date,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<void> => {
        try {
          await axios.delete(
    
            `/api/animator/mailSubject/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Objet de la newsletter supprimé !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error("Erreur lors de la suppression de l'objet de la newsletter")
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Creates a monthlyInfo for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       * @param date
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public saveMonthlyInfo = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<void> => {
        try {
    
            `/api/animator/monthlyInfo`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth() + 1,
    
              year: date.getFullYear(),
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          )
    
          toast.success('Information du mois enregistrée !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de l’enregistrement des informations du mois')
    
       * Gets the information for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
      public getSingleMonthlyInfo = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<IMonthlyInfo | null> => {
    
          const { data } = await axios.get(
    
            `/api/animator/monthlyInfo/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          return data as IMonthlyInfo
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          console.error('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
      /**
    
       * Deletes a Monthly Info for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public deleteMonthlyInfo = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<void> => {
        try {
    
            `/api/animator/monthlyInfo/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Informations du mois supprimées !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de la suppression des informations du mois')
    
          console.error(e)
        }
      }
    
      /**
       * Creates a monthlyNews for selected month
       * @param date
       * @param title
       * @param content
       */
      public saveMonthlyNews = async (
        date: Date,
        title: string,
        content: string,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<void> => {
        try {
          await axios.put(
    
            `/api/animator/monthlyNews`,
    
              month: date.getMonth() + 1,
    
              year: date.getFullYear(),
              title: title,
              content: content,
            },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Nouveautés du mois enregistrés !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de l’enregistrement des nouveautés du mois')
    
          console.error(e)
        }
      }
    
      /**
       * Gets a news title and content for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
       */
      public getSingleMonthlyNews = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<IMonthlyNews | null> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
          const { data } = await axios.get(
    
            `/api/animator/monthlyNews/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          )
    
          return data as IMonthlyNews
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.error('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
    
      /**
       * Deletes a Monthly News for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
       */
      public deleteMonthlyNews = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<void> => {
        try {
    
            `/api/animator/monthlyNews/${date.getFullYear()}/${
              date.getMonth() + 1
            }`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Nouveautés du mois supprimées !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de la suppression des nouveautés du mois')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
       * Creates a poll with question and link for selected month
       * @param date
       * @param question
       * @param link
       */
    
      public savePoll = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        date: Date,
        question: string,
        link: string,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<void> => {
        try {
    
            `/api/animator/poll`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth() + 1,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              year: date.getFullYear(),
              link: link,
              question: question,
            },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          )
    
          toast.success('Sondage enregistré !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de l’enregistrement du sondage')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Gets a poll with question and link for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public getSinglePoll = async (
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        axiosHeaders: AxiosRequestConfig
    
      ): Promise<IPoll | null> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
    
          const { data } = await axios.get(
    
            `/api/animator/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          return data as IPoll
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.error('error', e)
          return null
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Deletes a poll for selected month
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
       * @param axiosHeaders
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      public deletePoll = async (
        date: Date,
        axiosHeaders: AxiosRequestConfig
      ): Promise<void> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
    
            `/api/animator/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            axiosHeaders
    
          toast.success('Sondage supprimé !')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        } catch (e) {
    
          if (e.response.status === 403) {
    
            toast.error("Accès refusé : vous n'avez pas les droits nécessaires")
    
            toast.error('Erreur lors de la suppression du sondage')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    }