All files / src/app/core/model jobs-list-options.ts

56% Statements 42/75
71.42% Branches 20/28
50% Functions 14/28
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117    20x 62x 62x 62x 62x 62x   62x       62x 62x 62x 62x       22x       21x       28x       25x       17x       27x       9x       9x                 10x 10x 9x   10x       10x 10x       2x 2x       3x 3x                       21x 10x   10x 1x     9x   10x     11x   11x 3x     8x   11x       21x   21x    
import { JobStatus, JobStatusMeta } from "./job.interface";
 
export class JobsListOptions {
    private page: number = 0;
    private itemsPerPage: number = 10;
    private jobStatus: keyof typeof JobStatus | null = null;
    private jobStatusMeta: keyof typeof JobStatusMeta | null = null;
    private searchQuery: string | null = null;
    private currentSort: string;
    private mustReload: boolean | null = null;
 
 
    constructor(jobsStatus: string | null = null, jobStatusMeta: string | null = null, searchQuery: string | null = null, sort: string | null = null) {
        this.jobStatus = jobsStatus as keyof typeof JobStatus;
        this.jobStatusMeta = jobStatusMeta as keyof typeof JobStatusMeta;
        this.searchQuery = searchQuery;
        this.currentSort = sort ?? 'createdAt,desc';
    }
 
    getCurrentPage(): number {
        return this.page;
    }
 
    getItemsPerPage(): number {
        return this.itemsPerPage;
    }
 
    getStatus() :string | null{
        return this.jobStatus;
    }
 
    getStatusMeta() :string | null{
        return this.jobStatusMeta;
    }
 
    getQuery(): string | null {
        return this.searchQuery;
    }
    
    getSort(): string {
        return this.currentSort;
    }
 
    getMustReload(): boolean | null {
        return this.mustReload;
    }
 
    equals(jobsListOptions: JobsListOptions) :boolean {
        return this.page === jobsListOptions.getCurrentPage()
            && this.itemsPerPage === jobsListOptions.getItemsPerPage()
            && this.jobStatus === jobsListOptions.getStatus() 
            && this.jobStatusMeta === jobsListOptions.getStatusMeta()
            && this.searchQuery === jobsListOptions.getQuery()
            && this.currentSort == jobsListOptions.getSort();
    }
 
    changePagination(page: number, itemsPerPage: number |null): this {
        this.page = page;
        if(itemsPerPage !== null) {
            this.itemsPerPage = itemsPerPage;
        }
        return  this;
    }
    
    forceReload(force: boolean | null): this {
        this.mustReload = force;
        return this;
    }
 
    query(searchQuery: string | null): this {
        this.searchQuery = searchQuery;
        return this;
    }
 
    sort(sort: string): this {
        this.currentSort = sort;
        return this;
    }
 
    /**
     * 
     * @param status the new job status as a string (keyof JobStatus)
     * @param statusMeta the new job "meta" status as a string (keyof JobStatusMeta)
     * @returns true if changed, false otherwise
     */
    filter(status: string | null, statusMeta: string | null): this {
        // having two properties to handle status or statusMeta filters may seem unclean, as they are exclusive at the moment
        // but it actually allow us to change our mind in the future and use both filters cumulatively 
        if(status !== null) {
            const newStatus: keyof typeof JobStatus = status as keyof typeof JobStatus;
            // clicking on the current status removes the filter
            if(newStatus === this.jobStatus) {
                this.jobStatus = null;
            }
            else {
                this.jobStatus = status as keyof typeof JobStatus;
            }
            this.jobStatusMeta = null;
        }
        else {
            const newStatusFilter: keyof typeof JobStatusMeta = statusMeta as keyof typeof JobStatusMeta;
            // clicking on the current status filter removes the filter
            if(newStatusFilter === this.jobStatusMeta) {
                this.jobStatusMeta = null;
            }
            else {
                this.jobStatusMeta = statusMeta as keyof typeof JobStatusMeta;
            }
            this.jobStatus = null;
        }
 
        // in any case, filtering or canceling a filter resets the pagination
        this.page = 0;
 
        return this;
    }
}