Skip to content
Snippets Groups Projects
functions.service.ts 4.74 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Injectable } from '@angular/core';
    
    import { TranslateService } from '@ngx-translate/core';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { LatinisePipe } from 'ngx-pipes';
    import { AuthService } from './auth.service';
    import { HeaderService } from './header.service';
    import { TimeLimitPipe } from '../plugins/timeLimit.pipe';
    
    @Injectable({
        providedIn: 'root'
    })
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        constructor(
    
            public translate: TranslateService,
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            private authService: AuthService,
            private headerService: HeaderService,
            private latinisePipe: LatinisePipe,
        ) { }
    
            if (value === null || value === undefined) {
    
                return true;
    
            } else if (Array.isArray(value)) {
                if (value.length > 0) {
                    return false;
                } else {
                    return true;
                }
            } else if (String(value) !== '') {
                return false;
            } else {
                return true;
            }
        }
    
        formatFrenchDateToTechnicalDate(date: string) {
            if (!this.empty(date)) {
                let arrDate = date.split('-');
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                arrDate = arrDate.concat(arrDate[arrDate.length - 1].split(' '));
                arrDate.splice(2, 1);
    
    
                if (this.empty(arrDate[3])) {
                    arrDate[3] = '00:00:00';
                }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
                const formatDate = `${arrDate[2]}-${arrDate[1]}-${arrDate[0]} ${arrDate[3]}`;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
        formatFrenchDateToObjectDate(date: string, delimiter: string = '-') {
            if (!this.empty(date)) {
                let arrDate = date.split(delimiter);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                arrDate = arrDate.concat(arrDate[arrDate.length - 1].split(' '));
                arrDate.splice(2, 1);
    
    
                if (this.empty(arrDate[3])) {
                    arrDate[3] = '00:00:00';
                }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
                const formatDate = `${arrDate[2]}-${arrDate[1]}-${arrDate[0]} ${arrDate[3]}`;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
                return new Date(formatDate);
            } else {
                return date;
            }
        }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        formatDateObjectToDateString(date: Date, limitMode: boolean = false, format: string = 'dd-mm-yyyy') {
    
            if (date !== null) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                let formatDate: any[] = [];
                format.split('-').forEach((element: any) => {
                    if (element === 'dd') {
                        let day: any = date.getDate();
                        day = ('00' + day).slice(-2);
                        formatDate.push(day);
                    } else if (element === 'mm') {
                        let month: any = date.getMonth() + 1;
                        month = ('00' + month).slice(-2);
                        formatDate.push(month);
                    } else if (element === 'yyyy') {
                        let year: any = date.getFullYear();
                        formatDate.push(year);
                    }
                });
    
    
                let limit = '';
                if (limitMode) {
                    limit = ' 23:59:59';
                }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                return `${formatDate.join('-')}${limit}`;
    
    
        listSortingDataAccessor(data: any, sortHeaderId: any) {
            if (typeof data[sortHeaderId] === 'string') {
                return data[sortHeaderId].toLowerCase();
            }
            return data[sortHeaderId];
        }
    
    
        filterUnSensitive(template: any, filter: string, filteredColumns: any) {
            let filterReturn = false;
            filter = this.latinisePipe.transform(filter);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            filteredColumns.forEach((column: any) => {
                let val = template[column];
    
                if (typeof template[column] !== 'string') {
    
                    val = val === undefined || null ? '' : JSON.stringify(val);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                filterReturn = filterReturn || this.latinisePipe.transform(val.toLowerCase()).includes(filter);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        debug(msg: string, route: string) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                route : route,
    
                session : 'No user logged !',
                refreshSession : 'No user logged !',
                user : 'No user logged !'
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            };
    
            if (this.authService.getToken() != null) {
                info = {
                    route : route,
                    session : {
                        id : this.authService.getAppSession(),
                        expireIn : new Date((JSON.parse(atob(this.authService.getToken().split('.')[1])).exp) * 1000)
                    },
                    refreshSession : {
                        id : this.authService.getAppSession(),
                        expireIn : new Date((JSON.parse(atob(this.authService.getRefreshToken().split('.')[1])).exp) * 1000)
                    },
                    user : this.headerService.user,
                };
            }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            if (msg !== '') {
                console.log(msg, info);
            } else {
                console.log(info);
            }
        }