import { LocaleService } from '../common/i18n'; import { Observable } from 'rxjs/Rx'; import { Injectable } from '@angular/core'; import { Http, RequestOptions, ResponseContentType, Response } from '@angular/http'; import { SeCurisResourceServices } from './base'; import "file-saver"; declare var saveAs: FileSaver; export const LIC_STATUS = { CREATED: 'CR', ACTIVE: 'AC', REQUESTED: 'RE', PREACTIVE: 'PA', EXPIRED: 'EX', BLOCKED: 'BL', CANCELLED: 'CA' } export const COLORS_BY_STATUS = { 'CR': '#808080', 'AC': '#329e5a', 'RE': '#2981d4', 'EX': '#ea7824', 'BL': '#ff0000', 'CA': '#a21717' }; /** * These transitions could be get from server, class License.Status, but * we copy them for simplicity, this info won't change easily */ export const LIC_ACTIONS_BY_STATUS = { edit: [LIC_STATUS.REQUESTED, LIC_STATUS.CREATED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE, LIC_STATUS.CANCELLED, LIC_STATUS.BLOCKED], add_request: [LIC_STATUS.CREATED], activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE], send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], block: [LIC_STATUS.CANCELLED], unblock: [LIC_STATUS.BLOCKED], cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE], 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED, LIC_STATUS.BLOCKED] } export const LICENSE_ACTIONS : any[] = [{ /* command: 'add_request', icon: 'add_to_photos', name: 'Add request' },{ */ command: 'activate', icon: 'play_circle_outline', name: 'Activate' },{ command: 'send', icon: 'send', name: 'Send' },{ command: 'download', icon: 'file_download', name: 'Download' },{ command: 'block', icon: 'do_not_disturb_on', name: 'Block' },{ command: 'unblock', icon: 'do_not_disturb_off', name: 'Unblock' },{ command: 'cancel', icon: 'cancel', name: 'Cancel' }] @Injectable() export class LicensesService extends SeCurisResourceServices { constructor(http: Http, $L: LocaleService) { super($L, http, 'license'); } public getByPack(packId: number) { let url = `${this.resource}?packId=${packId}`; return this.http.get(url).map(response => response.json()); } public activate(id: number) { return super.action(id, "activate"); } public block(id: number) { return super.action(id, "block"); } public unblock(id: number) { return super.action(id, "unblock"); } public send(id: number) { return super.action(id, "send"); } public cancel(id: number) { return super.action(id, "cancel"); } public download(id: number) { let url = `${this.resource}/${id}/download`; return this.http.get(url).map((response : Response) => { let filename = JSON.parse(response.headers.get('Content-Disposition').match(/".*"$/g)[0]); let content = JSON.stringify(response.json(), null, 2); saveAs( new Blob([ content ], { type : 'application/octet-stream' }), filename); return Observable.of(true); }).catch(err => super.processErrorResponse(err)); } public isActionAvailable(action:string, lic:any) { var validStatuses = LIC_ACTIONS_BY_STATUS[action]; return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1; } getStatusName(statusCode: string): string { return this.$L.get(`pack.status.${statusCode}`, this.$L.get('Unknown')); } getStatusColor(statusCode: string): string { return COLORS_BY_STATUS[statusCode] || '#cccccc'; } }