aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/todo-list.service.ts
blob: b1bc5ff374bfdf1af476d801d56508a529b2d027 (plain)
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
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap, concatMap, map, toArray } from 'rxjs/operators';

interface WiqlWorkItemResult {
  id: number;
  url: string;
}

interface WiqlResult {
  workItems: WiqlWorkItemResult[];
}

interface WorkItemResult {
  id: number;
  fields: { [name: string]: any };
}

export interface WorkItem {
  id: number;
  title: string;
}

@Injectable({
  providedIn: 'root'
})
export class TodoListService {

  private username = 'crupest';
  private pat = 'ehnmegogmk6r7qlkpy6zdl2hnfl6ntqbvggzxvvgp4a5vhr7lmnq';
  private organization = 'crupest-web';
  private project = 'Timeline';
  private fieldId = 'System.Title';

  private headers: HttpHeaders;

  constructor(private client: HttpClient) {
    this.headers = new HttpHeaders({
      'Accept': 'application/json',
      'Authorization': `Basic ${btoa(this.username + ':' + this.pat)}`
    });
  }

  getWorkItemList(): Observable<WorkItem[]> {
    return this.client.post<WiqlResult>(`https://dev.azure.com/${this.organization}/${this.project}/_apis/wit/wiql?api-version=5.0`, {
      query: 'SELECT [System.Id] FROM workitems WHERE [System.TeamProject] = @project'
    }, { headers: this.headers }).pipe(
      switchMap(result => result.workItems),
      concatMap(result => this.client.get<WorkItemResult>(result.url, {headers: this.headers})),
      map(result => <WorkItem>{ id: result.id, title: result.fields[this.fieldId] }),
      toArray()
    );
  }
}