import { Response } from '@angular/http'; import { Observable } from 'rxjs'; import { AuthHttp } from 'angular2-jwt'; import { Invite } from './invite.model'; import { InviteSerializer } from './invite.serializer'; import { InviteConverter } from './invite.converter'; import { InviteCollection } from './invite.collection'; import { InviteCollectionConverter } from './invite.collection.converter'; import { UrlParamBuilderService } from '../service/url-param-builder.service'; import { IErrorHandler } from '../error/error-handler.interface'; import { ApiPathComponent } from '../api-path-component'; export class InviteService { public constructor( private authHttp: AuthHttp, private inviteConverter: InviteConverter, private collectionConverter: InviteCollectionConverter, private serializer: InviteSerializer, private baseUrl: string, private urlParamBuilderService: UrlParamBuilderService, private errorHandler: IErrorHandler ) {} public getMany(organizationId: string, filters?: {[key: string]: any}, sort?: string[], page?: number, limit?: number): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.organizations + '/' + organizationId + '/' + ApiPathComponent.invites + '?' + this.urlParamBuilderService.build(filters, sort, page, limit) ; return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.collectionConverter.toCollection(res.json())); } public getOne(organizationId: string, userId: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.organizations + '/' + organizationId + '/' + ApiPathComponent.invites + '/' + userId; return this.authHttp.get(url) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.inviteConverter.toOne(res.json())); } public create(invite: Invite): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.organizations + '/' + invite.organization.id + '/' + ApiPathComponent.invites; return this.authHttp.post(url, this.serializer.serialize(invite)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.inviteConverter.toOne(res.json())); } public update(invite: Invite): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.organizations + '/' + invite.organization.id + '/' + ApiPathComponent.invites + '/' + invite.inviteCode; return this.authHttp.put(url, this.serializer.serialize(invite)) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => this.inviteConverter.toOne(res.json())); } public delete(organizationId: string, userId: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.organizations + '/' + organizationId + '/' + ApiPathComponent.invites + '/' + userId; return this.authHttp.delete(url) .catch((error?: any) => this.errorHandler.handle(error)) .map(() => undefined); } public accept(inviteCode: string): Observable { const url: string = this.baseUrl + '/' + ApiPathComponent.invites + '/' + inviteCode; return this.authHttp.put(url, {}) .catch((error?: any) => this.errorHandler.handle(error)) .map((res: Response) => undefined); } }