All files / owid-grapher/db/model SuggestedChartRevision.ts

40% Statements 56/140
100% Branches 0/0
0% Functions 0/5
40% Lines 56/140

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 118 119 120 121 122 123 124 125 126 1271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x                                                       1x 1x                                                                 1x 1x                                     1x 1x           1x 1x
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
 
import { SuggestedChartRevisionStatus } from "../../adminSiteClient/SuggestedChartRevision"
 
@Entity("suggested_chart_revisions")
export class SuggestedChartRevision extends BaseEntity {
    @PrimaryGeneratedColumn() id!: number
    @Column() chartId!: number
    @Column({ type: "json" }) suggestedConfig: any
    @Column({ type: "json" }) originalConfig: any
    @Column() createdBy!: number
    @Column() updatedBy!: number
 
    @Column({
        type: "enum",
        enum: SuggestedChartRevisionStatus,
        default: SuggestedChartRevisionStatus.pending,
    })
    status!: SuggestedChartRevisionStatus
 
    @Column({ default: "" }) suggestedReason!: string
    @Column({ default: "" }) decisionReason!: string
    @Column() createdAt!: Date
    @Column() updatedAt!: Date
 
    existingConfig?: any
 
    canApprove?: boolean
    canReject?: boolean
    canFlag?: boolean
    canPending?: boolean
 
    static isValidStatus(status: SuggestedChartRevisionStatus): boolean {
        return Object.values(SuggestedChartRevisionStatus).includes(status)
    }
 
    static checkCanApprove(
        suggestedChartRevision: SuggestedChartRevision
    ): boolean {
        // note: a suggestion can be approved if status == "rejected" |
        // "flagged" | "pending" AND the original config version equals
        // the existing config version (i.e. the existing chart has not
        // been changed since the suggestion was created).
        const status = suggestedChartRevision.status
        const originalVersion = suggestedChartRevision.originalConfig?.version
        const existingVersion = suggestedChartRevision.existingConfig?.version
        const originalVersionExists =
            originalVersion !== null && originalVersion !== undefined
        const existingVersionExists =
            existingVersion !== null && existingVersion !== undefined
        if (
            [
                SuggestedChartRevisionStatus.rejected,
                SuggestedChartRevisionStatus.flagged,
                SuggestedChartRevisionStatus.pending,
            ].includes(status) &&
            originalVersionExists &&
            existingVersionExists &&
            originalVersion === existingVersion
        ) {
            return true
        }
        return false
    }
 
    static checkCanReject(
        suggestedChartRevision: SuggestedChartRevision
    ): boolean {
        // note: a suggestion can be rejected if: (1) status ==
        // "pending" | "flagged"; or (2) status == "approved" and the
        // suggested config version equals the existing chart version
        // (i.e. the existing chart has not changed since the suggestion
        // was approved).
        const status = suggestedChartRevision.status
        const suggestedVersion = suggestedChartRevision.suggestedConfig?.version
        const existingVersion = suggestedChartRevision.existingConfig?.version
        const suggestedVersionExists =
            suggestedVersion !== null && suggestedVersion !== undefined
        const existingVersionExists =
            existingVersion !== null && existingVersion !== undefined
        if (
            [
                SuggestedChartRevisionStatus.flagged,
                SuggestedChartRevisionStatus.pending,
            ].includes(status)
        ) {
            return true
        }
        if (
            status === "approved" &&
            suggestedVersionExists &&
            existingVersionExists &&
            suggestedVersion === existingVersion
        ) {
            return true
        }
        return false
    }
 
    static checkCanFlag(
        suggestedChartRevision: SuggestedChartRevision
    ): boolean {
        // note: a suggestion can be flagged if status == "pending" or
        // if it is already flagged. Flagging a suggestion that is
        // already flagged is a hack for updating the decisionReason
        // column in the SuggestedChartRevisionApproverPage UI without
        // changing the status column.
        const status = suggestedChartRevision.status
        if (
            [
                SuggestedChartRevisionStatus.flagged,
                SuggestedChartRevisionStatus.pending,
            ].includes(status)
        ) {
            return true
        }
        return false
    }
 
    static checkCanPending(
        suggestedChartRevision: SuggestedChartRevision
    ): boolean {
        // note: a suggestion cannot be altered to pending from another status
        return false
    }
}