import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";

@Injectable({providedIn: "root"})
export class OrgService {

  constructor(private httpClient: HttpClient) {

  }

  getOrg(privateId: string, memberId: string): Observable<OrgInfo> {
    return this.httpClient.get(`/api/old/private/${privateId}`, {params: {'memberId': memberId}}).pipe(map(r => <OrgInfo>r))
  }

  addPrivateToPerson(personId: string): Observable<string> {
    return this.httpClient.post(`/api/new/private`, {ownerId: personId}).pipe(map((res: any) => <string>res.id))
  }

  getOrgsInfo(ownerId: string): Observable<OrgInfo[]> {
    return this.httpClient.get(`/api/old/private/${ownerId}/list`).pipe(map((res: any[]) => res.map(r => <OrgInfo>r)))
  }
}

export class OrgInfo {
  privateId: string
  personId: string
  position: string
}
