All files / src/models build.ts

23.33% Statements 7/30
26.08% Branches 6/23
37.5% Functions 3/8
24.13% Lines 7/29

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        1x                                           1x 1x 1x                                                 2x   2x 2x                                                                                                                    
import { Model } from './model';
import { Game } from './game';
import { IWorkflow } from './workflow';
 
export namespace IBuild {
  export interface File {
    compressedBytes: number;
    md5: string;
    path: string;
    uncompressedBytes: number;
  }
 
  export interface Node {
    _id?: string;
    children?: string[];
    displayName?: string;
    finishedAt?: Date;
    message?: string;
    name?: string;
    outboundNodes?: string[];
    phase?: string;
    startedAt?: Date;
    templatename?: string;
    type?: string;
  }
 
  export enum Platform {
    Server64 = 'server64',
    Windows64 = 'windows64',
  }
 
  export interface Reference {
    _id: string;
    files: string[];
  }
}
 
export class Build extends Model {
  public _id: string;
  public createdAt: Date;
  public entrypoint: string;
  public files: IBuild.File[];
  public game: Game;
  public gameId: string;
  public name: string;
  public namespaceId: string;
  public platform: IBuild.Platform;
  public publishedAt: Date;
  public reference: IBuild.Reference;
  public status: IWorkflow.Status;
  public updatedAt: Date;
 
  constructor(params: Partial<Build> = {}) {
    super(params);
 
    this.game = this.game ? new Game(this.game) : null;
    this.publishedAt = params.publishedAt ? new Date(params.publishedAt) : null;
  }
 
  public getNestedStatusNodes() {
    if (!this.status || !this.status.nodes) {
      return [];
    }
 
    const nodes = JSON.parse(JSON.stringify(this.status.nodes));
    const sortedNodes = nodes.sort((a, b) => {
      if (a.startedAt === b.startedAt) {
        return 0;
      }
 
      return a.startedAt > b.startedAt ? 1 : -1;
    });
 
    for (const node of sortedNodes) {
      if (node.children) {
        for (const childId of node.children) {
          const child = sortedNodes.find(n => n._id === childId);
          child.parent = node._id;
        }
      }
    }
 
    const children = this.getChildren(sortedNodes);
 
    return [
      {
        children,
        finishedAt: this.status.finishedAt,
        message: this.status.message,
        phase: this.status.phase,
        startedAt: this.status.startedAt,
        type: 'Workflow',
      },
    ];
  }
 
  private getChildren(data, parent?) {
    return data.reduce((previous, current) => {
      const obj = Object.assign({}, current);
 
      if (parent === current.parent) {
        const children = this.getChildren(data, current._id);
 
        if (children.length) {
          obj.children = children;
        }
 
        previous.push(obj);
      }
 
      return previous;
    }, []);
  }
}